apache jena - grimstad.uia.no · s p o apache jena is an open source semantic web framework for...

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

Upload: others

Post on 11-Sep-2019

7 views

Category:

Documents


0 download

TRANSCRIPT

1

Knowledge RepresentationXI – IKT437

Part

IRDF

Jan Pettersen Nytun, UiA

Apache Jena

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

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

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

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:

6

FrameworkArchitecture [6]

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

7

Focus of rest of thispresentation:

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

S

OP

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

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

S

OP

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

OP 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

OP

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

OP Fixing the package names:

Jan Pettersen Nytun, UIA, page 14

S

OP

AnotherProblem!

Jan Pettersen Nytun, UIA, page 15

S

OP

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

OP 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

OP

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

20

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

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

OP

…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

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");

}

23

Read model from file

S

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

[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

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

• Videregående Jena: http://docslide.us/documents/jena5448cad4b1af9f606b8b485f.html

Jan Pettersen Nytun, UIA, page 25