1 Knowledge Representation XI – IKT437 Knowledge Representation XI – IKT437 Part I RDF Jan Pettersen Nytun, UiA Apache Jena
S O P 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 From Wikipedia, the free encyclopedia
S O P The Jena Framework Includes – A RDF API Reading and writing RDF in RDF/XML, Turtle, … 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 O P – 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 O P - The framework has various internal reasoners and the Pellet reasoner can be set up to work in Jena.Pellet reasoner - An OWL API Jan Pettersen Nytun, UIA, page 5 From Wikipedia, the free encyclopedia:
6 Framework Architecture [6] Fuseki is an HTTP interface to RDF data. It supports SPARQL for querying and updating.
7 Focus of rest of this presentation:
S O P Some Useful Links Jena project page: Some Jena tutorials: The API Javadocs: Jena documentation overview: Tutorial: Jena Semantic Web Framework: Jena: A Semantic Web Framework: 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. ( Jan Pettersen Nytun, UIA, page 9
S O P package task; 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")); } Gave output: TaskApp started! Oracle Corporation _31 Jan Pettersen Nytun, UIA, page 10 Apache Jena 3 requires Java 8 (from Jena version onwards) I Tested Eclipse
S O P I used: A complete beginner's guide to starting a Jena project in Eclipse ( This guide is a bit outdated, but still useful. Jan Pettersen Nytun, UIA, page 11
S O P I performed Step 3: Adding the Jena libraries I downloaded and unzipped the Jena libraries. – Download site: Added the libraries to the Eclipse project as described in “A complete beginner's guide to starting a Jena project in Eclipse”. Jan Pettersen Nytun, UIA, page 12
S O P Jan Pettersen Nytun, UIA, page 13 The new version of Jena has new package names, it should be org.apache.jena…. But package com.hp.jena… is still not found?
S O P Fixing the package names: Jan Pettersen Nytun, UIA, page 14
S O P Another Problem! Jan Pettersen Nytun, UIA, page 15
S O P …. Jena use log4j as logging system, and the warning messages tell explicitly that you are lacking of a log4j.properties to initialize it… Jan Pettersen Nytun, UIA, page 16
S O P Solve the “logging” problem Jan Pettersen Nytun, UIA, page 17 The downloaded Jena contains file jena-log4j. Copy this file to the bin catalog of you Eclipse project and rename it to log4j.
S O P Start Programming in Jena Jan Pettersen Nytun, UIA, page 18
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 = " 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". Making a triple with Jena
20 Output: "do task one". "TASK SET NO. 1". Output: "do task one". "TASK SET NO. 1".
public static void printTriples(Model model){ StmtIterator iter = model.listStatements(); 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 21 Print all triples
S O P … String fileName = "testFile.ttl"; … try { OutputStream outFile = new FileOutputStream(fileName); model. write (outFile,"Turtle"); } catch (FileNotFoundException e) { e.printStackTrace(); } …… 22 Write model to file
S O P … 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"); } … 23 Read model from file
S O P References Jan Pettersen Nytun, UIA, page 24 [1] Book: David Poole and Alan Mackworth, Artificial Intelligence: Foundations of Computational Agents, Cambridge University Press, 2010, [2] [3] SPARQL 1.1 Query Language, W3C Recommendation 21 March 2013, [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, [6] Getting started with Apache Jena,
S O P Tutorial: Jena Semantic Web Framework: Jena: A Semantic Web Framework: nlp.blogspot.no/2013/06/introduction-to- jena.htmlhttp://trimc- nlp.blogspot.no/2013/06/introduction-to- jena.html Videregående Jena: 9f606b8b485f.html 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 25