0

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?

Philipp
  • 1
  • 1
  • 1
    your Turtle translation is wrong, neither does it contain the individual, nor did you make the class a subclass of the restriction. You can also not use Manchester syntax like `test_onto:hasRelation some test_onto:Object_Class` in Turtle. Please use a tool for converting from RDF/XML to Turtle to see the difference. – UninformedUser Aug 26 '22 at 15:27
  • 1
    That said, I don't get exactly what you're asking for? Is just the property restriction given and you want to get all individuals that do belong to this complex class expression? – UninformedUser Aug 26 '22 at 15:31
  • 1
    By the way, your RDF/XML is also wrong - that's not a valid OWL ontology - did you write it by hand? – UninformedUser Aug 26 '22 at 15:39
  • Once you fixed the data issues, that query works: `select * where { ?x a ?cls . ?cls rdfs:subClassOf [ owl:onProperty :hasRelation ; owl:someValuesFrom :Object_Class ] . }` – UninformedUser Aug 26 '22 at 15:45

1 Answers1

0

@UninformedUser, thank you for your responses! Yes, I did modify the code manually, as the printout from Protégé seemed too large for me, sorry about that. I fixed it now.

Also, I managed to figure out a SPARQL query that does pretty much what you suggested:

PREFIX to:      <urn:absolute:test_onto#>
PREFIX rdfs:    <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?indiv ?class ?restr
WHERE {
    ?indiv rdf:type ?class .
    ?class rdfs:subClassOf+ ?restr .
    ?restr a owl:Restriction .
    ?restr owl:onProperty to:hasSomeRelation
    ?restr owl:someValuesFrom to:object_class .
    }

Thank you very much for your help!

*Edit: * added ?restr owl:onProperty to:hasSomeRelation

Philipp
  • 1
  • 1