微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

未绑定变量的可选 Sparql 语句在 Stardog 中不起作用

如何解决未绑定变量的可选 Sparql 语句在 Stardog 中不起作用

背景

嗨!
我有一个规范,它显示与另一个规范中的某些关系相关联的问题。我的规范中的所有对象都有所谓的片段语言化(relation:hasFragmentVerbalisation)。
为了正确显示我的问题,我需要区分我有一个“简单”的 Iri 的情况,我可以直接使用它的语言表达,或者它是某种类型的(在这种情况下,如果它是房间或员工的 iri)在在哪些情况下,我想返回所述类型的所有资源的首选标签(因此,基本上如果我的对象代表一个房间,我想返回我数据库中的所有房间)。

我目前正在处理一个查询并尝试添加如下所示的语句部分:

    OPTIONAL {
        FILTER(isIRI(?object)).
        ?object relation:hasFragmentVerbalisation ?objectVerb .
    }

   OPTIONAL {
        FILTER (?objectVerb= "Person"@en || ?objectVerb = "Teacher"@en)
        ?a employee:prefLabel ?employeename .
        BIND( ?employeename as ?final ) .
    }

    OPTIONAL {
        FILTER (?objectVerb= "room"@en || ?objectVerb = "lecture hall"@en)
        ?b room:prefLabel ?roomnumber .
        BIND( ?roomnumber as ?final) .
        FILTER (regex(?roomnumber,"20" ))
    }

    #OPTIONAL {
    #    FILTER (!BOUND(?final))
    #    BIND( IF ( isURI(?object),?objectVerb,?object) as ?final ) .
    #}

我想在这里发生什么

我检索到了 ?object。我的查询的主要结果是 ?final 对象。我现在想涵盖三种可能的情况:

  • 一个可选语句:这里我只想检索片段语言。
  • 第二个可选语句:如果我的 IRI 实际上指向一个员工,我想将我员工的所有首选标签返回为 ?final。基本上,我会返回我所有员工的列表。
  • 第三个可选语句:如果我的 IRI 指向一个房间,我想返回 ?final 的所有房间号。正则表达式过滤器对于我在这里的问题并不重要。
  • 第四个(outcommented)可选语句:如果第二个和第三个语句不适合,则不应绑定 ?final

问题

总的来说,除了out外,其他人都评论了一件作品。 但是,我在这里遇到两个问题:

  1. 对于第二个和第三个 Optionals,而不是将 ?objectVerb 与预定义的字符串进行比较,我宁愿询问(仅伪代码:)Do this only if ?object rdf:type iri:for:person:or:room。但是,我似乎无法做出这样的声明。
  2. “outcommented”可选调用(最后一个)不起作用 完全不知道为什么。我在这里尝试的是:
    • 如果我的 ?final 变量还没有被任何东西绑定,我想检查一下 如果 ?object 是 IRI,如果不是,我希望将先前检索到的 ?objectVerb-value 设置为 ?final 的值

这里有人能指出解决方案吗? 感谢您的帮助。

编辑: 完整查询

PREFIX relation: <urn:xxx:beziehungen#> 
PREFIX yyy: <urn:xxx#> 
PREFIX employee: <urn:xxx:fb5:employee#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-Syntax-ns#> 
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX room: <urn:yyy:assets:location:room#>  
PREFIX label: <urn:xxx:assets:labels#>
PREFIX wording: <urn:xxx:assets:wording#>

