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

    SPARQL OPTIONAL support via Sesame adapter with 11g2


      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
                      "xxxxxxxxx", "xxxxxxxxxx");
      OraclePool pool = new OraclePool(ds);
      OracleSailStore sail = new OracleSailStore(pool, model);
      // Sail sail = new MemoryStore();
      Repository repository = new SailRepository(sail);
      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);
       * Building the query.
      StringBuilder queryStringBuilder = new StringBuilder();
      queryStringBuilder.append(String.format("PREFIX rdf: <%s>\n",
      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 . ",
              " <%s> foaf:givenName ?givenName . ", subjectString));
       * Optional part of the query.
              " OPTIONAL { <%s> foaf:familyName ?familyName }. ",
      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.");
      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
          '{ ?s rdf:type ?t . 
             ?s foaf:givenName ?givenName .
             OPTIONAL { ?s foaf:familyName ?familyName } .
      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

      Any idea what I'm doing wrong here?

      Best wishes,


      [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
          Can you please rerun the query with the OPTIONAL keyword taken out?


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

            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,