Posts mit dem Label Jena werden angezeigt. Alle Posts anzeigen
Posts mit dem Label Jena werden angezeigt. Alle Posts anzeigen

Donnerstag, 17. Januar 2013

Access Triple Store via Java

The easiest way to access triple stores via Java is to use Jena ARQ.
ARQ is a query engine for Jena that supports the SPARQL RDF Query language. SPARQL is the query language developed by the W3C RDF Data Access Working Group.
You can easily access the data in the store using SELECT, ASK, DESCRIBE, and CONSTRUCT queries.
Query query = QueryFactory.create("queryString");
QueryExecution queryExec = QueryExecutionFactory.sparqlService( endpoint, query );
ResultSet result = queryExec.execSelect(); // or execSelectTriples()
It results in a ResultSet or an Interator of triples which can easily be processed further.
while (result.hasNext()) {
    QuerySolution solution = result.next();
    Resource id = solution.getResource("id");
    Resource title = solution.getLiteral("title");
}
One QuerySolution thereby is one result row of the query and each cell can be accessed by the variable name used in the SPARQL query.

But sometimes you do not want to just extract data from your triple store, but INSERT, UPDATE or DELETE triples. This can also be done using Jena.
UpdateRequest update = UpdateFactory.create(queryString);           
UpdateProcessor uExec = UpdateExecutionFactory.createRemote(update, endpoint);
uExec.execute(); 
Attention: This just works with triple stores supporting SPARQL 1.1 UPDATE.

Dienstag, 22. Mai 2012

How to generate Java files from OWL-files in Jena - Extension

This is an extension of Andra Waagmester's "How to generate Java files from OWL-files in Jena."

For some files, like the PAV OWL-file, you will just get the header, but none of the predicates or classes. Here is the reason why:

Schemagen is picking a namespace for your ontology, and is defaulting to the value of the xmlns-attribute:

<rdf:RDF xmlns="&pav;2.0/"
     xml:base="&pav;2.0/"

Hence http://purl.org/pav/2.0/

However, all of their declarations are actually in a non-versioned namespace:
xmlns:pav="http://purl.org/pav/"

So it is necessary to tell schemagen to use the right namespace with -a, or change the file so that the namespaces are consistent. Run:

java -cp lib/icu4j-3.4.4.jar:lib/jena-arq-2.9.0-incubating.jar:lib/jena-core-2.7.0-incubating.jar:lib/jena-iri-0.9.0-incubating.jar:lib/log4j-1.2.16.jar:lib/slf4j-api-1.6.4.jar:lib/slf4j-log4j12-1.6.4.jar:lib/xercesImpl-2.10.0.jar:lib/xml-apis-1.4.01.jar jena.schemagen -i $inputFile -o "./src" --package "de.fraunhofer.scai.bio.uima.xcas2rdf.vocabulary" --owl -a "http://purl.org/pav/" pav

.. and it will generate what is expected.

Another tip is to add  --ontology to get OntProperty, OntClass, etc, declarations generated in the output file.