繁体   English   中英

使用 JENA JAVA 进行 SPARQL 查询打印枚举类

[英]SPARQL Query Printing Enumerate Class with JENA JAVA

我做了这个猫头鹰模型。 有 Sensor 和 Location 两个类,Location 是枚举类。

: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。 我使用Resource打印传感器,但它打印的位置为空。 我无法弄清楚打印位置的正确方法。

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);
          }
}

这与 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}";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM