SPARQL code missing individuals from classes that are not subclasses - sparql

I am currently working on a sparql query that computes every individual and the class that the individual exists in. Here is an (awful) example of what my ontology looks like:
Owl:Thing
-Lion
-Elephant
-Pets
-Dog
-Cat
Where Lion and Elephant are classes with no subclasses and Dog and Cat are subclasses of the class Pet. When I run my code, I got the individuals from Dog and Cat, but no individuals from Lion or Elephant.
SELECT ?indiv ?class
WHERE {
?class rdf:type owl:Class .
?indiv rdf:type owl:NamedIndividual .
?indiv rdf:type ?class
} ORDER BY ?class ?indiv
So basically, the result of my code are the individuals that come from only classes that are subclasses, when I am expecting to have the individuals from all classes. Could anyone please help?

Related

Query SPARQL resulting level 1 hierarchy

So, the case is i have an ontology, which contains subclasses and no individual like the picture below.
I want to do a query with SPARQL which will result in level 1 in the hierarchy (subclass of Thing). is that possible to do it?
To allow the questioner to actually select an answer, this from #UninformedUser's comments:
SELECT ?cls
WHERE {
?cls a owl:Class .
FILTER NOT EXISTS {
?cls rdfs:subClassOf ?sup .
FILTER(?sup != owl:Thing)
}
}
"this works if all classes are "tagged" as owl:Class"
I think you're saying "find all the things that are subclasses of owl:Thing and only subclasses of owl:Thing so why not look for all things rdfs:subClassOf other things and remove all the non owl:Thing matches:
SELECT *
WHERE {
?x rds:subClassOf ?y .
FILTER (!sameTerm(?y, owl:Thing))
}

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.

How to make property of property in Protégé?

I have a following problem to model in OWL using Protégé:
Multiple Songs could be performed in different Performances. Each Song could be arranged by different Arranger in different Performance.
I already know how to relate a Song to a Performance using object property. Now, how to map a Song-Performance pair to an Arranger? (In relational database, I would call this as a "descriptive attribute" of a many-to-many Song-Performance relationship).
I know that I could use an annotation to an object property, but I would like to be able to infer something from this property. (For example: what Song has an Arranger arranged, and in which Performance?) As far as I know, I am not able to do inference from an annotation.
It's not necessary to add properties of properties to model this scenario, although a property is an object (a uri) and therefore can include any property, not just annotation properties. rdfs:subPropertyOf is a good example. Statement reification isn't necessary either. It's a matter of creating an object that holds information about the song and performance.
Here is a model that represents an Arranger's relationship to a Song-Performance:
ex:SongPerformance a owl:Class .
ex:Arranger a owl:Class .
ex:arranged rdfs:domain ex:Arranger ;
rdfs:range ex:SongPerformance .
ex:songPerformed rdfs:domain ex:SongPerformance ;
rdfs:range ex:Arranger .
ex:performedIn rdfs:domain ex:SongPerformance ;
rdfs:range ex:Arranger .
Given this list, an example instance is:
ex:Arranger-1 ex:arranged ex:SP1 .
ex:SP1 ex:performedIn ex:Performance_1 ;
ex:songPerformed ex:Song1 .
Then you can find which songs has an arranger arranged in a given performance through the following SPARQl query:
SELECT ?arranger ?song ?performance
WHERE {
?arranger a ex:Arranger ;
ex:arranged ?sp .
?sp ex:songPerformed ?song ;
ex:performedIn ?performance .
}

Sub-group and Super-group (Class/Subclass) relation in OWL