SELECT disTINCT ?first ?second ?third ?fourth ?fourt4 ?fifth ?resulttype
WHERE {
    ?wording rdf:first ?first1 .
    ?wording rdf:rest/rdf:first ?second2 .
    ?wording rdf:rest/rdf:rest/rdf:first ?third3 .
    ?wording rdf:rest/rdf:rest/rdf:rest/rdf:first ?fourt4 .
    ?wording rdf:rest/rdf:rest/rdf:rest/rdf:rest/rdf:first ?fifth5 .

    relation:hasBureau rdfs:range ?resulttype .
    
    {
        SELECT distinct ?wording (COUNT(?wording) AS ?sum) 
        WHERE {
            relation:hasBureau relation:bigWord ?wording .
            ?wording rdf:rest*/rdf:first ?element .
        }
        GROUP BY ?wording
        HAVING (?sum = 5)
    }

    OPTIONAL {
        FILTER(isIRI(?first1)).
        ?first1 relation:fragmentWord ?firstVerb.
    }
    BIND( IF ( isURI(?first1),?firstVerb,?first1) as ?first ) .
    FILTER(BOUND(?first)).

    OPTIONAL {
        FILTER(isIRI(?second2)).
        ?second2 relation:fragmentWord ?secondVerb.
    }

    OPTIONAL {
        FILTER(isIRI(?third3)).
        ?third3 relation:fragmentWord ?thirdVerb.
    }
    BIND( IF ( isURI(?third3),?thirdVerb,?third3) as ?third ) .
    FILTER(BOUND(?third)).

    OPTIONAL {
        FILTER(isIRI(?fourt4)).
        ?fourt4 relation:fragmentWord ?fourthVerb .
    }

    OPTIONAL {
        FILTER(isIRI(?fifth5)).
        ?fifth5 relation:fragmentWord ?fifthVerb.
    }
    BIND( IF ( isURI(?fifth5),?fifthVerb,?fifth5) as ?fifth ) .
    FILTER(BOUND(?fifth)).

    OPTIONAL {
        FILTER (?fourthVerb = "Person"@de || ?fourthVerb = "Dozent"@de || ?fourthVerb = "Lehrender"@de || ?fourthVerb = "Angestellter"@de)
        ?a employee:prefLabel ?employeename .
        BIND( ?employeename as ?fourth ) .
    }

    OPTIONAL {
        FILTER (?fourthVerb = "Hörsaal"@de || ?fourthVerb = "Zimmer"@de || ?fourthVerb = "Seminarraum"@de)
        ?b room:prefLabel ?roomnumber .
        BIND( ?roomnumber as ?fourth ) .
    }
    FILTER(BOUND(?fourth)).

    #OPTIONAL {
    #    FILTER (!BOUND(?fourth))
    #    BIND( IF ( isURI(?fourt4),?fourthVerb,?fourt4) as ?fourth ) .
    #}

    OPTIONAL {
        FILTER (?secondVerb = "Büro"@de)
        BIND( "Büro"@de as ?second ) .
    }

        OPTIONAL {
        FILTER (?secondVerb = "Buero"@de)
        BIND( "Buero"@de as ?second ) .
    }

    OPTIONAL {
        FILTER (?secondVerb = "Person"@de || ?secondVerb = "Dozent"@de || ?secondVerb = "Lehrender"@de || ?secondVerb = "Angestellter"@de)
        ?a employee:prefLabel ?employeename .
        BIND( ?employeename as ?second ) .
    }

    #OPTIONAL {
    #    FILTER (!BOUND(?second))
    #    BIND( IF ( isURI(?second2),?secondVerb,?second2) as ?second ) .
    #}
}

还有一个极其简短的数据集来运行它:

PREFIX relation: <urn:xxx:beziehungen#> 
PREFIX yyy: <urn:xxx#> 
PREFIX employee: <urn:xxx:fb5:employee#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-Syntax-ns#> 
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX room: <urn:yyy:assets:location:room#>  
PREFIX label: <urn:xxx:assets:labels#>
PREFIX wording: <urn:xxx:assets:wording#>

<urn:xxx:fb5:employee#>
    rdfs:subClassOf wording:fragment ;
    relation:fragmentWord "Person"@de,"Dozent"@de,"Lehrender"@de,"Angestellter"@de .

<urn:yyy:assets:location:room#> 
    rdfs:subClassOf wording:fragment ;
    relation:fragmentWord "Raum"@de,"Zimmer"@de,"Seminarraum"@de,"Hörsaal"@de .

