2 Replies Latest reply on Jun 4, 2010 11:29 AM by 772653

    SPARQL OPTIONAL support via Sesame adapter with 11g2

    772653
      Hello,

      I'm trying to get OPTIONAL to work in a SPARQL query via Sesame. I'm not sure what I'm doing wrong or if it needs a patch.

      In the following example, I'm just inserting a foaf:Person with a foaf:givenName but no foaf:familyName and I'm trying to get the foaf:familyName optionally.
      Using a Sesame MemoryStore, this works fine, however, when using the OracleSailStore, this part of the query doesn't behave as optional, I get no results at all.
      If I run a similar query with optional and SEM_MATCH manually in SQL Developer, it works fine, as expected (except the problem of specifying the rdf:type as explained in my previous post [1]).


      Here is some sample code:
      /*
       * Setting up the connection.
       */
      String model = "test";
      OracleDataSource ds = OraclePool
              .getOracleDataSource(
                      "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=xxxxxxxxxxxxxx)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=xxxxxxxxxx)))",
                      "xxxxxxxxx", "xxxxxxxxxx");
      OraclePool pool = new OraclePool(ds);
      OracleSailStore sail = new OracleSailStore(pool, model);
      // Sail sail = new MemoryStore();
      
      Repository repository = new SailRepository(sail);
      repository.initialize();
      
      RepositoryConnection repositoryConnection = repository.getConnection();
      ValueFactory vf = repositoryConnection.getValueFactory();
      
      /*
       * Storing some test data.
       */
      URI context = vf.createURI("http://dummy.example/something/");
      String subjectString = context + "#me";
      URI subject = vf.createURI(subjectString);
      URI predicate;
      Value object;
      
      predicate = RDF.TYPE;
      object = vf.createURI(FOAF_NS + "Person");
      repositoryConnection.add(subject, predicate, object, context);
      
      predicate = vf.createURI(FOAF_NS + "givenName");
      object = vf.createLiteral("Bruno");
      repositoryConnection.add(subject, predicate, object, context);
      
      repositoryConnection.commit();
      
      /*
       * Building the query.
       */
      StringBuilder queryStringBuilder = new StringBuilder();
      queryStringBuilder.append(String.format("PREFIX rdf: <%s>\n",
              RDF.NAMESPACE));
      queryStringBuilder.append(String.format("PREFIX foaf: <%s> ", FOAF_NS));
      queryStringBuilder.append("SELECT * ");
      queryStringBuilder.append(String.format("FROM <%s> ", context));
      queryStringBuilder.append("WHERE { ");
      queryStringBuilder.append(String.format(" <%s> rdf:type foaf:Person . ",
              subjectString));
      queryStringBuilder.append(String.format(
              " <%s> foaf:givenName ?givenName . ", subjectString));
      /*
       * Optional part of the query.
       */
      queryStringBuilder.append(String.format(
              " OPTIONAL { <%s> foaf:familyName ?familyName }. ",
              subjectString));
      queryStringBuilder.append("} ");
      
      TupleQuery tupleQuery = repositoryConnection.prepareTupleQuery(
              QueryLanguage.SPARQL, queryStringBuilder.toString());
      TupleQueryResult result = tupleQuery.evaluate();
      if (result.hasNext()) {
          BindingSet bindingSet = result.next();
          Value givenName = bindingSet.getValue("givenName");
          System.out.println("Found (at least) one result: " + givenName);
      } else {
          System.out.println("No result found.");
      }
      
      repositoryConnection.close();
      Using the same test data, the following query seems to behave correctly (familyName is indeed optional and null in this particular example):
      SELECT  givenName, s, familyName, t
        FROM TABLE(SEM_MATCH(
          '{ ?s rdf:type ?t . 
             ?s foaf:givenName ?givenName .
             OPTIONAL { ?s foaf:familyName ?familyName } .
            }',
          SEM_Models('test'),
          null, 
          SEM_ALIASES(SEM_ALIAS('foaf','http://xmlns.com/foaf/0.1/')),
          null));
      I've read in another thread [2] that a patch might be required for OPTIONAL to work in SEM_MATCH, but (a) it seems to work already with SEM_MATCH as written manually and (b) this patch seems to be for 11g1, but I'm using 11.2.0.1.0.


      Any idea what I'm doing wrong here?


      Best wishes,

      Bruno.

      [1] Curly brace query with URI as an object
      [2] Re: enabling Semantic Technologies Support in 11gr2 help needed

      Edited by: harbulot on 03-Jun-2010 07:40

      It seems it also has something to do with the context and FROM in the SPARQL query.
        • 1. Re: SPARQL OPTIONAL support via Sesame adapter with 11g2
          alwu-Oracle
          Can you please rerun the query with the OPTIONAL keyword taken out?

          Thanks,

          Zhe Wu
          • 2. Re: SPARQL OPTIONAL support via Sesame adapter with 11g2
            772653
            Hi,

            I've tried this:
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX foaf: <http://xmlns.com/foaf/0.1/>
            SELECT * FROM <http://dummy.example/something/>
            WHERE {  ?x foaf:givenName ?givenName . ?x foaf:familyName ?familyName . } 
            I still get no results (which isn't surprising, because there is no statement with familyName in this example, since I'm trying to get optional to work).


            In contrast, the following queries work (but they're not the queries I'm after):

            (no FROM/context):
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX foaf: <http://xmlns.com/foaf/0.1/>
            SELECT *
            WHERE {  ?x foaf:givenName ?givenName .   OPTIONAL { ?x foaf:familyName ?familyName . } .} 
            (no optional):
            PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
            PREFIX foaf: <http://xmlns.com/foaf/0.1/>
            SELECT * FROM <http://dummy.example/something/>
            WHERE {  ?x foaf:givenName ?givenName . } 
            Best wishes,

            Bruno.