问题
I build this ontology in protege. I have this individual ev001
that has these types Room
,hasRoom only {rm001}
and rm001
has data property roomName "room 1"^^xsd:string
.
Right now I have a SPARQL query that returns
Event Room RoomName
ev001 {rm001}
My question is, how to get the room name from there, here is my query so far
SELECT ?event ?room ?roomname
WHERE { ?x owl:onProperty base:hasRoom .
?event a base:FilmScreening ;
a ?x .
?x owl:allValuesFrom ?room .
}
Any advice is appreciated, thank you very much
回答1:
In general, it looks like you'd just need:
SELECT ?event ?room ?roomname
WHERE {
?event base:hasRoom ?room .
?room base:roomName ?roomname.
}
You don't need to be retrieving all the axiom stuff with owl:onProperty, etc. However, in your case, the ontology is structured a bit strangely. E.g., you have content like:
<!-- http://www.example.org/ontologies/loncon3#pi00314001 -->
<owl:NamedIndividual rdf:about="http://www.example.org/ontologies/loncon3#pi00314001">
<!-- ... -->
<rdf:type>
<owl:Restriction>
<owl:onProperty rdf:resource="http://www.example.org/ontologies/loncon3#hasRoom"/>
<owl:allValuesFrom>
<owl:Class>
<owl:oneOf rdf:parseType="Collection">
<rdf:Description rdf:about="http://www.example.org/ontologies/loncon3#rm03005"/>
</owl:oneOf>
</owl:Class>
</owl:allValuesFrom>
</owl:Restriction>
</rdf:type>
<!-- ... -->
</owl:NamedIndividual>
In the Manchester syntax, that says that pi00314001 has the type:
hasRoom only { rm03005 }
Based on your question, it sounds like you expect the
pi00314001 hasRoom rm03005
is in your data, or at least inferable from it. Unfortunately, that's not what it actually means. When you say that an individual X has the type
p only D
it means it if X has any value for the property p, then that value must be an instance of D. Similarly, the content in your ontology says that if pi00314001 has a value for the property hasRoom, then that value must be from the class { rm03005 }. It doesn't say that pi00314001 actually has a value for that property, so you don't actually know whether it has rm03005 as a value for hasRoom or not.
If it's under your control, I think you'd want to add some actual object property assertions to your ontology, so that the query I mentioned above will work. Right now your ontology is telling you more about what's possible than what's actually the case.
That said, if you do want to retrieve the room from the data as it's structured now, you can follow the structure of the data and make that work too. It'd be something like:
select ?event ?roomName {
?event a [ owl:onProperty base:hasRoom ;
owl:allValuesFrom/owl:oneOf/rdf:rest*/rdf:first ?room ] .
?room base:roomName ?roomName .
}
来源:https://stackoverflow.com/questions/36432726/sparql-how-to-get-individual-data-property-from-linked-individual