SPARQL how to get all triples with Individuals? - sparql

I'm just getting started with SPARQL. I am not sure where the error is. I came across the error as I was testing out the reasoner.
i use Stardog as database and the reasoner is definitely on.
i have a simple model with two classes: Building and Room and with 1 individual each: building001 and room001.
I have two ObjectProperties hasLocation and isLocationOf .
Both ObjectProperties are inverse to each other.
The triple: Building001 hasLocation Room001 I have predefined, the reasoner should actually recognize the triple Room001 isLocationOf Building001 itself.
I now want to output all triples that belong to individuals. So I should get the two triples just described as a result.
With the query:
select * where {
?subject ?property ?object.
?subject a owl:NamedIndividual.
?property a owl:ObjectProperty.
?object a owl:NamedIndividual.
}
I only get the triple Building001 hasLocation Room001 as result.
with the query:
select * where {
?subject ?property ?object
}
I get the following triples as result :
Building001 hasLocation Room001
Room001 isLocationOf Building001
Building001 rdf:type Building
Building001 rdf:type owl:Thing
Room001 rdf:type Room
Room001 rdf:type owl:Thing
How do I get all the triples that belong to individuals? without getting rdf:type in the result?
Many thanks in advance!!

You can just filter out rdf:type:
SELECT * {
?s ?p ?o
FILTER (?p != rdf:type)
}
You'll notice when you turn reasoning on the triples associated with your schema are not returned but the rdf:type triples are. That's because these triples are considered part of your individual assertions, not your schema.

Related

Infer data with SPARQL in Protege

I'm trying to get my head around inferring RDF data. Say that I have these triples (RDF Turtle), which I created using Protege:
#prefix owl: <http://www.w3.org/2002/07/owl#> .
#prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
:hasSpouse rdf:type owl:ObjectProperty ,
owl:SymmetricProperty ;
rdfs:domain :People ;
rdfs:range :People .
:People rdf:type owl:Class .
:Jane_Doe rdf:type owl:NamedIndividual ,
:People .
:John_Doe rdf:type owl:NamedIndividual ,
:People ;
:hasSpouse :Jane_Doe .
The reasoner in Protege will kindly highlight the expected inference, that is :Jane_Doe :hasSpouse :John_Doe.
How can I see that inference with SPARQL? If I run this query in Protege (SPARQL tab):
SELECT ?subject
WHERE {?subject hasSpouse ?object .}
It shows the asserted triple, not the inferred one. I understand how to do it manually, e.g. :
CONSTRUCT {?object ?prop ?subject }
WHERE { ?prop rdf:type owl:SymmetricProperty .
?subject ?prop ?object .}
I'd see now the inferred data I'm expecting but 1) that would be losing the point imho (i.e; reinventing the wheel) 2) I cannot have 2 queries in this tab (construct, then select). There's got to be a way to do this automatically, just like the reasoner did.
I read in Stack Overflow a post saying to use 'Snap SPARQL' plugin in Protege. I tried but simple queries don't work (like the first one above). It's like it's a different language. How does it work?
So, how can I get the benefit of these owl properties with SPARQL? How can I have an OWL-aware SPARQL in Protege? Am I taking this the wrong way? What's the right way?
thanks for your help.
Nicolas
You need to make your inferences a part of your knowledge.
To do so, go to the SWRL Tab and click successively on the button
at the bottom of that Tab, start from the left to the right.

SPARQL select instance of equivalent class

