Ani*_*vid 1 rdf semantic-web owl ontology sparql
这是我的查询
PREFIX : <http://example.org/rs#>
select ?item (SUM(?similarity) as ?summedSimilarity)
(group_concat(distinct ?becauseOf ; separator = " , ") as ?reason) where
{
values ?x {:instance1}
{
?x ?p ?instance.
?item ?p ?instance.
?p :hasSimilarityValue ?similarity
bind (?p as ?becauseOf)
}
union
{
?x a ?class.
?item a ?class.
?class :hasSimilarityValue ?similarity
bind (?class as ?becauseOf)
}
filter (?x != ?item)
}
group by ?item
Run Code Online (Sandbox Code Playgroud)
在我的第一bind
个子句中,我不仅要绑定变量?p
,还要绑定变量?instance
。另外,添加像that is why
.
所以第一次绑定应该导致以下结果:
?p that is why ?instance
这在 SPARQL 中可能吗?
请不要关心数据是否有意义,这只是向您展示我的问题的查询
如果我理解正确,您只是在寻找concat函数。正如我之前提到的,您应该真正浏览SPARQL 1.1 标准,至少浏览目录。你不需要记住它,但它会让你知道什么是可能的,以及去哪里找。此外,如果您提供我们可以使用的示例数据,这将非常有帮助,因为它使内容更加清晰弄清楚你想要做什么。您标题的措辞不是特别清楚,并且该问题并没有真正提供您要完成的工作的示例。只是因为我看过你过去的一些问题,我才知道你的目标是什么。无论如何,这里有一些数据:
@prefix : <urn:ex:>
:p :hasSimilarity 0.3 .
:A :hasSimilarity 0.6 .
:a :p :b ; #-- is is related to :b
a :A . #-- and is an :A .
:c :p :b . #-- :c is also related to :b
:d a :A . #-- :d is also an :A .
:e :p :b ; #-- :e is related to :b
a :A . #-- and is also an :A .
Run Code Online (Sandbox Code Playgroud)
这是查询及其结果。您只需使用concat将变量的str形式与适当的字符串连接起来,然后将结果绑定到变量。
@prefix : <urn:ex:>
:p :hasSimilarity 0.3 .
:A :hasSimilarity 0.6 .
:a :p :b ; #-- is is related to :b
a :A . #-- and is an :A .
:c :p :b . #-- :c is also related to :b
:d a :A . #-- :d is also an :A .
:e :p :b ; #-- :e is related to :b
a :A . #-- and is also an :A .
Run Code Online (Sandbox Code Playgroud)
prefix : <urn:ex:>
select ?item
(sum(?factor_) as ?factor)
(group_concat(distinct ?reason_; separator=", ") as ?reason)
{
values ?x { :a }
{ ?x ?p ?instance .
?item ?p ?instance .
?p :hasSimilarity ?factor_ .
bind(concat("has common ",str(?p)," value ",str(?instance)) as ?reason_) }
union
{ ?x a ?class.
?item a ?class.
?class :hasSimilarity ?factor_ .
bind(concat("has common class ",str(?class)) as ?reason_)
}
filter (?x != ?item)
}
group by ?item
Run Code Online (Sandbox Code Playgroud)