gpt4 book ai didi

xml - 如何使用 SPARQL 查询两个事物之间的距离

转载 作者:数据小太阳 更新时间:2023-10-29 02:56:59 26 4
gpt4 key购买 nike

我用 RDF/XML 创建了一个小地铁 map ,想知道如何查询两个站点之间的距离。我是 SPARQL 的新手,不知道如何开始。

“距离”的意思是,我想知道两个站点之间有多少个站点。后面想计算时长,那是另外一回事了。

这是我的第一种方法:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ex: <http://example.com>

SELECT (count(?mid) as ?distance) WHERE {
<http://example.com/StopD> ex:via* ?mid .
?mid ex:via+ <http://example.com/StopC> .
}

我认为我的查询不起作用是因为我使用的是空白节点吗?不起作用意味着我没有得到两个停靠点(如 StopA 和 StopB)之间的图表数量。我脑子里有这样的东西:http://answers.semanticweb.com/questions/3491/how-can-i-calculate-the-length-of-a-path-between-2-graph-nodes-in-sparql/24609

那是我的 map 草图。线条旁边的数字代表两个站点之间的行程时间:

semantic-metro-map.JPG

我的 RDF 代码描述了每个车站及其相邻车站的可用线路和旅行持续时间。乍一看它看起来很多余,但我想在未来包括单向路线(例如公交车),所以我认为第一次尝试就可以了。

