Openanzo has a powerful RDF javascript library in the org.openanzo.js project. In previous posts I talked about some of the low level API available to create, store and find RDF statements. In this tutorial I’ll talk about a simple object called anzo.rdf.GraphResource which takes a graph and resource and creates a bean with familiar getters and setters to allow you to easily modify the properties of that resource.

 // register namespaces to make this example simpler
 anzo.rdf.registerNamespace("ex", "");
 anzo.rdf.registerNamespace("foaf", "");

 // 1. build a sample graph with some data in it
 var graph = new anzo.rdf.NamedGraph('http://graph_1');
 graph.add('ex:me', "foaf:firstName", "Rouben");
 graph.add('ex:me', "foaf:surname", "Meschian");
 graph.add('ex:me', "foaf:knows", "ex:sean");

 // 2. create a bean and specify which properties you want to create methods for
 var bean = new anzo.rdf.GraphResource('ex:me', graph, [ "foaf:firstName", "foaf:surname" ]);
 bean.getFirstName(); // returns a single literal "Rouben"
 bean.setSurname("Smith"); // sets my surname to "Smith"

 // 3. add additional properties whenever you want
 bean.getKnows(); // returns an array with Sean and Ben's URIs