180 likes | 468 Views
Practical RDF Chapter 8. Jena: RDF in Java. Shelley Powers, O’Reilly SNU IDB Lab. Somin Kim. Outline. Introduction Jena Architecture Creating and Serializing an RDF Model Parsing and Querying an RDF Document Persistent Model Storage. Introduction. A Java Framework for RDF, DAML and OWL
E N D
Practical RDFChapter 8. Jena: RDF in Java Shelley Powers, O’Reilly SNU IDB Lab. Somin Kim
Outline Introduction Jena Architecture Creating and Serializing an RDF Model Parsing and Querying an RDF Document Persistent Model Storage
Introduction A Java Framework for RDF, DAML and OWL Developed by Brian McBride of HP Labs Derived from SiRPAC Can create, parse, navigate and search RDF, DAML and OWL model Easy to use Available at http://jena.sourceforge.net
Network API Joseki Sesame Query RDQL SesameEngine Inference RDFS API DAML API OWL API Readers ARP Writers ARP Ontology API N3 N3 Storages n-triple Memory RDBMS Berkeley DB n-triple Jena Architecture
Creating and Serializing an RDF Model (1/8) A new model is created, with the resource and one predicate repeated with two different objects …… // Create an empty graph Model model = new ModelMem( ); // Create the resource Resource postcon = model.createResource(sURI); // Create the predicate (property) Property related = model.createProperty(sPostcon, sRelated); // Add the properties with associated values (objects) postcon.addProperty(related, "http://burningbird.net/articles/monsters3.htm"); postcon.addProperty(related, "http://burningbird.net/articles/monsters2.htm"); // Print RDF/XML of model to system output model.write(new PrintWriter(System.out));…… addProperty( property, property value) • Example1
Creating and Serializing an RDF Model (2/8) <rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#' xmlns:NS0='http://www.burningbird.net/postcon/elements/1.0/' > <rdf:Descriptionrdf:about='http://burningbird.net/articles/monsters1.htm'> <NS0:related>http://burningbird.net/articles/monsters3.htm</NS0:related> <NS0:related>http://burningbird.net/articles/monsters2.htm</NS0:related> </rdf:Description> </rdf:RDF> http://www.w3.org/1999/02 /22-rdf-syntax-ns#type Document http://burningbird.net/articles/monsters1.htm http://burningbird.net/articles/monsters2.htm http://burningbird.net/ bbd/postcon/elements/1.0/ Output
Creating and Serializing an RDF Model (3/8) • Problem with creating the Property and Resource objects directly in the application • You have to duplicate this functionality across all applications that want to use the vocabulary • It adds to the overall size and complexity of an application • A Java Wrapper Class is a better approach
Creating and Serializing an RDF Model (4/8) …… // Resource names String sResource = "http://burningbird.net/articles/monsters1.htm"; String sRelResource1 = "http://burningbird.net/articles/monsters2.htm"; String sRelResource2 = "http://burningbird.net/articles/monsters3.htm"; try { // Create an empty graph Model model = new ModelMem( ); // Create the resource and add the properties cascading style Resource article = model.createResource(sResource) .addProperty(POSTCON.related, model.createResource(sRelResource1)) .addProperty(POSTCON.related, model.createResource(sRelResource2)); // Print RDF/XML of model to system output model.write(new PrintWriter(System.out)); } …… • Example2 : Using wrapper class to add properties to resource
Creating and Serializing an RDF Model (5/8) public class POSTCON extends Object { // URI for vocabulary elements protected static final String uri = "http://burningbird.net/postcon/elements/1.0/"; public static String getURI( ) // Return URI for vocabulary elements { return uri; } static final String nbio = "bio"; // Define the property labels and objects public static Property bio = null; static final String nrelated = "related"; public static Property related = null; ...... static { // Instantiate the properties and the resource try { // Instantiate the properties bio = new PropertyImpl(uri, nbio); related = new PropertyImpl(uri, nrelated); ...... } catch (RDFException e) { ErrorHelper.logInternalError("POSTCON", 1, e); } } } • (cont.) vocabulary wrapper class <POSTCON>
Creating and Serializing an RDF Model (6/8) …… // Create the bio bnode resource and add properties Resource bio = model.createResource( ) .addProperty(DC.creator, "Shelley Powers") .addProperty(DC.publisher, "Burningbird") .addProperty(DC.title, model.createLiteral("Tale of Two Monsters: Legends", "en")); …… • Example3 : Adding a blank node to a model • Blank node : a resource that does not have a specific URI
Creating and Serializing an RDF Model (7/8) …… // Create an empty graph Model model = new ModelMem( ); // Create the resource // and add the properties cascading style Resource article = model.createResource(sResource).addProperty(RDF.type, POSTCON.resource); …… http://www.w3.org/1999/02 /22-rdf-syntax-ns#type http://burningbird.net/articles/monsters1.htm http://burningbird.net/postcon/elements/1.0/Document • Example4 : Creating a Typed Node • Typed node means that it is defined with a specific rdf:type property
Creating and Serializing an RDF Model (8/8) ...... Model model = new ModelMem( ); // Create an empty graph Seqhist = model.createSeq( ) // Create Seq .add (1, model.createResource(sHistory1) .addProperty(POSTCON.movementtype, model.createLiteral("Add")) .addProperty(POSTCON.reason, model.createLiteral("New Article")) .addProperty(DC.date, model.createLiteral("1998-01-01T00:00:00-05:00"))) .add (2, model.createResource(sHistory2) .addProperty(POSTCON.movementtype, model.createLiteral("Move")) ...... // Create the resource and add the properties cascading style Resource article = model.createResource(sResource) .addProperty(POSTCON.history, hist); ...... • Example5 : Creating a Container • RDF container : a grouping of related items • Alt, Seq, Bag
Parsing and Querying an RDF Document (1/2) …… // Create memory model, read in RDF/XML document ModelMem model = new ModelMem( ); model.read(sUri); // Print out objects in model using toString NodeIteratoriter = model.listObjects( ); while (iter.hasNext( )) { System.out.println(" " + iter.next( ).toString( )); } …… • Basic dump: to print it out in N-Triples format • Instead of objects, you could also dump out the subjects(ResIterator and listSubjects) or even the entire statement (StmtIterator and listStatements)
Parsing and Querying an RDF Document (2/2) import com.burningbird.postcon.vocabulary.POSTCON; public class pracRDFSeventh extends Object { public static void main (String args[]) { String sUri = args[0]; String sResource = args[1]; try { ModelMem model = new ModelMem( ); // Create memory model, read in RDF/XML document model.read(sUri); Resource res = model.getResource(sResource); //Find Resource StmtIteratoriter = res.listProperties( ); //Find Properties while (iter.hasNext( )) { // Print out triple - subject | property | object // Next statement in queue Statement stmt = iter.next( ); // Get subject, print Resource res2 = stmt.getSubject( ); System.out.print(res2.getNameSpace( ) + res2.getLocalName( )); // Get predicate, print …… // Get object, print …… } ...... Accessing Specific Values
Persistent Model Storage (1/3) • Jena also provides the capability to persist data to relational database storage • Jena supports differing storage layouts • Generic : all statements are stored in a single table and resources and literals are indexed using integer identifiers generated by the database • GenericProc : similar to generic, but data access is through stored procedures • MMGeneric : similar to generic, but can store multiple models • Hash : similar to generic, but uses MD5 hashes to generate the identifiers • MMHash : Similar to hash, but can store multiple models
Persistent Model Storage (2/3) public class pracRDFEighth extends Object { public static void main (String args[]) { // Pass two RDF documents, connection string, String sUri = args[0]; String sUri2 = args[1]; String sConn = args[2]; String sUser = args[3]; String sPass = args[4]; Class.forName("com.mysql.jdbc.Driver").newInstance( ); // Load driver class // Establish connection - replace with your own conn info Connection con = DriverManager.getConnection(sConn, "user", "pass"); DBConnectiondbcon = new DBConnection(con); ModelRDB.create(dbcon, "MMGeneric", "Mysql"); // Format database ModelRDB model1 = ModelRDB.createModel(dbcon, "one"); // Create and read first model model1.read(sUri); ModelRDB model2 = ModelRDB.createModel(dbcon, "two"); // Create and read second model model2.read(sUri2); } } Persisting two RDF/XML models to a MySQL database
Persistent Model Storage (3/3) public class pracRDFNinth extends Object { public static void main (String args[]) { String sConn = args[0]; String sUser = args[1]; String sPass = args[2]; // load driver class Class.forName("com.mysql.jdbc.Driver").newInstance( ); // Establish connection - replace with your own conn info Connection con = DriverManager.getConnection(sConn, sUser, sPass); DBConnectiondbcon = new DBConnection(con); // Open two existing models ModelRDB model1 = ModelRDB.open(dbcon, "one"); ModelRDB model2 = ModelRDB.open(dbcon, "two"); // Print out objects in first model using toString …… // Print out triples in second model - find resource …… }}} Accessing RDF models stored in MySQL database