【问题标题】:retrieve individuals from OWL file using sparql query Jena使用 sparql 查询 Jena 从 OWL 文件中检索个人
【发布时间】:2016-02-26 08:14:29
【问题描述】:

这是我的 CS.owl 的一部分,它显示了 CloudService 类的各个 DropBox。

 <!-- http://www.semanticweb.org/ontologies/SaaS-24-03-2013.owl#DropBox -->
    <owl:NamedIndividual rdf:about="&SaaS-24-03-2013;DropBox">
        <rdf:type rdf:resource="&SaaS-24-03-2013;CloudService"/>
        <hasPriceModel rdf:resource="&SaaS-24-03-2013;Freemium"/>
        <hasDeliveryModel rdf:resource="&SaaS-24-03-2013;Software-as-a-Service"/>
    </owl:NamedIndividual>

我需要使用 Jena 检索 CloudService 类的个人(例如 DropBox)。 以下 SPARQL 查询在 Protege 4.3 中完美运行。它检索许多服务,包括“DropBox”。 我需要使用 Jena 运行它。这是我的代码

        String query = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "+
                        "PREFIX owl: <http://www.w3.org/2002/07/owl#> "+
                            "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> "+
                                "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "+
                                    "PREFIX : <http://www.semanticweb.org/ontologies/SaaS-24-03-2013.owl#> "+
                                        "SELECT ?Service "+
                                                " WHERE {"+
                                            " ?Service  a   :CloudService} "; 
           model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_RULE_INF);
                model.read("ontologies/CS.owl");
        Query query = QueryFactory.create(SparqlQuery);
                QueryExecution qe = QueryExecutionFactory.create(query, model);
                com.hp.hpl.jena.query.ResultSet results =  qe.execSelect();         
    qe.close();
ResultSetFormatter.out(System.out, results);
}

查询在列标题下返回空结果 虽然它适用于 Protege 并返回结果(包括 DropBox) 我的代码有什么问题?

【问题讨论】:

    标签: java sparql jena owl


    【解决方案1】:

    您关闭了执行qe,然后尝试打印结果。代码必须完成调用qe.close 的结果。移动ResultSetFormatter 行。

    【讨论】:

    • 我还使用了以下显示结果 while(results.hasNext()) { QuerySolution qs = results.next(); System.out.println(qs); }
    • 如果您还有其他问题,请打开一个新主题,或者如果它密切相关,请修改现有问题。
    • 当更改使答案无效时,请不要在回答后编辑问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-10
    • 2012-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多