Warm tip: This article is reproduced from serverfault.com, please click

rdfs-为什么我的 Ontlgoy 中的某些类没有在我的 SPARQL 查询中返回?

(rdfs - Why are some of the classes in my ontolgoy not being returned in my SPARQL query?)

发布于 2021-01-28 13:45:41

我很困惑为什么我收到的结果太少(预期为 256,返回 224)。当我运行下面的代码时,一切都完全按照我的意愿返回,除了我错过了本体中位于最高级别或低于最高级别的所有类我不明白我在查询中哪里过于“严格”,以至于这些类没有在表中返回。它们也都有父类(无论是本体的最顶层类“顶级概念”,还是枚举类型类之类的东西,无论哪种方式,仍然应该找到叶子。感谢我的代码可能的提示或指针无意中过滤掉了这些类。

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

Questioner
Robin
Viewed
0
Robin 2021-01-29 20:25:01

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

问题是查询不接受这些没有父类的类。对于某些联合案例,这可以涵盖 0 个父类、1 个父类或 2 个父类。

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 )
                   
                
                }

            }}}}