Knowledge Representation XI – IKT437

Apache Jena Part I RDF

Jan Pettersen Nytun, UiA 1 S P O

From Wikipedia, the free encyclopedia Apache Jena is an open source Semantic Web framework for Java. It provides an API to extract data from and write to RDF graphs.

Jan Pettersen Nytun, UIA, page 2 S P O The Jena Framework Includes

– A RDF API Reading and writing RDF in RDF/XML, , … Triples can be stored in memory or in database.

– ARQ Engine ARQ is a query engine that supports SPARQL.

Jan Pettersen Nytun, UIA, page 3 S P O – TDB Engine TDB can be used as a high performance RDF store on a single machine. A TDB store can be accessed and managed with the provided command line scripts and via the Jena API.

– Apache Jena Fuseki Apache Jena Fuseki is a SPARQL server supporting query and update. Fuseki is tightly integrated with TDB to provide a robust, transactional persistent storage layer.

Jan Pettersen Nytun, UIA, page 4 S P O From Wikipedia, the free encyclopedia:

- The framework has various internal reasoners and the Pellet reasoner can be set up to work in Jena.

- An OWL API

Jan Pettersen Nytun, UIA, page 5 Framework Architecture [6] Fuseki is an HTTP interface to RDF data. It supports SPARQL for querying and updating.

6 Focus of rest of this presentation:

7 S P O Some Useful Links

• Jena project page: http://jena.apache.org/index.html • Some Jena tutorials: http://jena.apache.org/tutorials/ • The API Javadocs: http://jena.apache.org/documentation/javadoc/ • Jena documentation overview: http://jena.apache.org/documentation/ • Tutorial: Jena Semantic Web Framework: http://kill.devc.at/node/84 • Jena: A Semantic Web Framework: http://trimc-nlp.blogspot.no/2013/06/introduction-to-jena.html

Jan Pettersen Nytun, UIA, page 8 S O P Start Using Jena (11 September 2015)

• I downloaded and installed Eclipse Mars (4.5) Release for Windows.

(http://www.eclipse.org/downloads/)

Update – last test used: Eclipse IDE for Java Developers Version: Neon Release (4.6.0) Build id: 20160613-1800

Jan Pettersen Nytun, UIA, page 9 S P O package task; I Tested Eclipse

public class TaskApp { public static void main(String[] args) { System.out.println("TaskApp started!"); System.out.println(System.getProperty("java.vendor")); System.out.println(System.getProperty("java.vendor.url")); System.out.println(System.getProperty("java.version")); } } Apache Jena 3 requires Java 8 Gave output: (from Jena version 3.0.0 onwards) TaskApp started! Oracle Corporation http://java.oracle.com/

1.8.0_31 Jan Pettersen Nytun, UIA, page 10 S P O

I downloaded the Jena libraries.

Jan Pettersen Nytun, UIA, page 11 S P O

I unzipped the Jena libraries. (Download site: http://jena.apache.org/download/index.cgi)

Jan Pettersen Nytun, UIA, page 12 S P O I simply copied the libraries to my project:

Jan Pettersen Nytun, UIA, page 13 S O P Adding the Jena libraries

Jan Pettersen Nytun, UIA, page 14 S P O

Jan Pettersen Nytun, UIA, page 15 package task; import org.apache.jena.datatypes.xsd.XSDDatatype; import org.apache.jena.rdf.model.Model; import org.apache.jena.rdf.model.ModelFactory; import org.apache.jena.rdf.model.Property; import org.apache.jena.rdf.model.Resource; public class TaskApp { public static void main(String[] args) { Model m = ModelFactory.createDefaultModel(); String NS = "http://example.com/test#"; Resource r = m.createResource(NS + "r"); Property p = m.createProperty(NS + "p"); r.addProperty(p, "HelloWorld",XSDDatatype.XSDstring); m.write(System.out,"Turtle"); } } Jan Pettersen Nytun, UIA, page 16 S P O Another Problem!

Jan Pettersen Nytun, UIA, page 17 S P O

…. Jena use as logging system, and the warning messages tell that you are lacking of a log4j.properties to initialize it…

Jan Pettersen Nytun, UIA, page 18 S P O Solve the “logging” problem

The downloaded Jena contains file jena-log4j.properties copy this file to the bin catalog of you Eclipse project and rename it to log4j.properties

Jan Pettersen Nytun, UIA, page 19 S P O

Start Programming in Jena

Jan Pettersen Nytun, UIA, page 20 package task; import org.apache.jena.datatypes.xsd.XSDDatatype; import org.apache.jena.rdf.model.Model; import org.apache.jena.rdf.model.ModelFactory; Making a import org.apache.jena.rdf.model.Property; import org.apache.jena.rdf.model.Resource; triple with public class TaskApp { Jena public static void main(String[] args) { Model m = ModelFactory.createDefaultModel(); String NS = "http://example.com/test#"; Resource r = m.createResource(NS + "r"); Property p = m.createProperty(NS + "p"); r.addProperty(p, "HelloWorld",XSDDatatype.XSDstring); m.write(System.out,"Turtle"); } Output: } "HelloWorld" . Output: "do task one" .

"TASK SET NO. 1" .

22 public static void printTriples(Model model){ StmtIterator iter = model.listStatements(); Print all triples while (iter.hasNext()) { Statement stmt = iter.nextStatement(); // get next statement Resource subject = stmt.getSubject(); // get the subject Property predicate = stmt.getPredicate(); // get the predicate RDFNode object = stmt.getObject(); // get the object

System.out.print(subject.toString()); System.out.print(" " + predicate.toString() + " "); if (object instanceof Resource) { System.out.print(object.toString()); } else { // object is a literal System.out.print(" \"" + object.toString() + "\""); } System.out.println(" ."); } } Jan Pettersen Nytun, UIA, page 23 S O P Write model to file

… String fileName = "testFile.ttl"; … try { OutputStream outFile = new FileOutputStream(fileName); model.write(outFile,"Turtle"); } catch (FileNotFoundException e) { e.printStackTrace(); } ……

24 S O P Read model from file

… String fileName = "testFile.ttl"; … InputStream in = FileManager.get().open( fileName ); if (in == null) { throw new IllegalArgumentException( "File: " + fileName + " not found"); } else { model.read(in, null, "Turtle"); } …

25 S P O References [1] Book: David Poole and Alan Mackworth, Artificial Intelligence: Foundations of Computational Agents, Cambridge University Press, 2010, http://artint.info/

[2] http://www.w3.org/TR/swbp-n-aryRelations/

[3] SPARQL 1.1 Query Language, W3C Recommendation 21 March 2013, http://www.w3.org/TR/2013/REC-sparql11-query-20130321/

[4] Semantic Web for the Working Ontologist, Second Edition: Effective Modeling in RDFS and OWL, May 20, 2011, by Dean Allemang, James Hendler

[5] Appreciating SPARQL CONSTRUCT more, Bob DuCharme's weblog, http://www.snee.com/bobdc.blog/2009/09/appreciating-sparql-construct.html

[6] Getting started with Apache Jena, http://jena.apache.org/getting_started/index.html

Jan Pettersen Nytun, UIA, page 26 S P O

• Tutorial: Jena Semantic Web Framework: http://kill.devc.at/node/84 • Jena: A Semantic Web Framework: http://trimc- nlp.blogspot.no/2013/06/introduction-to- jena.html • More Jena: http://docslide.us/documents/jena5448cad4b1af 9f606b8b485f.html

Jan Pettersen Nytun, UIA, page 27