Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Amna Basharat
Spring 2009
Outline
What is Jena
Packages
Jena Example
SPARQL
SPARQL Example
My project
DEMO
What is Jena
Jena is a Java framework
for building Semantic Web
applications
includes
a RDF API
reading and writing RDF in
RDF/XML, N3 and N-Triples
an OWL API
in-memory and persistent storage
SPARQL query engine
http://jena.sourceforge.net/
Packages
Jena includes
Jena and ARQ
a package providing API about
RDF and SPARQL
Jena CVS
a tool used to manage changes
within source code
Joseki
a RDF publishing server
Eyeball
an "RDF lint" for checking
RDF/OWL models
ARQ - Application API
8
Introduction – ARQ
Application API
The application API is in the
package com.hp.hpl.jena.query.
11
SELECT queries
The basic steps in making a SELECT
query are outlined in the example
below.
A query is created from a string
using the QueryFactory.
The query and model or RDF dataset
to be queried are then passed
to QueryExecutionFactory to
produce an instance of a query
execution.
Result are handled in a loop and
12
finally the query execution is
closed.
Example Code Structure
t com.hp.hpl.jena.query.* ;
model = ... ;
g queryString = " .... " ;
query = QueryFactory.create(queryString) ;
Execution qexec = QueryExecutionFactory.create(query, model) ;
ally { qexec.close() ; }
13
Simplifying the Query Formulation and
Execution
It is important to cleanly close the query execution
when finished.
System resources connected to persistent storage
may need to be released.
The step of creating a query and then a query
execution
import can
com.hp.hpl .jenabe reduced
.query .* ; to one step in some
common cases:
Model model = ... ;
String queryString = " .... " ;
QueryExecution qexec = QueryExecutionFactory.create(queryString, model) ;
try {
ResultSet results = qexec.execSelect() ;
. . .
} finally { qexec.close() ; }
14
Example: Formatting a
ResultSet
Instead of a loop to deal with each row in
the result set, the application can call an
operation of the ResultSetFormatter.
This is what the command line
applications do.
Example: processing results to produce a
simple text presentation:
ResultSetFormatter fmt = new ResultSetFormatter(results, query) ;
fmt.printAll(System.out) ;
OR
ResultSetFormatter.out(System.out, results, query) ;
15
Example: Processing results
The results are objects from the Jena
RDF API and API calls, which do not
modify the model, can be mixed with
query results processing:
for ( ; results.hasNext() ; )
{
// Access variables: soln.get("x") ;
RDFNode n = soln.get("x") ; // "x" is a variable in the query
// If you need to test the thing returned
if ( n.isLiteral() )
((Literal)n).getLexicalForm() ;
if ( n.isResource() )
{
Resource r = (Resource)n ;
if ( ! r.isAnon() )
{
... r.getURI() ...
}}}
16
CONSTRUCT Queries
CONSTRUCT queries return a single
RDF graph. As usual, the query
execution should be closed after use.
17
DESCRIBE Queries
DESCRIBE queries return a single RDF
graph. Different handlers for
the DESCRIBE operation can be loaded
by added by the application.
Query query = QueryFactory.create(queryString) ;
QueryExecution qexec = QueryExecutionFactory.create(query, model) ;
Model resultModel = qexec.execDescribe() ;
qexec.close() ;
18
ASK Queries
The operation Query.execAsk() returns
a boolean value indicating whether the
query pattern matched the graph or
dataset or not.
Query query = QueryFactory.create(queryString) ;
QueryExecution qexec = QueryExecutionFactory.create(query, model) ;
boolean result = qexec.execAsk() ;
qexec.close()
19
Formatting XML results
The ResultSetFormatter class has
methods to write out the
SPARQL Query Results XML Format.
See ResultSetFormatter.outputAsXML
method.
20
Datasets
The examples above are all queries on a single
model.
A SPARQL query is made on a dataset, which is
a default graph and zero or more named
graphs.
Datasets can
String dftGraphURI be constructed
= "file :default-graph.ttlusing
" ;
List namedGraphURIs = new ArrayList() ;
the DatasetFactory:
namedGraphURIs .add("file:named-1.ttl") ;
namedGraphURIs.add("file:named-2.ttl") ;
21
Using Existing Models
Already existing models can also be
used: A DataSource is an updatable
dataset:
22
Examples
23
Example 1
http://example.org/book#
1
DC.title DC.description
DC.description
Resource r1 =
m.createResource( "http://example.org/book#1" ) ;
http://example.org/book#
1
DC.title DC.description
DC.description
SPARQL - the book http://example.org/book#
2
A book about DC.description
DC.title
SPARQL
DC.title DC.description
Resource r1 =
m.createResource( "http://example.org/book#1" ) ;
Resource r2 =
m.createResource( "http://example.org/book#2" ) ;
Resource r3 =
m.createResource( "http://example.org/book#3" ) ;
sources dc:description
?y
http://example.org/book#
1
DC.title DC.description
DC.description
SPARQL - the book http://example.org/book#
2
A book about DC.description
DC.title
SPARQL
DC.title DC.description
for ( ; rs.hasNext() ; ) {
QuerySolution rb = rs.nextSolution() ;
RDFNode x = rb.get( "title" ) ;
if ( x.isLiteral() ) {
Literal titleStr= ( Literal )x ;
System.out.println( " “ + titleStr ) ;
}
}
}
finally Titles:
qexec.close() ; Jena - an RDF framework for
}
Java
}
Q u e ry in g R e m o te S P A R Q L
S e rv ice s
33
ARQ - Querying Remote SPARQL Services
SPARQL is a query language and a
remote access protocol. The remote access
protocol can be used with plain HTTP or over
SOAP.
See Joseki for an implementation of an RDF
publishing server, using the SPARQL protocol
(HTTP and SOAP). Joseki uses ARQ to provide
SPARQL query access to Jena models,
including Jena persistent models.
ARQ includes a query engine capable of using
the HTTP version. A version using SOAP is
include in Joseki.
34
Firewalls and Proxies
Don't forget to set the proxy for Java if you are
accessing a public server from behind a blocking
firewall.
Most home firewalls do not block outgoing
requests; many corporate firewalls do block
outgoing requests.
If, to use your web browser, you need to set a
proxy, you need to do so for a Java program.
Simple examples include:
-DsocksProxyHost=YourSocksServer-
DsocksProxyHost=YourSocksServer
-DsocksProxyPort=port-
Dhttp.proxyHost=WebProxy
-Dhttp.proxyPort=Port
This can be done in the application if it is done
before any network connection are made:
System.setProperty("socksProxyHost",
35
37
OWL API Comparisons
http://wiki.yoshtec.com/java-owl-api
38
A Comparison
API-Name Type Generates Tested Version License Problems / Issues / Remarks
Java
Classes
Jastor Type 2 Yes Tried CPL Unable to run with Jena 2.4 or Jena
2.5
39
From the above Comparison,
ProtegeOWL API seems to be an
OK Choice.
40
How to use protege
OWL API in Eclipse
1-Create Java Project
2-Goto Project->Properties-> Java
Build Paths
42
Tasks
Creating Ontology Factory
Publishing: Publishing new
Knowledge into Your OWL
Ontology using Ontology Factory
Querying/Knowledge Retrieval:
Reasoning: Invoking a Reasoner
throught the Code
43
Creating Ontology
Factory
44
Publishing : Publishing new
Knowledge into Your OWL
Ontology using Ontology
Factory
45
Querying / Knowledge
Retrieval
46
An Introduction to RDF and the
Jena RDF API
http://jena.sourceforge.net/tutorial/R
47
Jena Tutorial
http://jena.hpl.hp.com/juc2006/proce
48
R e a so n in g : In v o k in g a
R e a so n e r th ro u g h t th e
Code
49