【问题标题】:Bind result of ASK query in sparql在 sparql 中绑定 ASK 查询的结果
【发布时间】:2016-03-01 17:43:17
【问题描述】:

我正在学习 sparql,目前正在处理一个选择查询,我想将 ASK 查询的结果绑定到对象变量。

ASK 查询正在运行,但我在另一个查询中使用该查询时遇到问题。

这是 ASK 查询:

PREFIX schema: <http://domain.com/app/schema/>

ASK { GRAPH <http://domain.com/app/data/something> {

     ?s schema:code "ANS"^^<http://www.w3.org/2001/XMLSchema#string> ;
     schema:someid "12345678"^^<http://www.w3.org/2001/XMLSchema#string> ;
     schema:startdate ?startdate .

     OPTIONAL { ?s schema:enddate ?enddate }
     BIND(IF(BOUND(?enddate), ?enddate, now()) AS ?resultdate)
     FILTER(?resultdate >= now() && ?startdate < now())
     }
}

这取决于 id 返回 true 或 false。

我现在想要的是一个返回两列的查询:

+----------+--------+
|    ID    | STATUS |
+----------+--------+
| 12345678 | true   |
| 87654321 | false  |
+----------+--------+

我做了一些尝试,但无法生成具有有效语法的查询:

PREFIX schema2: <http://domain.com/app2/schema/>
PREFIX schema: <http://domain.com/app/schema/>

select ?s ?p ?o 
where 
{ GRAPH <http://http://domain.com/app2/data/something>
{?s ?p ?o } .

BIND() {
    ASK { GRAPH <http://domain.com/app/data/something> {

    ?s schema:code "ANS"^^<http://www.w3.org/2001/XMLSchema#string> ;
    schema:someid "12345678"^^<http://www.w3.org/2001/XMLSchema#string> ;
    schema:startdate ?startdate .

    OPTIONAL { ?s schema:enddate ?enddate }
    BIND(IF(BOUND(?enddate), ?enddate, now()) AS ?resultdate)
    FILTER(?resultdate >= now() && ?startdate < now())
    }
}
}

FILTER(?s = <http://domain.com/app2/data/something/100024>)
}

关于如何实现这一点的任何建议/示例?

【问题讨论】:

    标签: sparql


    【解决方案1】:

    您可以将BIND 与表达式一起使用,但不能与查询表单(SELECTASK 等)一起使用。

    您可以做的是将ASK 替换为exists 模式。像这样的

    PREFIX schema2: <http://domain.com/app2/schema/>
    PREFIX schema: <http://domain.com/app/schema/>
    
    select ?id ?status
    where 
    { 
       GRAPH <http://http://domain.com/app2/data/something>
       { 
          ?s schema:someid ?id ;
       }
    
       BIND( exists 
       {
          GRAPH <http://domain.com/app/data/something> 
          {
             ?s schema:code "ANS"^^<http://www.w3.org/2001/XMLSchema#string> ;
                schema:startdate ?startdate .
    
             OPTIONAL { ?s schema:enddate ?enddate }
             BIND(IF(BOUND(?enddate), ?enddate, now()) AS ?resultdate)
             FILTER(?resultdate >= now() && ?startdate < now())
          }
       } as ?status)
    }
    

    【讨论】:

    • 这太棒了!不知道 EXISTS 运算符。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多