Jena – A Semantic Web Framework for Java

antlertextureSoftware and s/w Development

Jul 14, 2012 (6 years and 6 days ago)


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

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)

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

Model.createTypedLiteral(“My name”) -- “My
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:






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

Model m...

RDFList listm.createList();

RDF Persistence

In memory:

Default (

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,

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

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


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)

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:

: the reasoner used for OWL inference

: the reasoner used for RDFS inference

: a reasoner for doing subclass and supproperty

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 Components

ModelFactory creating your models

ReasonerRegistry look up ready to use or custom implemented

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):
then add (

Base graph (your starting Model(s))

Axiomatic Triples (relative to the language)

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

Two main ways:

Triple Pattern (Model.listStatements())

SPARQL (separately documented Jena ARQ API)

Reasoning transparent: returns both explicit and inferable (derived)

SELECT * WHERE { ?x a ?y}

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

?y - any class declared in the Model

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

ARQ Jena SPARQL Query Engine

SPARQL primitives:




Focus on SELECT: Used for patterns more complicated than single

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

Usual while(rs.hasNext()) loop

Convenient ResultSetFormatter class


Writing a SPARQL query

Used more often

String myQueryString  “SELECT ?x WHERE { ?x http://FN "John Smith" } ”

Query myQuery  QueryFactory.create(myQueryString)

Triple Patterns

Simple 3-constraint model

Model m...

m.createProperty(“teaches”), (RDFNode) null)

All the RDFNode that are objects of the statements in whichluigi is the subject and http://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

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

Filtering in/out
Extended Iterators: Filtering

Given an implementation of Filter abstract class:

A class that extends Filter implementing boolean

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 