3

我使用 Apache Jena 库在 Java 中创建了一个本体模型,然后我输入了披萨本体。我正在尝试进行 sparql 查询,但表格打印为空白,尽管我的查询通常有答案。难道我做错了什么...?这是代码:

OntModel model = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM_MICRO_RULE_INF);
String inputFileName="pizza.owl";
InputStream in = FileManager.get().open( inputFileName );
if (in == null) {
    throw new IllegalArgumentException(
         "File: " + inputFileName + " not found");
}
model.read(in, null);

String queryString =
        "prefix pizza: <www.co-ode.org/ontologies/pizza/pizza.owl#Pizza> "+        
        "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(queryString);
QueryExecution qe = QueryExecutionFactory.create(query, model);
com.hp.hpl.jena.query.ResultSet results =  qe.execSelect();

ResultSetFormatter.out(System.out, results, query);
qe.close();
4

1 回答 1

3

您的前缀声明是错误的。您不小心包含了Pizza类的名称,并且还遗漏了http协议前缀。更正了,应该是:

"prefix pizza: <http://www.co-ode.org/ontologies/pizza/pizza.owl#> "+

前缀在 RDF 和 SPARQL 中的工作方式是您将 替换为prefix:任何定义的前缀,并且生成的字符串必须与您尝试匹配的资源的 URI完全匹配。它必须是完全匹配的——即使是字母大小写的差异也是显着的。

顺便说一句,您还可以通过以下方式简化加载本体FileManager

OntModel model = ModelFactory.createOntologyModel( 
                                  OntModelSpec.OWL_MEM_MICRO_RULE_INF);
FileManager.get().readModel( model, "pizza.owl" );
于 2012-11-06T22:52:32.423 回答