What most people don’t know is that openanzo has a powerful RDF javascript library. The library can be used as is, but of course it’s designed to work seamlessly with the rest of the openanzo stack. The RDF library is located in the org.openanzo.js project in src/main/resources/docroot/anzo/rdf

Statements, Resources, Literals …

Every RDF library has to have these basics. Here is the inheritance hierarchy:
  • anzo.rdf.Value – base class for all rdf nodes
    • anzo.rdf.Resource – base class for all rdf resources
      • anzo.rdf.URI – equals, toString, serialize, getNamespace, getLocalName
      • anzo.rdf.BNode – equals, toString, serialize
    • anzo.rdf.Literal – equals, toString, serialize, getNativeValue

anzo.rdf.URI

A javascript object representing a URI.
  
   var uri = anzo.createURI("http://www.example.org/foo");
   uri.serialize(); // "<http://www.example.org/foo>"
   uri.getNamespace(); // "http://www.example.org/"
   uri.getLocalName(); // "foo"
We can use short prefixes if we register the namespace:
  
   anzo.rdf.registerNamespace("ex", "http://www.example.org/");
   var uri = anzo.createURI("ex:foo");

anzo.rdf.BNode

Blank nodes are supported by all parts of the library.
  
   var bnode = anzo.createBNode("324453142134353214");
   bnode.toString(); // "324453142134353214"
   bnode.serialize(); //  "_:324453142134353214"

anzo.rdf.Literal

An anzo.rdf.Literal is the object that holds a value like a string, number, boolean, etc.
Creating a literal:
  
      var lit1= anzo.createLiteral("dog");
      lit1.toString(); // "dog"
      lit1.serialize(); // "dog"

      var lit2 = anzo.createLiteral("dog", "en"); // we can specify the language of the literal as the second argument
      lit2.toString(); // "dog"
      lit2.serialize(); // "dog"@en
Creating a typed literal:
  
      var lit3 = anzo.createTypedLiteral(5, "http://www.w3.org/2001/XMLSchema#int");
      lit3.toString(); // "5"
      lit3.serialize(); // "5"^^<http://www.w3.org/2001/XMLSchema#int>
      lit3.getNativeValue(); // returns a javascript number 5
      lit3.datatype; // returns http://www.w3.org/2001/XMLSchema#int as an anzo.rdf.URI
The getNativeValue method allows you to get a native javascript object from any literal. This makes rendering and editing rdf data with widgets from common libraries such as YUI, dojo and jquery very easy. Here is a table showing the mappings between the literal and the type of javascript value you can expect when calling getNativeValue:
XSD DatatypeJavascript Type
http://www.w3.org/2001/XMLSchema#dateTimeDate
http://www.w3.org/2001/XMLSchema#dateDate
http://www.w3.org/2001/XMLSchema#timeDate
http://www.w3.org/2001/XMLSchema#gYearMonthDate
http://www.w3.org/2001/XMLSchema#gYearDate
http://www.w3.org/2001/XMLSchema#gMonthDayDate
http://www.w3.org/2001/XMLSchema#gDayDate
http://www.w3.org/2001/XMLSchema#gMonthDate
http://www.w3.org/2001/XMLSchema#stringString
http://www.w3.org/2001/XMLSchema#booleanBoolean
http://www.w3.org/2001/XMLSchema#decimalNumber
http://www.w3.org/2001/XMLSchema#doubleNumber
http://www.w3.org/2001/XMLSchema#integerNumber
http://www.w3.org/2001/XMLSchema#longNumber
http://www.w3.org/2001/XMLSchema#shortNumber
http://www.w3.org/2001/XMLSchema#byteNumber
http://www.w3.org/2001/XMLSchema#nonPositiveIntegerNumber
http://www.w3.org/2001/XMLSchema#negativeIntegerNumber
http://www.w3.org/2001/XMLSchema#nonnegativeIntegerNumber
http://www.w3.org/2001/XMLSchema#positiveIntegerNumber
http://www.w3.org/2001/XMLSchema#unsignedLongNumber
http://www.w3.org/2001/XMLSchema#unsignedIntNumber
http://www.w3.org/2001/XMLSchema#unsignedShortNumber
http://www.w3.org/2001/XMLSchema#unsignedByteNumber
All the restString

Example of rendering an xsd:date literal in a dojo widget:
  
  var date = new Date(1216330344703); // Thu Jul 17 21:32:24.703 UTC 2008
  var dateLit = anzo.createTypedLiteral(date, "http://www.w3.org/2001/XMLSchema#date");
  dojoWidget.attr('value', dateLit.getNativeValue()); // this renders the literal value in the widget
The serialize method serializes the rdf node into a string that can then easily be deserialized by calling anzo.rdf.getValue(serializedString). This serialization can be used in constructing a SPARQL query:
  
var query = "select ?x where { ?x " + uri.serialize() + " "+lit.serialize() + " }";
// the query will look something like this: select ?x where { ?x <http://www.example.org/age> "5"^^<http://www.w3.org/2001/XMLSchema#int> }

anzo.rdf.Statement

A statement in this library can be used as either a triple or a quad.
  
   var triple = anzo.createStatement("ex:foo", "ex:bar", "dog");
   var quad = anzo.createStatement("ex:foo", "ex:bar", "dog", "ex:graph");

Graphs

anzo.rdf.NamedGraph

An in memory RDF graph. While intended to be used as a named graph, this graph could also be used as just a regular graph with no name.
  
    anzo.rdf.registerNamespace("ex", "http://www.example.org/");

    var graph = new anzo.rdf.NamedGraph("ex:mygraph"); // the graph name URI is optional
    graph.add("ex:foo", "ex:title", "dog");
    graph.find("ex:foo", null, "dog"); // returns an array with a single statement in it
    graph.contains(null, "ex:title"); // true
    graph.size(); // 1
    graph.clear(); // removes all statements - same as calling remove();
Note that in the graph methods allow you to pass native javascript objects. The library will try to guess what you meant and create the proper rdf nodes for you underneath. Be very careful using this feature as the guessed types may not be exactly what you want. The library has a number of other features including a quad store, parsers, etc that I will cover in future tutorials. -Happy hacking Openanzo is an opensource project distributed under the Eclipse Public License.