2016-02-26 90 views
0

这是我的CS.owl的一部分,它显示了CloudService类的单个DropBox。使用sparql查询从OWL文件检索个人Jena

<!-- 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> 

我需要检索使用耶拿类CloudService的个体(如投放箱)。 以下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); 
} 

查询列标题 下返回空的结果虽然它适用于门生并返回结果(包括DropBox的) 什么是错我的代码?

回答

1

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

+0

非常感谢@AndyS –

+0

我也使用了以下显示结果而(results.hasNext()){ \t \t \t QuerySolution QS = results.next(); \t \t \t System.out.println(qs); \t} –

+0

如果您还有其他问题,请打开一个新的主题,或者如果密切相关,请修改现有问题。 – AKSW