为什么我的 SPARQL 查询中没有返回我的本体中的某些 类?

Why are some of the classes in my ontolgoy not being returned in my SPARQL query?

我很困惑为什么我收到的结果太少(预期 256,返回 224)。当我 运行 下面的代码时,一切都 returns 完全符合我的要求,除了 我错过了我的 ontology 中的所有 classes最高级别,或低于最高级别。我不明白我的查询哪里太“严格”了,以至于这些 class 没有在 table 中返回。它们也都有父级 classes(无论是 ontology 的最顶层 class“顶级概念”,还是类似于枚举类型 class,无论哪种方式, leaf 应该仍然可以找到。如果我的代码可能会无意中过滤掉那些 classes.

,请提供提示或指示,我们将不胜感激
SELECT DISTINCT ?leaf ?parentclasses
WHERE {
GRAPH <>
#a leaf is the lowest level class: A class that does not have a subclass
{
    {
        {
            #I want anything which is a class
            {
                ?leaf rdf:type owl:Class.
            }
            #i also want the subclass of any superclass if that exists
            {
                ?leaf rdfs:subClassOf ?superclass .
            }
            #squeezed to specific section of OTL.
            filter strstarts(str(?leaf), "URIgoeshere")
            #Only keep the results that do not have a preflabel
            OPTIONAL {
                ?leaf skos:prefLabel ?subclasslabel.
                
            }
            #make sure the subclasslabel is in dutch
            #filter( langMatches(lang(?subclasslabel),"nl") )
            
            #give me the label of the superclass
            OPTIONAL {
                ?superclass skos:prefLabel ?superclasslabel.
            }
            #make sure it's in dutch
            FILTER (lang(?superclasslabel) = "nl")
            #if it exists, give me also the superclass of the superclass creating a supersuperclass
            {
                ?superclass rdfs:subClassOf ?supersuperclass.
                #give me the label of the supersuperclass
                OPTIONAL {
                    ?supersuperclass skos:prefLabel ?supersuperclasslabel.
                }
                #make sure it's in dutch
                FILTER (lang(?supersuperclasslabel) = "nl")
                #keep the leafs that are NOT The values whereby the subclass is not empty. (double negative for removing leafs where the subclass has a subclass below it)
                FILTER NOT EXISTS {
                    ?subclass rdfs:subClassOf ?leaf 
                    FILTER (?subclass != owl:Nothing ) 
                }
                #concatenate the two parentclass variables into one    
                BIND(concat(str(?superclasslabel), str("-"), str(?supersuperclasslabel) ) as ?parentclasses) 
            }
        }
    }
}

}

这是一个与我的数据库结构相同的ttl文件:https://file.io/jjwkAWbK4jrF

以下是我对问题的最终解决方案。它比我预期的要复杂,但它有效。

问题是这些 class 没有父 class 的元素没有被查询接受。在某些联合案例中,这可能涵盖 0 个父 classes、1 个父 class 或 2 个父 classes.

SELECT DISTINCT ?leaf ?parentclasses
WHERE {
GRAPH <>
#a leaf is the lowest level class: A class that does not have a subclass
{{{{{
                    
                   
                    
        #I want anything which is a class
        {?leaf rdf:type owl:Class.}
                    
#squeezed 
        filter strstarts(str(?leaf), "graph")           
        #keep the leafs that are NOT The values whereby the subclass is not empty. 
 (double negative for removing leafs where the subclass has a subclass below it)
            FILTER NOT EXISTS {?subclass rdfs:subClassOf ?leaf 
            FILTER (?subclass != owl:Nothing ) }
 }              
                {
         {?leaf rdfs:subClassOf ?superclass .}
               
 #grab dutch label if available
optional {
?superclass skos:prefLabel ?superclassnllabel .
filter( langMatches(lang(?superclassnllabel),"nl") )
}

# take either as the label, but dutch over empty
                bind( coalesce( ?superclassnllabel, replace(str(? 
superclass),"^[^#]*#", "" ) ) as ?superclasslabel )
            
                {
                    
                {?superclass rdfs:subClassOf ?supersuperclass.}
                

        #grab dutch label if available
 
?supersuperclass skos:prefLabel ?supersuperclassnllabel .
                    filter( langMatches(lang(?supersuperclassnllabel),"nl") )



 # take either as the label, but dutch over empty
                    bind( coalesce( ?supersuperclassnllabel, replace(str(? 
 supersuperclass),"^[^#]*#", "" ) ) as ?supersuperclasslabel )
                BIND(concat(str(?superclasslabel), str(" - "), str(? 
supersuperclasslabel) ) as ?parentclasses)
                }
                union
                {
                    
                    {?superclass ?p ?o.filter(!isblank(?superclass))}
                    FILTER NOT EXISTS {?superclass rdfs:subClassOf ?supersuperclass}
                        BIND(concat(str(?superclasslabel), str(" - "), str("Top 
Concept") ) as ?parentclasses) 
                #concatenate the two parentclass variables into one    
                
                }


        
            }
            }
            
            union
            {
                   #figure this out, WHY IS IT HERE? 
                {?leaf rdf:type owl:Class .filter(!isblank(?leaf))}
                FILTER strstarts(str(?leaf), "graph") 
                FILTER NOT EXISTS {?leaf rdfs:subClassOf ?superclass}
                FILTER NOT EXISTS {?subclass rdfs:subClassOf ?leaf 
                FILTER (?subclass != owl:Nothing ) }
                BIND (str("Top Class")  as ?parentclasses )
                   
                
                }

            }}}}