practical rdf chapter 8. jena: rdf in java

17
Practical RDF Chapter 8. Jena: RDF in Java Shelley Powers, O’Reilly SNU IDB Lab. Somin Kim

Upload: calvin

Post on 22-Mar-2016

131 views

Category:

Documents


2 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Practical RDF Chapter 8. Jena: RDF in Java

Practical RDF

Chapter 8. Jena: RDF in JavaShelley Powers, O’Reilly

SNU IDB Lab.Somin Kim

Page 2: Practical RDF Chapter 8. Jena: RDF in Java

2

Outline Introduction Jena Architecture Creating and Serializing an RDF Model Parsing and Querying an RDF Document Persistent Model Storage

Page 3: Practical RDF Chapter 8. Jena: RDF in Java

3

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

Page 4: Practical RDF Chapter 8. Jena: RDF in Java

4

Jena Architecture

Network APIJoseki

Readers

ARP

n-triple

N3InferenceDAML APIRDFS API

StoragesMemory Berkeley

DBRDBMS

Ontology API

QuerySesameEngineRDQL Sesame

Writers

ARP

n-triple

N3

OWL API

Page 5: Practical RDF Chapter 8. Jena: RDF in Java

5

Creating and Serializing an RDF Model (1/8)

Example1 ……

// 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)

A new model is created,

with the resource and one predicate

repeated with two different

objects

Page 6: Practical RDF Chapter 8. Jena: RDF in Java

6

Creating and Serializing an RDF Model (2/8)

Output<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:Description rdf: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://burningbird.net/articles/monsters1.htm

http://burningbird.net/articles/monsters2.htm

Document

http://burningbird.net/bbd/postcon/elements/1.0/

http://www.w3.org/1999/02/22-rdf-syntax-ns#type

Page 7: Practical RDF Chapter 8. Jena: RDF in Java

7

Creating and Serializing an RDF Model (3/8)

Problem with creating the Property and Resource ob-jects 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

Page 8: Practical RDF Chapter 8. Jena: RDF in Java

8

Creating and Serializing an RDF Model (4/8)

Example2 : Using wrapper class to add properties to resource …… // 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)); }

……

Page 9: Practical RDF Chapter 8. Jena: RDF in Java

9

Creating and Serializing an RDF Model (5/8)

(cont.) vocabulary wrapper class <POSTCON>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); } } }

Page 10: Practical RDF Chapter 8. Jena: RDF in Java

10

Creating and Serializing an RDF Model (6/8)

Example3 : Adding a blank node to a model

– Blank node : a resource that does not have a specific URI

……// 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"));

……

Page 11: Practical RDF Chapter 8. Jena: RDF in Java

11

Creating and Serializing an RDF Model (7/8)

Example4 : Creating a Typed Node

– Typed node means that it is defined with a specific rdf:type property

……// 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://burningbird.net/articles/monsters1.htm http://burningbird.net/postcon/elements/1.0/Document

http://www.w3.org/1999/02/22-rdf-syntax-ns#type

Page 12: Practical RDF Chapter 8. Jena: RDF in Java

12

Creating and Serializing an RDF Model (8/8)

Example5 : Creating a Container

– RDF container : a grouping of related items Alt, Seq, Bag

...... Model model = new ModelMem( ); // Create an empty graph

Seq hist = 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);

......

Page 13: Practical RDF Chapter 8. Jena: RDF in Java

Parsing and Querying an RDF Document (1/2)

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)

13

……// Create memory model, read in RDF/XML document ModelMem model = new ModelMem( ); model.read(sUri);

// Print out objects in model using toString NodeIterator iter = model.listObjects( ); while (iter.hasNext( )) { System.out.println(" " + iter.next( ).toString( )); }

……

Page 14: Practical RDF Chapter 8. Jena: RDF in Java

Parsing and Querying an RDF Document (2/2)

Accessing Specific Values

14

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 StmtIterator iter = 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 ……}

......

Page 15: Practical RDF Chapter 8. Jena: RDF in Java

15

Persistent Model Storage (1/3) Jena also provides the capability to persist data to re-

lational database storage Jena supports differing storage layouts

– Generic : all statements are stored in a single table and re-sources 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

Page 16: Practical RDF Chapter 8. Jena: RDF in Java

Persistent Model Storage (2/3) Persisting two RDF/XML models to a MySQL database

16

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"); DBConnection dbcon = 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);} }

Page 17: Practical RDF Chapter 8. Jena: RDF in Java

Persistent Model Storage (3/3) Accessing RDF models stored in MySQL database

17

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); DBConnection dbcon = 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 ……

}}}