2017-02-15 9 views
1

이 올빼미 모델을 만들었습니다. 센서와 위치의 두 클래스가 있으며 위치는 열거 된 클래스입니다.SPARQL 쿼리 인쇄 JENA를 사용하여 클래스 열거 JA

: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 ; . 

각 센서의 위치를 ​​ID 번호로 지정하려고합니다. Protege에서 내 쿼리를 작성했는데 정상적으로 작동합니다. 그러나 JENA에서는 위치에 대해 null을 인쇄합니다. 자원을 사용하여 센서를 인쇄했지만 위치는 null을 인쇄합니다. 나는 그 위치를 인쇄하는 properer 방법을 이해할 수 없었다.

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에서 String을 연결하면 줄 바꿈 또는 공백을 사용해야합니다. 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