SPARQL: How to get an insance of an ontology, if depth of the class hierarchy is unknown? - semantics

I have a question about SPARQL. I have an ontology of animals:
Animals (is a superclass with object property <hasColor>)
------ Mammals (subclass of Animals)
------------- Dog (subclass of Mammals)
---------------- dog1 (a instance with property <hasColor>="white")
---------------- dog2 (a instance with property <hasColor>="red" )
------ Bird (subclass of Animals)
Is it possible to find with SPARQL "all Animals, that are 'white' " or "all instances of Animals"? And backwards: How can I know, if a instance (dog1) belongs to Animals?
NOTE: The depth and breadth of the class hierarchy is unknown in advance.
Also the query below will not work
SELECT ?x WHERE {?x rdfs:subClassOf :Animals . ?x :hasСolor "white"}
And the next query (find all Animals, that are 'white') works only if the depth of class hierarchy is known.
(So if the hierarchy is known, can I make the specified steps (from top of hierarchy to bottom) to reach the goal: in this case 2 steps.
SELECT ?z WHERE {
?x rdfs:subClassOf :Animals .
?y rdfs:subClassOf ?x .
?z rdf:type ?y .
?z :hasColor "white"
}
The same is true for the next example - "find all instances of Animals"
SELECT ?z WHERE {
?x rdfs:subClassOf :Animals .
?y rdfs:subClassOf ?x .
?z rdf:type ?y .
}
What to do, if the hierarchie is unknown?
The query will be processed with SDB (is a component of Jena).
I want something like :
select ?x where {?x rdfs:subClassOf :Animals . ?x :hasСolor "white"})
UPD. Solution for "find all Animals (instances), that are 'white'" might look like this:
SELECT ?y WHERE { ?x rdfs:subClassOf* :Animals . ?y rdf:type ?x . ?y
:hasColor "white"}

You can use transitivity in your SPARQL query (using *) :
SELECT ?y WHERE { ?x rdfs:subClassOf* :Animals .
?y rdf:type ?x .
?y :hasColor "white" }

Related

The mechanism of "FILTER NOT EXISTS" in SPARQL

Assuming the triples are following:
#prefix : <http://example/> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
#prefix foaf: <http://xmlns.com/foaf/0.1/> .
:alice rdf:type foaf:Person .
:alice foaf:name "Alice" .
:bob rdf:type foaf:Person .
and then we perform 3 queries based on SPARQL 1.1:
Q1:
SELECT ?s
WHERE
{
?s ?p ?o .
FILTER NOT EXISTS { ?s foaf:name ?y }
}
Q2:
SELECT ?s
WHERE
{
?s ?p ?o .
FILTER NOT EXISTS { ?x foaf:name ?y }
}
Q3:
SELECT ?s
WHERE
{
?s ?p ?o .
FILTER NOT EXISTS { ?x foaf:mailbox ?y }
}
These three queries return three different solutions. Could anyone help me figure out why Q2 evaluates to no query solution in contrast to Q1 and Q3? Many thanks in advance :)
Q2 returns no solution because in your data, there exists a statement that matches ?x foaf:name ?y: ?x = :alice and ?y = "Alice". You've put no further constraints on either ?x or ?y. So no matter what the other variables in your query (?s, ?p and ?o) are bound to, the NOT EXISTS condition will always fail and therefore the query returns no result.

SPARQL Query: How to get all individual and data property assertions?

Same as you know if we retrieve the object property or data property and subclass can do that by joining them in one variable :
Query 1
SELECT ?x ?y
WHERE { ?x rdfs:subClassOf ?y.
?x rdf:type owl:ObjectProperty.
}
the x var it's same object property and subclass of other class
im need to join (all individual "NamedIndividual")
with object property or subclass .
the problem that is ( ?x rdf:type owl:NamedIndividual . )
couldn't use "?x" in any other location same as :
?x rdfs:subClassOf ?y.
Query 2
SELECT ?x ?y
WHERE { ?x rdfs:subClassOf ?y.
?x rdf:type owl:NamedIndividual .
}
Query 3
SELECT ?x ?y
WHERE { ?x rdf:type owl:ObjectProperty.
?x rdf:type owl:NamedIndividual .
}
So: Query 2 and Query 3 cannot be implemented.
how I can solve this problem?

What does !a mean in SPARQL?

I'm using SPARQL to construct a query and would like to rule out certain results.
I'm aware FILTER NOT EXISTS can be used to do the task. For example:
SELECT * {
?sub a ?type .
FILTER NOT EXISTS {?sub a :NotExpectedType.}
}
But during exploration, I accidentally found I could write !a. That's a valid SPARQL query, but the result is different from FILTER NOT EXISTS. For example:
SELECT * {
?sub a ?type .
?sub !a :NotExpectedType .
}
So what does the !a mean? (I'm aware a is a shortcut for rdf:type, but that doesn't help me understanding yet.)
!a is a property path, and it means “a path of length one with a property other than rdf:type”.
SELECT ?x {
?x a ?type.
FILTER NOT EXISTS { ?x a :MyClass }
}
The query above will find the ?x where no triple ?x rdf:type :MyClass exists.
SELECT ?x {
?x a ?type.
?x !a :MyClass.
}
This query will find the ?x where a triple ?x ?property :MyClass exists for some ?property other than rdf:type.

