Edit: Solution below
My problem is: I want to find individuals of a class, which has a certain object property restriction to a certain other class. In owlready2
, I can search for all INDIRECT_is_a
of all classes and filter this way. However, this apprears to me to be poorly executed, as I would have to create and search all ancestoral trees of all classes instead of going via the occurences of hasRelation
(which Protégé finds, no problem).
The closest I found was this post. But what if I don't want to specify the domain and range of my object property, but instead specify a restriction of the class as searched_class hasRelation some object_class
?
To paraphrase my SPARQL Query: Which individual's class has the (inherited) restriction hasRelation some test:object_class
?
My test ontology in RDF:
<?xml version="1.0"?>
<rdf:RDF xmlns="urn:absolute:test_onto#"
xml:base="urn:absolute:test_onto"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<owl:Ontology rdf:about="urn:absolute:test_onto"/>
<!-- urn:absolute:test_onto#hasExactRelation -->
<owl:ObjectProperty rdf:about="urn:absolute:test_onto#hasExactRelation"/>
<!-- urn:absolute:test_onto#hasSomeRelation -->
<owl:ObjectProperty rdf:about="urn:absolute:test_onto#hasSomeRelation"/>
<!-- urn:absolute:test_onto#child_class -->
<owl:Class rdf:about="urn:absolute:test_onto#child_class">
<rdfs:subClassOf rdf:resource="urn:absolute:test_onto#parent_class"/>
</owl:Class>
<!-- urn:absolute:test_onto#object_class -->
<owl:Class rdf:about="urn:absolute:test_onto#object_class"/>
<!-- urn:absolute:test_onto#parent_class -->
<owl:Class rdf:about="urn:absolute:test_onto#parent_class">
<rdfs:subClassOf>
<owl:Restriction>
<owl:onProperty rdf:resource="urn:absolute:test_onto#hasSomeRelation"/>
<owl:someValuesFrom rdf:resource="urn:absolute:test_onto#object_class"/>
</owl:Restriction>
</rdfs:subClassOf>
<rdfs:subClassOf>
<owl:Restriction>
<owl:onProperty rdf:resource="urn:absolute:test_onto#hasExactRelation"/>
<owl:qualifiedCardinality rdf:datatype="http://www.w3.org/2001/XMLSchema#nonNegativeInteger">1</owl:qualifiedCardinality>
<owl:onClass rdf:resource="urn:absolute:test_onto#object_class"/>
</owl:Restriction>
</rdfs:subClassOf>
</owl:Class>
<!-- urn:absolute:test_onto#instance -->
<owl:NamedIndividual rdf:about="urn:absolute:test_onto#instance">
<rdf:type rdf:resource="urn:absolute:test_onto#child_class"/>
</owl:NamedIndividual>
</rdf:RDF>
, and in Turtle:
@prefix : <urn:absolute:test_onto#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@base <urn:absolute:test_onto> .
<urn:absolute:test_onto> rdf:type owl:Ontology .
:hasExactRelation rdf:type owl:ObjectProperty .
:hasSomeRelation rdf:type owl:ObjectProperty .
:child_class rdf:type owl:Class ;
rdfs:subClassOf :parent_class .
:object_class rdf:type owl:Class .
:parent_class rdf:type owl:Class ;
rdfs:subClassOf [ rdf:type owl:Restriction ;
owl:onProperty :hasSomeRelation ;
owl:someValuesFrom :object_class
] ,
[ rdf:type owl:Restriction ;
owl:onProperty :hasExactRelation ;
owl:qualifiedCardinality "1"^^xsd:nonNegativeInteger ;
owl:onClass :object_class
] .
:instance rdf:type owl:NamedIndividual ,
:child_class .
.
prefix : <http://example.org/test_onto#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ?x where {
[ ?x a owl:Class ] :hasSomeRelation some :object_class .
}
does not work, as there is no instance of object_class.
How can I accheive this without instantiating test_onto:object_class?