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

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?

Related

Get range class of Datatype property

I have the following SPARQL query
SELECT DISTINCT ?p ?class ?type
WHERE {
?resource ?p ?target .
?p rdfs:range ?class .
?class rdf:type ?type .
}
I get results only for the object properties.
If ?class = xsd:float, then rdf:type = rdfs:Datatype, right?
Why I do not receive also results for the type of data properties?

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 based on some property

I want to get all the pizza names which has cheese toppings but the result shows (_:b0) which is kind of an owl restriction following is my query
PREFIX pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>
SELECT ?X WHERE {
?X rdfs:subClassOf* [
owl:onProperty pizza:hasTopping ;
owl:someValuesFrom pizza:CheeseTopping
]
}
using Pizza ontology from stanford
This works (Without reasoning enabled)
PREFIX pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>
SELECT ?X ?topping WHERE {
?X rdfs:subClassOf ?Y .
?Y owl:someValuesFrom ?topping .
?topping rdfs:subClassOf* pizza:CheeseTopping
}
ORDER BY ?X
Some are listed more than once as they could contain more than one CheeseTopping. To remove duplicates:
PREFIX pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>
SELECT DISTINCT ?X WHERE {
?X rdfs:subClassOf ?Y .
?Y owl:someValuesFrom ?topping .
?topping rdfs:subClassOf* pizza:CheeseTopping
}
ORDER BY ?X
This works if you enable a reasoner:
PREFIX pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>
SELECT DISTINCT ?X WHERE {
?X rdfs:subClassOf pizza:CheeseyPizza
}
Ref:
Used the pizza ontology from here: http://protege.stanford.edu/ontologies/pizza/pizza.owl
That query works but is really complex and might be incomplete because some pizzas use complex OWL constructs:
PREFIX pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#>
SELECT DISTINCT ?pizza WHERE {
{
?pizza rdfs:subClassOf* pizza:Pizza .
?pizza owl:equivalentClass|rdfs:subClassOf [
rdf:type owl:Restriction ;
owl:onProperty pizza:hasTopping ;
owl:someValuesFrom/rdfs:subClassOf* pizza:CheeseTopping
]
} UNION {
?pizza owl:equivalentClass _:b0 .
_:b0 rdf:type owl:Class ;
owl:intersectionOf _:b1 .
_:b1 (rdf:rest)*/rdf:first ?otherClass.
?otherClass rdf:type owl:Restriction ;
owl:onProperty pizza:hasTopping ;
owl:someValuesFrom/rdfs:subClassOf* pizza:CheeseTopping
}
}

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

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

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" }