在使用 Jena 的 SPARQL 查询时,我试图以更易读的格式获取数据,但是我不知道如何以正确的方式提取数据。
就目前而言,输出是:
"http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#SaucelitoCanyon http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#SaucelitoCanyon"
相反,只想将“Saucelito Canyon”作为输出。
public JenaQuery() {
String wineRegion = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
+ "PREFIX owl: <http://www.w3.org/2002/07/owl#>\n"
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n"
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"
+ "PREFIX wine:<http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>\n"
+ "SELECT ?region ?winery \n"
+ "WHERE {?wine wine:locatedIn ?region . \n"
+ "?region wine:locatedIn wine:CaliforniaRegion . \n"
+ "?wine wine:hasMaker ?winery}";
String inputFileName = "wine.rdf";
// create an empty model
Model model = ModelFactory.createDefaultModel();
// use the FileManager to find the input file
InputStream in;
in = FileManager.get().open(inputFileName);
if (in == null) {
throw new IllegalArgumentException(
"File: " + inputFileName + " not found");
}
// read the RDF/XML file
model.read(in, null);
try (QueryExecution qexec = QueryExecutionFactory.create(wineRegion, model)) {
ResultSet results = qexec.execSelect();
while (results.hasNext()) {
QuerySolution row = results.next();
RDFNode winery = row.get("winery");
System.out.println(winery);
}
qexec.close();
}
}
由于您已经在 SPARQL 查询中获得了前缀,因此可以使用strafter and str函数将 URI 转换为字符串并在前缀后面加上后缀。在下文中,我使用了values ?winery { ... }
绑定?winery
到特定的 URI,但您的查询已经处理了这个问题。重要的部分是bind之后负责字符串处理。
PREFIX wine: <http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#>
SELECT ?winery ?localname
WHERE {
values ?winery { wine:SomeWinery }
bind( strafter(str(?winery),str(wine:)) as ?localname )
}
winery localname
<http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#SomeWinery> "SomeWinery"
也就是说,在结构良好的本体中,个人通常会拥有一个rdfs:标签这应该为个人提供一个字符串标签。当它可用时,您可能会考虑简单地检索该值。例如。,
SELECT ?winery ?name
WHERE {
#-- ...
?winery rdfs:label ?name
}
这在之前已经描述过我的答案 https://stackoverflow.com/a/20845304/1281433 to 检索 owl 中特定子类的类名 https://stackoverflow.com/q/20830056/1281433,但该问题不涉及 Jena,因此它并不完全重复,即使相同的基于 SPARQL 的解决方案也有效。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)