Sparql delete entity and all its linked elements

I have a Fuseki DB with triples like the following:
<https://tomcat.antudo.it/data/bucket.json-WSP1WS8#row=1>
<https://tomcat.antudo.it/data/bucket.json-WSP1WS8#ws7col0>
"ws6dat1aa"
...
<https://tomcat.antudo.it/data/bucket.json-WSP1WS8#row=2>
<https://tomcat.antudo.it/data/bucket.json-WSP1WS8#ws6col2>
"ws6dat2aa"
...
<http://www.w3.org/2002/07/owl#bottomDataProperty>
<http://www.w3.org/2002/07/owl#propertyDisjointWith>
<https://tomcat.antudo.it/data/bucket.json-WSP1WS8#ws7col0>
I want to delete everything related to:
<https://tomcat.antudo.it/data/bucket.json-WSP1WS8#row=1>
The problem is that i'm not able to write a query that also target triples like that one:
<http://www.w3.org/2002/07/owl#bottomDataProperty>
<http://www.w3.org/2002/07/owl#propertyDisjointWith>
<https://tomcat.antudo.it/data/bucket.json-WSP1WS8#ws7col0>
I have used the following query to delete triples about a list of entities:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
DELETE {
?x ?y ?z
}
WHERE {
{
?x ?y ?z.
?x rdfs:seeAlso <https://tomcat.antudo.it/data/#dcat_test.csv-WSP1WS6>.
}
union
{
?x ?y ?z.
?x rdfs:seeAlso <https://tomcat.antudo.it/data/#dcat_test.csv-WSP1WS7>.
}
}
I think i have to do something similar to that:
SPARQL: Delete instance and all of its properties with linked subproperties but i'm stuck. Any help will be appreciated
The following should work if you want to delete the resource entirely from the graph. Note that you need to remove the triples "both ways".
DELETE {
<https://tomcat.antudo.it/data/bucket.json-WSP1WS8#row=1> ?p ?o .
?s1 ?p1 <https://tomcat.antudo.it/data/bucket.json-WSP1WS8#row=1> .
}
WHERE {
<https://tomcat.antudo.it/data/bucket.json-WSP1WS8#row=1> ?p ?o .
OPTIONAL {
?s1 ?p1 <https://tomcat.antudo.it/data/bucket.json-WSP1WS8#row=1> .
}
}

semantic web request result difference

PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT * WHERE {
?x foaf:primaryTopic ?n .
?x rdf:type ?z .
FILTER (regex(?x, '^http://en.wikipedia.org/wiki/Barack_Obama$')).
}
LIMIT 10
when i call this request from http://dbpedia.org/snorql, it works. but when i try below
SELECT * WHERE {
?x foaf:primaryTopic ?n .
?x rdf:type ?z .
?x owl:activeYearsStartDate ?v .
FILTER (regex(?x, '^http://en.wikipedia.org/wiki/Barack_Obama$')).
}
it doesn't work. but why?
I expect that you meant
?x dbpedia-owl:activeYearsStartDate ?v .
rather than
?x owl:activeYearsStartDate ?v .
Also, using regex here is more expensive than it needs to be. You could check whether the string value of a URI is something in particular with
filter( str(?x) = "…" )
but even that is a lot more work than you need. If you want ?x to be <http://en.wikipedia.org/wiki/Barack_Obama>, just use it instead of ?x, or bind ?x to it with values:
SELECT * WHERE {
<http://en.wikipedia.org/wiki/Barack_Obama> foaf:primaryTopic ?n .
<http://en.wikipedia.org/wiki/Barack_Obama> rdf:type ?z .
<http://en.wikipedia.org/wiki/Barack_Obama> owl:activeYearsStartDate ?v .
}
SELECT * WHERE {
values ?x { <http://en.wikipedia.org/wiki/Barack_Obama> }
?x foaf:primaryTopic ?n .
?x rdf:type ?z .
?x owl:activeYearsStartDate ?v .
}
That still looks suspicious, though. Are you sure you didn't want <http://dbpedia.org/resource/Barack_Obama>? And the use of foaf:primaryTopic stuff really makes me think that you want something more like:
select * {
?x foaf:isPrimaryTopicOf <http://en.wikipedia.org/wiki/Barack_Obama> ;
a ?type ;
dbpedia-owl:activeYearsStartDate ?v
}
limit 10
SPARQL results