Library Version 12.1.3.0

Example5e.java: SPARQL query with INFERENCE/ASSERTED ONLY hints
Prev | Chapter 8. Quick Start for the RDF Graph Feature | Next

## Example5e.java: SPARQL query with INFERENCE/ASSERTED ONLY hints

Example 8.12 shows the SPARQL query with additional features including an inference only setting (INF_ONLY). It inserts triples that assert the following:

- John is a parent of Mary.

- John is a parent of Jack.

- Amy is a parent of Mary.

- Jack is a sibling of Mary (specified as inferred).

It then finds all the triples in the Oracle NoSQL Database. Example 5f in the RDF Graph feature package describes the same exercise using an asserted only setting (ASSERTED_ONLY). Note that Example 5f is not shown in this manual.

    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 Example5e
    {

      public static void main(String[] args) throws Exception
      {

    String szStoreName  = args[0];
    String szHostName   = args[1];
    String szHostPort   = args[2];
    int iRuleBaseId     = Integer.parseInt(args[3]);

    // Create Oracle NoSQL connection
    OracleNoSqlConnection conn
         = OracleNoSqlConnection.createInstance(szStoreName,
                                                szHostName,
                                                szHostPort);

    // Create model from default graph
    Model model = OracleModelNoSql.createOracleDefaultModelNoSql(conn);
    OracleGraphNoSql graph = (OracleGraphNoSql) model.getGraph();

    // Clear model
    model.removeAll();

    // 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")));

    // Create Oracle NoSQL inferred graph

    InferredGraphNoSql inferredGraph =
                            new InferredGraphNoSql(conn,
                                                   iRuleBaseId);

    // Add inferred triples
    inferredGraph.add(Triple.create(Node.createURI("u:Jack"),
                                        Node.createURI("u:siblingOf"),
                                        Node.createURI("u:Mary")));

    // Close inferred graph;
    inferredGraph.close();

    String prefix = " PREFIX ORACLE_SEM_FS_NS: " +
                    " <http://oracle.com/semtech#" +
                    "include_rulebase_id=" + iRuleBaseId +
                    ",inf_only>";
    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 with an inference only setting.

    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 Example5e.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 Example5e <store_name> \
    <host_name> <host_port> <rule_baseID>

    Execute query PREFIX ORACLE_SEM_FS_NS:
    <http://oracle.com/semtech#inf_only>
    select ?x ?y ?z WHERE {?x ?y ?z}

    ---------------------------------------
    | x        | y             | z        |
    =======================================
    | <u:Jack> | <u:siblingOf> | <u:Mary> |
    ---------------------------------------

The following represents the expected output of the java command if an asserted only setting is specified.

    --------------------------------------
    | x        | y            | z        |
    ======================================
    | <u:John> | <u:parentOf> | <u:Jack> |
    | <u:John> | <u:parentOf> | <u:Mary> |
    | <u:Amy>  | <u:parentOf> | <u:Jack> |
    --------------------------------------

Prev | Up | Next
Example5d.java: SPARQL query with DOP | Home | Example5g.java: SPARQL query with PLAN query hint