我对RDF / Sparql相当新,所以对任何不正确的术语表示道歉,并且对于下面相当可怕的例子也是如此:
给出以下RDF数据集:
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix e: <http://www.example.com/#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
e:Freemason a owl:Class .
e:Civilian a owl:Class .
e:Marty a e:Freemason .
e:Eugene a e:Freemason .
e:Mike a e:Freemason .
e:Alan a e:Freemason .
e:Paul a e:Civilian .
e:Marty foaf:knows e:Eugene .
e:Eugene foaf:knows e:Mike .
e:Eugene foaf:knows e:Paul .
e:Paul foaf:knows e:Alan .
我正在尝试识别e:Marty
仅通过其他e:Freemason
知道的朋友的朋友。
所以:
这是我的SPARQL查询:
prefix e: <http://www.example.com/#>
prefix foaf: <http://xmlns.com/foaf/0.1/>
SELECT *
{
<http://www.example.com/#Marty> foaf:knows+ ?target .
?target a e:Freemason .
}
返回:
http://www.example.com/#Eugene
http://www.example.com/#Mike
http://www.example.com/#Alan
此处,Alan符合is-a-freemason
条件。
我如何修改查询以排除Alan?
答案 0 :(得分:1)
我不知道纯SPARQL中的解决方案,抱歉。 在OpenLink Virtuoso的SPARQL-BI中,解决方案是this query
/*
Step 4:
Query Payment class by this user,
set payment dates in ascending order
and then take the first one.
*/
return firstPaymentQuery…
prefix e: <http://www.example.com/#>
prefix foaf: <http://xmlns.com/foaf/0.1/>
select *
where
{
{ select ?orig ?target
where
{ ?orig foaf:knows ?target .
?target a e:Freemason .
}
}
option ( TRANSITIVE,
T_IN(?orig),
T_OUT(?target),
T_DISTINCT,
T_MIN(1)
)
filter ( ?orig = <http://www.example.com/#Marty> )
}
答案 1 :(得分:1)
这是一个使用SPARQL的示例,该示例已从规范中弃用(出于我从未理解的原因),但仍在Virtuoso中得到支持(在不可预见的将来会如此)
PREFIX e: <http://www.example.com/#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT *
FROM <http://kingsley.idehen.net/DAV/home/kidehen/Public/Linked%20Data%20Documents/Tutorials/club-member-test.ttl>
{
<http://www.example.com/#Marty> foaf:knows{2} ?target .
?target a e:Freemason .
}
实时链接: