3

I'm trying out a custom Stardog rule. The custom rule basically looks like the following:

@prefix rule: <tag:stardog:api:rule:> .
[] a rule:SPARQLRule ;
  rule:content """
PREFIX : <http://url/draft#>
IF {
      ?x a :Person; :has_yob ?yob.
      BIND (2014 - ?yob AS ?age)
   }
THEN {
      ?x :has_age ?age
}
""" .

I've uploaded this ttl file with the following java code:

final Connection conn = ConnectionConfiguration.to("db_name").server("snarl").connect();
conn.begin();
conn.add().io().context(new URIImpl("http://url/rules")).file(ttlFile);
conn.commit();

As I'd like to keep the rules in a separate graph, I've loaded the rule triples in the http://url/rules graph. The default graph, represented as tag:stardog:api:context:default in Stardog, contains the ontology axioms. When I use the following SPARQL query, the Stardog rule works as expected:

PREFIX : <http://url/draft#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT ?s ?age
FROM <tag:stardog:api:context:default> 
FROM <http://url/rules>
FROM NAMED <http://url/datasource>
WHERE {
  ?s rdf:type :Person .
  ?s :has_age ?age .
}

You're probably wondering what's wrong by now. I guess I have a wrong understanding of the FROM and FROM NAMED clauses. When I leave FROM <http://url/rules> out of the query, I expect no results from the query. And yet, I'm still getting the results like with the original query. How is this possible? This is how I think of these clauses:

  • FROM <tag:stardog:api:context:default>: use the ontology axioms from the default graph
  • FROM <http://url/rules>: use the rules in this particular query
  • FROM NAMED <http://url/datasource>: the actual data that needs to be queried

So I repeat my question, why am I getting the correct results when I leave the second FROM clause out of the SPARQL query? FYI, I'm always using reasoning type SL.

EDIT after @user1538695 answer

When I persist rules in the schema (TBox), I still have to add FROM <tag:stardog:api:context:default> in my queries. I only want to query one named graph and use the schema for reasoning. Shouldn't this be possible without having to explicitly mention the default graph (schema)? This is what my current query looks like:

PREFIX : <http://url/draft#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

SELECT ?s ?age
FROM <tag:stardog:api:context:default> 
FROM <http://url/datasource>
WHERE {
  ?s rdf:type :Person .
  ?s :has_age ?age .
}
Stanislav Kralin
  • 10,115
  • 4
  • 30
  • 52
tstorms
  • 4,761
  • 1
  • 22
  • 47

1 Answers1

3

First, there is a misunderstanding about the FROM NAMED clause. FROM NAMED clause specifies the named graphs that will be used for matching the graph patterns in a GRAPH clause. If your query has no GRAPH clauses the FROM NAMED will have no effect.

Second, Stardog considers rules to be part of the schema and the schema of a database is fixed via the reasoning.schema.graphs database option. No matter what FROM or FROM NAMED clauses your query uses, the schema axioms and rules used will be the same. FROM or FROM NAMED clauses will only determine the named graphs from which the instances will be matched to the query.

Finally, the default value of reasoning.schema.graphs in Stardog 2.x is the default graph (in Stardog 3.0 default value is changing to all graphs) so any axioms or rules in named graphs will be ignored unless you change this option. But there is also the query.all.graphs configuration option which instructs Stardog to use the union of all graphs as the default graph. Therefore, having reasoning.schema.graphs=default but changing query.all.graphs would indirectly change the schema graphs as well.

Based on this information if it still looks like you are not getting expected answers you should put together a minimal example and send it to the Stardog mailing list.

Evren Sirin
  • 346
  • 1
  • 2