Apache Jena Framework Philippe Genoud Université Joseph Fourier Grenoble (France) (Philippe.Genoud@imag.fr) Astrakhan State University November 2012 1
What is Jena? Introduction An open source semantic web framework written in Java developed at HP-Labs (Bristol-UK) now an apache project : http://jena.apache.org/ November 2010: adopted by the Apache Software Foundation (incubation) April 2012: graduated as a top-level project 2
Jena is composed of Introduction an API for reading, processing and writing RDF data in XML, N-triples and Turtle formats; an ontology API for handling OWL and RDFS ontologies; a rule-based inference engine for reasoning with RDF and OWL data sources; stores to allow large numbers of RDF triples to be efficiently stored on disk; a query engine compliant with the latest SPARQL specification servers to allow RDF data to be published to other applications using a variety of protocols, including SPARQL 3
Model interface com.hp.hpl.jena.rdf.model.model represent an RDF Graph RDF model is a set of Statements (triples). Methods are provided for creating resources, properties and literals creating the Statements (triples) which link them, for adding statements to and removing them from a model, for querying a model set operations for combining models. 4
Model Factory design pattern to create models com.hp.hpl.jena.rdf.model.modelfactory static Model createdefaultmodel() Answer a new memory based Model (RDF Graph) static ModelMaker createfilemodelmaker(string root) Answer a ModelMaker that constructs memory-based Models that are backed by files static OntModel createontologymodel() Answer a new ontology model which will process in-memory models of ontologies expressed the default ontology language (OWL) 5
Light Switch1 status RDFNodes on locatedin Bedroom <interface> RDFNode each element of a RDF statement is a RDFNode <interface> Resource <interface> Literal <interface> Property com.hp.hpl.jena.rdf.model.model offers methods to create elements Resource createresource( ) Literal createliteral( ) Property createproperty(string namespace, String localname) 6
Triples to create an RDF triple in a Model a Property or a Literal must be "added" to a Resource. Resource methods to create triples with the resource (this) as subject. Resource addproperty(property p, RDFNode o) addproperty(property p, RDFNode o) addproperty(property p, java.lang.string o) Resource addliteral( ) addliteral(property p, Literal o) addliteral(property value, double d) 7
Triples addproperty and addliteral methods return the Resource (this) facilitates creation of multiple triples with the same subject Resource johnsmith = model.createresource(personuri).addproperty(vcard.fn, fullname).addproperty(vcard.title, "Officer"); vcard : a standard file format for electronic business cards e.g. vcards are often attached to e-mail messages http://www.w3.org/tr/vcard-rdf/ RDF vocabulary corresponding to vcard com.hp.hpl.jena.vocabulary : package containing constant classes with predefined constant objects for classes and properties defined in well known vocabularies. com.hp.hpl.jena.vocabulary.vcard : vcard static Property NAME. com.hp.hpl.jena.vocabulary.rdf : The standard RDF vocabulary static Property type... com.hp.hpl.jena.vocabulary.dc : Dublin Core... 8
Statement Statement object to access the components of a triple Resource getsubject() Property getpredicate() RDFNode getobject()list all statements of the model StatementIterator to access statements asserted in a RDF Model Model methods to get a StatementIterator StmtIterator liststatements() List all statements of the model StmtIterator liststatements(resource s, Property p, RDFNode o) List all the statements matching a pattern for(statementiterator it = model.liststatements();it.hasnext();) { } Statement stmt = it.nextstatement(); System.out.println(stmt.getSubject().getLocalName()); 9
Navigating a Model for(statementiterator it = model.liststatements();it.hasnext();) { } Statement stmt = it.nextstatement(); System.out.println(stmt.getSubject().getLocalName()); Reification Subject query Property Object RDF Consumer Model animation from www.try.idv.tw/try/files/070517-jena.ppt C.F.Liao ( 廖峻鋒 )
Model I/O read and write methods of Model interface e.g. write(java.io.outputstream out, java.lang.string lang, java.lang.string base) lang : output format "RDF/XML" (default), "RDF/XML-ABBREV", "N3", "TURTLE", "N-TRIPLE" base : The base uri for relative URI calculations. null means use only absolute URI's URL FileWriter FileReader Model System.out 11
Lab Creating a Simple RDF Model 12
Objectives Creating a simple RDF statement Placing this RDF into a Model Dumping the Model to system console print add Model (Memory) RDF Producer
Creating a RDF statement // declare URI prefix String ns = "http://www.ispace.tw/smarthome#"; ex1 demo Model model = ModelFactory.createDefaultModel(); Resource light = model.createresource(ns+"light1"); Property lightstatus = model.createproperty(ns+"status"); Literal statusvalue = model.createliteral("off"); light.addproperty(lightstatus, statusvalue); model.write(system.out,"n-triple"); Model status (light,status,off) light1 off