SPARQL是否可以使用静态文本绑定两个变量及其标签?

时间:2016-03-02 14:48:57

标签: sparql rdf semantic-web owl ontology

这是我的查询

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

在我的第一个bind子句中,我不仅要绑定变量?p,还要绑定变量?instance。另外,添加that is why等文字。

所以第一个绑定应该得到以下结果: ?p that is why ?instance

可以在SPARQL中使用吗?

请不要关心数据是否有效,这只是一个向您显示问题的查询

1 个答案:

答案 0 :(得分:4)

如果我理解正确,您只需查找concat功能。正如我之前提到的,你应该真正浏览SPARQL 1.1 standard,至少通过目录。你不需要记住它,但它会让你知道什么是可能的东西,并想知道在哪里看。此外,如果您提供我们可以使用的示例数据,那么非常有用,因为它可以更清晰来弄清楚您尝试了什么去做。你的标题的措辞并不是特别清楚,这个问题并没有真正提供你想要完成的事情的例子。只是因为我已经看到你过去的一些问题,我才知道你的目标是什么。无论如何,这里有一些数据:

January = 3
feb = 3
march = 1
april = 3
may = 4 
june = 2

以下是查询及其结果。您只需使用 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 .
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