SPARQL 1.1 entailment regimes and query with FROM clause (follow-up) - sparql

This is a follow-up question from SPARQL 1.1 entailment regimes and query with FROM clause
I'm currently documenting/testing about SPARQL 1.1 entailment regimes and the recommendation repeatedly states that
The scoping graph is graph-equivalent to the active graph...
So it would seems that the inference scoping graph depends on the query.
The question is: does the scoping graph stems from the query's dataset (FROM/FROM NAMED clauses) or does it refer to the real current active graph context for the triple pattern being evaluated ?
With the following graphs
# Named graph: <urn:rdfs-schema-graph>
#prefix ex:<http://www.example.org/> .
ex:Article rdfs:subClassOf ex:Publication .
ex:publishes rdfs:range ex:Publication .
# Named graph: <urn:data-graph>
#prefix ex:<http://www.example.org/> .
ex:book1 a ex:Publication .
ex:book2 a ex:Article .
ex:MITPress ex:publishes ex:book3 .
What should the following query return (here under RDFS-entailment regime, for instance) and according to the recommendation ?
PREFIX ex: <http://www.example.org/>
SELECT ?s
FROM <urn:rdfs-schema-graph>
FROM NAMED <urn:data-graph>
WHERE {
GRAPH <urn:data-graph> {
?s a ex:Publication .
}
}
should I get back all three resources:
<http://www.example.org/book1>
<http://www.example.org/book2>
<http://www.example.org/book3>
or just
<http://www.example.org/book1>
since the active graph on the triple pattern is scoped to the NAMED graph while inferencing axioms are "located" in the default graph ?
Thanks for your insight,
Max.

Related

Sparql query to read from all named graphs without knowing the names

I am looking to run a SPARQL query over any dataset. We dont know the names of the named graphs in the datasets.
These are lots of documentation and examples of selection from named graphs when you know the name of the named graph/s. There are examples showing listing named graphs.
We are running the Jena from Java so it would be possible to run 2 queries, the first gets the named graphs and we inject these into the 2nd.
But surely you can write a single query that reads from all named graphs when you dont know their names?
Note: we are looking to stay away from using default graph/s as their behaviour seems implementation dependent.
Example:
{
?s foaf:name ?name ;
vCard:nickname ?nickName .
}
If you want the pattern to match within one graph and wish to try each graph, use the GRAPH ?g form.
GRAPH ?g
{ ?s foaf:name ?name ;
vc:nickname ?nickName .
}
If you want to make a query where the pattern matches across named graphs, -- e.g. foaf:name in one graph and vCard:nickname in another, same subject --
then set union default graph tdb2:unionDefaultGraph true then the default graph as seen by the query is the union (actually, RDF merge - no duplicates) of all the named graphs. Use the pattern as originally given.
Fuseki configuration file extract:
:dataset_tdb2 rdf:type tdb2:DatasetTDB2 ;
tdb2:location "DB2" ;
## Optional - with union default for query and update WHERE matching.
tdb2:unionDefaultGraph true ;
.
In code, not Fuseki, the application can use Dataset.getUnionModel().

GraphDB: Use RDF-star for edge labels in custom graph?

https://graphdb.ontotext.com/documentation/standard/exploring-data.html#create-your-own-visual-graph describes the ability to create a visual graph using SPARQL queries, and for the "Edge Basics" step it says:
Edge basics: This query SELECT the ?label binding that determines the text of the edge. If empty, the edge IRI’s local name is used.
In the UI for this step, there is also the "RDFS or SKOS label" sample query, which fills:
# Note that ?edge is the edge's IRI and must be used in the query.
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
SELECT ?label {
# Select either rdfs:label or skos:prefLabel as the label
?edge rdfs:label | skos:prefLabel ?label .
}
My question is how do I leverage this capability? More specifically, what is the edge IRI (i.e. ?edge) that is the subject of the rdfs:label statement?
Is it simply the RDF predicate in the graph, as in the following?
:someSubject :somePredicate :someObject .
:somePredicate rdfs:label "Some friendly label for every :somePredicate relation" .
Is it possible somehow to use RDF-star, as in the following?
:someSubject :somePredicate :someObject .
<<:someSubject :somePredicate :someObject>> rdfs:label "A friendly label for the particular relation between :someSubject and :someObject" .
It appears to be the former case, as I get the following result when I load the above statements and use the sample query above:
someSubject --"Some friendly label for every :somePredicate relation"--> someObject
I'm curious if there is any way to leverage RDF-star statements in order to render edge-specific labels?

