Knowledge Representation XI IKT 437 Apache Jena Part

  • Slides: 25
Download presentation
Knowledge Representation XI – IKT 437 Apache Jena Part I RDF Jan Pettersen Nytun,

Knowledge Representation XI – IKT 437 Apache Jena Part I RDF Jan Pettersen Nytun, Ui. A 1

S P O From Wikipedia, the free encyclopedia Apache Jena is an open source

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

S P O 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 P O – TDB Engine TDB can be used as a high performance

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

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

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

Focus of rest of this presentation: 7

S P O Some Useful Links • Jena project page: http: //jena. apache. org/index.

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 P O Start Using Jena (11 September 2015) • I downloaded and installed

S P O Start Using Jena (11 September 2015) • I downloaded and installed Eclipse Mars (4. 5) Release for Windows. (http: //www. eclipse. org/d ownloads/) Jan Pettersen Nytun, UIA, page 9

S P O package task; I Tested Eclipse public class Task. App { public

S P O package task; I Tested Eclipse public class Task. App { public static void main(String[] args) { System. out. println("Task. App started!"); System. out. println(System. get. Property("java. vendor")); System. out. println(System. get. Property("java. vendor. url")); System. out. println(System. get. Property("java. version")); } } Gave output: Task. App started! Oracle Corporation http: //java. oracle. com/ 1. 8. 0_31 Apache Jena 3 requires Java 8 (from Jena version 3. 0. 0 onwards) Jan Pettersen Nytun, UIA, page 10

S P O I used: A complete beginner's guide to starting a Jena project

S P O 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

S P O I performed Step 3: Adding the Jena libraries • I downloaded

S P O I performed Step 3: Adding the Jena libraries • I downloaded and unzipped the Jena libraries. – Download site: 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

S P O But package com. hp. jena… is still not found? The new

S P O But package com. hp. jena… is still not found? The new version of Jena has new package names, it should be org. apache. jena…. Jan Pettersen Nytun, UIA, page 13

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

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

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

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

S P O …. Jena use log 4 j as logging system, and the

S P O …. Jena use log 4 j as logging system, and the warning messages tell explicitly that you are lacking of a log 4 j. properties to initialize it… Jan Pettersen Nytun, UIA, page 16

S P O Solve the “logging” problem The downloaded Jena contains file jena-log 4

S P O Solve the “logging” problem The downloaded Jena contains file jena-log 4 j. Copy this file to the bin catalog of you Eclipse project and rename it to log 4 j. Jan Pettersen Nytun, UIA, page 17

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

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

package task; import org. apache. jena. datatypes. xsd. XSDDatatype; import org. apache. jena. rdf.

package task; import org. apache. jena. datatypes. xsd. XSDDatatype; import org. apache. jena. rdf. model. Model. Factory; import org. apache. jena. rdf. model. Property; import org. apache. jena. rdf. model. Resource; Making a triple with Jena public class Task. App { public static void main(String[] args) { Model m = Model. Factory. create. Default. Model(); String NS = "http: //example. com/test#"; Resource r = m. create. Resource(NS + "r"); Property p = m. create. Property(NS + "p"); r. add. Property(p, "Hello. World", XSDDatatype. XSDstring); m. write(System. out, "Turtle"); } Output: } <http: //example. com/test#r> <http: //example. com/test#p> "Hello. World".

Output: <http: //uia. no/termit/task#task. Set 1> <http: //uia. no/termit/task#has. Task> "do task one". <http:

Output: <http: //uia. no/termit/task#task. Set 1> <http: //uia. no/termit/task#has. Task> "do task one". <http: //uia. no/termit/task#task. Set 2> <http: //uia. no/termit/task#has. Task. Set. Name> "TASK SET NO. 1". 20

public static void print. Triples(Model model){ Stmt. Iterator iter = model. list. Statements(); Print

public static void print. Triples(Model model){ Stmt. Iterator iter = model. list. Statements(); Print all triples while (iter. has. Next()) { Statement stmt = iter. next. Statement(); // get next statement Resource subject = stmt. get. Subject(); // get the subject Property predicate = stmt. get. Predicate(); // get the predicate RDFNode object = stmt. get. Object(); // get the object System. out. print(subject. to. String()); System. out. print(" " + predicate. to. String() + " "); if (object instanceof Resource) { System. out. print(object. to. String()); } else { // object is a literal System. out. print(" "" + object. to. String() + """); } System. out. println(". "); } } Jan Pettersen Nytun, UIA, page 21

S P O Write model to file … String file. Name = "test. File.

S P O Write model to file … String file. Name = "test. File. ttl"; … try { Output. Stream out. File = new File. Output. Stream(file. Name); model. write(out. File, "Turtle"); } catch (File. Not. Found. Exception e) { e. print. Stack. Trace(); } …… 22

S P O Read model from file … String file. Name = "test. File.

S P O Read model from file … String file. Name = "test. File. ttl"; … Input. Stream in = File. Manager. get(). open( file. Name ); if (in == null) { throw new Illegal. Argument. Exception( "File: " + file. Name + " not found"); } else { model. read(in, null, "Turtle"); } … 23

S P O References [1] Book: David Poole and Alan Mackworth, Artificial Intelligence: Foundations

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. w 3. org/TR/swbp-n-ary. Relations/ [3] SPARQL 1. 1 Query Language, W 3 C Recommendation 21 March 2013, http: //www. w 3. org/TR/2013/REC-sparql 11 -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 Du. Charme'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 24

S P O • Tutorial: Jena Semantic Web Framework: http: //kill. devc. at/node/84 •

S P O • Tutorial: Jena Semantic Web Framework: http: //kill. devc. at/node/84 • Jena: A Semantic Web Framework: http: //trimcnlp. blogspot. no/2013/06/introduction-tojena. html • Videregående Jena: http: //docslide. us/documents/jena 5448 cad 4 b 1 af 9 f 606 b 8 b 485 f. html Jan Pettersen Nytun, UIA, page 25