2017-04-24 96 views
1

我有一個SPARQL查詢SPARQL查詢結果在耶拿語法

同樣的查詢給門徒和耶拿之間的不同結果的結果的問題

在門徒,查詢是:

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

SELECT ?subject 
    WHERE { ?subject rdfs:label ?object} 

結果是:字符串(症狀的標籤)

在耶拿,代碼:

String path = "file:///D:/onto/owl ontologies/symp.owl"; 
OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM); 
String stringQuery 
     = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " 
     + "SELECT ?symptoms " 
     + " WHERE { ?symptoms rdfs:label ?object }"; 



    Query query = QueryFactory.create(stringQuery); 
    QueryExecution executeQuery = QueryExecutionFactory.create(query,model); 
    org.apache.jena.query.ResultSet res = executeQuery.execSelect(); 

    model.read(path); 

    while (res.hasNext()) { 
     QuerySolution qs = res.nextSolution(); 
     Resource symp = qs.getResource("symptoms"); 

     System.out.println(symp); 
    } 

結果是:這些URI

使用的本體:http://purl.obolibrary.org/obo/symp.owl

我怎樣才能得到只有標籤 「症狀」 感謝您的幫助。

回答

4

你要選擇的,而不是在您的查詢的主題對象:

PREFIX rdfs: http://www.w3.org/2000/01/rdf-schema# 

SELECT ?label WHERE { ?symptoms rdfs:label ?label} 

一般來說,給變量「更好」的名字能避免這樣的問題,像你的情況。

然後你必須得到字面的詞彙形式在Java代碼:

while (res.hasNext()) { 
     QuerySolution qs = res.next(); 
     String symp = qs.getLiteral("label").getLexicalForm(); 

     System.out.println(symp); 
} 

爲什麼門徒會顯示一些人類可讀的形式,即使您的查詢的原因是,默認渲染器中設置UI。要麼是使用URI的簡短內容,要麼是一些自定義呈現。

+0

非常感謝你** @ AKSW **它完美的作品 –