Example8.java: SPARQL Construct query

This example shows a SPARQL CONSTRUCT query. It inserts triples that assert the following:

It then constructs an RDF graph with information about who loves whom.

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 Example8
{
  public static void main(String[] args) throws Exception 
  {
    
String szStoreName  = args[0];
String szHostName   = args[1];
String szHostPort   = args[2];
String szModelName  = args[3];
    
System.out.println("Create Oracle NoSQL connection");
OracleNoSqlConnection conn 
= OracleNoSqlConnection.createInstance(szStoreName,
                                       szHostName, 
                                       szHostPort);
    
System.out.println("Create Oracle NoSQL model");
    
OracleModelNoSql model = 
                 OracleModelNoSql.createOracleModelNoSql(szModelName,
                                                         conn);
    
System.out.println("Clear model");
model.removeAll();
    
System.out.println("Get graph from model");
OracleGraphNoSql graph = model.getGraph();
    
System.out.println("Add 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")));
    
String szQuery = "CONSTRUCT { ?s <u:loves> ?o } " +
                 "WHERE {?s <u:parentOf> ?o }";
    
System.out.println("Execute construct query " + szQuery);
    
Query query = QueryFactory.create(szQuery) ;
QueryExecution qexec = QueryExecutionFactory.create(query, model);
    
Model constructModel = qexec.execConstruct();
System.out.println("Construct result = " + constructModel.toString());
    
qexec.close();
constructModel.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 Example8.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 Example8 <store_name> \
<host_name> <host_port> <graph_name>
Execute construct query CONSTRUCT { ?s <u:loves> ?o } 
WHERE {?s <u:parentOf> ?o }
Construct result = <ModelCom   {u:Amy @u:loves u:Jack; 
u:John @u:loves u:Mary; u:John @u:loves u:Jack} | >