relation:hasBureau 
    rdf:type rdf:Property ;
    owl:minCardinality "1" ;
    rdfs:domain employee:name ;
    rdfs:range room:number ;
    rdfs:label "hat ein Büro"@de,"has a bureau"@en ;
    relation:fragmentWord "Büro"@de,"Buero"@de ;
    relation:bigWord ("In welchem" relation:hasBureau "ist" <urn:xxx:fb5:employee#> "?") ;
    relation:bigWord ("Finde ich"  <urn:xxx:fb5:employee#> "in" <urn:yyy:assets:location:room#> "?") .

employee:PersonOne employee:prefLabel "Employee One".
employee:PersonOne relation:hasBureau room:23129 . 
room:23129 room:prefLabel "23-129".
employee:PersonTwo employee:prefLabel "Employee Two".
employee:PersonTwo relation:hasBureau room:23232 . 
room:23232 room:prefLabel "23-232".

解决方法

不是回答而是显示一些查询以获得反馈:

数据:

PREFIX relation: <urn:xxx:beziehungen#> 
PREFIX yyy: <urn:xxx#> 
PREFIX employee: <urn:xxx:fb5:employee#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX room: <urn:yyy:assets:location:room#>  
PREFIX label: <urn:xxx:assets:labels#>
PREFIX wording: <urn:xxx:assets:wording#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>

<urn:xxx:fb5:employee#>
    rdfs:subClassOf wording:fragment ;
    relation:fragmentWord "Person"@de,"Dozent"@de,"Lehrender"@de,"Angestellter"@de .

<urn:yyy:assets:location:room#> 
    rdfs:subClassOf wording:fragment ;
    relation:fragmentWord "Raum"@de,"Zimmer"@de,"Seminarraum"@de,"Hörsaal"@de .

relation:hasBureau 
    rdf:type rdf:Property ;
    owl:minCardinality "1" ;
    rdfs:domain employee:name ;
    rdfs:range room:number ;
    rdfs:label "hat ein Büro"@de,"has a bureau"@en ;
    relation:fragmentWord "Büro"@de,"Buero"@de ;
    relation:bigWord ("In welchem" relation:hasBureau "ist" <urn:xxx:fb5:employee#> "?") ;
    relation:bigWord ("Finde ich"  <urn:xxx:fb5:employee#> "in" <urn:yyy:assets:location:room#> "?") .

employee:PersonOne employee:prefLabel "Employee One".
employee:PersonOne relation:hasBureau room:23129 . 
room:23129 room:prefLabel "23-129".

employee:PersonTwo employee:prefLabel "Employee Two".
employee:PersonTwo relation:hasBureau room:23232 . 
room:23232 room:prefLabel "23-232".

查询:

PREFIX relation: <urn:xxx:beziehungen#> 
PREFIX yyy: <urn:xxx#> 
PREFIX employee: <urn:xxx:fb5:employee#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX room: <urn:yyy:assets:location:room#>  
PREFIX label: <urn:xxx:assets:labels#>
PREFIX wording: <urn:xxx:assets:wording#>

SELECT DISTINCT ?first ?second ?third ?fourth ?fifth ?resulttype
WHERE {
    ?wording rdf:first ?first1 .
    ?wording rdf:rest/rdf:first ?second2 .
    ?wording rdf:rest/rdf:rest/rdf:first ?third3 .
    ?wording rdf:rest/rdf:rest/rdf:rest/rdf:first ?fourth4 .
    ?wording rdf:rest/rdf:rest/rdf:rest/rdf:rest/rdf:first ?fifth5 .

    relation:hasBureau rdfs:range ?resulttype .
    
    ?a employee:prefLabel ?employeename .
    ?b room:prefLabel ?roomnumber .

    
    {
        SELECT ?wording (COUNT(?wording) AS ?sum) 
        WHERE {
            relation:hasBureau relation:bigWord ?wording .
            ?wording rdf:rest*/rdf:first ?element .
        }
        GROUP BY ?wording
        HAVING (?sum = 5)
    }

    OPTIONAL {
        ?first1 relation:fragmentWord ?firstVerb .
    }
    BIND( COALESCE(?firstVerb,?first1) as ?first )

    OPTIONAL {
        ?second2 relation:fragmentWord ?secondVerb .
    }
    BIND( IF(?second2 = <urn:xxx:fb5:employee#>,?employeename,IF(?second2 = room:,?roomnumber,COALESCE(?secondVerb,?second2))) as ?second )


    OPTIONAL {
        ?third3 relation:fragmentWord ?thirdVerb .
    }
    BIND( COALESCE(?thirdVerb,?third3) as ?third )

    OPTIONAL {
        ?fourth4 relation:fragmentWord ?fourthVerb .
    }
    BIND( IF(?fourth4 = <urn:xxx:fb5:employee#>,IF(?fourth4 = room:,COALESCE(?fourthVerb,?fourth4))) as ?fourth )

    OPTIONAL {
        ?fifth5 relation:fragmentWord ?fifthVerb .
    }
    BIND( COALESCE(?fifthVerb,?fifth5) as ?fifth )

    
}

结果:

--------------------------------------------------------------------------------
| first        | second         | third | fourth         | fifth | resulttype  |
================================================================================
| "In welchem" | "Buero"@de     | "ist" | "Employee Two" | "?"   | room:number |
| "In welchem" | "Büro"@de      | "ist" | "Employee Two" | "?"   | room:number |
| "In welchem" | "Buero"@de     | "ist" | "Employee One" | "?"   | room:number |
| "In welchem" | "Büro"@de      | "ist" | "Employee One" | "?"   | room:number |
| "Finde ich"  | "Employee Two" | "in"  | "23-232"       | "?"   | room:number |
| "Finde ich"  | "Employee Two" | "in"  | "23-129"       | "?"   | room:number |
| "Finde ich"  | "Employee One" | "in"  | "23-232"       | "?"   | room:number |
| "Finde ich"  | "Employee One" | "in"  | "23-129"       | "?"   | room:number |
--------------------------------------------------------------------------------

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?