2017-02-15 112 views
1

我做了這個貓頭鷹模型。有兩個類傳感器和位置,而位置是枚舉類。SPARQL查詢使用JENA JAVA打印枚舉類

:Sensor rdf:type owl:Class; 
:Location rdf:type owl:Class ; 
       owl:equivalentClass [ rdf:type owl:Class ; 
             owl:oneOf (:Bathroom 
                :Bedroom 
               ) 
            ] . 
:hasLocation rdf:type owl:ObjectProperty ; 
        rdfs:domain [ rdf:type owl:Class ; 
            :Sensor 
           ] ; 
        rdfs:range :Location . 
:Bathing rdf:type owl:NamedIndividual , 
         ADLOntology:Bathing . 
:Bathroom rdf:type owl:NamedIndividual , 
          ADLOntology:Location . 
:Window rdf:type owl:NamedIndividual , 
          :Sensor ; 
        :hasLocation :Bedroom; 
        :hasId "55"^^xsd:int ; . 

我想獲得每個傳感器的位置與他們的身份證號碼。我在Protege中寫了我的查詢,它工作正常。但是,在JENA上,該位置打印爲空。我使用資源來打印傳感器,但是對於它打印空位置。我無法弄清楚打印位置的方式。

String file = "C:/users/src/data.ttl"; 
Model model = FileManager.get().loadModel(file); 
String queryString = "PREFIX : <http://semanticweb.org/sensor#>" + 
        "SELECT ?sensor ?location" + 
        "WHERE {?sensor :hasId \"55"\^^xsd:int." + 
          "?sensor :hasLocation ?location}"; 
Query query = QueryFactory.create(queryString); 
try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) { 
      ResultSet result = qexec.execSelect(); 
      for (; result.hasNext();) { 
        QuerySolution soln = result.nextSolution(); 
        Resource sensor= soln.getResource("sensor"); 
        Resource location = soln.getResource("location"); 
        System.out.println("Sensor" + sensor); 
        System.out.println("Location" + location); 
      } 
} 

回答

1

這與OWL中的枚舉無關。

該查詢僅查找RDF圖中的映射。在你的例子中,一旦你仔細檢查,將生成SPARQL查詢。請注意,您將Java中的字符串連接起來,並且必須使用換行符或空格。您的查詢在SELECT部分​​中的?location變量後缺少,因此它將導致?locationWHERE

解決方案:

添加缺少的空間,即

String queryString = "PREFIX : <http://semanticweb.org/sensor#>" + 
        "SELECT ?sensor ?location " + 
        "WHERE {?sensor :hasId \"55"\^^xsd:int." + 
          "?sensor :hasLocation ?location}"; 

或斷線

String queryString = "PREFIX : <http://semanticweb.org/sensor#>" + 
        "SELECT ?sensor ?location\n" + 
        "WHERE {?sensor :hasId \"55"\^^xsd:int." + 
          "?sensor :hasLocation ?location}"; 
+0

感謝這麼多,有沒有什麼好的教程,你可以推薦 – Ali