This project has retired. For details please refer to its Attic page.
Tutorial 01 - Store Triples in a Simple Graph

Tutorial 01 - Store Triples in a Simple Graph

Author: Hasan (hasan@apache.org)
Last update: November 11, 2017

Problem Definition

Suppose that we want to store the statement "Hasan is an Apache Clerezza user" in an RDF Graph. We can rephrase the statement to be more precise to "There is someone having the first name Hasan, who is an Apache Clerezza user". This statement can be represented in Turtle, assuming the use of an example ontology, as follows.

@prefix ex: <http://clerezza.apache.org/2017/01/example#> . _:a ex:hasFirstName "hasan" . _:a ex:isA ex:ClerezzaUser .

We are looking for a solution using the Apache Clerezza API to store in memory the two triples in a graph.

Solution

The solution below uses the following Apache Clerezza libraries:

  • commons-rdf-api-0.2-SNAPSHOT.jar
  • commons-rdf-impl-utils-0.2-SNAPSHOT.jar
1 package example.pkg01; 2 3 import java.util.Iterator; 4 import org.apache.clerezza.commons.rdf.BlankNode; 5 import org.apache.clerezza.commons.rdf.Graph; 6 import org.apache.clerezza.commons.rdf.IRI; 7 import org.apache.clerezza.commons.rdf.Triple; 8 import org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl; 9 import org.apache.clerezza.commons.rdf.impl.utils.TripleImpl; 10 import org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph; 11 12 public class Example01 { 13 14 public static void main(String[] args) { 15 BlankNode subject = new BlankNode(); 16 17 IRI isA = new IRI("http://clerezza.apache.org/2017/01/example#isA"); 18 IRI clerezzaUser = new IRI("http://clerezza.apache.org/2017/01/example#ClerezzaUser"); 19 20 IRI hasFirstName = new IRI("http://clerezza.apache.org/2017/01/example#hasFirstName"); 21 PlainLiteralImpl firstName = new PlainLiteralImpl("Hasan"); 22 23 TripleImpl subjectType = new TripleImpl(subject, isA, clerezzaUser); 24 TripleImpl subjectFirstName = new TripleImpl(subject, hasFirstName, firstName); 25 26 Graph myGraph = new SimpleGraph(); 27 myGraph.add(subjectType); 28 myGraph.add(subjectFirstName); 29 30 Iterator iterator = myGraph.filter(null,null,null); 31 Triple triple; 32 while (iterator.hasNext()) { 33 triple = iterator.next(); 34 System.out.println(triple.getSubject().toString()); 35 System.out.println(triple.getPredicate().toString()); 36 System.out.println(triple.getObject().toString()); 37 } 38 } 39 }

Running the above Java program results in:

org.apache.clerezza.commons.rdf.BlankNode@1c170f0 <http://clerezza.apache.org/2017/01/example#isA> <http://clerezza.apache.org/2017/01/example#ClerezzaUser> org.apache.clerezza.commons.rdf.BlankNode@1c170f0 <http://clerezza.apache.org/2017/01/example#hasFirstName> "Hasan"

Discussion

The subject of the two triples is a blank node as defined at line 15. The predicate 'hasFirstName' in the first triple is represented by a specific IRI 'http://clerezza.apache.org/2017/01/example#hasFirstName', whereas the object is a plain literal having the value 'Hasan'. The second triple has the predicate 'isA' which is defined as the IRI 'http://clerezza.apache.org/2017/01/example#isA', and its object is of type IRI as well, namely the IRI 'http://clerezza.apache.org/2017/01/example#ClerezzaUser'.

Both triples are instantiated using TripleImpl as shown at line 23 and 24. The triples are then added to a SimpleGraph which is an in-memory implementation of a graph based on java.util.AbstractCollection.

Line 30 is an important statement to retrieve triples from a graph. The method 'filter' has the function to restrict triples to be retrieved. It takes three arguments: subject, predicate, and object for each triple in the graph to match against. A null argument matches any value. Triples which match the given filter arguments are returned by the method in form of an iterator.

Note: Any comments and suggestions for improvements are welcome. Please send your feedback to dev@clerezza.apache.org