graphy

Query linked-data graphs by abstracting away traditional JSON-LD interaction
Contents
Install
$ npm install --save graphy
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
Collection objects are arrays that are also an entity.
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:
let w_list = k_banana.stages.$('rdf:');
w_list.first.$id('ns:');
w_list = w_list.rest;
w_list.first.$id('plant:');
w_list = w_list.rest;
w_list.first.$id('plant:');
w_list = w_list.rest;
w_list.first.$id('plant:');
w_list = w_list.rest;
w_list.$id('rdf:');
let a_stages = [];
let w_list = k_banana.stages.$('rdf:');
while(w_list.$id('rdf:') !== 'nil') {
a_stage.push(w_list.first.$id('plant:') || w_list.first.$id('ns:'));
w_list = w_list.rest;
}
a_stages;
# API Reference
## Graphy
The module itself.
graphy(jsonld: Jsonld_object, ready: function)
Calls ready(network: Network)
once a graphy network has been created for the given jsonld
object.
## Network
An array of the jsonld objects in this graph, each represented by a graphy [entity](#entity).
### network.select(name: string[, namespace: string])
Returns the graphy entity for the IRI given by `name`, which may be a prefixed name or full IRI. Optional `namespace` argument will set the accessor namespace for the returned object (see [entity.$()](#e.$)).
### network.top([map: function])
Returns an array of only entities that are named things (ie not blanknodes) or blanknodes that do not appear in the object position of any triples in the current graph. These can be thought of as top-level nodes. Accepts an optional `map` function callback to transform the entities before returning the array. These entities will have empty an accessor namespace by default.
### network.shorten(iri: string)
Shortens an IRI using prefixes defined in the @context of the original jsonld object.
### network.[...]
[Network](#network) is an array, so it supports all native functions. Each item in the array is a graphy entity with an empty accessor namespace.
## Entity
A reference to an RDF entity/jsonld object covered by the set of methods/properties documented in this section. An entity can be obtained by [`network.select()`](#n.select), or any one of the array interface methods on network.
### entity()
> Only for types: `node`
Returns a Map of {predicate => object} pairs for all triples stemming from this entity as the subject. The key of each pair is a string representing the full predicate IRI, the value is an entity representing the object.
entity([namespace: string])
Only for types: iri
Returns the IRI of this entity suffixed by the current accessor namespace or the namespace
argument if it is used. You can obtain the full IRI no matter the current accessor namespace by using an empty string for namespace
.
entity()
Only for types: literal
Returns the value portion of the literal. See entity.$type
and entity.$n3.datatype
for getting the datatype of a literal.
entity(access_name: string)
Only for types: node
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(access_name: string[, map_callback: function])
> Only for types: `node`
Same as entity()
except that it maps every object pointed to by access_name
to the given map_callback
function before returning the array of entities.
entity()
Only for types: collection
Returns the array
entity(item_index: integer)
Only for types: collection
Returns the item at item_index
in the collection.
entity(map_callback: function)
Only for types: collection
Applies the given map_callback
to every item in the array and returns the resulting array.
### entity.$(namespace: string)
Sets the accessor namespace of the returned object to the expanded version of the IRI given by `namespace`, may be either an n3 prefix or a full IRI. By chaining this call, you can change the accessor namespace on the same line to access properties or IRIs by their suffix.
entity.$()
This no-args version of the namespace method will instead return the full IRI of the current accessor namespace.
### entity.$n3()
Returns a terse n3 representation of the current entity as a string. It is prefixed by the longest matching URI available in the original JSON-LD context, unless the resulting suffix would contain invalid characters for a prefixed IRI in either SPARQL or TTL. The string is compatible with SPARQL and TTL as long as the corresponding prefix is also included in the document.
### entity.$n3.datatype()
> Only for types: `literal`
Returns the IRI datatype of this literal in terese n3 form.
### entity.$nquad()
Returns the n-quad representation of this entity. Useful for serializing to SPARQL/TTL without worrying about prefixes.
> Caution: `.$nquad()` does not currently support nested RDF collections (it will produce blanknode collisions)
### entity.$in(namespace: string)
Returns true if the current entity's IRI is in the given `namespace`. Will always return false for blanknodes, collections and literals.
### entity.$is()
Calling this function returns the reference type of this entity as a string. You can also use a shorthand check by testing if `.$is[ref_type]` is defined as `true`. eg: `if(entity.$is.iri === true) ...`. Possible values for type are:
- *node* - this entity exists as the subject of some triple(s). This entity contains predicates that point to objects
- *iri* - this is a mere symbollic reference to an IRI, which exists as the object of some triple. If you encounter this type, it means that you reached a named thing (ie: not a blanknode). Use [`entity.$node()`](#e.node) to obtain the node of this IRI if it exists in the current graph
- *literal* - an RDF literal
- *collection* - an RDF collection
### entity.$node([namespace: string])
> Only for types: `iri`
Will return the node object for accessing triples that have this IRI as its subject. If there are no triples in the current jsonld graph that have this IRI as its subject, calling .$node()
will return undefined for this IRI. Passing an optional namespace
argument will set the accessor namespace on the returned graphy entity.
### entity['@id']
Reflects the json-ld `@id` property.
### entity.$id([namespace: string])
The suffix of the `@id` property after removing the current accessor namespace from the beginning. If the current accessor namespace does not match, or this IRI is a blanknode, this method 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']
Reflects the json-ld `@type` property. For literals, this will be the datatype. For nodes, this will be an array of objects pointed to by the `rdf:type` predicate.
### entity.$types
> Only for types: `node`
An array of graphy IRI entities that are pointed to by the @type
property (which is the rdf:type
predicate) for this entity.
### entity.$types([namespace: string])
> Only for types: `node`
Returns an array of strings that are the suffixes of the IRIs pointed to by the @type
property after removing the current accessor namespace (or the given namespace
argument) from the beginning of the IRI. If the namespace does not match any of the IRIs, this will return an empty array []
.
### entity.$type([namespace: string])
> Only for types: `node`
Shortcut for .$types(..)[0]
. 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
ISC © Blake Regalia