Jena – A Semantic Web Framework for Java

antlertextureSoftware and s/w Development

Jul 14, 2012 (5 years and 1 month ago)

511 views

Jena – A Semantic Web Framework for Java
This part is not exam
material, but it is
essential for
Assignment 2!
Your Guides

http://jena.sourceforge.net/documentation.html

Read relevant tutorials

Read javadoc if necessary

Not found what you’re after, yet? Subscribe to jena-
dev mailing list (huge traffic) and ask the experts
A Model in Jena

A RDF Model is a set of Statements, i.e. a set of triples (S, P, O)
where:

S is the subject of the Statement

P is the predicate of the Statement

O is the object of the Statement
A Model in Jena

RDF Model is a set of Statements (S, P, O):

In RDF, S and O are nodes, P is the property linking these nodes

P can only be a named Resource (i.e. has a URI)

S can be a named or unnamed Resource (with or without
URI) i.e. it can be a blank node

O can be a named or unnamed Resource or a Literal

In Jena, a RDFNode can be a Resource or a Literal

RDFNode.isResource(), RDFNode.isURIResource(),
RDFNode.isLiteral(), RDFNode.isAnon()
A Model in Jena

A RDF Model is a set of Statements (S, P, O)

A Model acts as factory for RDFNodes:

Model.createResource(), Model.createResource(“
http://
myExample.com/mySubject
”)

Model.createTypedLiteral(“My name”) -- “My
name”xsd:string
A Model in Jena

A Model is created by a Factory:

Model m  ModelFactory.createDefaultModel();

An OntModel is a Model  a Reasoner:

OntModel mModelFactory.createOntologyModel();

Many ways to store Models in Jena, depending on:

Persistence choice

Reasoning required

RDF or ontology abstraction
Reading a Model

Jena supports the following serialisation languages:

RDF/XML

RDF/XML-ABBREV

N3

N-TRIPLE

TURTLE

Can read from

The URL of the Model

An InputStream a base URI and a language

Using a Reader yields problems (use InputStream or read the tutorial)
Writing a Model

Can write on to

An OutputStream

An OutputStream and specify the language and a base URI

A PrintStream

Using a Writer yields problems (use OutputStream or read the
tutorial)
Operations on Models

Models represent RDF Graphs:

Resources with the same URI are the same Resource regardless
of the Model that created them

An RDF Graph does not contain single nodes, therefore a Model
only contains Statements

Models can be joined like sets:

Model m  ...

Model g  ...

m.add(g); // union of m and g

m.remove(g); // m  g
RDF structures

A Model acts like a factory for all kind of RDF
objects:

Model m...

RDFList listm.createList();

list.add(m.createResource());
RDF Persistence

In memory:

Default (
ModelFactory.createDefaultModel()
)

File based

Uses file system

Stores the Model onto file-system only when closed

DB based

Many RDBMS compatible

MySQL, PostgreSQL, Oracle, SQL Server, HSQLDB,
Derby...

Different and efficient storage (w.r.t. queries) of triples
Reasoning

Support for different kinds of inference over RDF-based models (used –
among the others - for RDFS, OWL, and DAML).

Method(s)
createRDFSModel()

inference model using the built-in RDFS
inference rules and statements in the base model.

It's possible to use other reasoning systems than RDFS. For these a
Reasoner is required:

Method createInfModel(Reasoner reasoner, Model base)

inference
model using the rules of a generic reasoner over the model base.

Need to implement one (com.hp.hpl.jena.reasoner.Reasoner interface)

Or use a well-known ready to use:


getOWLReasoner()
: the reasoner used for OWL inference


getRDFSReasoner()
: the reasoner used for RDFS inference


getTransitiveReasoner()
: a reasoner for doing subclass and supproperty
closure.
Abstraction

RDF primitives: Triple, Resource, Statement, Property

Ontology primitives: OntClass, Individual, OntProperty …

Model RDF abstraction (createModel, createInfModel)

OntModel Ontology abstraction (createOntologyModel,
different profiles for different reasoner) e.g.:

OntModelSpec.OWL_MEM_RDFS_INF OWL ontologies, model stored
in memory, using RDFS entailment only

OntModelSpec.RDFS_MEM RDFS ontologies, in memory, but doing no
additional inferences

