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]
1082408
[2]
4157698
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.