(尤其是@JeenBroekstra)
使用下面的图表,我有一个当前适用于Sesame存储库的SPARQL查询:
PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT *
FROM <http://spinrdf.org/sp>
FROM <http://spinrdf.org/spl>
FROM <http://spinrdf.org/spin>
FROM <http://topbraid.org/examples/kennedysSPIN>
FROM <http://topbraid.org/examples/kennedys>
FROM NAMED <http://topbraid.org/examples/kennedys>
WHERE {
GRAPH <http://topbraid.org/examples/kennedys> {
?s ?p ?o .
} .
OPTIONAL {
FILTER (sameTerm(rdf:type, ?p)) .
?o rdfs:subClassOf+ ?supC .
} .
OPTIONAL {
FILTER (sameTerm(rdf:type, ?p) && NOT EXISTS { ?s a ?newSupC . }) .
?o rdfs:subClassOf+ ?newSupC .
} .
FILTER (bound(?newSupC)) .
}
由于最后两个可选模式共享一些过滤器,我认为我可以将它们合并,然后我得到了这个查询:
PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT *
FROM <http://spinrdf.org/sp>
FROM <http://spinrdf.org/spl>
FROM <http://spinrdf.org/spin>
FROM <http://topbraid.org/examples/kennedysSPIN>
FROM <http://topbraid.org/examples/kennedys>
FROM NAMED <http://topbraid.org/examples/kennedys>
WHERE {
GRAPH <http://topbraid.org/examples/kennedys> {
?s ?p ?o .
} .
OPTIONAL {
FILTER (sameTerm(rdf:type, ?p)) .
?o rdfs:subClassOf+ ?supC .
OPTIONAL {
FILTER ( NOT EXISTS { ?s a ?supC . } ) .
BIND (?supC as ?newSupC)
} .
} .
FILTER (bound(?newSupC)) .
}
但是在执行它时,我得到一个空的结果集。
在提交错误请求之前想到这里问:你能告诉我查询错在哪里或者这是芝麻中的错误吗?
提前致谢, 最大
编辑:我当然把FILTER(绑定(?newSupC))只是为了暴露失败的模式。在全球视野中,我仍然需要所有结果,即使是变量?newSupC unbound。
编辑:以下是我使用的配置的更多详细信息: 除了命名图http://topbraid.org/examples/kennedys之外,我使用RDFS蕴涵更新了图表。 Sesame存储库是一个没有推理支持的简单Native存储。
答案 0 :(得分:3)
在想到我太过于试图将两个可选模式合并在一起 解决方案更加直接:
PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT *
FROM <http://spinrdf.org/sp>
FROM <http://spinrdf.org/spl>
FROM <http://spinrdf.org/spin>
FROM <http://topbraid.org/examples/kennedysSPIN>
FROM <http://topbraid.org/examples/kennedys>
FROM NAMED <http://topbraid.org/examples/kennedys>
WHERE {
GRAPH <http://topbraid.org/examples/kennedys> {
?s ?p ?o .
} .
OPTIONAL {
FILTER (sameTerm(rdf:type, ?p)) .
?o rdfs:subClassOf ?supC .
} .
BIND (IF(NOT EXISTS { ?s a ?supC . }, ?supC, ?__unbound__) as ?newSupC) .
}
无论如何,谢谢你的时间和建议,伙计们。 最大