Top Banner
1 Knowledge Representation XI IKT437 Part I RDF Jan Pettersen Nytun, UiA Apache Jena
27

Apache Jenagrimstad.uia.no/janpn/IKT437/2016/slides/pdf/Jena_1_2016.pdfjena.html •More Jena: 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 27 Title PowerPoint Presentation Author

Mar 07, 2018

Download

Documents

vudieu
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Apache Jenagrimstad.uia.no/janpn/IKT437/2016/slides/pdf/Jena_1_2016.pdfjena.html •More Jena: 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 27 Title PowerPoint Presentation Author

1

Knowledge RepresentationXI – IKT437

Part

IRDF

Jan Pettersen Nytun, UiA

Apache Jena

Page 2: Apache Jenagrimstad.uia.no/janpn/IKT437/2016/slides/pdf/Jena_1_2016.pdfjena.html •More Jena: 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 27 Title PowerPoint Presentation Author

S

OP

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

Page 3: Apache Jenagrimstad.uia.no/janpn/IKT437/2016/slides/pdf/Jena_1_2016.pdfjena.html •More Jena: 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 27 Title PowerPoint Presentation Author

S

OP

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

Page 4: Apache Jenagrimstad.uia.no/janpn/IKT437/2016/slides/pdf/Jena_1_2016.pdfjena.html •More Jena: 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 27 Title PowerPoint Presentation Author

S

OP

– 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 serversupporting query and update.Fuseki is tightly integrated with TDB to provide a robust, transactional persistent storage layer.

Jan Pettersen Nytun, UIA, page 4

Page 5: Apache Jenagrimstad.uia.no/janpn/IKT437/2016/slides/pdf/Jena_1_2016.pdfjena.html •More Jena: 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 27 Title PowerPoint Presentation Author

S

OP

- 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

From Wikipedia, the free encyclopedia:

Page 6: Apache Jenagrimstad.uia.no/janpn/IKT437/2016/slides/pdf/Jena_1_2016.pdfjena.html •More Jena: 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 27 Title PowerPoint Presentation Author

6

FrameworkArchitecture [6]

Fuseki is an HTTP interface to RDF data. It supports SPARQL forquerying and updating.

Page 7: Apache Jenagrimstad.uia.no/janpn/IKT437/2016/slides/pdf/Jena_1_2016.pdfjena.html •More Jena: 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 27 Title PowerPoint Presentation Author

7

Focus of rest of thispresentation:

Page 8: Apache Jenagrimstad.uia.no/janpn/IKT437/2016/slides/pdf/Jena_1_2016.pdfjena.html •More Jena: 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 27 Title PowerPoint Presentation Author

S

OP 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

Page 9: Apache Jenagrimstad.uia.no/janpn/IKT437/2016/slides/pdf/Jena_1_2016.pdfjena.html •More Jena: 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 27 Title PowerPoint Presentation Author

S

OP

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 DevelopersVersion: Neon Release (4.6.0)Build id: 20160613-1800

Jan Pettersen Nytun, UIA, page 9

Page 10: Apache Jenagrimstad.uia.no/janpn/IKT437/2016/slides/pdf/Jena_1_2016.pdfjena.html •More Jena: 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 27 Title PowerPoint Presentation Author

S

OP

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

Page 11: Apache Jenagrimstad.uia.no/janpn/IKT437/2016/slides/pdf/Jena_1_2016.pdfjena.html •More Jena: 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 27 Title PowerPoint Presentation Author

S

OP

Jan Pettersen Nytun, UIA, page 11

I downloaded the Jena libraries.

Page 12: Apache Jenagrimstad.uia.no/janpn/IKT437/2016/slides/pdf/Jena_1_2016.pdfjena.html •More Jena: 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 27 Title PowerPoint Presentation Author

S

OP

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

Jan Pettersen Nytun, UIA, page 12

Page 13: Apache Jenagrimstad.uia.no/janpn/IKT437/2016/slides/pdf/Jena_1_2016.pdfjena.html •More Jena: 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 27 Title PowerPoint Presentation Author

S

OP

I simply copied the libraries to my project:

Jan Pettersen Nytun, UIA, page 13

Page 14: Apache Jenagrimstad.uia.no/janpn/IKT437/2016/slides/pdf/Jena_1_2016.pdfjena.html •More Jena: 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 27 Title PowerPoint Presentation Author

S

OP

Adding the Jena libraries

Jan Pettersen Nytun, UIA, page 14

Page 15: Apache Jenagrimstad.uia.no/janpn/IKT437/2016/slides/pdf/Jena_1_2016.pdfjena.html •More Jena: 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 27 Title PowerPoint Presentation Author

S

OP

Jan Pettersen Nytun, UIA, page 15

Page 16: Apache Jenagrimstad.uia.no/janpn/IKT437/2016/slides/pdf/Jena_1_2016.pdfjena.html •More Jena: 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 27 Title PowerPoint Presentation Author

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

Page 17: Apache Jenagrimstad.uia.no/janpn/IKT437/2016/slides/pdf/Jena_1_2016.pdfjena.html •More Jena: 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 27 Title PowerPoint Presentation Author

S

OP AnotherProblem!

Jan Pettersen Nytun, UIA, page 17

Page 18: Apache Jenagrimstad.uia.no/janpn/IKT437/2016/slides/pdf/Jena_1_2016.pdfjena.html •More Jena: 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 27 Title PowerPoint Presentation Author

S

OP

…. Jena use log4j 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

Page 19: Apache Jenagrimstad.uia.no/janpn/IKT437/2016/slides/pdf/Jena_1_2016.pdfjena.html •More Jena: 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 27 Title PowerPoint Presentation Author

S

OP Solve the “logging” problem

Jan Pettersen Nytun, UIA, page 19

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

Page 20: Apache Jenagrimstad.uia.no/janpn/IKT437/2016/slides/pdf/Jena_1_2016.pdfjena.html •More Jena: 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 27 Title PowerPoint Presentation Author

S

OP

Start Programming in Jena

Jan Pettersen Nytun, UIA, page 20

Page 21: Apache Jenagrimstad.uia.no/janpn/IKT437/2016/slides/pdf/Jena_1_2016.pdfjena.html •More Jena: 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 27 Title PowerPoint Presentation Author

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:

<http://example.com/test#r><http://example.com/test#p> "HelloWorld" .

Making a triple with

Jena

Page 22: Apache Jenagrimstad.uia.no/janpn/IKT437/2016/slides/pdf/Jena_1_2016.pdfjena.html •More Jena: 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 27 Title PowerPoint Presentation Author

22

Output:<http://uia.no/termit/task#taskSet1>

<http://uia.no/termit/task#hasTask>

"do task one" .

<http://uia.no/termit/task#taskSet2>

<http://uia.no/termit/task#hasTaskSetName>

"TASK SET NO. 1" .

Page 23: Apache Jenagrimstad.uia.no/janpn/IKT437/2016/slides/pdf/Jena_1_2016.pdfjena.html •More Jena: 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 27 Title PowerPoint Presentation Author

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 23

Print all triples

Page 24: Apache Jenagrimstad.uia.no/janpn/IKT437/2016/slides/pdf/Jena_1_2016.pdfjena.html •More Jena: 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 27 Title PowerPoint Presentation Author

S

OP

…String fileName = "testFile.ttl";

…try {

OutputStream outFile = new FileOutputStream(fileName);

model.write(outFile,"Turtle");

} catch (FileNotFoundException e) { e.printStackTrace(); }

……

24

Write model to file

Page 25: Apache Jenagrimstad.uia.no/janpn/IKT437/2016/slides/pdf/Jena_1_2016.pdfjena.html •More Jena: 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 27 Title PowerPoint Presentation Author

S

OP

…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

Read model from file

Page 26: Apache Jenagrimstad.uia.no/janpn/IKT437/2016/slides/pdf/Jena_1_2016.pdfjena.html •More Jena: 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 27 Title PowerPoint Presentation Author

S

OP References

Jan Pettersen Nytun, UIA, page 26

[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

Page 27: Apache Jenagrimstad.uia.no/janpn/IKT437/2016/slides/pdf/Jena_1_2016.pdfjena.html •More Jena: 9f606b8b485f.html Jan Pettersen Nytun, UIA, page 27 Title PowerPoint Presentation Author

S

OP

• 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/jena5448cad4b1af9f606b8b485f.html

Jan Pettersen Nytun, UIA, page 27