RDFHelper Documentation

From WebLab Wiki
Jump to navigationJump to search

Add an RDF Statement in a PoK

A common task when using the WebLab is to add a statement on a PieceOfKnowlegde. The following example explains how to add a statement that describes the creator of a PoK.

// Since we want to write something in a PoK, we instanciate a Jena PoK Helper that is able to write RDF/XML.
PoKHelper helper = new JenaPoKHelper(pok);

// Dublin Core ontology contains a widely used property to define a relation between a resource and a creator.
helper.createLitStat(pok.getUri(), DublinCore.CREATOR_PROPERTY_NAME, "John Smith");

For such easy task we recommend to rely on the WebLab Dublin Core Annotator.

Retrieve the RDF Statement from the PoK

We want to read the value of a statement in a PieceOfKnowlegde. To achieve this task, we can use RDFHelpers (here we use PoKHelper). The next example explains how to retrieve the value of the property of a subject.

// Since we want to read something in a PoK, we instanciate a JenapoKHelper that is able to read RDF/XML triplets.
PoKHelper helper = new JenaPoKHelper(pok);
// We search for a specific property from Dublin Core ontology defining a relation between a resource and a creator in the PoK.
List<String> values = helper.getLitsOnPredSubj(pok.getUri(), DublinCore.CREATOR_PROPERTY_NAME);

For such easy task we recommend to rely on the WebLab Dublin Core Annotator.

Write several statements

When you have to write a lot of statements at the same time, you could encounter slowdown issues due to RDF model loading and unloading .

To avoid this problem, you should commit modifications only once all statements are in memory. It can be done using the parameter in the constructor on PoKHelper and at the end call commit(). Here is an example:

PoKHelper helper = new JenaPoKHelper(pok, false);
for (int i = 0; i < 10000; i++) {
	helper.createLitStat("weblab://mysubject#"Integer.toString(i), "weblab://myproperty", "anyValue");
helper.commit(); // Do NOT forget to call commit else all your statements will be discarded.

Read several statements

No performance issue appear when reading multiple statements. You call the getLitsOnPred method on a PoKHelper, as used when reading a single property but in order to read multiple values.

Any way, you are often enforce to read multiple statements in order to resolve an RDF Graph. To do that we encourage to use the Semantic Resource.