Professional Documents
Culture Documents
https://jena.apache.org/about_jena/architecture.html
• interface Model
– represents a RDF Graph
– a model is a set of Statements (triples).
• methods are provided for
– creating resources, properties and literals
– creating the Statements (triples) which link them,
– for adding statements to and removing them from a
model,
– for querying a model
– set operations for combining models.
10/15/2019 (C) Philippe Genoud - Université Grenoble alpes 6
Core RDF API
package org.apache.jena.rdf.model
• Factory design pattern to create models
– class ModelFactory
• static Model createDefaultModel()
– answer a new memory based Model (RDF Graph)
• static ModelMaker createFileModelMaker(String root)
– answer a ModelMaker that constructs memory-based Models that
are backed by files
• static OntModel createOntologyModel()
– answer a new ontology model which will process in-memory models
of ontologies expressed the default ontology language (OWL)
• …
Light status
Switch1
on
representing RDF triples (Statements)
locatedIn
<interface> <interface>
Resource Literal
subject
<interface>
Property
<interface> <interface>
property
Statement
* Model
Creating a new Statement does not add it to the set of statements in the model
– Resource addLiteral(…)
• addLiteral(Property p, Literal o)
• addLiteral(Property p, double d)
• …
newly created Statements are automatically added to the set of statements in the model
the Resource belongs to.
Reification
query model.listStatements()
Subject
Property
Object
RDF Consumer Model
animation from www.try.idv.tw/try/files/070517-jena.ppt C.F.Liao (廖峻鋒)
10/15/2019 (C) Philippe Genoud - Université Grenoble alpes 14
Model I/O
• read and write methods of Model interface
– e.g. write(java.io.OutputStream out,
java.lang.String lang, java.lang.String base)
• lang : output format "RDF/XML" (default), "RDF/XML-ABBREV" , "N3", "TURTLE",
"N-TRIPLE"
• base : The base uri for relative URI calculations. null means use only absolute URI's
FileReader
Model System.out
.ttl
• javadoc
– http://jena.apache.org/documentation/javadoc/arq/index.html
for ( ; results.hasNext() ; ) {
QuerySolution soln = results.nextSolution() ;
RDFNode
can be reduced to one xstep
= soln.get("varName") ;
Resource
in some common cases: r = soln.getResource("varR") ;
Literal l = QueryExecution qexec = ;
soln.getLiteral("varL")
} QueryExecutionFactory.create(queryString, model);
} finally {
qexec.close() ;
}
for ( ; results.hasNext() ; ) {
QuerySolution soln = results.nextSolution() ;
RDFNode n = soln.get("varName") ;
Resource r = soln.getResource("varR") ;
Literal l = soln.getLiteral("varL") ;
}
• CONSTRUCT
import com.hp.hpl.jena.query.* ;
• DESCRIBE
import com.hp.hpl.jena.query.* ;
• ASK
import com.hp.hpl.jena.query.* ;
String queryStr = "select distinct ?Concept where {[] a ?Concept} LIMIT 10";
Query query = QueryFactory.create(queryStr);
// Remote execution.
QueryExecution qexec =
QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);
// Set the DBpedia specific timeout.
((QueryEngineHTTP)qexec).addParam("timeout", "10000") ;
// Execute.
ResultSet rs = qexec.execSelect();
ResultSetFormatter.out(System.out, rs, query);
qexec.close();
infModel.write(out, "Turtle");
10/15/2019 (C) Philippe Genoud - Université Grenoble alpes 28
Jena inferences : command line tools
• possibility to perform RDFS inferences with riot
command line tool
• ex:
turtle --rdfs=foaf.rdf –formatted=turtle writers.ttl
> writersWithInferences.ttl