我无法解决以下问题: “用每个行星的顶点和与之相对应的第二个行星,来构造一个与太阳的距离有关的图形。”
这是图的转储:
:Saturn
skos:exactMatch dbr:Saturn;
rdf:type dbo:Planet;
v:orbits :Sun;
v:apoapsis [rdf:value 9.0412; v:uom unit:AU] ;
v:orbitalPeriod [rdf:value 29.45; v:uom unit:YR ];
v:radius [rdf:value 60268; v:uom unit:KM] ;
v:temperature
[rdf:value -139;
v:uom unit:Deg_C ];
.
因此图中的一些有关太阳系的数据。所有行星(dbo:Planet)都具有v:aapapsis属性和一个值,该值指示与太阳的距离。我已经想出了如何找到所有更大的价值,但是我只想要下一个最大的价值。结果看起来像这样:
:Mars v:apoapsis 1.666 ;
v:nextPlanet :Saturn , :Jupiter , :Uranus .
:Mercury v:apoapsis 0.467 ;
v:nextPlanet :Saturn , :Jupiter , :Uranus , :Mars , :Earth , :Venus .
:Earth v:apoapsis 1.017 ;
v:nextPlanet :Saturn , :Jupiter , :Uranus , :Mars .
:Venus v:apoapsis 0.728 ;
v:nextPlanet :Saturn , :Jupiter , :Uranus , :Mars , :Earth .
:Jupiter v:apoapsis 5.4588 ;
v:nextPlanet :Saturn , :Uranus .
:Saturn v:apoapsis 9.0412 ;
v:nextPlanet :Uranus .
预期结果应如下所示:
:Mars v:apoapsis 1.666 ;
v:nextPlanet :Jupiter .
:Mercury v:apoapsis 0.467 ;
v:nextPlanet :Venus .
:Uranus v:apoapsis 20.11 ;
v:nextPlanet :Neptune .
我是SPARQL的新手,并且对迭代此类任务的元素感到厌烦。不需要完整的解决方案,我只想知道如何解决此问题,并且对某些想法感到满意。谢谢。
我最突出的查询如下:
CONSTRUCT{?planet v:apoapsis ?AUdist;
v:nextPlanet ?nextPlanet .}
WHERE {
?planet a dbo:Planet.
?planet v:apoapsis ?dist.
?dist v:uom unit:AU;
rdf:value ?AUdist .
FILTER(?AUdist > ?AUdist2)
{
SELECT ?nextPlanet ?AUdist2
WHERE {
?nextPlanet a dbo:Planet.
?nextPlanet v:apoapsis ?dist2.
?dist2 v:uom unit:AU;
rdf:value ?AUdist2 .
}
ORDER BY ASC(?AUdist2)
}
{
}
}ORDER BY ASC(?AUdist)
答案 0 :(得分:2)
想法是在子查询中获取最小距离值,然后在外部查询中获取相应的行星:
CONSTRUCT {
?planet v:apoapsis ?dist;
v:nextPlanet ?nextPlanet .
} WHERE {
?planet v:apoapsis ?dist ;
v:nextPlanet ?nextPlanet .
?nextPlanet v:apoapsis ?nextDist
BIND(abs(?dist - ?nextDist) as ?diff)
FILTER(?diff = ?minDiff)
# get planet and the minimum distance to its next planet
{
SELECT ?planet (min(?diff) as ?minDiff) {
?planet v:apoapsis ?dist ;
v:nextPlanet/v:apoapsis ?nextDist
BIND(abs(?dist - ?nextDist) as ?diff)
} GROUP BY ?planet
}
}
注意,此处的查询从中间结果开始。您没有共享全部数据,因此,我必须测试从您那里得到的数据。
答案 1 :(得分:0)
这种查询的一般方法是:
GROUP BY
和MIN
查找给定值1的最小值2。您已经完成了步骤1和2。要稍微重写一下查询,请执行以下操作:
SELECT * {
?planet v:apoapsis/rdf:value ?dist.
?otherPlanet v:apoapsis/rdf:value ?otherDist.
FILTER (?dist < ?otherDist)
}
现在,在第3步中,我们要按?planet
分组,并在每个组中找到最小的?otherDist
:
SELECT ?planet (MIN(?otherDist) AS ?nextDist) {
?planet v:apoapsis/rdf:value ?dist.
?otherPlanet v:apoapsis/rdf:value ?otherDist.
FILTER (?dist < ?otherDist)
}
GROUP BY ?planet
那是困难的部分。剩下的就是将上面的查询变成CONSTRUCT
查询内的子查询,该查询找到与?nextPlanet
相对应的?nextDist
并构造目标图。