Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Knowledge Representation XI – IKT437 Knowledge Representation XI – IKT437 Part I RDF Jan Pettersen Nytun, UiA Apache Jena.

Similar presentations


Presentation on theme: "1 Knowledge Representation XI – IKT437 Knowledge Representation XI – IKT437 Part I RDF Jan Pettersen Nytun, UiA Apache Jena."— Presentation transcript:

1 1 Knowledge Representation XI – IKT437 Knowledge Representation XI – IKT437 Part I RDF Jan Pettersen Nytun, UiA Apache Jena

2 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

3 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

4 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

5 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 6 Framework Architecture [6] Fuseki is an HTTP interface to RDF data. It supports SPARQL for querying and updating.

7 7 Focus of rest of this presentation:

8 S O P Some Useful Links Jena project page: http://jena.apache.org/index.html http://jena.apache.org/index.html Some Jena tutorials: http://jena.apache.org/tutorials/ http://jena.apache.org/tutorials/ The API Javadocs: http://jena.apache.org/documentation/javadoc/ http://jena.apache.org/documentation/javadoc/ Jena documentation overview: http://jena.apache.org/documentation/ http://jena.apache.org/documentation/ Tutorial: Jena Semantic Web Framework: http://kill.devc.at/node/84 http://kill.devc.at/node/84 Jena: A Semantic Web Framework: http://trimc-nlp.blogspot.no/2013/06/introduction-to-jena.html http://trimc-nlp.blogspot.no/2013/06/introduction-to-jena.html Jan Pettersen Nytun, UIA, page 8

9 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/) Jan Pettersen Nytun, UIA, page 9

10 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 http://java.oracle.com/ 1.8.0_31 Jan Pettersen Nytun, UIA, page 10 Apache Jena 3 requires Java 8 (from Jena version 3.0.0 onwards) I Tested Eclipse

11 S O P I used: A complete beginner's guide to starting a Jena project in Eclipse (http://www.iandickinson.me.uk/articles/jena-eclipse-helloworld/) This guide is a bit outdated, but still useful. Jan Pettersen Nytun, UIA, page 11

12 S O P I performed Step 3: Adding the Jena libraries I downloaded and unzipped the Jena libraries. – Download site: http://jena.apache.org/download/index.cgi http://jena.apache.org/download/index.cgi 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

13 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?

14 S O P Fixing the package names: Jan Pettersen Nytun, UIA, page 14

15 S O P Another Problem! Jan Pettersen Nytun, UIA, page 15

16 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

17 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.

18 S O P Start Programming in Jena Jan Pettersen Nytun, UIA, page 18

19 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"); } Output: "HelloWorld". Making a triple with Jena

20 20 Output: "do task one". "TASK SET NO. 1". Output: "do task one". "TASK SET NO. 1".

21 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

22 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

23 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

24 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, http://artint.info/http://artint.info/ [2] http://www.w3.org/TR/swbp-n-aryRelations/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/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.htmlhttp://www.snee.com/bobdc.blog/2009/09/appreciating-sparql-construct.htmlhttp://jena.apache.org/getting_started/index.html

25 S O P Tutorial: Jena Semantic Web Framework: http://kill.devc.at/node/84 http://kill.devc.at/node/84 Jena: A Semantic Web Framework: http://trimc- nlp.blogspot.no/2013/06/introduction-to- jena.htmlhttp://trimc- nlp.blogspot.no/2013/06/introduction-to- jena.html Videregående Jena: http://docslide.us/documents/jena5448cad4b1af 9f606b8b485f.html http://docslide.us/documents/jena5448cad4b1af 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 25


Download ppt "1 Knowledge Representation XI – IKT437 Knowledge Representation XI – IKT437 Part I RDF Jan Pettersen Nytun, UiA Apache Jena."

Similar presentations


Ads by Google