I need some help regarding OWL syntax. I have a synthetic population class called 'Person'. Person contains population information, and PersonWithinAdminRegion is another class represents subgroup of person class. For example 'Person' class contains information about all persons in the USA. FloridaPerson or MiamiPerson can be an example of PersonWithinAdminRegion. Basically, PersonWithinAdminRegion is a subgroup of supergroup Person. It not sub-class because sub-class inherits properties of super-class and add some more. It is not the case in my situation. My question is how to show sub-group of a super-group in OWL syntax?
Person a owl:Class.
PersonWithinAdminRegion ? ?
There is no inheritance in OWL. Therefore no properties are inherited by subclasses. The subclass hierarchy is a type of subsumption hierarchy with only one semantic: a member of a subclass is a member of the (super) class. An example:
:Person a owl:Class .
:PersonWithinAdminRegion a owl:Class .
:PersonWithinAdminRegion rdfs:subClassOf :Person .
:FloridaPerson a :PersonWithinAdminRegion .
From this, one can infer:
:FloridaPerson a :Person .
And that is all. No other properties or values will be inferred. E.g. RDFS and OWL semantics are more like set theory (classification) than object-oriented class definition.
So it seems the above will get you the semantics that you want. If you wanted to go deeper into the subsumption hierarchy, let's say:
:PersonInFloridaRegion rdfs:subClassOf :PersonWithinAdminRegion .
:p1 a :PersonInFloridaRegion .
...then you will be able to infer:
:p1 a :PersonWithinAdminRegion .
:p1 a :Person .
...and so on.
Even though I am still not sure of your modelling perspective, and whether there are some properties that Person has, but PersonWithinAdminRegion, doesn't. Here is an idea for solution.
You can create a generic class Person, and a subclass of it USAPerson, then a PersonWithinAdminRegion, which is a subclass of Persons as well and a sibling of PersonWithinAdminRegion. Thus:
Both USAPerosn and PersonWithinAdminRegion are Person;
Persons will include USAPersons, so when make a subset of Person you can still have the control over USAPerosn;
PersonWithinAdminRegion can still not have properties that USAPerson have.
Person a owl:Class
USAPerson a owl:Class
PersonWithinAdminRegion a owl:Class
USAPerson rdfs:subClassOf Peron
PersonWithinAdminRegion rdfs:subClassOf Peron
Hope this helps.

OWL type inference with a restriction

I am studying the notion of OWL restrictions with Protege 4 using FaCT++ and a trivial ontology. Suppose I have an individual foo of class Something:
:Something a owl:Class.
:foo a :Something, owl:NamedIndividual.
and another class defined from a restriction on the hasBar property:
:hasBar a owl:ObjectProperty.
:SomethingElse owl:equivalentClass [a owl:Restriction;
owl:onProperty :hasBar;
owl:allValuesFrom :Something].
If I assert that:
:x :hasBar :foo.
why can't I infer from it that x is a SomethingElse (via the fact that foo is a Something)? The only way I can make it work is if the range of hasBar is defined:
:hasBar a owl:ObjectProperty;
rdfs:range :Something.
But I'd like to avoid that, because it puts a constraint on what can be the subject of hasBar (which causes me further trouble).
I think it is simpler to reason over real examples, let's consider the following knowledge base:
:eats rdf:type owl:ObjectProperty .
:Vegetable rdf:type owl:Class ;
rdfs:subClassOf owl:Thing .
:Vegetarian rdf:type owl:Class ;
owl:equivalentClass [ rdf:type owl:Restriction ;
owl:onProperty :eats ;
owl:allValuesFrom :Vegetable
] .
:Carrot rdf:type :Vegetable ,
owl:NamedIndividual .
:John rdf:type owl:NamedIndividual , owl:Thing ;
:eats :carrot .
You have some equivalences with your example: hasBar is eats, Vegetarian is SomethingElse, Vegetable is Something, foo is carrot and finally x is John.
Now you would like to infer that John is a Vegetarian (= x is SomethingElse).
It makes sense that it doesn't work with an owl:allValuesFrom. What you are saying here is that all instances of vegetarian, if they have a property, they must have Vegetable in range. So from that you could deduce that carrot is a vegetable for example, assuming you would know that John is a vegetarian in the first place.
It makes sense in natural language too: In your ontology you only know that John eats a carrot, this doesn't automatically make him a vegetarian (non-vegetarian people eat also carrots).
You could use a owl:someValuesFrom instead of a owl:allValuesFrom. This way, you would define every vegetarian has someone that eats some vegetable. In this case if we know that John eats a carrot, therefore he would be classified as vegetarian by the reasoner, based on your definition of the concept vegetarian.
Universal (allValuesFrom) and existential (someValuesFrom) restrictions are complicated to understand, there is often no right or wrong solution, it mostly depends to what you want to achieve.