You'll notice that the query:

static final String fromAliceQuery = "" +
        "SELECT (COUNT(*) AS ?count)\n" +
        "WHERE {\n" +
        " GRAPH <"+aliceGraphUri+"> { ?s ?p ?o . }\n" +
        "}\n";

gets the same answers in both cases (count of 2).

Using FROM will:

FROM/FROM NAMED completely overrides the dataset to be queried.

I got that the wrong way round. If you pass in a dataset, it will use that dataset. In effect, it ignores the FROM.

Calling
  queryDataset(null, fromAliceQuery);
will illustrate that (you get a 404).


1/ Default dataset - You gave it a dataset so the dataset has empty default graph and 2 named graphs.

{ ?s ?p ?o . } matches on the empty default - no results.

To access the named graphs, GRAPH is needed.

2/ TDB dataset: Picks the graph http://example.org/alice out of the dataset and puts it in a new dataset with that as the default graph.

TDB dataset, dynamic datasets applies.

{ ?s ?p ?o . } matches the default graph which is a link to a specific graph (the alice graph) from the dataset.

Or use unionDefaultGraph in which case the default graph is the union of all the named graph, hiding any real default graph.

        Andy

On 09/02/15 18:52, Willie Milnor wrote:
Andy,

Thanks for your reply, and for any further assistance/clarification.

I've previously read the link to dynamic datasets, which seems to be
specific to TDB datasets.  I am also familiar with as well as section 13.2
of the SPARQL spec, and I understand the differences in GRAPH and FROM/FROM
NAMED.

But what I'm seeing is that the use of FROM results in different query
results from a general Dataset as opposed to a TDB datasets, even loaded
with the same quads.  I'm not trying to pull data from the web,
necessarily, rather I am loading it from a set of files.

Below is my code.  I am loading the same quads into both the general
Dataset and the TDB dataset.  When run, the count query returns 0 against
the Dataset and 2 against the TDB dataset.

Code:

static final String aliceGraphUri = "http://example.org/alice";;
static final String bobGraphUri = "http://example.org/bob";;
  static final String fromAliceQuery = "" +
"SELECT (COUNT(*) AS ?count)\n" +
"FROM <"+aliceGraphUri+">\n" +
"WHERE {\n" +
" ?s ?p ?o .\n" +
"}\n";
  /**
  * @param args
  */
public static void main(String[] args) {
  Dataset defaultDataset = DatasetFactory.createMem();
Dataset tdbDataset = TDBFactory.createDataset("C:/tempTDB");
  populateDataset(defaultDataset);
populateDataset(tdbDataset);
  System.out.println("\n---------------------------------" +
"-------------------\nFROM "+aliceGraphUri+"\n");
queryDataset(defaultDataset, fromAliceQuery);
queryDataset(tdbDataset, fromAliceQuery);
  }

private static void populateDataset(Dataset dataset) {
  DatasetGraph datasetGraph = dataset.asDatasetGraph();
  datasetGraph.add(new Quad(
NodeFactory.createURI(bobGraphUri), // graph name
NodeFactory.createURI("urn:bob"),
NodeFactory.createURI("http://xmlns.com/foaf/0.1/name";),
NodeFactory.createLiteral("Bob")));

datasetGraph.add(new Quad(
NodeFactory.createURI(bobGraphUri), // graph name
NodeFactory.createURI("urn:bob"),
NodeFactory.createURI("http://xmlns.com/foaf/0.1/mbox";),
NodeFactory.createURI("mailto:bob@example";)));
  // add data to the alice named graph
datasetGraph.add(new Quad(
NodeFactory.createURI(aliceGraphUri), // graph name
NodeFactory.createURI("urn:alice"),
NodeFactory.createURI("http://xmlns.com/foaf/0.1/name";),
NodeFactory.createLiteral("Alice")));

datasetGraph.add(new Quad(
NodeFactory.createURI(aliceGraphUri), // graph name
NodeFactory.createURI("urn:alice"),
NodeFactory.createURI("http://xmlns.com/foaf/0.1/mbox";),
NodeFactory.createURI("mailto:alice@example";)));
  }
  private static void queryDataset(Dataset dataset, String sparql) {
Query query = QueryFactory.create(sparql);
QueryExecution qexec = QueryExecutionFactory.create(query, dataset);
ResultSetFormatter.out(qexec.execSelect());
qexec.close();
}




On Mon, Feb 9, 2015 at 1:08 PM, Andy Seaborne <[email protected]> wrote:

On 09/02/15 15:07, Willie Milnor wrote:

Hello,

I want to be able to load a set of ontologies into dataset in order to be
able to have named graph support in my SPARQL queries against said
dataset.  Specifically, I would like to be able to use FROM, FROM NAMED,
and GRAPH clauses in my SPARQL.

  From my investigation, it appears that full named graph support is only
available with TDB datasets, but not available when creating a dataset
using DatasetFactory.  Specifically, against a DatasetFactory created
dataset, using FROM in the query appears to have no affect at all, and
even
using FROM NAMED has no affect over simply having GRAPH in the query.

Am I correct, or is there something I'm missing?  Perhaps there is a
specific implementation of DatasetGraph that I should be creating?

I have sample code, but thought I'd simply ask the question in hopes that
there's a simple answer, either way.  Please let me know if the sample
code
will be useful.


GRAPH and FROM/FROM NAMED do different things.

GRAPH accesses named graphs in the dataset (i.e. happens during query
execution)

FROM/FROM NAMED describe the dataset to be queried (i.e. happens before
the query starts).

FROM/FROM NAMED completely overrides the dataset to be queried.

In the case of a TDB dataset, the FROM/FROM NAMED pick graph out of the
dataset to form a new one specifically for the query.

https://jena.apache.org/documentation/tdb/dynamic_datasets.html

For a general query, on a general Dataset from DatasetFactory, the graphs
are drawn from the web at large.

If you want FROM/FROM NAMED to pull graphs from the web, use a general
dataset.

If you want some graphs from a TDB dataset and some from the web at large,
create a general Dataset, and add graphs from TDB.

If you use a TDB dataset alone, you can't have dynamic data from the web.
It's database, not a web cache.

         Andy






Reply via email to