Anzo JS-RDF
A lightweight javascript RDF library.
Classes
anzo.rdf.Value:
The supertype of all RDF model objects (URIs, blank nodes and literals).
anzo.rdf.Literal:
An RDF literal consisting of a value and optionally a language or a datatype (but not both).
Literals can be created by calling anzo.createLiteral(val, lang) or anzo.createTypedLiteral(val, datatype).
Note: Literals are cached in the factory - indexed by value and, if specified, language/datatype.
anzo.rdf.Resource:
The supertype of all RDF resources (URIs and blank nodes).
anzo.rdf.URI:
Consists of a namespace and a local name, which are derived from its value string by splitting it in two using the following algorithm:
- Split after the first occurrence of the '#' character,
- If this fails, split after the last occurrence of the '/' character,
- If this fails, split after the last occurrence of the ':' character.
The last step should never fail as every legal (full) URI contains at least one ':' character to separate the scheme from the rest of the URI.
URIs can be created by calling anzo.createURI(val)
Note: URIs are cached in the factory - indexed by the given value
anzo.rdf.BNode:
An RDF blank node has an identifier to be able to compare it to other blank nodes internally.
Blank nodes can be created by calling anzo.createBNode(id)
Note: BNodes are cached in the factory - indexed by the given value
anzo.rdf.Statement
An RDF statement, with an optional associated namedGraphUri. A statement is equal to another statement if their subject, predicate, object and namedGraphUri members are equal. When treating statements like triples, one can use the tripleEquals method to compare only the subject, predicate and object members of the two statements.
var stmtTriple = anzo.createStatement('http://a', 'http://b', 'http://c');
var stmtQuad = anzo.createStatement('http://a', 'http://b', 'http://c', 'http://d');
stmtQuad.equals(stmtTriple) is false
stmtQuad.tripleEquals(stmtTriple) is true
Statements are created by calling anzo.createStatement(s, p, o, c)
The arguments to anzo.createStatements can either be values:
anzo.createStatement('http://foo', 'http://firstName', 'Rouben', 'http://zoo');
Or anzo.rdf.Values:
var s = anzo.create('http://foo');
var p = anzo.createURI('http://firstName');
var o = anzo.createTypedLiteral('Rouben', XMLSchema.xsString);
var c = anzo.createURI('http://zoo');
anzo.createStatement(s, p, o, c);
Or any combination of the two:
var o = anzo.createTypedLiteral('Rouben', XMLSchema.xsString);
var c = anzo.createURI('http://zoo');
anzo.createStatement('http://foo', 'http://firstName', o, c);
In cases where an anzo.rdf.Value is not specified, the system is forced to guess the type of value that is specified with the following algorithms:
- argument 1:
if valid URI -> anzo.rdf.URI else -> anzo.rdf.BNode
- argument 2:
assumed to always be an anzo.rdf.URI
- argument 3:
1. if is a string that is a URI -> anzo.rdf.URI 2. if is a number -> anzo.rdf.Literal, datatype = XMLSchema.xsFloat 3. if is a boolean -> anzo.rdf.Literal, datatype = XMLSchema.xsBoolean 4. if is a string -> anzo.rdf.Literal, datatype = XMLSchema.xsString 5. For all other cases, call toString on the value -> anzo.rdf.Literal, datatype = XMLSchema.xsString
- argument 4:
assumed to always be an anzo.rdf.URI
Warning: The algorithm to guess the value type is not perfect. It does not, for example, deal with Literal languages. Be sure to use anzo.rdf.Values to specify values that are outside the scope of this algorithm.
anzo.rdf.IQuadStore
The superclass of all Stores that store statements where each statement has its subject, predicate, object and namedGraphUri specified (thereby making it a quad). Arguments to the public methods such as find, add, etc can be passed as values or as anzo.rdf.Values, or any combination of the two.
anzo.rdf.QuadStore?
An in-memory storage of statements. Each statement must specify their subject, predicate, object and namedGraphUri (thereby making them quads). This store maintains indexes to allow fast lookup when calling the find method.
The following hashes are maintained by the quad store:
- contextMap: Hash of namedGraphUris to statements
- subjMap: Hash of subjects to statements
- propMap: Hash of predicates to statements
- objMap: Hash of objects to statements
- poMap: Hash of predicates to hashes of objects to sets of statements (var hash1=poMap[p]; var stmts=hash1[o];)
- psMap: Hash of predicates to hashes of subjects to sets of statements (var hash1=psMap[p]; var stmts=hash1[s];)
A set of all statements is also kept.
Note that hashes must be performed on the values of the nodes rather then the objects since there is no guarantee that the objects stored in the statements are the same objects the user will use to call the find method. A way to do this is to have a hashCode method that returns a hash value based on the value of the node. Therefore, if any two nodes have the same values they will also have the same hash codes.
Each time a statement is added to the store, it is added to the set of statements as well as to all the hashes (indexes). When a statement is removed, it is removed from everything.
When the user calls
find(/*Resource*/s, /*URI*/p, /*Value*/o, /*array of named graph uris*/cArray)
here is what happens:
if all parameters are null
return the set of all statements
if only one of the values is specified, return the statements its hash returns
(i.e. if s was specified, return subjMap[s])
(i.e. if cArray was specified, iterate through the array and add contextMap[c[n]] to a running set and return that set)
if s and p are specified, cArray is optional
1. get the subject to statements hash by calling (var hash1 = psMap[p])
2. get the statements the given subject hashes to in that hash (var stmts = hash1[s]);
3. if cArray is not null and not empty, filter the list of stmts by the cArray
4. return the filtered stmts array
if p and o are specified, cArray is optional
1. get the object to statements hash by calling (var hash1 = poMap[p])
2. get the statements the given object hashes to in that hash (var stmts = hash1[o])
3. if cArray is not null and not empty, filter the list of stmts by the cArray
4. return teh filtered stmts array
if s, p, o are specified
1. get the three sets of statements each hashes to
2. pick the smallest set of the three and iterate through it filtering out all statements that don’t match the pattern
3. return the matching set
if s and cArray are specified
1. get statements hashed to by the subject (subjMap[s])
2. get statements hashed to by the names in the cArray (contextMap[cArray[i]])
3. loop through the smallest set and filter on the given pattern
4. return the filtered statements
if p and cArray are specified
1. get statements hashed to by the predicate (propMap[p])
2. get statements hashed to by the names in the cArray (contextMap[cArray[i]])
3. loop through the smallest set and filter on the given pattern
4. return the filtered statements
if o and cArray are specified
1. get statements hashed to by the object(objectMap[o])
2. get statements hashed to by the names in the cArray (contextMap[cArray[i]])
3. loop through the smallest set and filter on the given pattern
4. return the filtered statements
if s, o and cArray are specified
1. get statements hashed to by the subject (sMap[s])
2. get statements hashed to by the object (objectMap[o])
3. get statements hashed to by the names in the cArray (contextMap[cArray[i]])
4. pick the smallest set and filter it on the given pattern
5. return the filtered statements
if s, p, o, cArray are defined
1. create statements matching those patterns
2. call contains on the statements set for each of those statements
3. add each statement that returns true when calling the contains method to a set
4. return the set
in all cases where the given pattern contains more then one specified variable
if a single hash returns either null or an empty set, return an empty set
anzo.rdf.INamedGraph
An RDF graph that holds a collection of anzo.rdf.Statements. A named graph also has a name in the form of a URI (a.k.a. namedGraphUri) associated with it. This name is used to identify the graph. Arguments to the public methods such as find, add, etc can be passed as values or as anzo.rdf.Values, or any combination of the two.
anzo.rdf.NamedGraph
An implementation of the INamedGraph that stores all of its statements in a IQuadStore. The IQuadStore store it uses can be specified in the constructor. If not specified, the NamedGraph creates and uses an anzo.rdf.QuadStore? to store its statements. The NamedGraph can be seen as a view into the QuadStore? that only shows the statements associated with its name (namedGraphUri).
All statements in a graph are treated as triples, but stored in the underlying quad store as quads.
Example 1:
var graph = new anzo.rdf.NamedGraph('http://foo');
graph.add('http://a', 'http://b', 'http://c');
var resultStmt = graph.find('http://a', 'http://b', 'http://c')[0];
resultStmt .subject is 'http://a'
resultStmt .predicate is 'http://b'
resultStmt .object is 'http://c'
resultStmt .namedGraphUri is 'http://foo'
graph.remove('http://a', 'http://b', 'http://c'); // this removes the statement
Example 2 (adding a statement without a specified named graph uri):
var graph = new anzo.rdf.NamedGraph('http://foo');
var stmtTriple = anzo.createStatement('http://a', 'http://b', 'http://c');
graph.add(stmtTriple); // stores the triple in the statement and the name of the graph in the store
var resultStmt = graph.find('http://a', 'http://b', 'http://c')[0];
resultStmt.equals(stmtTriple) is false since the namedGraphUris don't match
resultStmt.tripleEquals(stmtTriple) is true since namedGraphUris are avoided
graph.remove(stmtTripe); // this will remove the statement since the graph compares triples not quads
Example 3 (adding a statement with a different named graph uri):
var graph = new anzo.rdf.NamedGraph('http://foo');
var stmtQuad = anzo.createStatement('http://a', 'http://b', 'http://c', 'http://d');
graph.add(stmtQuad); // stores the triple in the statement and the name of the graph in the store
var resultStmt = graph.find('http://a', 'http://b', 'http://c')[0];
resultStmt.equals(stmtQuad) is false
resultStmt.tripleEquals(stmtQuad) is true
graph.remove(stmtQuad); // this will remove the statement since the graph compares triples not quads
The name of the graph is either set in the constructor or generated by default if one is not specified.
anzo.rdf.GraphResource?
A ‘bean’ that takes a resource and a graph and provides convenience methods to access and set the properties of the given resource in the given graph. It has a getProperty method that allows users to easily access the properties of the resource by passing in a predicate:
var graph = new anzo.rdf.NamedGraph('http://graph_1');
graph.add('http://rouben', "http://xmlns.com/foaf/0.1/firstName", "Rouben");
var bean = new anzo.rdf.GraphResource('http://rouben', graph);
bean.getProperty("http://xmlns.com/foaf/0.1/firstName")[0].value // this returns 'Rouben'
Convenience methods can be generated for specified predicate(s) by using the createPropertyMethods method. Method names are created using the following algorithm.
- get: 'get'+predicate.localName (http://xmlns.com/foaf/0.1/firstName -> getFirstName)
- set: 'set'+predicate.localName (http://xmlns.com/foaf/0.1/firstName -> setFirstName)
- add: 'add'predicate.localName (http://xmlns.com/foaf/0.1/firstName -> addFirstName)
- remove: 'remove'+predicate.localName (http://xmlns.com/foaf/0.1/firstName -> removeFirstName)
- removeAll: 'removeAll'+predicate.localName (http://xmlns.com/foaf/0.1/firstName -> removeAllFirstName)
Event listeners can be connected via the 'connect' method:
// hook an event listener to listen to change of first name:
var stub = bean.connect('http://xmlns.com/foaf/0.1/knows', function() { <DO SOMETHING> });
bean.disconnect(stub); // unhook the listener
bean.disconnectAll(); // unhook all registered listeners
Example 1 (create methods for a single valued property):
var graph = new anzo.rdf.NamedGraph('http://graph_1');
graph.add('http://rouben', "http://xmlns.com/foaf/0.1/knows", "http://sean");
graph.add('http://rouben', "http://xmlns.com/foaf/0.1/firstName", "Rouben");
var bean = new anzo.rdf.GraphResource('http://rouben', graph);
bean.createPropertyMethods("http://xmlns.com/foaf/0.1/firstName");
bean.getFirstName()[0].value // this returns 'Rouben'
bean.connect("http://xmlns.com/foaf/0.1/firstName", function() { < DO SOMETHING > });
bean.setFirstName('Ben'); // this will fire an event and <DO SOMETHING> is called
bean.disconnectAll(); // disconnect listeners among potentially other things
Example 2 (create methods for a multi valued property):
var graph = new anzo.rdf.NamedGraph('http://graph_1');
graph.add('http://rouben', "http://xmlns.com/foaf/0.1/knows", "http://sean");
graph.add('http://rouben', "http://xmlns.com/foaf/0.1/firstName", "Rouben");
var bean = new anzo.rdf.GraphResource('http://rouben', graph);
bean.createPropertyMethods("http://xmlns.com/foaf/0.1/knows");
bean.getKnows()[0].value // this returns 'http://sean'
bean.connect("http://xmlns.com/foaf/0.1/knows", function() { < DO SOMETHING > });
bean.addKnows('http://ben'); // this will fire an event and <DO SOMETHING> is called
bean.removeKnows('http://sean'); // this also fires an event
bean.removeAllKnows(); // remove all 'knows' properties
bean.disconnectAll(); // disconnect listeners the bean has registered with the graph
anzo.rdf.vocabulary.XMLSchema
Pre-canned RDF properties of the XML Schema.
anzo.rdf.vocabulary.RDF
Pre-canned RDF properties of the RDF ontology.
anzo.rdf.vocabulary.RDFS
Pre-canned RDF properties of the RDFS ontology.
anzo.rdf.vocabulary.OWL
Pre-canned RDF properties of the OWL ontology.
Parsing
All parsers process RDF serialized into a specific format and add the parsed statements into an anzo.rdf.INamedGraph.
RDF/XML Parser
A parser taken from the MIT Tabulator project is wrapped and tweaked to use objects in this library. It accepts an RDF/XML string and an anzo.rdf.INamedGraph. The string is parsed and the statements are added to the given graph.
anzo.rdf.parser.TabulatorParser.parse(rdfXmlString, graph);
N-Triple Parser
anzo.rdf.parser.NTripleParser can be used to parse the given N-Triple string. The statements are stored in a given graph:
anzo.rdf.parser.NTripleParser.parse(nTripleString, graph);
Attachments
- namedGraphQuadStoreInteraction.jpg (35.7 kB) -
Shows interaction between NamedGraphs and QuadStores?.
, added by rmeschian on 10/25/07 20:39:45.



