Hi all,

I’m trying to understand how SPARQL with named graphs works within Jena,
but I’m having strange results.

I have defined one default graph and two named graphs. Very basic example.

|Dataset dataset = TDBFactory.createDataset(tdbPath); Model m =
dataset.getDefaultModel();
m.add(ResourceFactory.createResource("http://example.org/alice";),
ResourceFactory
.createProperty("http://purl.org/dc/elements/1.1/publisher";),
ResourceFactory.createStringLiteral("Alice And"));
m.add(ResourceFactory.createResource("http://example.org/bob";),
ResourceFactory
.createProperty("http://purl.org/dc/elements/1.1/publisher";),
ResourceFactory.createStringLiteral("Bob But")); Model m1 =
dataset.getNamedModel("http://example.org/alice";);
m1.add(ResourceFactory.createResource("urn:alice"),
ResourceFactory.createProperty("http://xmlns.com/foaf/0.1/name";),
ResourceFactory.createStringLiteral("Alice"));
m1.add(ResourceFactory.createResource("urn:alice"),
ResourceFactory.createProperty("http://xmlns.com/foaf/0.1/mbox";),
ResourceFactory.createStringLiteral("[email protected]")); Model m2 =
dataset.getNamedModel("http://example.org/bob";);
m2.add(ResourceFactory.createResource("urn:bob"),
ResourceFactory.createProperty("http://xmlns.com/foaf/0.1/name";),
ResourceFactory.createStringLiteral("Bob"));
m2.add(ResourceFactory.createResource("urn:bob"),
ResourceFactory.createProperty("http://xmlns.com/foaf/0.1/mbox";),
ResourceFactory.createStringLiteral("[email protected]")); // szQuery is
an string properly initialized depending on example Query query =
QueryFactory.create(szQuery); QueryExecution qexec =
QueryExecutionFactory.create(query, dataset); try { ResultSet results =
qexec.execSelect(); ResultSetFormatter.out(System.out, results, query);
} finally { qexec.close(); } |

Now when I run an example query, I’m getting things not expected:

*Query*

|szQuery = "" + " PREFIX dc: <http://purl.org/dc/elements/1.1/>" + "
PREFIX foaf: <http://xmlns.com/foaf/0.1/>" + " SELECT ?graph ?who ?mbox"
+ " FROM NAMED <http://example.org/alice>" + " FROM NAMED
<http://example.org/bob>" + " WHERE " + " { \n" + " ?graph dc:publisher
?who . " + " GRAPH ?graph { ?x foaf:mbox ?mbox . } " + " }"; |

*Result*

|---------------------- | graph | who | mbox | ======================
---------------------- |

And I was expecting two rows with Alice’s and Bob’s data.

Why is not happening that? How do I include the default graph in the
SPARQL using Jena?

It seems that if I only use only named graphs or FROM with a reference
to a named graph, everything works as expected, but in a kind of mixed
case like the one above, there is something I’m not doing correctly.

Thanks a lot.
Jorge

​

Reply via email to