## Usage
Take the following graph:
@prefix ns: <vocab://ns/> .
@prefix color: <vocab://color/> .
@prefix plant: <vocab://plant/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
plant:Fruit
ns:contains plant:Seeds .
ns:Banana
a ns:Food, plant:Fruit, plant:EdiblePart;
rdfs:label "Banana"^^xsd:string;
ns:tastes "good" ;
ns:shape "curved"^^ns:Liberty ;
ns:data 25 ;
ns:appears color:Yellow ;
ns:class ns:Berry ;
plant:blossoms ns:YearRound ;
ns:alias ns:Cavendish, ns:Naner, ns:Bananarama ;
ns:stages (
ns:FindSpace
plant:Seed
plant:Grow
plant:Harvest
) ;
ns:considered [
a plant:Clone, plant:Fruit
] .
Here, example.json
is a JSON-LD file generated from the graph above:
var graphy = require('graphy');
var json_ld = require('./example.json');
graphy(json_ld, (q_network) => {
let k_banana = q_network.select('ns:Banana').$('ns:');
k_banana['@id'];
k_banana.$id();
k_banana['@type'];
k_banana.$types();
k_banana.$type();
k_banana.$types('plant:');
k_banana.$type('plant:');
k_banana.tastes();
k_banana.shape();
k_banana.shape['@type'];
k_banana.tastes['@type'];
k_banana.shape.$type();
k_banana.tastes.$type();
k_banana.tastes.$type('xsd:');
k_banana.class();
k_banana.class.$id();
k_banana.class['@id'];
k_banana.class['@type'];
k_banana.appears();
k_banana.appears('color:');
k_banana.appears.$id();
k_banana.appears.$id('color:');
k_banana.appears['@id'];
k_banana.appears['@type'];
k_banana.$types('plant:');
k_banana.$('plant:').blossoms();
k_banana.$('plant:').blossoms['@id'];
k_banana.$('plant:').blossoms('ns:');
k_banana.$n3();
k_banana.$n3(false);
k_banana.appears.$n3();
k_banana.tastes.$n3();
k_banana.tastes.$n3.value();
k_banana.tastes.$n3.datatype();
k_banana.class.$n3();
k_banana.class.$nquad();
k_banana.tastes.$nquad();
k_banana.stages.$n3();
k_banana.stages.$n3(false);
k_banana.$is();
k_banana.$is.node;
k_banana.appears.$is();
k_banana.data.$is();
k_banana.stages.$is();
k_banana.considered.$is();
k_banana.$is.node;
k_banana.appears.$is.iri;
k_banana.data.$is.literal;
k_banana.stages.$is.iri;
k_banana.stages.$is.literal;
k_banana.stages.$is.collection;
k_banana.considered.$is.blanknode;
k_banana.alias;
let a_items = k_banana('alias', function(k_alias) {
return k_alias();
});
a_items;
k_banana.stages().map(function(k_stage) {
return k_stage.$id() || k_stage.$id('plant:');
});
k_banana.stages(function(k_stage) {
return k_stage.$id() || k_stage.$id('plant:');
});
k_banana.stages(0).$id();
});
## Iterating
for..of
An entity of type node
supports iteration directly on the reference itself. The key of the iterator will be the predicate suffixed by the current accessor namespace, the value of the iterator will be an array of entities that are pointed to by that predicate:
for(let [s_predicate_suffix, a_objects] of k_banana) {
a_objects.forEach((k_node) => {
console.log(s_predicate+' => {'+k_node.$is()+'} '+k_node.$n3());
});
}
You can also iterate on the no-args call of a node entity, this will set the namespace to an empty string so that the key of each iterator is the full IRI of each predicate:
for(let [p_predicate, a_objects] of k_banana()) {
a_objects.forEach((k_node) => {
console.log(q_graph.shorten(p_predicate)+' => {'+k_node.$is()+'} '+k_node.$n3());
});
}
RDF Collections
In order to be consistent with the graph, rdf collection properties are emulated on collection objects. So instead of accessing a collection's elements via Array's properties/methods, you can also use the rdf:first
and rdf:rest
properties:
Returns an array of entities that are pointed to by the namespaced predicate suffix access_name
. If the current accessor namespace is empty, then the access name would be the full IRI of the predicate.
### entity.$type([namespace: string])
> Only for types: `node`
Shortcut for .$types[0].$id
. If this node entity has more than one rdf:type
, accessing this property will issue a warning. If the current accessor namespace does not match any of the IRIs, this will return undefined
. If a namespace
argument is passed, the method will use the given namespace instead of the current accessor namespace to suffix the IRI.
entity.$type([namespace: string])
Only for types: literal
Returns the datatype of this literal entity suffixed by the current accessor namespace or the namespace
argument if it is used.
License