RDF(在此处下载文件:http://gopeter.de/misc/metro.rdf)

<?xml version="1.0"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:ex="http://example.com/">

<rdf:Description rdf:about="http://example.com/StopA">

<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopB" />
<ex:Line rdf:resource="http://example.com/Line1" />
<ex:Duration>2</ex:Duration>
</ex:via>

<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopB" />
<ex:Line rdf:resource="http://example.com/Line2" />
<ex:Duration>7</ex:Duration>
</ex:via>

<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopD" />
<ex:Line rdf:resource="http://example.com/Line4" />
<ex:Duration>2</ex:Duration>
</ex:via>

<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopD" />
<ex:Line rdf:resource="http://example.com/Line2" />
<ex:Duration>6</ex:Duration>
</ex:via>

<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopE" />
<ex:Line rdf:resource="http://example.com/Line1" />
<ex:Duration>1</ex:Duration>
</ex:via>

<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopF" />
<ex:Line rdf:resource="http://example.com/Line4" />
<ex:Duration>3</ex:Duration>
</ex:via>

</rdf:Description>

<rdf:Description rdf:about="http://example.com/StopB">

<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopA" />
<ex:Line rdf:resource="http://example.com/Line1" />
<ex:Duration>2</ex:Duration>
</ex:via>

<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopA" />
<ex:Line rdf:resource="http://example.com/Line2" />
<ex:Duration>7</ex:Duration>
</ex:via>

<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopC" />
<ex:Line rdf:resource="http://example.com/Line2" />
<ex:Duration>10</ex:Duration>
</ex:via>

<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopF" />
<ex:Line rdf:resource="http://example.com/Line3" />
<ex:Duration>2</ex:Duration>
</ex:via>

</rdf:Description>

<rdf:Description rdf:about="http://example.com/StopC">

<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopB" />
<ex:Line rdf:resource="http://example.com/Line2" />
<ex:Duration>10</ex:Duration>
</ex:via>

</rdf:Description>

<rdf:Description rdf:about="http://example.com/StopD">

<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopA" />
<ex:Line rdf:resource="http://example.com/Line2" />
<ex:Duration>6</ex:Duration>
</ex:via>

<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopA" />
<ex:Line rdf:resource="http://example.com/Line4" />
<ex:Duration>2</ex:Duration>
</ex:via>

<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopF" />
<ex:Line rdf:resource="http://example.com/Line3" />
<ex:Duration>2</ex:Duration>
</ex:via>

</rdf:Description>

<rdf:Description rdf:about="http://example.com/StopE">

<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopA" />
<ex:Line rdf:resource="http://example.com/Line1" />
<ex:Duration>1</ex:Duration>
</ex:via>

</rdf:Description>

<rdf:Description rdf:about="http://example.com/StopF">

<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopA" />
<ex:Line rdf:resource="http://example.com/Line4" />
<ex:Duration>3</ex:Duration>
</ex:via>

<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopB" />
<ex:Line rdf:resource="http://example.com/Line3" />
<ex:Duration>2</ex:Duration>
</ex:via>

<ex:via rdf:parseType="Resource">
<ex:Stop rdf:resource="http://example.com/StopD" />
<ex:Line rdf:resource="http://example.com/Line3" />
<ex:Duration>2</ex:Duration>
</ex:via>

</rdf:Description>

</rdf:RDF>

最佳答案

为什么你的不起作用

让我们用更易于阅读的 Turtle 语法(如下)查看您的数据。 StopD 使用 ex:via 属性连接到三个空白节点。这意味着您将获得 ?midStopD ex:via* ?mid 的四个匹配项。但是,您不会得到更多,因为没有来自具有属性 ex:via 的空白节点的传出链接。这意味着?mid ex:via+ StopC 没有匹配,因为?mid 没有任何传出 ex:via 链接。像 ?mid ex:Stop/ex:via+ StopC 这样的东西会更好,因为 ex:Stop 链接让你从空白节点到另一个站点。

@prefix ex:    <http://example.com/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

ex:StopD ex:via [ ex:Duration "6" ;
ex:Line ex:Line2 ;
ex:Stop ex:StopA
] ;
ex:via [ ex:Duration "2" ;
ex:Line ex:Line4 ;
ex:Stop ex:StopA
] ;
ex:via [ ex:Duration "2" ;
ex:Line ex:Line3 ;
ex:Stop ex:StopF
] .

即使您可以将附加项 ex:Stop 添加到您的属性路径中,但这仍然不会按照您想要的方式计算距离,因为您不会被限制在一行中。即,您将在多条路径上获得优势。

完成这项工作

我重新创建了一个更简单的场景:

@prefix : <https://stackoverflow.com/q/24538144/1281433/> .

# B
# * *
# 2 * * 4
# * *
# * *
# A +++++++++ C
# 3
#
# *** line 1
# +++ line 2

:StopA a :Stop ; :toLink :Link1 , :Link3 .
:StopB a :Stop ; :toLink :Link2 .
:StopC a :Stop .

:Link1 :hasDuration 2 ;
:toStop :StopB ;
:Line1Self :Link1 .

:Link2 :hasDuration 4 ;
:toStop :StopC ;
:Line1Self :Link2 .

:Link3 :hasDuration 3 ;
:toStop :StopC ;
:Line2Self :Link3 .

每个站点都可以通过 :toStop 连接到任意数量的链接。每个链接的行都用 rolification property 表示为线。例如,link2 line1self link2 表示 link2 在 line1 上。这意味着我们使用属性路径“保持在正确的线上”。然后,要查找第 1 行从 stopA 到 stopB 的行程持续时间,您可以使用如下查询:

prefix : <https://stackoverflow.com/q/24538144/1281433/>

select (sum(?duration) as ?length) where {
:StopA :toLink/(:toStop/:toLink)*/:Line1Self ?link .
?link :hasDuration ?duration ;
:toStop/(:toLink/:Line1Self/:toStop)* :StopC .
}

----------
| length |
==========
| 6 |
----------

要检查不同的行,您只需更改 :LineXSelf 属性。例如,对于第 2 行:

prefix : <https://stackoverflow.com/q/24538144/1281433/>

select (sum(?duration) as ?length) where {
:StopA :toLink/(:toStop/:toLink)*/:Line2Self ?link .
?link :hasDuration ?duration ;
:toStop/(:toLink/:Line2Self/:toStop)* :StopC .
}
----------
| length |
==========
| 3 |
----------

限制

不过,这种方法有一些局限性。属性路径是您进行任意深度查询的唯一选择,但您不能在属性路径中使用变量,这意味着您不能执行以下操作来获取每条线上的距离:

prefix : <https://stackoverflow.com/q/24538144/1281433/>

select ?line (sum(?duration) as ?length) where {
values ?line { :Line1Self :Line2Self }

:StopA :toLink/(:toStop/:toLink)*/?line ?link .
?link :hasDuration ?duration ;
:toStop/(:toLink/?line/:toStop)* :StopC .
}
group by ?line

关于xml - 如何使用 SPARQL 查询两个事物之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24538144/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com