2012-11-24 135 views
1

我正在使用JENA,我正在爲學習目的進行一些簡單的查詢。其中之一是:查詢結果中沒有結果SPARQL

PREFIX uni:<http://www.university.fake/university#> 
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> 

SELECT ?person 
WHERE { 
    ?person rdf:type/rdfs:subClassOf <uni:Person>. 
    ?person uni:has_name ?name. 
    ?person uni:member_of ?dep. 
    ?dep uni:dep_city "Patras". 
} 

沒有第四屆三重其餘做,如果第三一個?dep被替換爲引用值產生的結果。但是,添加第四個三元組時,沒有結果。三元組本身確實會在自己的查詢中產生結果,所以我認爲那裏沒有任何問題。

一些RDF數據:

<rdf:Property rdf:about="uni:member_of"> 
    <rdfs:domain rdf:resource="uni:Person"/> 
    <rdfs:range rdf:resource="uni:Department"/> 
</rdf:Property> 
<rdf:Property rdf:about="uni:dep_city"> 
    <rdfs:domain rdf:resource="uni:Department"/> 
    <rdfs:range rdf:resource="uni:Literal"/> 
</rdf:Property> 
<rdf:Description rdf:about="uni:dep1"> 
    <uni:dep_name>CEID</uni:dep_name> 
    <uni:dep_city>Patras</uni:dep_city> 
</rdf:Description> 
<rdf:Description rdf:about="uni:prof2"> 
    <uni:has_name>Bob Ross</uni:has_name> 
    <uni:has_phone>6981234566</uni:has_phone> 
    <uni:has_age>52</uni:has_age> 
    <uni:member_of>CEID</uni:member_of> 
    <uni:teaches>Painting</uni:teaches> 
    <rdf:type rdf:resource="uni:Professor"/> 
</rdf:Description> 
+0

因此'?dep uni:dep_city「Patras」'起作用了嗎?嘗試從那裏反向工作,那就是嘗試'?person uni:member_of?dep。 ?dep uni:dep_city「Patras」等 – user205512

+1

另外,如果可能,請提供數據。很難診斷沒有數據。 – user205512

+0

是的,它的工作原理。您建議的第二個查詢不起作用,這可能是問題所在。RDF數據的一個示例是: \t Bob Ross \t 6981234566 \t 52 \t CEID \t 繪畫 \t \t CEID \t Patras ' –

回答

4

啊哈,所以給它好像數據,取決於一個人的角度來看,無論是查詢是錯誤或數據被嚴重建模。

由於現在的情況?person uni:member_of ?dep是不會工作的。您可以加入uni:dep_name(假設這些是唯一的密鑰)。

?person uni:member_of ?dep_name . 
?dep uni:dep_name ?dep_name ; 
    uni:dep_city "Patras" . 

但是我認爲數據真的應該得到糾正,使(以龜語法)uni:prof2 uni:member_of uni:dep1

事實上,你的數據問題更加嚴重,對於初學者甚至是使用rdf/xml的有經驗的人來說這種情況並不少見。

<rdf:Description rdf:about="uni:member_of"> 
    ... 

並不意味着你認爲這意味着什麼。 rdf:aboutrdf:resource屬性採用完整的URL,而不是縮寫形式。你應該使用rdf:about="http://example.com/namespace/university#member_of",或者任何uni命名空間的URL。

這個錯誤就是爲什麼你必須:

?person rdf:type/rdfs:subClassOf <uni:Person> 

而不是:

?person rdf:type/rdfs:subClassOf uni:Person 

在SPARQL和龜<uni:Person>的URL 'UNI:人' 匹配(不存在),而'uni:Person'是一個縮寫的URL,它將使用uni前綴進行擴展。

當你學習時,我強烈建議避免使用rdf/xml。 Turtle讀取和寫入更容易,並且在很大程度上與SPARQL語法(減去變量)相匹配。

+0

感謝您的提示!我會嘗試進入Turtle一段時間,但目前我仍然處於SPARQL基礎。事實證明,我的數據的確是非常模糊的,因爲我錯誤地認爲,如果模式將它們指定爲這些類型,推理引擎將識別來自不同資源的相同文字。 –

0

顯然這個錯誤是在<uni:member_of>CEID</uni:member_of>,這應該是對另一個資源的引用。正確的是<uni:member_of rdf:resource="uni:dep1"/>