This example describes how to add inferred triples for a named graph. Inferred triples are managed in the RDF Graph feature through an InferredNamedGraphNoSql object. Triples in the inferred graph are tagged with an integer rulebase ID.
import com.hp.hpl.jena.graph.*;
import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.rdf.model.Model;
import oracle.rdf.kv.client.jena.*;
public class Example1d {
public static void main(String[] args) throws Exception
{
String szStoreName = args[0];
String szHostName = args[1];
String szHostPort = args[2];
String szModelName = args[3];
// the rulebase id used for inferred triples
int iRuleBaseId = Integer.parseInt(args[4]);
OracleNoSqlConnection conn
= OracleNoSqlConnection.createInstance(szStoreName,
szHostName,
szHostPort);
// This object handle a model associated to a named graph
Model model =
OracleModelNoSql.createOracleModelNoSql(szModelName, conn);
OracleGraphNoSql graph = (OracleGraphNoSql) model.getGraph();
model.removeAll(); // removes all the triples from the associated
// model this will remove all asserted and
// inferred triples
graph.add(Triple.create(Node.createURI("u:John"),
Node.createURI("u:parentOf"),
Node.createURI("u:Mary")));
graph.add(Triple.create(Node.createURI("u:John"),
Node.createURI("u:parentOf"),
Node.createURI("u:Jack")));
graph.add(Triple.create(Node.createURI("u:Amy"),
Node.createURI("u:parentOf"),
Node.createURI("u:Jack")));
// This object handles all the inferred triples of
// the named graph produced with rulebase ID
InferredGraphNoSql inferredGraph =
new InferredNamedGraphNoSql(szModelName,
conn,
iRuleBaseId);
inferredGraph.add(Triple.create(Node.createURI("u:Jack"),
Node.createURI("u:siblingOf"),
Node.createURI("u:Mary")));
inferredGraph.close();
String prefix =
"PREFIX ORACLE_SEM_FS_NS: <http://oracle.com/semtech#" +
"include_rulebase_id=" + iRuleBaseId + ">";
String szQuery = prefix + " select ?x ?y ?z WHERE {?x ?y ?z} ";
System.out.println("Execute query " + szQuery);
Query query = QueryFactory.create(szQuery) ;
QueryExecution qexec = QueryExecutionFactory.create(query, model);
try {
ResultSet results = qexec.execSelect();
ResultSetFormatter.out(System.out, results, query);
}
finally {
qexec.close();
}
model.close();
conn.dispose();
}
}
The following are the commands to compile and run this example, as well as the expected output of the java command.
javac -classpath ./:./jena-core-2.7.4.jar:./jena-arq-2.9.4.jar: \
./sdordfnosqlclient.jar:./kvclient.jar:./xercesImpl-2.10.0.jar: \
./slf4j-api-1.6.4.jar:./slf4j-log4j12-1.6.4.jar:./log4j/1.2.16.jar: \
./jena-iri-0.9.4.jar:./xml-apis-1.4.01.jar Example1d.java
javac -classpath ./:./jena-core-2.7.4.jar:./jena-arq-2.9.4.jar: \
./sdordfnosqlclient.jar:./kvclient.jar:./xercesImpl-2.10.0.jar: \
./slf4j-api-1.6.4.jar: ./slf4j-log4j12-1.6.4.jar:./log4j/1.2.16.jar: \
./jena-iri-0.9.4.jar:./xml-apis-1.4.01.jar Example1d <store_name> \
<host_name> <host_port> \<graph_name> < \
rule_base_id>
Execute query select ?x ?y ?z WHERE {?x ?y ?z}
--------------------------------------
| x | y | z |
======================================
| <u:Jack> | <u:siblingOf> | <u:Mary> |
| <u:John> | <u:parentOf> | <u:Jack> |
| <u:John> | <u:parentOf> | <u:Mary> |
| <u:Amy> | <u:parentOf> | <u:Jack> |
--------------------------------------