2013-07-25 20 views
3

我想用CONSTRUCT替換我的SPARQL結果集中的屬性,它基本上行得通,除了前綴被自動替換爲「ns2」。有誰知道爲什麼,以及如何避免?爲什麼在創建SPARQL構造查詢時將前綴替換爲ns2

負責人查詢

" PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " + 
" PREFIX ma: <http://www.w3.org/ns/ma-ont#> " + 
" PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> " + 
" CONSTRUCT { ?subject ma:title ?label } " + 
" WHERE { "+ 
" ?subject rdfs:label ?label. " + 

結果舉例:

<http://dbpedia.org/resource/Japantown,_San_Francisco> ns2:title "Japantown, San Francisco"@en 
+1

我有點驚訝,如果這是在圖什麼將會發生,這Jena給你回來了,但是,只要'ns2'前綴用於'http:// www.w3.org/ns/ma-ont#',它不會改變結果圖的_meaning_,因爲URI是一樣的。 –

回答

4

的問題

對於它的價值,它看起來並不像耶拿是這樣做的,而是(假設DBpedia資源的存在意味着您正在查詢DBpedia)DBpedia的端點。鑑於這種簡單的數據:

@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . 
@prefix ma: <http://www.w3.org/ns/ma-ont#> . 

<http://dbpedia.org/resource/Japantown,_San_Francisco> rdfs:label "Japantown, San Francisco"@en . 

與此查詢:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
PREFIX ma: <http://www.w3.org/ns/ma-ont#> 

CONSTRUCT { ?subject ma:title ?label } 
WHERE { 
    ?subject rdfs:label ?label 
} 

耶拿的ARQ似乎保持在兩個RDF/XML和龜前綴:

$ arq --data data.n3 --query construct.sparql --results RDF/XML 
<rdf:RDF 
    xmlns:ma="http://www.w3.org/ns/ma-ont#" 
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
    xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"> 
    <rdf:Description rdf:about="http://dbpedia.org/resource/Japantown,_San_Francisco"> 
    <ma:title xml:lang="en">Japantown, San Francisco</ma:title> 
    </rdf:Description> 
</rdf:RDF> 
$ arq --data data.n3 --query construct.sparql 
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . 
@prefix ma:  <http://www.w3.org/ns/ma-ont#> . 

<http://dbpedia.org/resource/Japantown,_San_Francisco> 
     ma:title  "Japantown, San Francisco"@en . 

然而,運行一個非常類似的查詢(注意將保持我們的結果很小的VALUES)運行在DBpedia public SPARQL endpoint上,得到您提到的自動生成的名稱空間前綴。

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
PREFIX ma: <http://www.w3.org/ns/ma-ont#> 

CONSTRUCT { ?subject ma:title ?label } 
WHERE { 
    VALUES ?subject { <http://dbpedia.org/resource/Japantown,_San_Francisco> } 
    ?subject rdfs:label ?label 
} 

SPARQL Results

@prefix ns0: <http://www.w3.org/ns/ma-ont#> . 
<http://dbpedia.org/resource/Japantown,_San_Francisco> ns0:title "Japantown, San Francisco"@en . 

注意,這些結果是相同RDF圖;前綴只是一個方便的方法,使一些輸出更具可讀性。任何RDF處理工具都會看到它們完全相同。 Jena沒有做錯任何事情(事實上,Jena的好處在於它保留了前綴),但是在DBpedia上運行的Virtuoso實例也不是。

保留前綴

雖然前綴隻影響可讀性圖的系列化,圖的序列化的易讀性也很重要。即使DBpedia已經離開並在您之下進行了更改,仍然有一些方法可以保留前綴。

使用聯合查詢(SERVICE)

在命令行上使用ARQ(如上所示)與現有的數據局部地保持在理想的前綴中構造的模型。我們可以編寫一個類似的查詢,實際上可以遠程查詢DBpedia,但由於ARQ的工作是實際構建圖形,因此前綴會以您期望的方式結束。例如:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
PREFIX ma: <http://www.w3.org/ns/ma-ont#> 

CONSTRUCT { ?subject ma:title ?label } 
WHERE { 
    VALUES ?subject { <http://dbpedia.org/resource/Japantown,_San_Francisco> } 
    SERVICE <http://dbpedia.org/sparql> { 
    ?subject rdfs:label ?label 
    } 
} 

ARQ仍然需要--data的說法,所以我創建了一個空文件,empty-data.n3。這裏我們需要的數據實際上來自DBpedia。

$ arq --data empty-data.n3 --query construct-remote.sparql 
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . 
@prefix ma:  <http://www.w3.org/ns/ma-ont#> . 

<http://dbpedia.org/resource/Japantown,_San_Francisco> 
     ma:title  "Japantown, San Francisco"@en . 

更改在耶拿模型(PrefixMapping)

由於耶拿型號前綴是PrefixMapping,可以改變所使用的前綴。以下是運行遠程查詢(不使用聯合查詢)並在之後更新前綴的Java代碼。

import com.hp.hpl.jena.query.QueryExecutionFactory; 
import com.hp.hpl.jena.rdf.model.Model; 
import com.hp.hpl.jena.vocabulary.RDFS; 

public class RemoteQueryPrefixChange { 
    final static String MA_NS = "http://www.w3.org/ns/ma-ont#"; 

    final static String queryString = "" + 
      "PREFIX rdfs: <"+RDFS.getURI()+">\n" + 
      "PREFIX ma: <"+MA_NS+">\n" + 
      "\n" + 
      "CONSTRUCT { ?subject ma:title ?label }\n" + 
      "WHERE {\n" + 
      " VALUES ?subject { <http://dbpedia.org/resource/Japantown,_San_Francisco> }\n" + 
      " ?subject rdfs:label ?label\n" + 
      "}\n" + 
      ""; 

    final static String DBPEDIA_SERVICE = "http://dbpedia.org/sparql"; 

    public static void main(String[] args) { 
     Model results = QueryExecutionFactory.sparqlService(DBPEDIA_SERVICE, queryString).execConstruct(); 
     System.out.println("== Original Prefixes =="); 
     results.write(System.out, "TTL"); 
     System.out.println("== Updated Prefixes =="); 
     results.removeNsPrefix(results.getNsURIPrefix(MA_NS)); 
     results.setNsPrefix("ma", MA_NS); 
     results.write(System.out, "TTL"); 
    } 
} 

的輸出(請注意在更新序列模型的原始序列化ns2ma):

== Original Prefixes == 
@prefix ns2:  <http://www.w3.org/ns/ma-ont#> . 
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . 
@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . 

<http://dbpedia.org/resource/Japantown,_San_Francisco> 
     ns2:title "Japantown, San Francisco"@en . 
== Updated Prefixes == 
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . 
@prefix ma:  <http://www.w3.org/ns/ma-ont#> . 
@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . 

<http://dbpedia.org/resource/Japantown,_San_Francisco> 
     ma:title "Japantown, San Francisco"@en . 
+0

謝謝Joshua,驗證!我跑了一些測試,得出了相同的結論。 – user2498899

+0

@ user2498899由於我們已經知道發生了什麼,我已經用兩種方法從CONSTRUCT查詢中獲取結果,並使用更像前綴的前綴來更新答案。它不會更改實際的_graph_,但是當您獲得原始前綴時,它更易於讀取_serialization_。本更新回答了「如何避免」這個問題。 –

+0

@ user2498899更新後的答案是否適用於您,例如,解決'ns2'前綴並回到'ma'? –

相關問題