使用JENA JAVA的SPARQL查询打印枚举类
我做了这个猫头鹰模型。传感器和位置有两类,位置是枚举类使用JENA JAVA的SPARQL查询打印枚举类,sparql,jena,semantic-web,ontology,protege,Sparql,Jena,Semantic Web,Ontology,Protege,我做了这个猫头鹰模型。传感器和位置有两类,位置是枚举类 :Sensor rdf:type owl:Class; :Location rdf:type owl:Class ; owl:equivalentClass [ rdf:type owl:Class ; owl:oneOf ( :Bathroom
: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上,它为该位置打印空值。我使用资源打印传感器,但对于位置,它打印空值。我想不出打印位置的合适方法
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);
}
}
String file=“C:/users/src/data.ttl”;
Model Model=FileManager.get().loadModel(文件);
字符串queryString=“前缀:”+
“选择传感器位置”+
其中{?传感器:hasId\“55”\^^xsd:int+
“?传感器:位置?位置}”;
Query=QueryFactory.create(queryString);
try(QueryExecution qexec=QueryExecutionFactory.create(查询,模型)){
ResultSet result=qexec.execSelect();
对于(;result.hasNext();){
QuerySolution soln=result.nextSolution();
资源传感器=soln.getResource(“传感器”);
资源位置=soln.getResource(“位置”);
系统输出打印项次(“传感器”+传感器);
系统输出打印项次(“位置”+位置);
}
}
与OWL中的枚举无关
查询只是在RDF图中查找映射。在您的示例中,只要仔细检查SPARQL查询是如何生成的,它就会起作用。注意,在Java中连接字符串,必须使用换行符或空格。您的查询在SELECT part中的?location
变量之后都丢失,因此,它将导致?locationWHERE
解决方案:
添加缺少的空格,即
String queryString=“前缀:”+
“选择传感器位置”+
其中{?传感器:hasId\“55”\^^xsd:int+
“?传感器:位置?位置}”;
还是换行
String queryString=“前缀:”+
“选择传感器位置\n”+
其中{?传感器:hasId\“55”\^^xsd:int+
“?传感器:位置?位置}”;
非常感谢,有什么好的教程可以推荐吗