A simple SPIN rule doesn't work in RDF4J

I've just started using the triple store RDF4J (I am using its workbench, version 2.3.1, run on Windows 10 with Tomcat 9.0)
I want to use the SPIN rules in RDF4J. Therefore, I created a new repository (In memory with RDFS+SPIN support).
I wanted to start with the SPIN example in RDF4J documentation concerning how to add SPIN rules. That is, I added the data (in Turtle, and imported to RDF4J)
#prefix ex: <http://example.org/>.
ex:John a ex:Father ;
ex:parentOf ex:Lucy .
ex:Lucy a ex:Person .
And the rule:
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
#prefix sp: <http://spinrdf.org/sp#>.
#prefix spin: <http://spinrdf.org/spin#>.
#prefix ex: <http://example.org/>.
ex:Person a rdfs:Class ;
spin:rule [
a sp:Construct ;
sp:text """PREFIX ex: <http://example.org/>
CONSTRUCT { ?this ex:childOf ?parent . }
WHERE { ?parent ex:parentOf ?this . }"""
] .
And as instructed in the documentation, I exposed the query (with the checkbox 'Include inferred statements' checked),
PREFIX ex: <http://example.org/>
SELECT ?child
WHERE { ?child ex:childOf ?parent }
However, no result returned:
Could someone, please tell me am I doing something wrong, why the SPIN rule doesn't work in my RDF4J workbench, have I missed something?
(reposting my comment as an answer for future readers)
The SPIN reasoner currently assumes that all data is in the default context, I think. Make sure that your data was not added to a named graph.

Default RDFS inference in Virtuoso 7.x

