2017-07-06 39 views
2

我忙於使用三重商店的概念驗證。我有以下結構:對於某種START WITH和CONNECT BY(Oracle)的SPARQL查詢

enter image description here

有2種關係類型定義(三元組)。自上而下的關係(「isPartOf」)的父母和左 - 右,其中有孩子可以用其他版本的孩子替換(可選)

此外,每個孩子有一個「isValidStart」三元組,其日期作爲對象,這意味着該孩子自該日期起有效 水平子組中的最後一個孩子可以有一個關係「isInvalidEnd」,這意味着在此日期之後,此組是無效的

我想要做的是建立一個SPARQL查詢在那裏我能得到特定日期父母的孩子的。那是可能的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

+2

您可以顯示RDF數據嗎?是的,這是非常容易與SPARQL – AKSW

+0

可以做到的,我添加三重的。有一些「替代」更新的「文件」,應根據選擇日期選擇正確的一個 –

+0

好的,謝謝。這是否意味着a)給定一個父母(組)的日期,你想擁有所有的孩子或b )您是否想要在給定日期有效的孩子? – AKSW

回答

1

我工作的解決方案,我找到了一個工作解決方案。我把這個方法放在一個函數中,我可以用它來查找該項目(父項)下面的有效子項。當遞歸調用這個函數時,我得到一個完整的樹,只有有效的項目。

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) }   
} 
+0

我不確定這是否是最簡單的解決方案,但它會給出預期的結果。我仍然在學習SPARQL(新技術) –

2

不能完全確定這是否是你問什麼,但評論在線:

鑑於日期: "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) 
          } 
     } 
    } 
+0

感謝您使用此解決方案。將在今天晚些時候測試結果,並確認它是否正常工作,我認爲這是正確的或者接近 –

+1

我想這不是你想要的,但是我會修改這個查詢,可以幫助我的還有一個樣本日期並根據您已經提供的樣本數據得出預期結果 – AKSW

+0

現在它返回所有有效的孩子,而不僅是最後一個有效版本的孩子? 你想如何獲取樣本數據?它現在只是簡單的「emtpy」xml文檔,如:https://pastebin.com/TZVLgJn7 我可以爲您提供一個MarkLogic XQuery腳本來將所有數據插入MarkLogic,但我不知道您的格式確實需要。 –

相關問題