A simple linked data reasoner that uses N3 under the covers...
This module began life as part of the ActivityStrea.ms implementation (http://github.com/jasnell/activitystrea.ms). I am extracting it out as part of an effort to better modularize the ActivityStrea.ms project.
From v0.1.0 on, this module requires Node v4.0.0 / ES6. Also, the async callback versions of the methods have been removed in order to further simplify the API and implementation.
npm install reasoner
var rdf = require('linkeddata-vocabs').rdf;
var Reasoner = require('reasoner');
var Graph = Reasoner.Graph;
var graph = new Graph();
graph.add({
subject: 'http://example.org/foo',
predicate: rdf.type,
object: 'http://example.org/bar'
});
graph.add({
subject: 'http://example.org/foo',
predicate: rdf.type,
object: 'http://example.org/baz'
});
graph.add({
subject: 'http://example.org/foo',
predicate: rdfs.subClassOf,
object: 'http://example.org/boo'
});
graph.add({
subject: 'http://example.org/boo',
predicate: rdfs.subClassOf,
object: 'http://example.org/aaa'
});
graph.add({
subject: 'http://example.org/aaa',
predicate: rdfs.subClassOf,
object: 'http://example.org/ccc'
});
graph.add({
subject: 'http://example.org/bar',
predicate: rdfs.subClassOf,
object: 'http://example.org/aaa'
});
graph.add({
subject: 'http://example.org/bbb',
predicate: rdfs.subClassOf,
object: 'http://example.org/aaa'
});
var reasoner = new Reasoner(graph);
var node = reasoner.node('http://example.org/foo');
node.is('http://example.org/bar'); // true
node.is('http://example.org/boo'); // true
node.is('http://example.org/aaa'); // true
node.is('http://example.org/ccc'); // true
node.is('http://example.org/bbb'); // false
Creates a new Reasoner. Either a single Reasoner.Graph
instance, or an array of Reasoner.Graph
instances can be provided to the constructor to be bound automatically when the Reasoner is created.
The total number of triples known to the Reasoner.
Binds a single Reasoner.Graph
instance to the Reasoner.
Returns a Reasoner.Node
instance. A Node represents a single subject known to the Reasoner.
Utility method that creates new Reasoner.Literal
Creates a new Reasoner.Graph
instance. A Graph is essentially a thin wrapper around an N3.Store
object.
Returns the total number of triples known to the Graph.
Adds a single triple to the Graph. The argument is an object with three properties: subject
, predicate
and object
.
var Graph = require('reasoner').Graph;
var graph = new Graph();
graph.add({
subject: 'http://example.org/foo',
predicate: 'http://example.org/bar',
object: 'http://example.org/baz'
});
Merge the specified Graph into this Graph.
Search the Graph for triples matching the specified pattern.
var Graph = require('reasoner').Graph;
var graph = new Graph();
graph.add({
subject: 'http://example.org/foo',
predicate: 'http://example.org/bar',
object: 'http://example.org/baz'
});
var triples = graph.find({subject:'http://example.org/foo'});
for (let triple of triples) {
console.log(triple);
}
The Reasoner.Node
object provides access to explicit and inferred information about a single subject known to the Reasoner.
var Reasoner = require('reasoner');
var Graph = Reasoner.Graph;
var graph = new Graph();
graph.add({
subject: 'http://example.org/foo',
predicate: 'http://example.org/bar',
object: 'http://example.org/baz'
});
var reasoner = new Reasoner(graph);
var node = reasoner.node('http://example.org/foo');
// ...
The Subject URI
Searches for all triples known to the Reasoner for which this node is the object.
Searches for all triples known to the Reasoner for which this node is the subject.
Searches for literal values with the specified predicate.
var literals = node.literal('http://example.org/label');
for (let literal of literals) {
console.log(literal.valueOf());
console.log(literal.type);
console.log(literal.language);
};
Returns the collection of rdf:type explicitly defined for this subject.
Returns a listing of Reasoner.Node
objects representing other subjects defined as rdfs:subPropertyOf this subject.
Returns a listing of Reasoner.Node
objects representing other
subjects defined as rdfs:subClassOf this subject.
Returns a listing of Reasoner.Node
objects representing other
subjects for which this node can be considered a sub-property.
Returns a listing of Reasoner.Node
objects representing other
subjects for which this node can be considered a sub-class.
Returns true if this subject can be considered to be an instance of the specified type. Specifically, this will return true if:
- It has rdf:type type
- One of it's rdf:type's is type
- It has rdfs:subClassOf type
- One of it's superClasses is type
- It has rdfs:subPropertyOf type
- One of it's superProperties is type
Represents a Literal value.
Returns the value of the literal.
A Reasoner.Node
representing the Literal type.
A LanguageTag representing the Literal language. (The LanguageTag object is provided by the rfc5646
module in npm)