Example6.java: SPARQL ASK query

Thsi example 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.

import com.hp.hpl.jena.graph.*;
import com.hp.hpl.jena.query.*;
import oracle.rdf.kv.client.jena.*;

public class Example6
{
  
  public static void main(String[] args) throws Exception 
  {
        
String szStoreName  = args[0];
String szHostName   = args[1];
String szHostPort   = args[2];
String szModelName  = args[3];
    
// Create Oracle NoSQL connection
OracleNoSqlConnection conn 
      = OracleNoSqlConnection.createInstance(szStoreName,
                                             szHostName, 
                                             szHostPort);
    
    
// Create model from named graph
OracleModelNoSql model = 
OracleModelNoSql.createOracleModelNoSql(szModelName, 
                                        conn);    
// Clear model
model.removeAll();
    
// Get graph from model
OracleGraphNoSql graph = model.getGraph();
    
// Add triples
    
graph.add(Triple.create(Node.createURI("u:John"), 
                        Node.createURI("u:parentOf"),
                        Node.createURI("u:Mary")));
    
String szQuery = 
          " ASK { <u:John> <u:parentOf> <u:Mary> } ";
    
System.out.println("Execute ASK query " + szQuery);
    
Query query = QueryFactory.create(szQuery) ;
QueryExecution qexec = QueryExecutionFactory.create(query, model) ;
    
boolean b = qexec.execAsk();
System.out.println("Ask result = " + ((b)?"TRUE":"FALSE"));
    
// Close objects
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 Example6.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 Example6 <store_name> \
<host_name> <host_port> <graph_name>

Execute ASK query ASK { <u:John> <u:parentOf> <u:Mary> } 
Ask result = TRUE