sstenchlak / schema-generator

https://kodi-schema-generator.netlify.app/
0 stars 0 forks source link

Incorrect SPARQL query for getting CIM associations #9

Closed martinnec closed 3 years ago

martinnec commented 3 years ago

The current SPARQL queries for loading associations of a class in CIM have the following problems:

  1. Some associations are missing because an association may inherit its class membership from its ancestor. This is possible in both directions, either the current class is the domain of an association or it is its range.
  2. Some associations are missing because an association does not have specified the other class associated with the current one. The other class is inherited from its ancestor. Because we suppose that there is one and only one such other class, only the closest ancestor with the other class specified must be considered.
  3. Associations from (1) shall be presented to the user w.r.t. the inheritance hierarchy. This issue does not specify how this should be presented in the UI but extends the SPARQL query so that it will be possible.

Query for loading associations having the current element as domain (example https://slovník.gov.cz/legislativní/sbírka/56/2001/pojem/silniční-vozidlo, https://slovník.gov.cz/legislativní/sbírka/361/2000/pojem/vozidlo is also a good example):

PREFIX z: <https://slovník.gov.cz/základní/pojem/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>

CONSTRUCT {
    ?outwardsRelation a z:typ-vztahu ;
        rdfs:domain <https://slovník.gov.cz/legislativní/sbírka/56/2001/pojem/silniční-vozidlo> ;
        rdfs:range ?rangeElement ;
        skos:prefLabel ?outwardsRelationLabel ;
        skos:definition ?outwardsRelationDefinition ;
        skos:inScheme ?outwardsRelationGlossary ;
        # (4) relationship to the parent association in the inheritance hierarchy, only ?parentRelation returned also as ?outwardsRelation is valid. A ?parentRelation is returned as ?outwardsRelation iff it has the mandatory properties such as skos:prefLabel etc. START
        rdfs:subPropertyOf ?parentRelation .
        # (4) relationship to the parent association in the inheritance hierarchy END
    ?rangeElement a z:typ-objektu ;
        skos:prefLabel ?rangeLabel ;
        skos:definition ?rangeDefinition ;
        skos:inScheme ?rangeGlossary .
} WHERE {
    ?outwardsRelation a z:typ-vztahu ;
        skos:prefLabel ?outwardsRelationLabel ;
        skos:inScheme ?outwardsRelationGlossary .

    {
        ?outwardsRelation rdfs:subClassOf [
            owl:allValuesFrom <https://slovník.gov.cz/legislativní/sbírka/56/2001/pojem/silniční-vozidlo> ;
            owl:onProperty z:má-vztažený-prvek-1 
        ]
    } UNION {
        # (1) associations inheriting their membership START
        ?ancestorOutwardsRelation rdfs:subClassOf [
            owl:allValuesFrom <https://slovník.gov.cz/legislativní/sbírka/56/2001/pojem/silniční-vozidlo> ;
            owl:onProperty z:má-vztažený-prvek-1 
        ] .
        ?outwardsRelation rdfs:subClassOf+ ?ancestorOutwardsRelation .
        # (1) associations inheriting their membership END
    }

    {
        ?outwardsRelation rdfs:subClassOf [
            owl:allValuesFrom ?rangeElement ;
            owl:onProperty z:má-vztažený-prvek-2
        ]
    # (2) associations inheriting their other class START
    } UNION {
        # (2.1) associations inheriting their other class from an inheritance parent START
        ?outwardsRelation rdfs:subClassOf/rdfs:subClassOf [
            owl:allValuesFrom ?rangeElement ;
            owl:onProperty z:má-vztažený-prvek-2
        ]
        FILTER NOT EXISTS {
            ?outwardsRelation rdfs:subClassOf/owl:onProperty z:má-vztažený-prvek-2 .
        }
        # (2.1) associations inheriting their other class from an inheritance parent END
    } UNION {
        # (2.2) associations inheriting their other class from an inheritance grand^{+}parent START
        ### This branch is non-optimal. If query executions are too slow, this branch can be removed. The result will not be complete but this branch covers quite rare cases.
        ?outwardsRelation rdfs:subClassOf+/rdfs:subClassOf ?rangeDefinition .
        ?rangeDefinition owl:allValuesFrom ?rangeElement ;
            owl:onProperty z:má-vztažený-prvek-2 .
        FILTER NOT EXISTS {
            ?outwardsRelation rdfs:subClassOf+ ?specificOutwardsRelation .
            ?specificOutwardsRelation rdfs:subClassOf+ ?rangeDefinition ;
                rdfs:subClassOf ?rangeDefinition1 .
            ?rangeDefinition1 owl:onProperty z:má-vztažený-prvek-2 .
        }
        # (2.2) associations inheriting their other class from an inheritance grand^{+}parent END
    }
    # (2) associations inheriting their other class END

    OPTIONAL { ?outwardsRelation skos:definition ?outwardsRelationDefinition }

    ?rangeElement skos:prefLabel ?rangeLabel ;
        skos:inScheme ?rangeGlossary .

    OPTIONAL { ?rangeElement skos:definition ?rangeDefinition }

    # (4) relationship to the parent association in the inheritance hierarchy START
    OPTIONAL {
        ?outwardsRelation rdfs:subClassOf ?parentRelation .
        ?parentRelation a z:typ-vztahu.
        FILTER(?parentRelation != <https://slovník.gov.cz/základní/pojem/vztah>)
    }
    # (4) relationship to the parent association in the inheritance hierarchy END
}

And for range symmetrically:

PREFIX z: <https://slovník.gov.cz/základní/pojem/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>

CONSTRUCT {
    ?inwardsRelation a z:typ-vztahu ;
        rdfs:domain ?domainElement ;
        rdfs:range <https://slovník.gov.cz/legislativní/sbírka/361/2000/pojem/vozidlo> ;
        skos:prefLabel ?inwardsRelationLabel ;
        skos:definition ?inwardsRelationDefinition ;
        skos:inScheme ?inwardsRelationGlossary ;
        rdfs:subPropertyOf ?parentRelation .
    ?domainElement a z:typ-objektu ;
        skos:prefLabel ?domainLabel ;
        skos:definition ?domainDefinition ;
        skos:inScheme ?domainGlossary .
} WHERE {

    ?inwardsRelation a z:typ-vztahu ;
        skos:prefLabel ?inwardsRelationLabel ;
        skos:inScheme ?inwardsRelationGlossary .

    {
        ?inwardsRelation rdfs:subClassOf ?rangeDefinition .
        ?rangeDefinition owl:allValuesFrom <https://slovník.gov.cz/legislativní/sbírka/361/2000/pojem/vozidlo> ;
            owl:onProperty z:má-vztažený-prvek-2 .
    } UNION {
        ?ancestorInwardsRelation rdfs:subClassOf [
            owl:allValuesFrom <https://slovník.gov.cz/legislativní/sbírka/361/2000/pojem/vozidlo> ;
            owl:onProperty z:má-vztažený-prvek-2
        ] .
        ?inwardsRelation rdfs:subClassOf+ ?ancestorInwardsRelation .
    }

    {
        ?inwardsRelation rdfs:subClassOf ?domainDefinition .
        ?domainDefinition owl:allValuesFrom ?domainElement ;
            owl:onProperty z:má-vztažený-prvek-1 .
    } UNION {
        ?inwardsRelation rdfs:subClassOf/rdfs:subClassOf ?domainDefinition .
        ?rangeDefinition owl:allValuesFrom ?domainElement ;
            owl:onProperty z:má-vztažený-prvek-1 .
        FILTER NOT EXISTS {
            ?inwardsRelation rdfs:subClassOf/owl:onProperty z:má-vztažený-prvek-1 .
        }
    } UNION {
        ?inwardsRelation rdfs:subClassOf+/rdfs:subClassOf ?domainDefinition .
        ?rangeDefinition owl:allValuesFrom ?domainElement ;
            owl:onProperty z:má-vztažený-prvek-1 .
        FILTER NOT EXISTS {
            ?inwardsRelation rdfs:subClassOf+ ?specificInwardsRelation .
            ?specificInwardsRelation rdfs:subClassOf+ ?domainDefinition ;
                rdfs:subClassOf ?domainDefinition1 .
            ?domainDefinition1 owl:onProperty z:má-vztažený-prvek-1 .
        }
    }

    OPTIONAL { ?inwardsRelation skos:definition ?inwardsRelationDefinition }

    ?domainElement skos:prefLabel ?domainLabel ;
        skos:inScheme ?domainGlossary .

    OPTIONAL { ?domainElement skos:definition ?domainDefinition }

    OPTIONAL {
        ?inwardsRelation rdfs:subClassOf ?parentRelation .
        ?parentRelation a z:typ-vztahu.
        FILTER(?parentRelation != <https://slovník.gov.cz/základní/pojem/vztah>)
    }
}