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 graphFROM <http://url/rules>
: use the rules in this particular queryFROM 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 .
}