如何合并具有相同属性的RDF主题并将其值相加?

如何合并具有相同属性的RDF主题并将其值相加?,rdf,sparql,jena,n-triples,Rdf,Sparql,Jena,N Triples,考虑到以下三个因素: s1 nameProperty "Bozo" s1 laughProperty "Haha" s1 valueProperty "2.00"^^xml:double s2 nameProperty "Clown" s2 laughProperty "hehe" s2 valueProperty "3.00"^^xml:double s3 nameProperty "Bozo" s3 laughProperty "Haha" s3 valueProperty "1.00"

考虑到以下三个因素:

s1 nameProperty "Bozo"
s1 laughProperty "Haha"
s1 valueProperty "2.00"^^xml:double

s2 nameProperty "Clown"
s2 laughProperty "hehe"
s2 valueProperty "3.00"^^xml:double

s3 nameProperty "Bozo"
s3 laughProperty "Haha"
s3 valueProperty "1.00"^^xml:double
我希望合并同名的主题,并对它们的值进行笑和求和,结果有点像:

s1 nameProperty "Bozo"
s1 laughProperty "Haha"
s1 valueProperty "3.00"^^xml:double
s2 nameProperty "Clown"
s2 laughProperty "hehe"
s2 valueProperty "3.00"^^xml:double

如何使用SPARQL以最高效的方式执行此操作?(不需要保留主题。只要具有合并值的新主题共享相同的
nameProperty
laughProperty
),它们就可以插入)

如果您提供我们可以实际运行查询的数据,通常会很有帮助。以下数据与您的数据类似,但我们可以实际使用:

@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
@prefix : <urn:ex:>

:s1 :nameProperty "Bozo".
:s1 :laughProperty "Haha".
:s1 :valueProperty "2.00"^^xsd:double.

:s2 :nameProperty "Clown".
:s2 :laughProperty "hehe".
:s2 :valueProperty "3.00"^^xsd:double.

:s3 :nameProperty "Bozo".
:s3 :laughProperty "Haha".
:s3 :valueProperty "1.00"^^xsd:double.
@前缀xsd:。
@前缀:
:s1:nameProperty“Bozo”。
:s1:laughtroperty“哈哈”。
:s1:valueProperty“2.00”^^xsd:double。
:s2:nameProperty“小丑”。
:s2:呵呵。
:s2:valueProperty“3.00”^^xsd:double。
:s3:nameProperty“Bozo”。
:s3:laughtroperty“哈哈”。
:s3:valueProperty“1.00”^^xsd:double。
这是一个非常简单的构造查询。唯一棘手的部分是,由于我们需要一个分组依据,我们必须使用嵌套的选择查询,以便我们可以使用求和样本聚合函数

前缀:
构造{
?小丑:名称属性?名称;
:笑?笑;
:valueProperty?总计
}
在哪里{
{选择(样本(?s)作为小丑)?名称?笑(总和(?值)作为总数)其中{
?s:nameProperty?名称;
:笑?笑;
:valueProperty?值
}
按名称分组(笑)}
}
结果(在N3和N-Triples中,只是为了确保3.0e0实际上是xsd:double):

@前缀:。
@前缀xsd:。
:s3:laughtroperty“哈哈”;
:nameProperty“Bozo”;
:valueProperty 3.0e0。
:s2:呵呵;
:nameProperty“小丑”;
:valueProperty“3.00”^^xsd:double。

“呵呵”。
“小丑”。
"3.00"^^ .
“哈哈”。
“波佐”。
“3.0e0”^^^。
@prefix :      <urn:ex:> .
@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .

:s3     :laughProperty  "Haha" ;
        :nameProperty   "Bozo" ;
        :valueProperty  3.0e0 .

:s2     :laughProperty  "hehe" ;
        :nameProperty   "Clown" ;
        :valueProperty  "3.00"^^xsd:double .
<urn:ex:s2> <urn:ex:laughProperty> "hehe" .
<urn:ex:s2> <urn:ex:nameProperty> "Clown" .
<urn:ex:s2> <urn:ex:valueProperty> "3.00"^^<http://www.w3.org/2001/XMLSchema#double> .
<urn:ex:s3> <urn:ex:laughProperty> "Haha" .
<urn:ex:s3> <urn:ex:nameProperty> "Bozo" .
<urn:ex:s3> <urn:ex:valueProperty> "3.0e0"^^<http://www.w3.org/2001/XMLSchema#double> .