I'm trying to create SPARQL query that returns individuals which are an instance of an equivalent class expression (thus imitating some of the reasoning skills of protégé on Semantic Web). Can anyone help me figure out how to do this? I have a class named 'violation' which contains an equivalent class restriction. Now I want to find the individuals that meet the conditions of this restriction.
This is my query so far:
SELECT distinct ?Carmovement ?person ?restriction ?message
WHERE {?Carmovement carmovement:has_driver ?person .
?Carmovement rdf:type ?restriction . #rdf:type is no succes for getting instances
?object owl:equivalentClass ?restriction .
?object violin:message ?message}`
The query does not give any results because I have used "rdf:type", I wonder in which way I can evaluate whether an instance meets the condition of a class restriction.
Sample class restriction:
owl:equivalentClass [ rdf:type owl:Class ;
owl:intersectionOf (
<http://desibo.frsf.utn.edu.ar/ontologies/2012/9/CarMovement.owl#Car_Movement>
[ rdf:type owl:Restriction ;
owl:onProperty <http://desibo.frsf.utn.edu.ar/ontologies/2012/9/CarMovement.owl#has_driver> ;
owl:allValuesFrom <http://desibo.frsf.utn.edu.ar/ontologies/2012/9/CarMovement.owl#BarredPerson>
]
)
] ;
I expect to get individuals who have a property "has_driver" of type "BarredPerson".

SPARQL all predicate-object pairs of subject and all its superclasses

Imagine you do something crazy and store your object-oriented model as an RDF graph.
shows a simplified example of the inheritance hierarchy and the associated attributes.
In practice, you get such graph structure if you translate some UML class diagram into RDFS.
The question is: what SPARQL query can deliver all the predicate-object pairs necessary to instantiate a particular resource of "Class C". In other words: how do you get all the predicate-object pairs along the whole inheritance chain (only single inheritance).
Given this diagram, the predicate-object pairs of all members of the class:ClassC is simply:
SELECT ?inst ?p ?o
WHERE {
?inst a :ClassC .
Inst ?p ?o .
Keep in mind that there is not property inheritance in RDF/RDFS. If you want to find all of property/values pairs for ClassA with entailments for subclasses then useL
SELECT ?inst ?p ?o
WHERE {
?cls rdfs:subClassOf* :ClassA .
?inst a ?cls .
?inst ?p ?o
}
In this respect, RDFS works a bit backwards of one's expectations of OO inheritance.
With the info from #scotthenninger the following query did the job:
SELECT ?p ?o
WHERE {
:ClassC rdfs:subClassOf* ?anySuperClass .
?anySuperClass ?p ?o .
}
edit:
Similar query gets all the self-defined properties and their range along the inheritance chain:
SELECT ?prop ?obj
WHERE {
:ClassC rdfs:subClassOf* ?anySuperClass .
?prop rdfs:domain ?anySuperClass .
?prop rdfs:range ?obj .
}
End results combined:
foo:ID xsd:string
foo:name xsd:string
rdfs:comment xsd:string
foo:similarTo :ClassD

Custom SPARQL Construct with enumeration

Is it possible to execute SPARQL construct while adding information outside the scope of query? e.g., I want to execute SPARQL construct while defining enumeration information like this:
PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
construct {
?s a skos:Concept
?s ex:index <enumeration starting from 1 -- this is just a sample>
}
where {
?s a skos:Concept
}
is it possible to do something like that with pure SPARQL? what are the alternatives?
* Additional Information *
Probably I am not explained my problem clearly, so basically I want to achieve the following (assuming that ex:index is a valid datatypeProperty):
== Initial RDF triples ==
#prefix skos:<http://www.w3.org/2004/02/skos/core#>
#prefix ex: <http://example.org/> .
ex:abc rdf:type skos:Concept .
ex:def rdf:type skos:Concept .
...
ex:endOfSample rdf:type skos:Concept .
== RDF triples after SPARQL Update execution ==
#prefix skos:<http://www.w3.org/2004/02/skos/core#>
#prefix ex: <http://example.org/> .
ex:abc rdf:type skos:Concept ;
ex:index 1 .
ex:def rdf:type skos:Concept ;
ex:index 2 .
...
ex:endOfSample rdf:type skos:Concept ;
ex:index <endOfSampleNumber> .
You can construct any valid RDF value in a CONSTRUCT. However the query will fail if any of the variables in the CONSTRUCT graph pattern is unbound after executing the WHERE graph. I.e. there can be no binding for ?p in your query and the CONSTRUCT will never execute.
This is an example that should get you started:
PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
PREFIX ex:<http://example.org/construct#>
construct {
ex:someProp a owl:ObjectProperty .
?s ex:someProp (1 2 3)
}
where {
?s a skos:Concept
}
This will result in the construction of seven triples for the property value and the list structure.
The ex:someProp is added because there isn't a good object property in SKOS for ad-hoc lists. It would be best to define the property with some semantic meaning. Also note that while the {ex:someProp a owl:ObjectProperty} triple will be asserted for each match of {?s a skos:Concept}, it is the same triple, hence there will be only one in the end. The price is efficiency, so asserting the property outside of this query would be a better choice - it is included in the above query for the sake of example completeness.

Retrieving the wider dbpedia vocabulary for tagging pictures

I'm trying to develop a tool in JS for tagging pictures, so I need a set of possible "things" from dbpedia. I already tryed to retrieve this way:
select ?s ?l {
?s a owl:Class .
?s rdf:type ?l
FILTER regex(str(?s), "House", "i").
}
http://dbpedia.org/snorql/?query=select+%3Fs+%3Fl+%7B%0D%0A+++%3Fs+a+owl%3AClass+.%0D%0A+++%3Fs+rdf%3Atype+%3Fl%0D%0A+++FILTER+regex%28str%28%3Fs%29%2C+%22House%22%2C+%22i%22%29.%0D%0A%7D
And also this way:
select ?label
WHERE {
?concept a skos:Concept.
?concept skos:prefLabel ?label.
FILTER regex(str(?label), "^House", "i").
}
http://dbpedia.org/snorql/?query=select+%3Flabel+%0D%0AWHERE+%7B%0D%0A++%3Fconcept+a+skos%3AConcept.%0D%0A++%3Fconcept+skos%3AprefLabel+%3Flabel.%0D%0A++FILTER+regex%28str%28%3Flabel%29%2C+%22%5EHouse%22%2C+%22i%22%29.%0D%0A%7D
In the first case, I just have "instances" of the house "thing", but not the "House" class itself. In the second one, I never retrieve the "house" and the similar thing is "houses". Any alternative for retrieving a better vocabulary based in dbpedia dataset?
If you don't bother to restrict yourself to owl:Thing or to skos:Concept, you can just get things that have a label that contains "house". Rather than using regex, I chose to use contains and lcase, since a string containment could be less expensive than invoking a full regular expression processor.
select ?thing ?label where {
?thing rdfs:label ?label .
filter contains(lcase(?label), "house")
}
SPARQL results (limited to 200)