Examples
The following examples illustrate using a JRDF Graph.
JRDF Graph (Memory)
This example demonstrates how to:
- Create a JRDF graph and add triples
- Query the JRDF graph
- Remove triples from the JRDF graph
Create a JRDF Graph and add Triples
The following code creates a memory JRDF graph and inserts triples (the graph does not store the triples in any specific order):
//create Graph
Graph graph = new GraphImpl();
GraphElementFactory elementFactory = graph.getElementFactory();
//create Elements
BlankNode subject = elementFactory.createResource();
URIReference subject2 = elementFactory.createResource(new URI("http://example.org#subject"));
URIReference predicate = elementFactory.createResource(new URI("http://example.org#predicate"));
URIReference predicate2 = elementFactory.createResource(new URI("http://example.org#predicate2"));
Literal object = elementFactory.createLiteral("object");
Triple triple = elementFactory.createTriple(subject, predicate, object);
Triple triple2 = elementFactory.createTriple(subject2, predicate, object);
Triple triple3 = elementFactory.createTriple(subject2, predicate, subject);
Triple triple4 = elementFactory.createTriple(predicate, predicate2, subject);
//insert Triples
graph.add(triple);
graph.add(triple2);
graph.add(triple3);
graph.add(triple4);
The graph now contains the following triples (N3):
_blankNode123 <http://example.org#predicate> 'object'
<http://example.org#subject> <http://example.org#predicate> 'object'
<http://example.org#subject> <http://example.org#predicate> _blankNode123
<http://example.org#predicate> <http://example.org#predicate2> _blankNode123
Note - _blankNode123
represents subject
which is a BlankNode.
Query the JRDF Graph
The following code queries the JRDF graph to find all triples where subject2
is the SubjectNode. The results are a subgraph of the original JRDF graph.
//query the graph
Triple queryTriple = elementFactory.createTriple(subject2, null, null);
ClosableIterator queryResult = graph.find(queryTriple);
The contents of queryResult
are:
<http://example.org#subject> <http://example.org#predicate> 'object'
<http://example.org#subject> <http://example.org#predicate> _blankNode123
Remove a Triple from the JRDF Graph
The following code removes a Triple from the JRDF Graph.
//remove Triple
graph.remove(triple4);
The JRDF graph now contains:
_blankNode123 <http://example.org#predicate> 'object'
<http://example.org#subject> <http://example.org#predicate> 'object'
<http://example.org#subject> <http://example.org#predicate> _blankNode123
Mulgara Backed JRDF Graph (Client)
This example demonstrates how to:
- Create a Mulgara session
- Create a Mulgara backed JRDF graph
Create a Mulgara Session
The following code creates a Mulgara session object:
//location of the Mulgara Server
URI serverURI = new URI("rmi://mysite.com/server1");
//connect to the Mulgara Server
//true indicates a remote Server, if the Server is not remote use false
SessionFactory sessionFactory = SessionFactoryFinder.newSessionFactory(serverURI, true);
Session session = sessionFactory.newSession();
Note - The Mulgara server must be running. rmi://mysite.com/server1
is an example only. Replace this with the URI for your Mulgara server.
Create a Mulgara Backed JRDF Graph
The following code creates a new JRDF graph:
//create a new Mulgara Model
URI modelURI = new URI("rmi://mysite.com/server1#exampleGraph");
URI modelType = new URI("http://mulgara.org/mulgara#Model");
session.createModel(modelURI, modelType);
//create a JRDF Graph using the model
ItqlInterpreter interpreter = new ItqlInterpreter(new HashMap());
Graph = AbstractGraphFactory.createGraph(modelURI, session);
Note - You only have to create the model once. If using an existing model you do not have to create it.
Display iTQL Results as an JRDF Graph
This example demonstrates how to:
- Execute an iTQL query
- Create a read-only JRDF Graph using the results
Note - This example assumes you have already created a Mulgara session.
Execute an iTQL Query
The following code returns all triples for a model:
//create the query
String queryText = "select $s $p $o from <rmi://mysite.com/server1#testModel> where $s $p $o ; ";
ItqlInterpreter interpreter = new ItqlInterpreter(new HashMap());
Query query = interpreter.parseQuery(queryText);
//execute the query
Answer queryResult = session.query(query);
Note - The model rmi://mysite.com/server1#testModel
is an example only. Replace this with the URI for your model.
Create a JRDF Graph from the Results
The following code creates a new read-only JRDF graph from the results of the iTQL query in the previous example.
//create a JRDF Graph
Graph = AbstractGraphFactory.createGraph(queryResult);
Note - queryResult
must contain three columns to be valid for use as a JRDF graph.