D.3 Example Queries Using Graph Support for Apache Jena
This section describes example queries using the support for Apache Jena and is based on the RDF metadata that is stored in the MDSYS schema.
To run a query, you must do the following:
- Include the code in a Java source file. The examples used in this
section are supplied in files in the
examples
directory of the support for Apache Jena download. - Compile the Java source file. For
example:
> javac -classpath ../jar/'*' Test.java
- Run the compiled file. For
example:
> java -classpath ./:../jar/'*' Test jdbc:oracle:thin:@localhost:1521:orcl scott <password-for-scott> M1
- Test.java: Query Family Relationships
- Test6.java: Load OWL Ontology and Perform OWLPrime inference
- Test7.java: Bulk Load OWL Ontology and Perform OWLPrime inference
- Test8.java: SPARQL OPTIONAL Query
- Test9.java: SPARQL Query with LIMIT and OFFSET
- Test10.java: SPARQL Query with TIMEOUT and DOP
- Test11.java: Query Involving Named Graphs
- Test12.java: SPARQL ASK Query
- Test13.java: SPARQL DESCRIBE Query
- Test14.java: SPARQL CONSTRUCT Query
- Test15.java: Query Multiple Models and Specify "Allow Duplicates"
- Test16.java: SPARQL Update
- Test17.java: SPARQL Query with ARQ Built-In Functions
- Test18.java: SELECT Cast Query
- Test19.java: Instantiate Oracle Database Using OracleConnection
- Test20.java: Oracle Database Connection Pooling
Parent topic: MDSYS-Owned Semantic Network
D.3.1 Test.java: Query Family Relationships
Example D-1 Query Family Relationships
Example D-1 specifies that John is the father of Mary, and it selects and displays the subject and object in each fatherOf
relationship
import oracle.spatial.rdf.client.jena.*; import org.apache.jena.rdf.model.Model; import org.apache.jena.graph.*; import org.apache.jena.query.*; public class Test { public static void main(String[] args) throws Exception { String szJdbcURL = args[0]; String szUser = args[1]; String szPasswd = args[2]; String szModelName = args[3]; Oracle oracle = new Oracle(szJdbcURL, szUser, szPasswd); Model model = ModelOracleSem.createOracleSemModel( oracle, szModelName); model.getGraph().add(Triple.create( Node.createURI("http://example.com/John"), Node.createURI("http://example.com/fatherOf"), Node.createURI("http://example.com/Mary"))); Query query = QueryFactory.create( "select ?f ?k WHERE {?f <http://example.com/fatherOf> ?k .}"); QueryExecution qexec = QueryExecutionFactory.create(query, model); ResultSet results = qexec.execSelect(); ResultSetFormatter.out(System.out, results, query); model.close(); oracle.dispose(); } }
The following are the commands to compile and run Example D-1, as well as the expected output of the java
command.
javac -classpath ../jar/'*' Test.java
java -classpath ./:../jar/'*' Test jdbc:oracle:thin:@localhost:1521:orcl scott <password-for-scott> M1
---------------------------------------------------------
| f | k |
=========================================================
| <http://example.com/John> | <http://example.com/Mary> |
---------------------------------------------------------
Parent topic: Example Queries Using Graph Support for Apache Jena
D.3.2 Test6.java: Load OWL Ontology and Perform OWLPrime inference
Example D-2 loads an OWL ontology and performs OWLPrime inference. Note that the OWL ontology is in RDF/XML format, and after it is loaded into Oracle it will be serialized out in N-TRIPLE form. The example also queries for the number of asserted and inferred triples.
The ontology in this example can be retrieved from http://swat.cse.lehigh.edu/onto/univ-bench.owl
, and it describes roles, resources, and relationships in a university environment.
Example D-2 Load OWL Ontology and Perform OWLPrime inference
import java.io.*; import org.apache.jena.query.*; import org.apache.jena.rdf.model.Model; import org.apache.jena.util.FileManager; import oracle.spatial.rdf.client.jena.*; public class Test6 { public static void main(String[] args) throws Exception { String szJdbcURL = args[0]; String szUser = args[1]; String szPasswd = args[2]; String szModelName = args[3]; Oracle oracle = new Oracle(szJdbcURL, szUser, szPasswd); Model model = ModelOracleSem.createOracleSemModel(oracle, szModelName); // load UNIV ontology InputStream in = FileManager.get().open("./univ-bench.owl" ); model.read(in, null); OutputStream os = new FileOutputStream("./univ-bench.nt"); model.write(os, "N-TRIPLE"); os.close(); String queryString = " SELECT ?subject ?prop ?object WHERE { ?subject ?prop ?object } "; Query query = QueryFactory.create(queryString) ; QueryExecution qexec = QueryExecutionFactory.create(query, model) ; try { int iTriplesCount = 0; ResultSet results = qexec.execSelect() ; for ( ; results.hasNext() ; ) { QuerySolution soln = results.nextSolution() ; iTriplesCount++; } System.out.println("Asserted triples count: " + iTriplesCount); } finally { qexec.close() ; } Attachment attachment = Attachment.createInstance( new String[] {}, "OWLPRIME", InferenceMaintenanceMode.NO_UPDATE, QueryOptions.DEFAULT); GraphOracleSem graph = new GraphOracleSem(oracle, szModelName, attachment); graph.analyze(); graph.performInference(); query = QueryFactory.create(queryString) ; qexec = QueryExecutionFactory.create(query,new ModelOracleSem(graph)) ; try { int iTriplesCount = 0; ResultSet results = qexec.execSelect() ; for ( ; results.hasNext() ; ) { QuerySolution soln = results.nextSolution() ; iTriplesCount++; } System.out.println("Asserted + Infered triples count: " + iTriplesCount); } finally { qexec.close() ; } model.close(); OracleUtils.dropSemanticModel(oracle, szModelName); oracle.dispose(); } }
The following are the commands to compile and run Example D-2, as well as the expected output of the java
command.
javac -classpath ../jar/'*' Test6.java
java -classpath ./:../jar/'*' Test6 jdbc:oracle:thin:@localhost:1521:orcl scott <password-for-scott> M1
Asserted triples count: 293
Asserted + Infered triples count: 340
Note that this output reflects an older version of the LUBM ontology. The latest version of the ontology has more triples.
Parent topic: Example Queries Using Graph Support for Apache Jena
D.3.3 Test7.java: Bulk Load OWL Ontology and Perform OWLPrime inference
Example D-3 loads the same OWL ontology as in Test6.java: Load OWL Ontology and Perform OWLPrime inference, but stored in a local file using Bulk Loader. Ontologies can also be loaded using an incremental and batch loader; these two methods are also listed in the example for completeness.
Example D-3 Bulk Load OWL Ontology and Perform OWLPrime inference
import java.io.*; import org.apache.jena.graph.*; import org.apache.jena.rdf.model.*; import org.apache.jena.util.*; import oracle.spatial.rdf.client.jena.*; public class Test7 { public static void main(String[] args) throws Exception { String szJdbcURL = args[0]; String szUser = args[1]; String szPasswd = args[2]; String szModelName = args[3]; // in memory Jena Model Model model = ModelFactory.createDefaultModel(); InputStream is = FileManager.get().open("./univ-bench.owl"); model.read(is, "", "RDF/XML"); is.close(); Oracle oracle = new Oracle(szJdbcURL, szUser, szPasswd); ModelOracleSem modelDest = ModelOracleSem.createOracleSemModel(oracle, szModelName); GraphOracleSem g = modelDest.getGraph(); g.dropApplicationTableIndex(); int method = 2; // try bulk loader String tbs = "SYSAUX"; // can be customized if (method == 0) { System.out.println("start incremental"); modelDest.add(model); System.out.println("end size " + modelDest.size()); } else if (method == 1) { System.out.println("start batch load"); g.getBulkUpdateHandler().addInBatch( GraphUtil.findAll(model.getGraph()), tbs); System.out.println("end size " + modelDest.size()); } else { System.out.println("start bulk load"); g.getBulkUpdateHandler().addInBulk( GraphUtil.findAll(model.getGraph()), tbs); System.out.println("end size " + modelDest.size()); } g.rebuildApplicationTableIndex(); long lCount = g.getCount(Triple.ANY); System.out.println("Asserted triples count: " + lCount); model.close(); OracleUtils.dropSemanticModel(oracle, szModelName); oracle.dispose(); } }
The following are the commands to compile and run Example D-3, as well as the expected output of the java
command.
javac -classpath ../jar/'*' Test7.java
java -classpath ./:../jar/'*' Test7 jdbc:oracle:thin:@localhost:1521:orcl scott <password-for-scott> M1
start bulk load
end size 293
Asserted triples count: 293
Note that this output reflects an older version of the LUBM ontology. The latest version of the ontology has more triples.
Parent topic: Example Queries Using Graph Support for Apache Jena
D.3.4 Test8.java: SPARQL OPTIONAL Query
Example D-4 shows a SPARQL OPTIONAL query. It inserts triples that postulate the following:
-
John is a parent of Mary.
-
John is a parent of Jack.
-
Mary is a parent of Jill.
It then finds parent-child relationships, optionally including any grandchild (gkid) relationships.
Example D-4 SPARQL OPTIONAL Query
import org.apache.jena.query.*;
import oracle.spatial.rdf.client.jena.*;
import org.apache.jena.graph.*;
public class Test8
{
public static void main(String[] args) throws Exception
{
String szJdbcURL = args[0];
String szUser = args[1];
String szPasswd = args[2];
String szModelName = args[3];
Oracle oracle = new Oracle(szJdbcURL, szUser, szPasswd);
ModelOracleSem model = ModelOracleSem.createOracleSemModel(oracle,
szModelName);
GraphOracleSem g = model.getGraph();
g.add(Triple.create(
Node.createURI("u:John"), Node.createURI("u:parentOf"),
Node.createURI("u:Mary")));
g.add(Triple.create(
Node.createURI("u:John"), Node.createURI("u:parentOf"),
Node.createURI("u:Jack")));
g.add(Triple.create(
Node.createURI("u:Mary"), Node.createURI("u:parentOf"),
Node.createURI("u:Jill")));
String queryString =
" SELECT ?s ?o ?gkid " +
" WHERE { ?s <u:parentOf> ?o . OPTIONAL {?o <u:parentOf> ?gkid }} ";
Query query = QueryFactory.create(queryString) ;
QueryExecution qexec = QueryExecutionFactory.create(query, model) ;
try {
int iMatchCount = 0;
ResultSet results = qexec.execSelect() ;
ResultSetFormatter.out(System.out, results, query);
}
finally {
qexec.close() ;
}
model.close();
OracleUtils.dropSemanticModel(oracle, szModelName);
oracle.dispose();
}
}
The following are the commands to compile and run Example D-4, as well as the expected output of the java
command.
javac -classpath ../jar/'*' Test8.java
java -classpath ./:../jar/'*' Test8 jdbc:oracle:thin:@localhost:1521:orcl scott <password-for-scott> M1
----------------------------------
| s | o | gkid |
==================================
| <u:John> | <u:Mary> | <u:Jill> |
| <u:Mary> | <u:Jill> | |
| <u:John> | <u:Jack> | |
----------------------------------
Parent topic: Example Queries Using Graph Support for Apache Jena
D.3.5 Test9.java: SPARQL Query with LIMIT and OFFSET
Example D-5 shows a SPARQL query with LIMIT and OFFSET. It inserts triples that postulate the following:
-
John is a parent of Mary.
-
John is a parent of Jack.
-
Mary is a parent of Jill.
It then finds one parent-child relationship (LIMIT 1), skipping the first two parent-child relationships encountered (OFFSET 2), and optionally includes any grandchild (gkid) relationships for the one found.
Example D-5 SPARQL Query with LIMIT and OFFSET
import org.apache.jena.query.*; import oracle.spatial.rdf.client.jena.*; import org.apache.jena.graph.*; public class Test9 { public static void main(String[] args) throws Exception { String szJdbcURL = args[0]; String szUser = args[1]; String szPasswd = args[2]; String szModelName = args[3]; Oracle oracle = new Oracle(szJdbcURL, szUser, szPasswd); ModelOracleSem model = ModelOracleSem.createOracleSemModel(oracle, szModelName); GraphOracleSem g = model.getGraph(); g.add(Triple.create(Node.createURI("u:John"), Node.createURI("u:parentOf"), Node.createURI("u:Mary"))); g.add(Triple.create(Node.createURI("u:John"), Node.createURI("u:parentOf"), Node.createURI("u:Jack"))); g.add(Triple.create(Node.createURI("u:Mary"), Node.createURI("u:parentOf"), Node.createURI("u:Jill"))); String queryString = " SELECT ?s ?o ?gkid " + " WHERE { ?s <u:parentOf> ?o . OPTIONAL {?o <u:parentOf> ?gkid }} " + " LIMIT 1 OFFSET 2"; Query query = QueryFactory.create(queryString) ; QueryExecution qexec = QueryExecutionFactory.create(query, model) ; int iMatchCount = 0; ResultSet results = qexec.execSelect() ; ResultSetFormatter.out(System.out, results, query); qexec.close() ; model.close(); OracleUtils.dropSemanticModel(oracle, szModelName); oracle.dispose(); } }
The following are the commands to compile and run Example D-5, as well as the expected output of the java
command.
javac -classpath ../jar/'*' Test9.java
java -classpath ./:../jar/'*' Test9 jdbc:oracle:thin:@localhost:1521:orcl scott <password-for-scott> M1
------------------------------
| s | o | gkid |
==============================
| <u:John> | <u:Jack> | |
------------------------------
Parent topic: Example Queries Using Graph Support for Apache Jena
D.3.6 Test10.java: SPARQL Query with TIMEOUT and DOP
Example D-6 shows the SPARQL query from Test9.java: SPARQL Query with LIMIT and OFFSET with additional features, including a timeout setting (TIMEOUT=1, in seconds) and parallel execution setting (DOP=4).
Example D-6 SPARQL Query with TIMEOUT and DOP
import org.apache.jena.query.*;
import oracle.spatial.rdf.client.jena.*;
import org.apache.jena.graph.*;
public class Test10 {
public static void main(String[] args) throws Exception {
String szJdbcURL = args[0];
String szUser = args[1];
String szPasswd = args[2];
String szModelName = args[3];
Oracle oracle = new Oracle(szJdbcURL, szUser, szPasswd);
ModelOracleSem model = ModelOracleSem.createOracleSemModel(oracle, szModelName);
GraphOracleSem g = model.getGraph();
g.add(Triple.create(Node.createURI("u:John"), Node.createURI("u:parentOf"),
Node.createURI("u:Mary")));
g.add(Triple.create(Node.createURI("u:John"), Node.createURI("u:parentOf"),
Node.createURI("u:Jack")));
g.add(Triple.create(Node.createURI("u:Mary"), Node.createURI("u:parentOf"),
Node.createURI("u:Jill")));
String queryString =
" PREFIX ORACLE_SEM_FS_NS: <http://oracle.com/semtech#dop=4,timeout=1> "
+ " SELECT ?s ?o ?gkid WHERE { ?s <u:parentOf> ?o . "
+ " OPTIONAL {?o <u:parentOf> ?gkid }} "
+ " LIMIT 1 OFFSET 2";
Query query = QueryFactory.create(queryString) ;
QueryExecution qexec = QueryExecutionFactory.create(query, model) ;
int iMatchCount = 0;
ResultSet results = qexec.execSelect() ;
ResultSetFormatter.out(System.out, results, query);
qexec.close() ;
model.close();
OracleUtils.dropSemanticModel(oracle, szModelName);
oracle.dispose();
}
}
The following are the commands to compile and run Example D-6, as well as the expected output of the java
command.
javac -classpath ../jar/'*' Test10.java
java -classpath ./:../jar/'*' Test10 jdbc:oracle:thin:@localhost:1521:orcl scott <password-for-scott> M1
------------------------------
| s | o | gkid |
==============================
| <u:John> | <u:Jack> | |
------------------------------
Parent topic: Example Queries Using Graph Support for Apache Jena
D.3.7 Test11.java: Query Involving Named Graphs
Example D-7 shows a query involving named graphs. It involves a default graph that has information about named graph URIs and their publishers. The query finds graph names, their publishers, and within each named graph finds the mailbox value using the foaf:mbox
predicate.
Example D-7 Named Graph Based Query
import org.apache.jena.query.*; import oracle.spatial.rdf.client.jena.*; import org.apache.jena.graph.*; public class Test11 { public static void main(String[] args) throws Exception { String szJdbcURL = args[0]; String szUser = args[1]; String szPasswd = args[2]; String szModelName = args[3]; Oracle oracle = new Oracle(szJdbcURL, szUser, szPasswd); GraphOracleSem graph = new GraphOracleSem(oracle, szModelName); DatasetGraphOracleSem dataset = DatasetGraphOracleSem.createFrom(graph); // don't need the GraphOracleSem anymore, release resources graph.close(); // add data to the default graph dataset.add(new Quad( Quad.defaultGraphIRI, // specifies default graph Node.createURI("http://example.org/bob"), Node.createURI("http://purl.org/dc/elements/1.1/publisher"), Node.createLiteral("Bob Hacker"))); dataset.add(new Quad( Quad.defaultGraphIRI, // specifies default graph Node.createURI("http://example.org/alice"), Node.createURI("http://purl.org/dc/elements/1.1/publisher"), Node.createLiteral("alice Hacker"))); // add data to the bob named graph dataset.add(new Quad( Node.createURI("http://example.org/bob"), // graph name Node.createURI("urn:bob"), Node.createURI("http://xmlns.com/foaf/0.1/name"), Node.createLiteral("Bob"))); dataset.add(new Quad( Node.createURI("http://example.org/bob"), // graph name Node.createURI("urn:bob"), Node.createURI("http://xmlns.com/foaf/0.1/mbox"), Node.createURI("mailto:bob@example"))); // add data to the alice named graph dataset.add(new Quad( Node.createURI("http://example.org/alice"), // graph name Node.createURI("urn:alice"), Node.createURI("http://xmlns.com/foaf/0.1/name"), Node.createLiteral("Alice"))); dataset.add(new Quad( Node.createURI("http://example.org/alice"), // graph name Node.createURI("urn:alice"), Node.createURI("http://xmlns.com/foaf/0.1/mbox"), Node.createURI("mailto:alice@example"))); DataSource ds = DatasetFactory.create(dataset); String queryString = " PREFIX foaf: <http://xmlns.com/foaf/0.1/> " + " PREFIX dc: <http://purl.org/dc/elements/1.1/> " + " SELECT ?who ?graph ?mbox " + " FROM NAMED <http://example.org/alice> " + " FROM NAMED <http://example.org/bob> " + " WHERE " + " { " + " ?graph dc:publisher ?who . " + " GRAPH ?graph { ?x foaf:mbox ?mbox } " + " } "; Query query = QueryFactory.create(queryString); QueryExecution qexec = QueryExecutionFactory.create(query, ds); ResultSet results = qexec.execSelect(); ResultSetFormatter.out(System.out, results, query); qexec.close(); dataset.close(); oracle.dispose(); } }
The following are the commands to compile and run Example D-7, as well as the expected output of the java
command.
javac -classpath ./:./jena-2.6.4.jar:./sdordfclient.jar:./ojdbc6.jar:./slf4j-api-1.5.8.jar:./slf4j-log4j12-1.5.8.jar:./arq-2.8.8.jar:./xercesImpl-2.7.1.jar Test11.java java -classpath ./:../jar/'*' Test11 jdbc:oracle:thin:@localhost:1521:orcl scott <password-for-scott> M1 ------------------------------------------------------------------------ | who | graph | mbox | ======================================================================== | "alice Hacker" | <http://example.org/alice> | <mailto:alice@example> | | "Bob Hacker" | <http://example.org/bob> | <mailto:bob@example> | ------------------------------------------------------------------------
Parent topic: Example Queries Using Graph Support for Apache Jena
D.3.8 Test12.java: SPARQL ASK Query
Example D-8 shows a SPARQL ASK query. It inserts a triple that postulates that John is a parent of Mary. It then finds whether John is a parent of Mary.
Example D-8 SPARQL ASK Query
import org.apache.jena.query.*;
import oracle.spatial.rdf.client.jena.*;
import org.apache.jena.graph.*;
{
public static void main(String[] args) throws Exception
{
String szJdbcURL = args[0];
String szUser = args[1];
String szPasswd = args[2];
String szModelName = args[3];
Oracle oracle = new Oracle(szJdbcURL, szUser, szPasswd);
ModelOracleSem model = ModelOracleSem.createOracleSemModel(oracle,
szModelName);
GraphOracleSem g = model.getGraph();
g.add(Triple.create(Node.createURI("u:John"), Node.createURI("u:parentOf"),
Node.createURI("u:Mary")));
String queryString = " ASK { <u:John> <u:parentOf> <u:Mary> } ";
Query query = QueryFactory.create(queryString) ;
QueryExecution qexec = QueryExecutionFactory.create(query, model) ;
boolean b = qexec.execAsk();
System.out.println("ask result = " + ((b)?"TRUE":"FALSE"));
qexec.close() ;
model.close();
OracleUtils.dropSemanticModel(oracle, szModelName);
oracle.dispose();
}
}
The following are the commands to compile and run Example D-8, as well as the expected output of the java
command.
javac -classpath ../jar/'*' Test12.java
java -classpath ./:../jar/'*' Test12 jdbc:oracle:thin:@localhost:1521:orcl scott <password-for-scott> M1
ask result = TRUE
Parent topic: Example Queries Using Graph Support for Apache Jena
D.3.9 Test13.java: SPARQL DESCRIBE Query
Example D-9 shows a SPARQL DESCRIBE query. It inserts triples that postulate the following:
-
John is a parent of Mary.
-
John is a parent of Jack.
-
Amy is a parent of Jack.
It then finds all relationships that involve any parents of Jack.
Example D-9 SPARQL DESCRIBE Query
import org.apache.jena.query.*;
import org.apache.jena.rdf.model.Model;
import oracle.spatial.rdf.client.jena.*;
import org.apache.jena.graph.*;
public class Test13
{
public static void main(String[] args) throws Exception
{
String szJdbcURL = args[0];
String szUser = args[1];
String szPasswd = args[2];
String szModelName = args[3];
Oracle oracle = new Oracle(szJdbcURL, szUser, szPasswd);
ModelOracleSem model = ModelOracleSem.createOracleSemModel(oracle, szModelName);
GraphOracleSem g = model.getGraph();
g.add(Triple.create(Node.createURI("u:John"), Node.createURI("u:parentOf"),
Node.createURI("u:Mary")));
g.add(Triple.create(Node.createURI("u:John"), Node.createURI("u:parentOf"),
Node.createURI("u:Jack")));
g.add(Triple.create(Node.createURI("u:Amy"), Node.createURI("u:parentOf"),
Node.createURI("u:Jack")));
String queryString = " DESCRIBE ?x WHERE {?x <u:parentOf> <u:Jack>}";
Query query = QueryFactory.create(queryString) ;
QueryExecution qexec = QueryExecutionFactory.create(query, model) ;
Model m = qexec.execDescribe();
System.out.println("describe result = " + m.toString());
qexec.close() ;
model.close();
OracleUtils.dropSemanticModel(oracle, szModelName);
oracle.dispose();
}
}
The following are the commands to compile and run Example D-9, as well as the expected output of the java
command.
javac -classpath ../jar/'*' Test13.java
java -classpath ./:../jar/'*' Test13 jdbc:oracle:thin:@localhost:1521:orcl scott <password-for-scott> M1
describe result = <ModelCom {u:Amy @u:parentOf u:Jack;
u:John @u:parentOf u:Jack; u:John @u:parentOf u:Mary} | [u:Amy, u:parentOf, u:Jack] [u:John, u:parentOf,
u:Jack] [u:John, u:parentOf, u:Mary]>
Parent topic: Example Queries Using Graph Support for Apache Jena
D.3.10 Test14.java: SPARQL CONSTRUCT Query
Example D-10 shows a SPARQL CONSTRUCT query. It inserts triples that postulate the following:
-
John is a parent of Mary.
-
John is a parent of Jack.
-
Amy is a parent of Jack.
-
Each parent loves all of his or her children.
It then constructs an RDF graph with information about who loves whom.
Example D-10 SPARQL CONSTRUCT Query
import org.apache.jena.query.*;
import org.apache.jena.rdf.model.Model;
import oracle.spatial.rdf.client.jena.*;
import org.apache.jena.graph.*;
public class Test14
{
public static void main(String[] args) throws Exception
{
String szJdbcURL = args[0];
String szUser = args[1];
String szPasswd = args[2];
String szModelName = args[3];
Oracle oracle = new Oracle(szJdbcURL, szUser, szPasswd);
ModelOracleSem model = ModelOracleSem.createOracleSemModel(oracle, szModelName);
GraphOracleSem g = model.getGraph();
g.add(Triple.create(Node.createURI("u:John"), Node.createURI("u:parentOf"),
Node.createURI("u:Mary")));
g.add(Triple.create(Node.createURI("u:John"), Node.createURI("u:parentOf"),
Node.createURI("u:Jack")));
g.add(Triple.create(Node.createURI("u:Amy"), Node.createURI("u:parentOf"),
Node.createURI("u:Jack")));
String queryString = " CONSTRUCT { ?s <u:loves> ?o } WHERE {?s <u:parentOf> ?o}";
Query query = QueryFactory.create(queryString) ;
QueryExecution qexec = QueryExecutionFactory.create(query, model) ;
Model m = qexec.execConstruct();
System.out.println("Construct result = " + m.toString());
qexec.close() ;
model.close();
OracleUtils.dropSemanticModel(oracle, szModelName);
oracle.dispose();
}
}
The following are the commands to compile and run Example D-10, as well as the expected output of the java
command.
javac -classpath ../jar/'*' Test14.java
java -classpath ./:../jar/'*' Test14 jdbc:oracle:thin:@localhost:1521:orcl scott <password-for-scott> M1
Construct result = <ModelCom {u:Amy @u:loves u:Jack;
u:John @u:loves u:Jack; u:John @u:loves u:Mary} | [u:Amy, u:loves, u:Jack] [u:John, u:loves,
u:Jack] [u:John, u:loves, u:Mary]>
Parent topic: Example Queries Using Graph Support for Apache Jena
D.3.11 Test15.java: Query Multiple Models and Specify "Allow Duplicates"
Example D-11 queries multiple models and uses the "allow duplicates" option. It inserts triples that postulate the following:
-
John is a parent of Jack (in Model 1).
-
Mary is a parent of Jack (in Model 2).
-
Each parent loves all of his or her children.
It then finds out who loves whom. It searches both models and allows for the possibility of duplicate triples in the models (although there are no duplicates in this example).
Example D-11 Query Multiple Models and Specify "Allow Duplicates"
import org.apache.jena.query.*;
import org.apache.jena.rdf.model.Model;
import oracle.spatial.rdf.client.jena.*;
import org.apache.jena.graph.*;
public class Test15
{
public static void main(String[] args) throws Exception
{
String szJdbcURL = args[0];
String szUser = args[1];
String szPasswd = args[2];
String szModelName1 = args[3];
String szModelName2 = args[4];
Oracle oracle = new Oracle(szJdbcURL, szUser, szPasswd);
ModelOracleSem model1 = ModelOracleSem.createOracleSemModel(oracle, szModelName1);
model1.getGraph().add(Triple.create(Node.createURI("u:John"),
Node.createURI("u:parentOf"), Node.createURI("u:Jack")));
model1.close();
ModelOracleSem model2 = ModelOracleSem.createOracleSemModel(oracle, szModelName2);
model2.getGraph().add(Triple.create(Node.createURI("u:Mary"),
Node.createURI("u:parentOf"), Node.createURI("u:Jack")));
model2.close();
String[] modelNamesList = {szModelName2};
String[] rulebasesList = {};
Attachment attachment = Attachment.createInstance(modelNamesList, rulebasesList,
InferenceMaintenanceMode.NO_UPDATE,
QueryOptions.ALLOW_QUERY_VALID_AND_DUP);
GraphOracleSem graph = new GraphOracleSem(oracle, szModelName1, attachment);
ModelOracleSem model = new ModelOracleSem(graph);
String queryString = " CONSTRUCT { ?s <u:loves> ?o } WHERE {?s <u:parentOf> ?o}";
Query query = QueryFactory.create(queryString) ;
QueryExecution qexec = QueryExecutionFactory.create(query, model) ;
Model m = qexec.execConstruct();
System.out.println("Construct result = " + m.toString());
qexec.close() ;
model.close();
OracleUtils.dropSemanticModel(oracle, szModelName1);
OracleUtils.dropSemanticModel(oracle, szModelName2);
oracle.dispose();
}
}
The following are the commands to compile and run Example D-11, as well as the expected output of the java
command.
javac -classpath ../jar/'*' Test15.java
java -classpath ./:../jar/'*' Test15 jdbc:oracle:thin:@localhost:1521:orcl scott <password-for-scott> M1 M2
Construct result = <ModelCom {u:Mary @u:loves u:Jack; u:John @u:loves u:Jack} | [u:Mary, u:loves, u:Jack] [u:John, u:loves, u:Jack]>
Parent topic: Example Queries Using Graph Support for Apache Jena
D.3.12 Test16.java: SPARQL Update
Example D-12 inserts two triples into a model.
Example D-12 SPARQL Update
import org.apache.jena.util.iterator.*; import oracle.spatial.rdf.client.jena.*; import org.apache.jena.graph.*; import org.apache.jena.update.*; public class Test16 { public static void main(String[] args) throws Exception { String szJdbcURL = args[0]; String szUser = args[1]; String szPasswd = args[2]; String szModelName = args[3]; Oracle oracle = new Oracle(szJdbcURL, szUser, szPasswd); ModelOracleSem model = ModelOracleSem.createOracleSemModel(oracle, szModelName); GraphOracleSem g = model.getGraph(); String insertString = " PREFIX dc: <http://purl.org/dc/elements/1.1/> " + " INSERT DATA " + " { <http://example/book3> dc:title \"A new book\" ; " + " dc:creator \"A.N.Other\" . " + " } "; UpdateAction.parseExecute(insertString, model); ExtendedIterator ei = GraphUtil.findAll(g); while (ei.hasNext()) { System.out.println("Triple " + ei.next().toString()); } model.close(); OracleUtils.dropSemanticModel(oracle, szModelName); oracle.dispose(); } }
The following are the commands to compile and run Example D-12, as well as the expected output of the java
command.
javac -classpath ../jar/'*' Test16.java
java -classpath ./:../jar/'*' Test16 jdbc:oracle:thin:@localhost:1521:orcl scott <password-for-scott> M1
Triple http://example/book3 @dc:title "A new book"
Triple http://example/book3 @dc:creator "A.N.Other"
Parent topic: Example Queries Using Graph Support for Apache Jena
D.3.13 Test17.java: SPARQL Query with ARQ Built-In Functions
Example D-13 inserts data about two books, and it displays the book titles in all uppercase characters and the length of each title string.
Example D-13 SPARQL Query with ARQ Built-In Functions
import org.apache.jena.query.*; import oracle.spatial.rdf.client.jena.*; import org.apache.jena.update.*; public class Test17 { public static void main(String[] args) throws Exception { String szJdbcURL = args[0]; String szUser = args[1]; String szPasswd = args[2]; String szModelName = args[3]; Oracle oracle = new Oracle(szJdbcURL, szUser, szPasswd); ModelOracleSem model = ModelOracleSem.createOracleSemModel(oracle, szModelName); GraphOracleSem g = model.getGraph(); String insertString = " PREFIX dc: <http://purl.org/dc/elements/1.1/> " + " INSERT DATA " + " { <http://example/book3> dc:title \"A new book\" ; " + " dc:creator \"A.N.Other\" . " + " <http://example/book4> dc:title \"Semantic Web Rocks\" ; " + " dc:creator \"TB\" . " + " } "; UpdateAction.parseExecute(insertString, model); String queryString = "PREFIX dc: <http://purl.org/dc/elements/1.1/> " + " PREFIX fn: <http://www.w3.org/2005/xpath-functions#> " + " SELECT ?subject (fn:upper-case(?object) as ?object1) " + " (fn:string-length(?object) as ?strlen) " + " WHERE { ?subject dc:title ?object } " ; Query query = QueryFactory.create(queryString, Syntax.syntaxARQ); QueryExecution qexec = QueryExecutionFactory.create(query, model); ResultSet results = qexec.execSelect(); ResultSetFormatter.out(System.out, results, query); model.close(); OracleUtils.dropSemanticModel(oracle, szModelName); oracle.dispose(); } }
The following are the commands to compile and run Example D-13, as well as the expected output of the java
command.
javac -classpath ../jar/'*' Test17.java
java -classpath ./:../jar/'*' Test17 jdbc:oracle:thin:@localhost:1521:orcl scott <password-for-scott> M1
----------------------------------------------------------
| subject | object1 | strlen |
==========================================================
| <http://example/book3> | "A NEW BOOK" | 10 |
| <http://example/book4> | "SEMANTIC WEB ROCKS" | 18 |
----------------------------------------------------------
Parent topic: Example Queries Using Graph Support for Apache Jena
D.3.14 Test18.java: SELECT Cast Query
Example D-14 "converts" two Fahrenheit temperatures (18.1 and 32.0) to Celsius temperatures.
Example D-14 SELECT Cast Query
import org.apache.jena.query.*; import oracle.spatial.rdf.client.jena.*; import org.apache.jena.update.*; public class Test18 { public static void main(String[] args) throws Exception { String szJdbcURL = args[0]; String szUser = args[1]; String szPasswd = args[2]; String szModelName = args[3]; Oracle oracle = new Oracle(szJdbcURL, szUser, szPasswd); ModelOracleSem model = ModelOracleSem.createOracleSemModel(oracle, szModelName); GraphOracleSem g = model.getGraph(); String insertString = " PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> " + " INSERT DATA " + " { <u:Object1> <u:temp> \"18.1\"^^xsd:float ; " + " <u:name> \"Foo... \" . " + " <u:Object2> <u:temp> \"32.0\"^^xsd:float ; " + " <u:name> \"Bar... \" . " + " } "; UpdateAction.parseExecute(insertString, model); String queryString = " PREFIX fn: <http://www.w3.org/2005/xpath-functions#> " + " SELECT ?subject ((?temp - 32.0)*5/9 as ?celsius_temp) " + "WHERE { ?subject <u:temp> ?temp } " ; Query query = QueryFactory.create(queryString, Syntax.syntaxARQ); QueryExecution qexec = QueryExecutionFactory.create(query, model); ResultSet results = qexec.execSelect(); ResultSetFormatter.out(System.out, results, query); model.close(); OracleUtils.dropSemanticModel(oracle, szModelName); oracle.dispose(); } }
The following are the commands to compile and run Example D-14, as well as the expected output of the java
command.
javac -classpath ../jar/'*' Test18.java
java -classpath ./:../jar/'*' Test18 jdbc:oracle:thin:@localhost:1521:orcl scott <password-for-scott> M1
------------------------------------------------------------------------
| subject | celsius_temp |
========================================================================
| <u:Object1> | "-7.7222223"^^<http://www.w3.org/2001/XMLSchema#float> |
| <u:Object2> | "0.0"^^<http://www.w3.org/2001/XMLSchema#float> |
------------------------------------------------------------------------
Parent topic: Example Queries Using Graph Support for Apache Jena
D.3.15 Test19.java: Instantiate Oracle Database Using OracleConnection
Example D-15 shows a different way to instantiate an Oracle object using a given OracleConnection
object. (In a J2EE Web application, users can normally get an OracleConnection
object from a J2EE data source.)
Example D-15 Instantiate Oracle Database Using OracleConnection
import org.apache.jena.query.*; import org.apache.jena.graph.*; import oracle.spatial.rdf.client.jena.*; import oracle.jdbc.pool.*; import oracle.jdbc.*; public class Test19 { public static void main(String[] args) throws Exception { String szJdbcURL = args[0]; String szUser = args[1]; String szPasswd = args[2]; String szModelName = args[3]; OracleDataSource ds = new OracleDataSource(); ds.setURL(szJdbcURL); ds.setUser(szUser); ds.setPassword(szPasswd); OracleConnection conn = (OracleConnection) ds.getConnection(); Oracle oracle = new Oracle(conn); ModelOracleSem model = ModelOracleSem.createOracleSemModel(oracle, szModelName); GraphOracleSem g = model.getGraph(); g.add(Triple.create(Node.createURI("u:John"), Node.createURI("u:parentOf"), Node.createURI("u:Mary"))); g.add(Triple.create(Node.createURI("u:John"), Node.createURI("u:parentOf"), Node.createURI("u:Jack"))); g.add(Triple.create(Node.createURI("u:Mary"), Node.createURI("u:parentOf"), Node.createURI("u:Jill"))); String queryString = " SELECT ?s ?o WHERE { ?s <u:parentOf> ?o .} "; Query query = QueryFactory.create(queryString) ; QueryExecution qexec = QueryExecutionFactory.create(query, model) ; ResultSet results = qexec.execSelect() ; ResultSetFormatter.out(System.out, results, query); qexec.close() ; model.close(); OracleUtils.dropSemanticModel(oracle, szModelName); oracle.dispose(); } }
The following are the commands to compile and run Example D-15, as well as the expected output of the java
command.
javac -classpath ../jar/'*' Test19.java
java -classpath ./:../jar/'*' Test19 jdbc:oracle:thin:@localhost:1521:orcl scott <password-for-scott> M1
-----------------------
| s | o |
=======================
| <u:John> | <u:Mary> |
| <u:John> | <u:Jack> |
| <u:Mary> | <u:Jill> |
-----------------------
Parent topic: Example Queries Using Graph Support for Apache Jena
D.3.16 Test20.java: Oracle Database Connection Pooling
Example D-16 uses Oracle Database connection pooling.
Example D-16 Oracle Database Connection Pooling
import org.apache.jena.graph.*; import oracle.spatial.rdf.client.jena.*; public class Test20 { public static void main(String[] args) throws Exception { String szJdbcURL = args[0]; String szUser = args[1]; String szPasswd = args[2]; String szModelName = args[3]; // test with connection properties (taken from some example) java.util.Properties prop = new java.util.Properties(); prop.setProperty("MinLimit", "2"); // the cache size is 2 at least prop.setProperty("MaxLimit", "10"); prop.setProperty("InitialLimit", "2"); // create 2 connections at startup prop.setProperty("InactivityTimeout", "1800"); // seconds prop.setProperty("AbandonedConnectionTimeout", "900"); // seconds prop.setProperty("MaxStatementsLimit", "10"); prop.setProperty("PropertyCheckInterval", "60"); // seconds System.out.println("Creating OraclePool"); OraclePool op = new OraclePool(szJdbcURL, szUser, szPasswd, prop, "OracleSemConnPool"); System.out.println("Done creating OraclePool"); // grab an Oracle and do something with it System.out.println("Getting an Oracle from OraclePool"); Oracle oracle = op.getOracle(); System.out.println("Done"); System.out.println("Is logical connection:" + oracle.getConnection().isLogicalConnection()); GraphOracleSem g = new GraphOracleSem(oracle, szModelName); g.add(Triple.create(Node.createURI("u:John"), Node.createURI("u:parentOf"), Node.createURI("u:Mary"))); g.close(); // return the Oracle back to the pool oracle.dispose(); // grab another Oracle and do something else System.out.println("Getting an Oracle from OraclePool"); oracle = op.getOracle(); System.out.println("Done"); System.out.println("Is logical connection:" + oracle.getConnection().isLogicalConnection()); g = new GraphOracleSem(oracle, szModelName); g.add(Triple.create(Node.createURI("u:John"), Node.createURI("u:parentOf"), Node.createURI("u:Jack"))); g.close(); OracleUtils.dropSemanticModel(oracle, szModelName); // return the Oracle back to the pool oracle.dispose(); } }
The following are the commands to compile and run Example D-16, as well as the expected output of the java
command.
javac -classpath ../jar/'*' Test20.java
java -classpath ./:../jar/'*' Test20 jdbc:oracle:thin:@localhost:1521:orcl scott <password-for-scott> M1
Creating OraclePool
Done creating OraclePool
Getting an Oracle from OraclePool
Done
Is logical connection:true
Getting an Oracle from OraclePool
Done
Is logical connection:true
Parent topic: Example Queries Using Graph Support for Apache Jena