SPARQL 查询打印枚举 Class 与 JENA JAVA
SPARQL Query Printing Enumerate Class with JENA JAVA
我做了这个 owl 模型。有两个classes Sensor和Location,枚举Locationclass.
: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 来打印传感器,但对于它打印 null 的位置。我想不出打印位置的正确方法。
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 中连接字符串,并且必须使用换行符或 spaces。您的查询在 SELECT 部分的 ?location
变量之后都丢失了,因此,它将导致 ?locationWHERE
.
解决方案:
添加缺失的 space,即
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}";
我做了这个 owl 模型。有两个classes Sensor和Location,枚举Locationclass.
: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 来打印传感器,但对于它打印 null 的位置。我想不出打印位置的正确方法。
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 中连接字符串,并且必须使用换行符或 spaces。您的查询在 SELECT 部分的 ?location
变量之后都丢失了,因此,它将导致 ?locationWHERE
.
解决方案:
添加缺失的 space,即
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}";