如何在 CONSTRUCT 的图形上创建聚合
how to create aggregation on a graph from CONSTRUCT
这是我的查询:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rs: <http://www.welovethesemanticweb.com/rs#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
construct {
?subject0 rs:similarityValue ?similairty0.
?subject1 rs:similarityValue ?similairty1
}
WHERE {
{
?subject0 ?predicate0 ?object0.
rs:Impromptu_No._1 ?predicate0 ?object0.
?predicate0 rs:hasSimilarityValue ?similairty0Helper.
BIND(?similairty0Helper * (4/9) AS ?similairty0)
FILTER (?subject0 != rs:Impromptu_No)
}
union {
?subject1 ?predicate ?object.
?object ?predicate1 ?object1.
?predicate1 rs:hasSimilarityValue ?similairty1Helper.
rs:Impromptu_No._1 ?predicateHelper ?objectHelper.
?objectHelper ?predicate1 ?object1
BIND(?similairty1Helper * (1/9) AS ?similairty1)
FILTER (?subject1 != rs:Impromptu_No._1)
}
}
结果是:
rs:5th_Symphony
rs:similarityValue
0.011111111111111112e0 .
rs:Polonaise_heroique
rs:similarityValue
0.011111111111111112e0 , 0.17777777777777778e0 , 0.26666666666666666e0 .
rs:Preludes
rs:similarityValue
0.011111111111111112e0 , 0.26666666666666666e0 , 0.17777777777777778e0 .
rs:Requiem_Sequentia
rs:similarityValue
0.011111111111111112e0 .
rs:Le_nozze_di_Figaro
rs:similarityValue
0.011111111111111112e0 .
rs:Symphony_No._29_in_A_major
rs:similarityValue
0.011111111111111112e0 .
rs:Piano_Concerto_No._24
rs:similarityValue
0.011111111111111112e0 .
rs:Impromptu_No._1
rs:similarityValue
0.26666666666666666e0 , 0.17777777777777778e0 .
rs:Sonata_Pathetique
rs:similarityValue
0.011111111111111112e0 .
rs:Dies_Irae
rs:similarityValue
0.011111111111111112e0 .
rs:Piano_Sonata_No._31
rs:similarityValue
0.011111111111111112e0 , 0.26666666666666666e0 .
rs:Violin_Concerto_No._5_in_A_major
rs:similarityValue
0.011111111111111112e0 .
如您所见,对于每个实例,都有很多值,我想聚合它们并为每个实例制作它们的 SUM
。我会使用 SELECT
来做到这一点,但是使用 CONSTRUCT
,我不知道如何应用聚合。
看了之后,发现不能直接用CONSTRUCT
的聚合,而是需要SELECT
和CONSTRUCT
一起使用,好像有使用名为 "named graph" 的东西,但即使我尝试了也不知道该怎么做。
非常感谢您的帮助。
非常感谢,问候,
更新 1
我尝试过的方法之一是:
construct {
?subject0 rs:similarityValue ?similairty0.
?subject1 rs:similarityValue ?similairty1
}
WHERE {
GRAPH ?g {?subject0 rs:similarityValue ?similairty0}.
{
?subject0 ?predicate0 ?object0.
....
但我得到的结果是空的
首先,最好确保您可以 select 您尝试检索的所有信息。看起来你的目标是这样的:
prefix rs: <http://www.welovethesemanticweb.com/rs#>
select distinct ?s ?weight ?factor where {
#-- ?x is the special value of interest. This
#-- is pulled out into a VALUES block just for
#-- convenience; there's just one place to change
#-- rs:Impromptu_No._1, now.
values ?x { rs:Impromptu_No._1 }
#-- find ?s which are "one step" away from
#-- a common property/value with ?x, and
#-- take 4/9 as ?weight.
{
?s ?p ?o .
?x ?p ?o .
bind(4/9 as ?weight)
}
union
#-- find ?s which are are "two steps" away from
#-- a common property/value with ?x, and take
#-- 1/9 as ?weight
{
?s ?a ?b . ?b ?p ?o .
?x ?c ?d . ?d ?p ?o .
bind(1/9 as ?weight)
}
#-- get the similarity factor of the property
#-- and make sure that ?s is different from ?x.
?p rs:hasSimilarityValue ?factor .
filter(?s != ?x)
}
-----------------------------------------------------------------------------------------------------------------------
| s | weight | factor |
=======================================================================================================================
| rs:5th_Symphony | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Dies_Irae | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Le_nozze_di_Figaro | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Piano_Concerto_No._24 | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Piano_Sonata_No._31 | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Piano_Sonata_No._31 | 0.444444444444444444444444 | "0.6"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Polonaise_heroique | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Polonaise_heroique | 0.444444444444444444444444 | "0.4"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Polonaise_heroique | 0.444444444444444444444444 | "0.6"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Preludes | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Preludes | 0.444444444444444444444444 | "0.4"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Preludes | 0.444444444444444444444444 | "0.6"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Requiem_Sequentia | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Sonata_Pathetique | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Symphony_No._29_in_A_major | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Violin_Concerto_No._5_in_A_major | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
-----------------------------------------------------------------------------------------------------------------------
现在,似乎在这之后,您想要按 ?s 的值 group by 并对加权相似度求和:
select distinct ?s (sum(?weight * ?factor) as ?similarity) where {
values ?x { rs:Impromptu_No._1 }
{
?s ?p ?o .
?x ?p ?o .
bind(4/9 as ?weight)
}
union
{
?s ?a ?b . ?b ?p ?o .
?x ?c ?d . ?d ?p ?o .
bind(1/9 as ?weight)
}
?p rs:hasSimilarityValue ?factor .
filter(?s != ?x)
}
group by ?s
----------------------------------------------------------------
| s | similarity |
================================================================
| rs:5th_Symphony | 0.044444444444444446e0 |
| rs:Piano_Concerto_No._24 | 0.044444444444444446e0 |
| rs:Requiem_Sequentia | 0.044444444444444446e0 |
| rs:Dies_Irae | 0.044444444444444446e0 |
| rs:Piano_Sonata_No._31 | 0.31111111111111117e0 |
| rs:Symphony_No._29_in_A_major | 0.044444444444444446e0 |
| rs:Le_nozze_di_Figaro | 0.044444444444444446e0 |
| rs:Violin_Concerto_No._5_in_A_major | 0.044444444444444446e0 |
| rs:Sonata_Pathetique | 0.044444444444444446e0 |
| rs:Preludes | 0.48888888888888893e0 |
| rs:Polonaise_heroique | 0.48888888888888893e0 |
----------------------------------------------------------------
最后,由于您已获得所需的值,您现在可以构造您想要的三元组:
construct {
?s rs:similarityValue ?similarity
}
where {{
select distinct ?s (sum(?weight * ?factor) as ?similarity) where {
values ?x { rs:Impromptu_No._1 }
{
?s ?p ?o .
?x ?p ?o .
bind(4/9 as ?weight)
}
union
{
?s ?a ?b . ?b ?p ?o .
?x ?c ?d . ?d ?p ?o .
bind(1/9 as ?weight)
}
?p rs:hasSimilarityValue ?factor .
filter(?s != ?x)
}
group by ?s
}}
@prefix : <http://www.semanticweb.org/rs#> .
@prefix rs: <http://www.welovethesemanticweb.com/rs#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
rs:5th_Symphony rs:similarityValue 0.044444444444444446e0 .
rs:Polonaise_heroique
rs:similarityValue 0.48888888888888893e0 .
rs:Preludes rs:similarityValue 0.48888888888888893e0 .
rs:Requiem_Sequentia rs:similarityValue
0.044444444444444446e0 .
rs:Le_nozze_di_Figaro
rs:similarityValue 0.044444444444444446e0 .
rs:Symphony_No._29_in_A_major
rs:similarityValue 0.044444444444444446e0 .
rs:Piano_Concerto_No._24
rs:similarityValue 0.044444444444444446e0 .
rs:Sonata_Pathetique rs:similarityValue
0.044444444444444446e0 .
rs:Dies_Irae rs:similarityValue 0.044444444444444446e0 .
rs:Piano_Sonata_No._31
rs:similarityValue 0.31111111111111117e0 .
rs:Violin_Concerto_No._5_in_A_major
rs:similarityValue 0.044444444444444446e0 .
这是我的查询:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rs: <http://www.welovethesemanticweb.com/rs#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
construct {
?subject0 rs:similarityValue ?similairty0.
?subject1 rs:similarityValue ?similairty1
}
WHERE {
{
?subject0 ?predicate0 ?object0.
rs:Impromptu_No._1 ?predicate0 ?object0.
?predicate0 rs:hasSimilarityValue ?similairty0Helper.
BIND(?similairty0Helper * (4/9) AS ?similairty0)
FILTER (?subject0 != rs:Impromptu_No)
}
union {
?subject1 ?predicate ?object.
?object ?predicate1 ?object1.
?predicate1 rs:hasSimilarityValue ?similairty1Helper.
rs:Impromptu_No._1 ?predicateHelper ?objectHelper.
?objectHelper ?predicate1 ?object1
BIND(?similairty1Helper * (1/9) AS ?similairty1)
FILTER (?subject1 != rs:Impromptu_No._1)
}
}
结果是:
rs:5th_Symphony
rs:similarityValue
0.011111111111111112e0 .
rs:Polonaise_heroique
rs:similarityValue
0.011111111111111112e0 , 0.17777777777777778e0 , 0.26666666666666666e0 .
rs:Preludes
rs:similarityValue
0.011111111111111112e0 , 0.26666666666666666e0 , 0.17777777777777778e0 .
rs:Requiem_Sequentia
rs:similarityValue
0.011111111111111112e0 .
rs:Le_nozze_di_Figaro
rs:similarityValue
0.011111111111111112e0 .
rs:Symphony_No._29_in_A_major
rs:similarityValue
0.011111111111111112e0 .
rs:Piano_Concerto_No._24
rs:similarityValue
0.011111111111111112e0 .
rs:Impromptu_No._1
rs:similarityValue
0.26666666666666666e0 , 0.17777777777777778e0 .
rs:Sonata_Pathetique
rs:similarityValue
0.011111111111111112e0 .
rs:Dies_Irae
rs:similarityValue
0.011111111111111112e0 .
rs:Piano_Sonata_No._31
rs:similarityValue
0.011111111111111112e0 , 0.26666666666666666e0 .
rs:Violin_Concerto_No._5_in_A_major
rs:similarityValue
0.011111111111111112e0 .
如您所见,对于每个实例,都有很多值,我想聚合它们并为每个实例制作它们的 SUM
。我会使用 SELECT
来做到这一点,但是使用 CONSTRUCT
,我不知道如何应用聚合。
看了之后,发现不能直接用CONSTRUCT
的聚合,而是需要SELECT
和CONSTRUCT
一起使用,好像有使用名为 "named graph" 的东西,但即使我尝试了也不知道该怎么做。
非常感谢您的帮助。
非常感谢,问候,
更新 1
我尝试过的方法之一是:
construct {
?subject0 rs:similarityValue ?similairty0.
?subject1 rs:similarityValue ?similairty1
}
WHERE {
GRAPH ?g {?subject0 rs:similarityValue ?similairty0}.
{
?subject0 ?predicate0 ?object0.
....
但我得到的结果是空的
首先,最好确保您可以 select 您尝试检索的所有信息。看起来你的目标是这样的:
prefix rs: <http://www.welovethesemanticweb.com/rs#>
select distinct ?s ?weight ?factor where {
#-- ?x is the special value of interest. This
#-- is pulled out into a VALUES block just for
#-- convenience; there's just one place to change
#-- rs:Impromptu_No._1, now.
values ?x { rs:Impromptu_No._1 }
#-- find ?s which are "one step" away from
#-- a common property/value with ?x, and
#-- take 4/9 as ?weight.
{
?s ?p ?o .
?x ?p ?o .
bind(4/9 as ?weight)
}
union
#-- find ?s which are are "two steps" away from
#-- a common property/value with ?x, and take
#-- 1/9 as ?weight
{
?s ?a ?b . ?b ?p ?o .
?x ?c ?d . ?d ?p ?o .
bind(1/9 as ?weight)
}
#-- get the similarity factor of the property
#-- and make sure that ?s is different from ?x.
?p rs:hasSimilarityValue ?factor .
filter(?s != ?x)
}
-----------------------------------------------------------------------------------------------------------------------
| s | weight | factor |
=======================================================================================================================
| rs:5th_Symphony | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Dies_Irae | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Le_nozze_di_Figaro | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Piano_Concerto_No._24 | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Piano_Sonata_No._31 | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Piano_Sonata_No._31 | 0.444444444444444444444444 | "0.6"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Polonaise_heroique | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Polonaise_heroique | 0.444444444444444444444444 | "0.4"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Polonaise_heroique | 0.444444444444444444444444 | "0.6"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Preludes | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Preludes | 0.444444444444444444444444 | "0.4"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Preludes | 0.444444444444444444444444 | "0.6"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Requiem_Sequentia | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Sonata_Pathetique | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Symphony_No._29_in_A_major | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
| rs:Violin_Concerto_No._5_in_A_major | 0.111111111111111111111111 | "0.1"^^<http://www.w3.org/2001/XMLSchema#double> |
-----------------------------------------------------------------------------------------------------------------------
现在,似乎在这之后,您想要按 ?s 的值 group by 并对加权相似度求和:
select distinct ?s (sum(?weight * ?factor) as ?similarity) where {
values ?x { rs:Impromptu_No._1 }
{
?s ?p ?o .
?x ?p ?o .
bind(4/9 as ?weight)
}
union
{
?s ?a ?b . ?b ?p ?o .
?x ?c ?d . ?d ?p ?o .
bind(1/9 as ?weight)
}
?p rs:hasSimilarityValue ?factor .
filter(?s != ?x)
}
group by ?s
----------------------------------------------------------------
| s | similarity |
================================================================
| rs:5th_Symphony | 0.044444444444444446e0 |
| rs:Piano_Concerto_No._24 | 0.044444444444444446e0 |
| rs:Requiem_Sequentia | 0.044444444444444446e0 |
| rs:Dies_Irae | 0.044444444444444446e0 |
| rs:Piano_Sonata_No._31 | 0.31111111111111117e0 |
| rs:Symphony_No._29_in_A_major | 0.044444444444444446e0 |
| rs:Le_nozze_di_Figaro | 0.044444444444444446e0 |
| rs:Violin_Concerto_No._5_in_A_major | 0.044444444444444446e0 |
| rs:Sonata_Pathetique | 0.044444444444444446e0 |
| rs:Preludes | 0.48888888888888893e0 |
| rs:Polonaise_heroique | 0.48888888888888893e0 |
----------------------------------------------------------------
最后,由于您已获得所需的值,您现在可以构造您想要的三元组:
construct {
?s rs:similarityValue ?similarity
}
where {{
select distinct ?s (sum(?weight * ?factor) as ?similarity) where {
values ?x { rs:Impromptu_No._1 }
{
?s ?p ?o .
?x ?p ?o .
bind(4/9 as ?weight)
}
union
{
?s ?a ?b . ?b ?p ?o .
?x ?c ?d . ?d ?p ?o .
bind(1/9 as ?weight)
}
?p rs:hasSimilarityValue ?factor .
filter(?s != ?x)
}
group by ?s
}}
@prefix : <http://www.semanticweb.org/rs#> .
@prefix rs: <http://www.welovethesemanticweb.com/rs#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
rs:5th_Symphony rs:similarityValue 0.044444444444444446e0 .
rs:Polonaise_heroique
rs:similarityValue 0.48888888888888893e0 .
rs:Preludes rs:similarityValue 0.48888888888888893e0 .
rs:Requiem_Sequentia rs:similarityValue
0.044444444444444446e0 .
rs:Le_nozze_di_Figaro
rs:similarityValue 0.044444444444444446e0 .
rs:Symphony_No._29_in_A_major
rs:similarityValue 0.044444444444444446e0 .
rs:Piano_Concerto_No._24
rs:similarityValue 0.044444444444444446e0 .
rs:Sonata_Pathetique rs:similarityValue
0.044444444444444446e0 .
rs:Dies_Irae rs:similarityValue 0.044444444444444446e0 .
rs:Piano_Sonata_No._31
rs:similarityValue 0.31111111111111117e0 .
rs:Violin_Concerto_No._5_in_A_major
rs:similarityValue 0.044444444444444446e0 .