How to get Least common subsumer in ontology using SPARQL Query?

前端 未结 1 1361
终归单人心
终归单人心 2020-12-06 22:01

How to get Least Common Subsumer(LCS) of individuals in ontology using SPARQL Query? (I want to get common concept of two individuals. Here i have mentioned owl file code)

相关标签:
1条回答
  • 2020-12-06 22:50

    This may be a duplicate of finding common superclass and length of path in class hierarchies, but that question doesn't have an accepted answer, and is also asking about path length, which makes things more complicated. In general, you can find common superclasses of classes ?subclass1 and ?subclass2 with:

    ?subclass1 rdfs:subClassOf* ?superclass .
    ?subclass2 rdfs:subClassOf* ?superclass .
    

    You can make that shorter with:

    ?superclass ^rdfs:subClassOf* ?subclass1, ?subclass2 .
    

    That will find all common superclasses of the two subclasses. There may not be a single most specific common superclass, but you can find those that are as specific as possible by asking for only those superclasses that are not subclasses of any other superclass:

    ?superclass ^rdfs:subClassOf* ?subclass1, ?subclass2 .
    filter not exists { 
      ?moreSpecificSuperclass rdfs:subClassOf ?superclass ;
                              ^rdfs:subClassOf* ?subclass1, ?subclass2 .
    }
    

    To find the LCS of some particular instances, you'd need to do something like this:

    ?instance1 rdf:type/rdfs:subClassOf* ?lcs .
    ?instance2 rdf:type/rdfs:subClassOf* ?lcs .
    filter not exists {
      ?instance1 rdf:type/rdfs:subClassOf* ?sublcs .
      ?instance2 rdf:type/rdfs:subClassOf* ?sublcs .
      ?sublcs rdfs:subClassOf ?lcs .
    }
    

    As before, you can shorten that bit to:

    ?lcs ^(rdf:type/rdfs:subClassOf*) ?instance1, ?instance2 .
    filter not exists {
      ?sublcs ^(rdf:type/rdfs:subClassOf*) ?instance1, ?instance2 ;
              rdfs:subClassOf ?lcs . 
    }
    

    The first line ensures that ?type is a common type of ?x and ?y, and the filter expression ensures that there's no subclass of ?type that is also a common type of ?x and ?y.

    Here's a minimal example that shows that this approach works. Here's an ontology with the class hierarchy:

    owl:Thing
      A
        B
          C {c}
          D {d}
    
    <rdf:RDF
        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        xmlns="https://stackoverflow.com/q/23510851/1281433/lcs#"
        xmlns:owl="http://www.w3.org/2002/07/owl#"
        xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema#">
      <owl:Ontology rdf:about="https://stackoverflow.com/q/23510851/1281433/lcs"/>
      <owl:Class rdf:about="https://stackoverflow.com/q/23510851/1281433/lcs#A"/>
      <owl:Class rdf:about="https://stackoverflow.com/q/23510851/1281433/lcs#B">
        <rdfs:subClassOf rdf:resource="https://stackoverflow.com/q/23510851/1281433/lcs#A"/>
      </owl:Class>
      <owl:Class rdf:about="https://stackoverflow.com/q/23510851/1281433/lcs#C">
        <rdfs:subClassOf rdf:resource="https://stackoverflow.com/q/23510851/1281433/lcs#B"/>
      </owl:Class>
      <owl:Class rdf:about="https://stackoverflow.com/q/23510851/1281433/lcs#D">
        <rdfs:subClassOf rdf:resource="https://stackoverflow.com/q/23510851/1281433/lcs#B"/>
      </owl:Class>
      <owl:NamedIndividual rdf:about="https://stackoverflow.com/q/23510851/1281433/lcs#c">
        <rdf:type rdf:resource="https://stackoverflow.com/q/23510851/1281433/lcs#C"/>
      </owl:NamedIndividual>
      <owl:NamedIndividual rdf:about="https://stackoverflow.com/q/23510851/1281433/lcs#d">
        <rdf:type rdf:resource="https://stackoverflow.com/q/23510851/1281433/lcs#D"/>
      </owl:NamedIndividual>
    </rdf:RDF>
    

    The individuals c and d are elements of the classes C and D, respectively. The LCS is B. Here's the query and its results:

    prefix :      <https://stackoverflow.com/q/23510851/1281433/lcs#> 
    prefix owl:   <http://www.w3.org/2002/07/owl#>
    prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#>
    
    select ?lcs where {
      ?lcs ^(rdf:type/rdfs:subClassOf*) :c, :d ;
           a owl:Class .
      filter not exists { 
        ?llcs ^(rdf:type/rdfs:subClassOf*) :c, :d ;
              a owl:Class ;
              rdfs:subClassOf+ ?lcs .
      }
    }
    
    -------
    | lcs |
    =======
    | :B  |
    -------
    
    0 讨论(0)
提交回复
热议问题