This is a question about simple RDFS inference in Virtuoso 7.1 and DBpedia. I have a Virtuoso instance which was installed using this link as a reference. Now if I query the endpoint with the following query :
Select ?s
where { ?s a <http://dbpedia.org/ontology/Cricketer> . }
I get a list of Cricketers that are present in DBpedia. Suppose I want all the athletes (all sports and cricketers included, where Athlete is rdfs:superClassOf Cricketer), I just try the query
Select ?s
where { ?s a <http://dbpedia.org/ontology/Athlete> . }
For this I get all the correct answers. However I have an issue with rdfs:subPropertyOf. For example the property <http://dbpedia.org/ontology/capital> is the sub-property of <http://dbpedia.org/ontology/administrativeHeadCity>. So suppose I want all the capitals and the administrative head cities and I issue the query
Select ?s ?o
where { ?s <http://dbpedia.org/ontology/administrativeHeadCity> ?o . }
I get zero results. Why is it that subproperty inference isn't working in DBpedia? Is there something else that I have missed?
You've missed a couple of things.
First, Virtuoso is at 7.2.4 as of April 2016, and this version is strongly recommended over the old version from 2014, for many reasons.
#AKSW's advice about Property Paths will work much better with this later version, too.
Then, you can use inference on the DBpedia endpoint (including your local mirror), through the input:inference pragma, as shown on the live results of the query shown below --
DEFINE input:inference "http://dbpedia.org/resource/inference/rules/dbpedia#"
SELECT ?place ?HeadCity
WHERE
{
?place <http://dbpedia.org/ontology/administrativeHeadCity> ?HeadCity
}
ORDER BY ?place ?HeadCity
You can also see a list of predefined inference rule sets.
And... more of the relevant documentation.
(ObDisclaimer: I work for OpenLink Software, producer of Virtuoso.)
There is no automatic inference enabled in DBpedia. DBpedia itself is a dataset loaded into Virtuoso.
The reason that you get all instances with a superclass like dbo:Athlete is that subclass-inheritance is fully materialized in the current DBpedia dataset:
(s rdf:type c1), (c1 rdfs:subClassOf c2) -> (s rdf:type c2)
That means that for each individual x, the DBpedia dataset contains all the classes C it belongs to - in fact also the superclasses.
That procedure was not done for subproperty-inheritance, i.e.,
(s p1 o), (p1 rdfs:subPropertyOf p2) -> (s p2 o)
You can solve that problem with SPARQL 1.1 property paths:
SELECT ?s ?o WHERE {
?p rdfs:subPropertyOf* <http://dbpedia.org/ontology/administrativeHeadCity> .
?s ?p ?o .
}

in turtle or RDF can I add a predicate/object on all subjects that match a criteria?

I am doing some experiments with importing triples formulated in the turtle language
within the openrdf-workbench webapp in Tomcat, which has incorporated a SPARQL endpoint.
I wonder if with turtle, or, generally, in RDF / RDFS is it possible to add a certain predicate/object declaration on all (implicit) subjects conditionally to the existence of another predicate/object.
For example, if I have the following triples defined:
foo:a foo:b foo:c
foo:d foo:b foo:c
foo:e foo:b foo:c
foo:f foo:b foo:c
I would like to automatically add the following predicate/subject to all subjects that match predicate=foo:b and object=foo:c:
(implicit subject) foo:g foo:h
in order to automatically produce the following triples:
foo:a foo:g foo:h
foo:d foo:g foo:h
foo:e foo:g foo:h
foo:f foo:g foo:h
Is this possible?
Alternatively: is there any way to define some triples in order to enable SPARQL to find foo:a/d/e/f when queried for subjects that have foo:g foo:h as predicate/object?
Part 1 - Creating additional information
The first part of your question can be solved in one of two ways:
Using Inference
Using SPARQL Update
Inferencing
Inference is a technique whereby you define rules that infer additional triple based on your existing triples. You typically either use a pre-defined set of rules or use your own custom rules. I think Sesame only supports pre-defined rule sets out of the box so you may want to take a look at OWLIM which is an alternative back end that can be used with Sesame and has much more customisable rules AFAIK.
Inferencing can typically be applied in two ways, one where you only store the rules and you compute the additional information every time a rule fires and another where you pre-compute all the additional information and add it to your database. Which you will want to use depends on how you intend to use your system and there are performance trade offs involved. I'm not going into detail because that's really a whole other question - see Forward vs Backward Chaining for some discussion
SPARQL Update
Alternatively if your rules are relatively simple and you are OK with pre-computing the extra information and adding it to your database you can write SPARQL Updates to do this e.g.
PREFIX foo: <http://example.org/foo#>
INSERT
{
?x foo:g foo:h .
}
WHERE
{
?x foo:b foo:c .
}
Part 2 - Querying the Data
I am guessing you are fairly new to SPARQL because from what you've described this sounds trivial to me.
If I wanted to find all subjects which had the predicate foo:g and the object foo:h I would simply write the following:
PREFIX foo: <http://example.org/foo#>
SELECT ?x
WHERE
{
?x foo:g foo:h .
}
You can do this type of inference using OWL with an axiom of the form
p value a &sqsubseteq; q value b
which says that if something has a as a value for property p, then it also has b as a value for property q. As an example, here's an ontology with four individuals (a, b, c, d), two object properties (p, q), and the axiom (p value c &sqsubseteq; q value d).
#prefix : <http://example.org/add-predicate-object#> .
#prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
#prefix owl: <http://www.w3.org/2002/07/owl#> .
#prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
<http://example.org/add-predicate-object> a owl:Ontology .
:p a owl:ObjectProperty .
:q a owl:ObjectProperty .
[ a owl:Restriction ;
owl:onProperty :p ;
owl:hasValue :c ;
rdfs:subClassOf [ a owl:Restriction ;
owl:onProperty :q ;
owl:hasValue :d ] . ] .
:a a owl:Thing, owl:NamedIndividual ; :p :c .
:b a owl:Thing, owl:NamedIndividual ; :p :c .
:c a owl:Thing, owl:NamedIndividual .
:d a owl:Thing, owl:NamedIndividual .
In Protégé, the axiom looks like this:
You can enable a reasoner and query for instances of q value d and see:
or you can browse to individuals and see the results: