2016-02-26 48 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