1

我有这个 java 代码,它使用 apache jena api 查询披萨本体

    String queryStr =
"prefix pizza: <" + PIZZA_NS + "> "               +
"prefix rdfs: <" + RDFS.getURI() + "> "           +
"prefix owl: <" + OWL.getURI() + "> "             +
"select ?pizza where {?pizza a owl:Class ; "      +
"rdfs:subClassOf ?restriction. "                  +
"?restriction owl:onProperty pizza:hasTopping ;"  +
"owl:someValuesFrom pizza:PeperoniSausageTopping" +
"}";


Query query = QueryFactory.create(queryStr);
QueryExecution qe = QueryExecutionFactory.create(query, model);
ResultSet rs = qe.execSelect();


ArrayList rsList = (ArrayList)ResultSetFormatter.toList(rs);
for(int i=0;i<rsList.size();i++){
    out.println(rsList.get(i).toString());
}

它返回这个:

( ?pizza = <http://www.co-ode.org/ontologies/pizza/pizza.owl#AmericanHot> )
( ?pizza = <http://www.co-ode.org/ontologies/pizza/pizza.owl#FourSeasons> )
( ?pizza = <http://www.co-ode.org/ontologies/pizza/pizza.owl#American> )

但我只需要

美国热

四个季节

美国人

如何获得这个结果?

4

2 回答 2

6

我以这种方式解决了它。

for ( ; rs.hasNext() ; ){
  QuerySolution soln = rs.nextSolution() ;
  RDFNode x = soln.get("pizza") ;
  out.println(x.asNode().getLocalName());
}

对于谁会有我同样的问题,其他信息可以从这里检索

于 2012-10-09T14:21:38.200 回答
3

SPARQL 1.1 函数 STRAFTER 可以帮助:

SELECT ?pizza (strafter(str(?pizza), "#") AS ?localName)
WHERE

但客户端解决方案同样出色,可与 SPARQL 1.0 一起使用。

于 2012-10-09T16:18:00.083 回答