OntModelSpec.OWL_DL_MEM_RULE_INF OWL ontologies, in
memory, with the full OWL Lite inference
Inference
http://jena.sourceforge.net/images/reasoner-overview.png
Inference Components

ModelFactory creating your models

ReasonerRegistry look up ready to use or custom implemented
reasoners

ReasonerInfGraph perform inferences

Graphs inner representation of Model(s)

Base Graph (the starting Model)

Ontology definitions (e.g.: RDF Schema)

Axiomatic Triples

Not necessary to handle them directly

OntModelSpec contains constant profiles
Rule Based Inference

Set of rules (e.g.: RDFS Entailment rules):
if(
triple_pattern_1)
then add (
triple_pattern_2
)

Base graph (your starting Model(s))

Axiomatic Triples (relative to the language)

Expansion of the Graph, i.e.: all the inferred triple
added
Queries

Two main ways:

Triple Pattern (Model.listStatements())

SPARQL (separately documented Jena ARQ API)

Reasoning transparent: returns both explicit and inferable (derived)
results

SELECT * WHERE { ?x a ?y}

?x - any subject of a triple :a rdf:type :b

?y - any class declared in the Model
Queries

Model.listStatements(Resource s, Resource p, RDFNode o)

Lists all the statement with the input subject (s), predicate (p)
and object (o).

RDFNode superclass of Resource and Literal (both can appear as
object in a statement)

Any of the three can be null

variable (any value)

Alternative build a Selector instance and invoke
Model.listStatements(Selector)
SPARQL Queries

ARQ Jena SPARQL Query Engine

SPARQL primitives:

SELECT

DESCRIBE

CONSTRUCT

Focus on SELECT: Used for patterns more complicated than single
triples

SQL-Like syntax

Query building with ARQ

Parse a String containing the query

Build the query Programmatically
Results of a Query

JDBC like

QueryExecution (for SELECT) returning a ResultSet (Iterator-like
structure)

Usual while(rs.hasNext()) loop

Convenient ResultSetFormatter class

asXML(ResultSet)

asRDF(ResultSet)
Writing a SPARQL query

Used more often

String myQueryString  “SELECT ?x WHERE { ?x http://
www.w3.org/2001/vcard-rdf/3.0FN "John Smith" } ”

Query myQuery  QueryFactory.create(myQueryString)

QueryExecutionFactory.create(myQuery,myModel).executeSelec
t()
Triple Patterns

Simple 3-constraint model

Model m...

m.listStatements(m.createResource(“http://www.csc.liv.ac.uk/luigi”),
m.createProperty(“http://www.csc.liv.ac.ukteaches”), (RDFNode) null)

All the RDFNode that are objects of the statements in which
http://www.csc.liv.ac.uk/luigi is the subject and http://
www.csc.liv.ac.ukteaches is the predicate.

m.listStatements(null, null, (RDFNode)null);

all the statements in your Model

m.listStatements(); does the same thing
Programmatic build of a SPARQL Query

Used not very often (discouraged in the documentation and by
Jena developers)

Create a Query object Query myQuery new Query()

Set the variables myQuery.addResultVar(“x”)

Create patterns

ElementGroup eg  new ElementGroup()

ElementTriplePatter etp new
ElementTriplePattern(Node.create(“http://www.csc.liv.ac.uk/luigi”),
Node.create(“http://
www.csc.liv.ac.ukteaches”),Node.createVariable(“x”))

Set the pattern myQuery.setPattern(eg)

Then as in String based queries
Extended Iterators

Implementation of java.util.Iterator

Methods for Iterator

Composition: method andThen(ClosableIterator) chaining this
iterator with the input one

Mapping method mapWith(Map) returns a new ExtendedIterator
containing the mappings for the elements within the starting
ExtendedIterator

Filtering in/out
Extended Iterators: Filtering

Given an implementation of Filter abstract class:

A class that extends Filter implementing boolean
accept(Object)

Method filterKeep(Filter) retains all the elements of
the ExtendedIterator accepted by the Filter

Method filterDrop(Filter) drops all the elements from
the ExtendedIterator accepted by the Filter

Filters can be composed

StmtIterator it 
aModel.listStatements().filterKeep(aFilter).filterKeep(anothe
rFilter).filterDrop(yetAnotherFilter)….