SPARQL 查询某种类型的 START WITH 和 CONNECT BY (Oracle)
SPARQL query for some kind of START WITH and CONNECT BY (Oracle)
我正忙于使用三重存储进行概念验证。我有以下结构:
定义了 2 个关系类型(三元组)。 Top-down 关系,其中 child 是("isPartOf" 其 parent 和 left-right 的一部分,其中 child 可以(可选)被替换("replaces") 由另一个版本 child.
每个 child 都有一个 "isValidStart" 三元组,日期为 object。这意味着此 child 自该日期起有效。
水平 child 组中的最后一个 child 可以有关系 "isInvalidEnd",这意味着在此日期之后该组无效。
我想要做的是构建一个 SPARQL 查询,我可以在其中获取特定日期的 parent 的 child。使用 SPARQL 可以做到这一点吗?我该怎么做?
我知道在 Oracle 中有任何类型的 START WITH / CONNECT BY 函数可以做一些这样的事情......但是我如何在 SPARQL 世界中做到这一点。
谢谢
</metadata/puid/test2> <http://purl.org/dc/terms/isPartOf> "/metadata/puid/test1" .
</metadata/puid/test2> <http://purl.org/dc/terms/isValidStart> "2015-04-01"^^<http://www.w3.org/2001/XMLSchema#date> .
</metadata/puid/test3> <http://purl.org/dc/terms/isPartOf> "/metadata/puid/test2" .
</metadata/puid/test3> <http://purl.org/dc/terms/isValidStart> "2015-04-01"^^<http://www.w3.org/2001/XMLSchema#date> .
</metadata/puid/test4> <http://purl.org/dc/terms/isPartOf> "/metadata/puid/test3" .
</metadata/puid/test4> <http://purl.org/dc/terms/isValidStart> "2015-04-01"^^<http://www.w3.org/2001/XMLSchema#date> .
</metadata/puid/test5> <http://purl.org/dc/terms/isPartOf> "/metadata/puid/test4" .
</metadata/puid/test5> <http://purl.org/dc/terms/isValidStart> "2015-04-01"^^<http://www.w3.org/2001/XMLSchema#date> .
</metadata/puid/test6> <http://purl.org/dc/terms/isPartOf> "/metadata/puid/test4" .
</metadata/puid/test6> <http://purl.org/dc/terms/isValidStart> "2015-04-01"^^<http://www.w3.org/2001/XMLSchema#date> .
</metadata/puid/test7> <http://purl.org/dc/terms/isPartOf> "/metadata/puid/test4" .
</metadata/puid/test7> <http://purl.org/dc/terms/isValidStart> "2015-04-01"^^<http://www.w3.org/2001/XMLSchema#date> .
</metadata/puid/test8> <http://purl.org/dc/terms/replaces> "/metadata/puid/test7" .
</metadata/puid/test8> <http://purl.org/dc/terms/isValidStart> "2015-07-01"^^<http://www.w3.org/2001/XMLSchema#date> .
</metadata/puid/test9> <http://purl.org/dc/terms/isPartOf> "/metadata/puid/test5" .
</metadata/puid/test9> <http://purl.org/dc/terms/isValidStart> "2015-04-01"^^<http://www.w3.org/2001/XMLSchema#date> .
</metadata/puid/test10> <http://purl.org/dc/terms/isPartOf> "/metadata/puid/test5" .
</metadata/puid/test10> <http://purl.org/dc/terms/isValidStart> "2015-04-01"^^<http://www.w3.org/2001/XMLSchema#date> .
</metadata/puid/test11> <http://purl.org/dc/terms/isPartOf> "/metadata/puid/test5" .
</metadata/puid/test11> <http://purl.org/dc/terms/isValidStart> "2015-04-01"^^<http://www.w3.org/2001/XMLSchema#date> .
</metadata/puid/test12> <http://purl.org/dc/terms/replaces> "/metadata/puid/test9" .
</metadata/puid/test12> <http://purl.org/dc/terms/isValidStart> "2015-07-01"^^<http://www.w3.org/2001/XMLSchema#date> .
</metadata/puid/test13> <http://purl.org/dc/terms/replaces> "/metadata/puid/test10" .
</metadata/puid/test13> <http://purl.org/dc/terms/isValidStart> "2015-05-01"^^<http://www.w3.org/2001/XMLSchema#date> .
</metadata/puid/test14> <http://purl.org/dc/terms/replaces> "/metadata/puid/test13" .
</metadata/puid/test14> <http://purl.org/dc/terms/isValidStart> "2015-08-01"^^<http://www.w3.org/2001/XMLSchema#date> .
</metadata/puid/test14> <http://purl.org/dc/terms/isValidEnd> "2015-12-01"^^<http://www.w3.org/2001/XMLSchema#date> .
// 免责声明:我是 SPARQL 世界的新手
不完全确定这是否是您要的,但在线评论:
指定日期:"2015-04-01"^^xsd:date
PREFIX : <http://purl.org/dc/terms/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?child
WHERE
{
# we have two options to check
# 1. all children that have not been replaced
{
# get the valid start values
?child :isValidStart ?start
# where the start was before the given date
FILTER ( ?start < "2015-04-01"^^xsd:date )
# and there was no other replacing child
FILTER NOT EXISTS { ?otherChild :replaces ?child }
}
UNION
# 2. children that haven been replaced
{
# start date of children that replace others
?child :isValidStart ?start ;
:replaces ?someChild
# but haven not been replaced themselves
FILTER NOT EXISTS { ?otherChild :replaces ?child }
# where the start was before the given date
FILTER(?start < "2015-04-01"^^xsd:date)
# and there isn't an end date before the given date
FILTER NOT EXISTS { ?child :isValidEnd ?end
FILTER ( ?end < "2015-04-01"^^xsd:date )
}
}
}
我研究了一个解决方案并找到了一个可行的解决方案。我把这个方法放在一个函数中,我可以用它来找到该项目(父项)下面的有效子项。当递归调用这个函数时,我得到了一棵完整的树,只有有效的项目。
PREFIX : <http://purl.org/dc/terms/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?child ?startDate ?endDate
WHERE
{
# get the valid start values
?child :isValidStart ?startDate
BIND(iri("'||$parent||'") as ?parent)
FILTER EXISTS{ {?child :isPartOf ?parent }
UNION {
?child (:replaces)+ ?prevVersion .
?prevVersion :isPartOf ?parent
}
}
# and there was no other valid replacing child
FILTER NOT EXISTS { ?replacer :replaces ?child ;
:isValidStart ?startDateReplacer
FILTER (?startDateReplacer <= "'||$selectionDate||'"^^xsd:date)
}
# where the start was before/at the given date
FILTER(?startDate <= "'||$selectionDate||'"^^xsd:date)
OPTIONAL { ?child :isValidEnd ?endDate }
# and not end date is before/at selection date
FILTER NOT EXISTS { FILTER (?endDate <= "'||$selectionDate||'"^^xsd:date) }
}
我正忙于使用三重存储进行概念验证。我有以下结构:
定义了 2 个关系类型(三元组)。 Top-down 关系,其中 child 是("isPartOf" 其 parent 和 left-right 的一部分,其中 child 可以(可选)被替换("replaces") 由另一个版本 child.
每个 child 都有一个 "isValidStart" 三元组,日期为 object。这意味着此 child 自该日期起有效。 水平 child 组中的最后一个 child 可以有关系 "isInvalidEnd",这意味着在此日期之后该组无效。
我想要做的是构建一个 SPARQL 查询,我可以在其中获取特定日期的 parent 的 child。使用 SPARQL 可以做到这一点吗?我该怎么做?
我知道在 Oracle 中有任何类型的 START WITH / CONNECT BY 函数可以做一些这样的事情......但是我如何在 SPARQL 世界中做到这一点。
谢谢
</metadata/puid/test2> <http://purl.org/dc/terms/isPartOf> "/metadata/puid/test1" .
</metadata/puid/test2> <http://purl.org/dc/terms/isValidStart> "2015-04-01"^^<http://www.w3.org/2001/XMLSchema#date> .
</metadata/puid/test3> <http://purl.org/dc/terms/isPartOf> "/metadata/puid/test2" .
</metadata/puid/test3> <http://purl.org/dc/terms/isValidStart> "2015-04-01"^^<http://www.w3.org/2001/XMLSchema#date> .
</metadata/puid/test4> <http://purl.org/dc/terms/isPartOf> "/metadata/puid/test3" .
</metadata/puid/test4> <http://purl.org/dc/terms/isValidStart> "2015-04-01"^^<http://www.w3.org/2001/XMLSchema#date> .
</metadata/puid/test5> <http://purl.org/dc/terms/isPartOf> "/metadata/puid/test4" .
</metadata/puid/test5> <http://purl.org/dc/terms/isValidStart> "2015-04-01"^^<http://www.w3.org/2001/XMLSchema#date> .
</metadata/puid/test6> <http://purl.org/dc/terms/isPartOf> "/metadata/puid/test4" .
</metadata/puid/test6> <http://purl.org/dc/terms/isValidStart> "2015-04-01"^^<http://www.w3.org/2001/XMLSchema#date> .
</metadata/puid/test7> <http://purl.org/dc/terms/isPartOf> "/metadata/puid/test4" .
</metadata/puid/test7> <http://purl.org/dc/terms/isValidStart> "2015-04-01"^^<http://www.w3.org/2001/XMLSchema#date> .
</metadata/puid/test8> <http://purl.org/dc/terms/replaces> "/metadata/puid/test7" .
</metadata/puid/test8> <http://purl.org/dc/terms/isValidStart> "2015-07-01"^^<http://www.w3.org/2001/XMLSchema#date> .
</metadata/puid/test9> <http://purl.org/dc/terms/isPartOf> "/metadata/puid/test5" .
</metadata/puid/test9> <http://purl.org/dc/terms/isValidStart> "2015-04-01"^^<http://www.w3.org/2001/XMLSchema#date> .
</metadata/puid/test10> <http://purl.org/dc/terms/isPartOf> "/metadata/puid/test5" .
</metadata/puid/test10> <http://purl.org/dc/terms/isValidStart> "2015-04-01"^^<http://www.w3.org/2001/XMLSchema#date> .
</metadata/puid/test11> <http://purl.org/dc/terms/isPartOf> "/metadata/puid/test5" .
</metadata/puid/test11> <http://purl.org/dc/terms/isValidStart> "2015-04-01"^^<http://www.w3.org/2001/XMLSchema#date> .
</metadata/puid/test12> <http://purl.org/dc/terms/replaces> "/metadata/puid/test9" .
</metadata/puid/test12> <http://purl.org/dc/terms/isValidStart> "2015-07-01"^^<http://www.w3.org/2001/XMLSchema#date> .
</metadata/puid/test13> <http://purl.org/dc/terms/replaces> "/metadata/puid/test10" .
</metadata/puid/test13> <http://purl.org/dc/terms/isValidStart> "2015-05-01"^^<http://www.w3.org/2001/XMLSchema#date> .
</metadata/puid/test14> <http://purl.org/dc/terms/replaces> "/metadata/puid/test13" .
</metadata/puid/test14> <http://purl.org/dc/terms/isValidStart> "2015-08-01"^^<http://www.w3.org/2001/XMLSchema#date> .
</metadata/puid/test14> <http://purl.org/dc/terms/isValidEnd> "2015-12-01"^^<http://www.w3.org/2001/XMLSchema#date> .
// 免责声明:我是 SPARQL 世界的新手
不完全确定这是否是您要的,但在线评论:
指定日期:"2015-04-01"^^xsd:date
PREFIX : <http://purl.org/dc/terms/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?child
WHERE
{
# we have two options to check
# 1. all children that have not been replaced
{
# get the valid start values
?child :isValidStart ?start
# where the start was before the given date
FILTER ( ?start < "2015-04-01"^^xsd:date )
# and there was no other replacing child
FILTER NOT EXISTS { ?otherChild :replaces ?child }
}
UNION
# 2. children that haven been replaced
{
# start date of children that replace others
?child :isValidStart ?start ;
:replaces ?someChild
# but haven not been replaced themselves
FILTER NOT EXISTS { ?otherChild :replaces ?child }
# where the start was before the given date
FILTER(?start < "2015-04-01"^^xsd:date)
# and there isn't an end date before the given date
FILTER NOT EXISTS { ?child :isValidEnd ?end
FILTER ( ?end < "2015-04-01"^^xsd:date )
}
}
}
我研究了一个解决方案并找到了一个可行的解决方案。我把这个方法放在一个函数中,我可以用它来找到该项目(父项)下面的有效子项。当递归调用这个函数时,我得到了一棵完整的树,只有有效的项目。
PREFIX : <http://purl.org/dc/terms/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?child ?startDate ?endDate
WHERE
{
# get the valid start values
?child :isValidStart ?startDate
BIND(iri("'||$parent||'") as ?parent)
FILTER EXISTS{ {?child :isPartOf ?parent }
UNION {
?child (:replaces)+ ?prevVersion .
?prevVersion :isPartOf ?parent
}
}
# and there was no other valid replacing child
FILTER NOT EXISTS { ?replacer :replaces ?child ;
:isValidStart ?startDateReplacer
FILTER (?startDateReplacer <= "'||$selectionDate||'"^^xsd:date)
}
# where the start was before/at the given date
FILTER(?startDate <= "'||$selectionDate||'"^^xsd:date)
OPTIONAL { ?child :isValidEnd ?endDate }
# and not end date is before/at selection date
FILTER NOT EXISTS { FILTER (?endDate <= "'||$selectionDate||'"^^xsd:date) }
}