2017-04-24 76 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 **它完美的作品 –