Using API Classes Instead of RDFhelper
Using API Classes Instead of RDFhelper
This section contains a short guide on how to use the API classes of RDFHelper.
Note
You should use RDFHelper to access the RDF data unit if possible, see: Working with the RDF Data Unit
Read data to get a list of graphs within the data unit.
Further, lets start by showing how as a DPU developer you may iterate over the input data unit in order to get access to RDF graphs which comes over input RDF data unit. The code below goes to innerExecute()
method of the DPU.
Code 1 - Iterating over input RDF graphs using API classes
Set<URI> rdfGraphs = new HashSet<>(); FilesDataUnit.Iteration it = null; try { it = input.getIteration(); while (it.hasNext()) { final URI dataGraphURI = it.next().getDataGraphURI(); rdfGraphs.add(dataGraphURI); } } catch (DataUnitException ex) { throw ContextUtils.dpuException(ctx, ex, "dpuName.error"); } finally { if (it != null) { try { it.close(); } catch (DataUnitException ex) { log.error("Error on close.", ex); } } }
In Lines 5 - 8, we iterate over the entries (RDF graphs) in the input RDF data unit.
In Line 6, we got RDF data graph URI: the URI of the RDF graph in the working RDF store.
We then on Line 7 add this to the set of such URIs.
As the iterator over entries does not extend
AutoClosable
we need to take care about it’s closing at the end (Line 14). That’s why we do all the work intry-catch
block (Lines 3 - 11 ) withfinally
statement (Lines 11 - 19). Also we catchDataUnitException
which may be thrown by the iterator in Lines 9 -11.
So after executing Code 1, we have a set of RDF graphs in which the input RDF data (triples) reside in the variable rdfGraphs.
We may then work with the RDF graphs using standard approach of OpenRDF API.
The code introduced in Code1 can be simplified by using helper - eu.unifiedviews.helpers.dataunit.rdf.RDFHelper
in uv-dataunit-helpers. See Working with the RDF Data Unit.
In this case, DPU developer does not need to manually handle iteration over input RDF graphs.
In general, as a DPU developer you should use helpers, if possible.
There are two methods as DPU developers you may use to add graphs to output RDF data unit:
URI addNewDataGraph(String symbolicName) throws DataUnitException;
This method creates a new entry in the output RDF data unit under the given
symbolicName
. ThesymbolicName
must be unique in the context of the data unit. It also associates the newly created entry with the (generated) data graph URI, which is returned by this method. Such data graph URI may be used by the DPU developer to store RDF triples to.Note
For an explanation of symbolicNames and other metadata of entries in data units, please see Basic Concepts for DPU Developers . The metadata generated is stored in the working RDF store of UnifiedViews.
void addExistingDataGraph(String symbolicName, URI existingDataGraphURI) throws DataUnitException;
This method adds an existing data graph URI to the
output
data unit under the givensymbolicName
. ThesymbolicName
must be unique in the context of the data unit. It automatically creates new entry in theoutput
data unit with the givensymbolicName
. For explanation of symbolicNames and other metadata of entries in data units, please see Basic Concepts for DPU Developers.In this case, the name of the data graph (the URI of the data graph where triples are stored) is specified by the DPU developer, who has to ensure that the name (URI) of such graph does not collide with the names of the graphs used by other entries/data units/DPUs. The URI of such a graph can be based on the prefix obtained by calling getBaseDataGraphURI() to ensure that the name of the graph does not collide with names of graphs in other data units/DPUs.
In order to add existing RDF graph to the output data unit, the code below may be used in the innerExecute()
method of the DPU.
Code 4 - Creating RDF graph in the output data unit using API classes
String graphName = ... URI graphURI = ... Symbolic symbolicName = output.addExistingDataGraph(graphName, graphURI);
In Line 3, the new entry in the output data unit is created and for such entry metadata
symbolicName
is set to be equal tographName
andexistingDataGraphURI
is set tographURI
.
Note
API classes does not support directly adding triples to the output RDF data unit or querying data units. To add triples/query data units, one has to use standard OpenRDF API methods.