使用对象 属性 Sparql Jena 获取文字
get Literal using the object property Sparql Jena
我有两个 sparql 查询:
public static String query2
= "PREFIX diag: <file:/D:/onto/owl_ontologies/diagnostic1.owl#> "
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
+ "PREFIX owl:<http://www.w3.org/2002/07/owl#>"
+ "SELECT ?disease ?symptom"
+ "WHERE { ?disease diag:hasSymptom ?symptom}";
String moreSymptomsQuery
= "PREFIX diag: <http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#> "
+ "SELECT ?symptom"
+ "WHERE {\"hyperglycemia\" diag:hasSymptom ?symptom}";
这是我的 OWL 文件的一部分
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:diag="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#">
<owl:Ontology rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl"/>
<owl:Class rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Symptom"/>
<owl:Class rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Desease"/>
<owl:ObjectProperty rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#hasSymptom">
<rdfs:range rdf:resource="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Symptom"/>
<rdfs:domain rdf:resource="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Desease"/>
</owl:ObjectProperty>
<owl:DatatypeProperty rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#DesId">
<rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
<rdfs:domain rdf:resource="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Desease"/>
</owl:DatatypeProperty>
<owl:DatatypeProperty rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#SympId">
<rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
<rdfs:domain rdf:resource="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Symptom"/>
</owl:DatatypeProperty>
<owl:DatatypeProperty rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#DesLabel">
<rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
<rdfs:domain rdf:resource="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Desease"/>
</owl:DatatypeProperty>
<owl:DatatypeProperty rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#SympLabel">
<rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
<rdfs:domain rdf:resource="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Symptom"/>
</owl:DatatypeProperty>
<!--this is an individual "desease" hasSymptom "Symptom" -->
<diag:Desease rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#hyperglycemia">
<diag:hasSymptom>hypergammaglobulinemia</diag:hasSymptom>
<diag:DesId>DES:000004</diag:DesId>
<diag:DesLabel>hyperglycemia</diag:DesLabel>
</diag:Desease>
这是我在耶拿的代码:
public void SelectQuesry() {
Query query = QueryFactory.create(queryName);
QueryExecution executeQuery = QueryExecutionFactory.create(query, modelDiag);
org.apache.jena.query.ResultSet res = executeQuery.execSelect();
while (res.hasNext()) {
QuerySolution qs = res.nextSolution();
Literal symp = qs.getLiteral("symptom");
System.out.println(symp.toString());
}
}
第一次查询有结果,第二次没有结果!!
我想了解每种疾病的症状...这很重要...感谢您的帮助。
文字从不 RDF 三元组的主题,这实际上意味着三元组模式
"hyperglycemia" diag:hasSymptom ?symptom
在您的第二个查询中没有匹配任何数据。
您必须使用 RDF 资源的 URI,即http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#hyperglycemia
,作为三重模式的主题:
PREFIX diag: <http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#>
SELECT ?symptom
WHERE {
diag:hyperglycemia diag:hasSymptom ?symptom
}
作为评论(我想我上次已经告诉过你):使用 N-Triples 语法而不是 RDF/XML 查看你的数据。这直接反映了 SPARQL 查询中的模式。
此外,在您的数据中,除疾病外,所有内容都是字符串文字。不知道为什么,但如果你真的在建模 ontology 那么我也会使用 RDF 资源来处理症状 - 至少当你想对症状做一些额外的陈述时。
我有两个 sparql 查询:
public static String query2
= "PREFIX diag: <file:/D:/onto/owl_ontologies/diagnostic1.owl#> "
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
+ "PREFIX owl:<http://www.w3.org/2002/07/owl#>"
+ "SELECT ?disease ?symptom"
+ "WHERE { ?disease diag:hasSymptom ?symptom}";
String moreSymptomsQuery
= "PREFIX diag: <http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#> "
+ "SELECT ?symptom"
+ "WHERE {\"hyperglycemia\" diag:hasSymptom ?symptom}";
这是我的 OWL 文件的一部分
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:diag="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#">
<owl:Ontology rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl"/>
<owl:Class rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Symptom"/>
<owl:Class rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Desease"/>
<owl:ObjectProperty rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#hasSymptom">
<rdfs:range rdf:resource="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Symptom"/>
<rdfs:domain rdf:resource="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Desease"/>
</owl:ObjectProperty>
<owl:DatatypeProperty rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#DesId">
<rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
<rdfs:domain rdf:resource="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Desease"/>
</owl:DatatypeProperty>
<owl:DatatypeProperty rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#SympId">
<rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
<rdfs:domain rdf:resource="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Symptom"/>
</owl:DatatypeProperty>
<owl:DatatypeProperty rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#DesLabel">
<rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
<rdfs:domain rdf:resource="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Desease"/>
</owl:DatatypeProperty>
<owl:DatatypeProperty rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#SympLabel">
<rdfs:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
<rdfs:domain rdf:resource="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#Symptom"/>
</owl:DatatypeProperty>
<!--this is an individual "desease" hasSymptom "Symptom" -->
<diag:Desease rdf:about="http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#hyperglycemia">
<diag:hasSymptom>hypergammaglobulinemia</diag:hasSymptom>
<diag:DesId>DES:000004</diag:DesId>
<diag:DesLabel>hyperglycemia</diag:DesLabel>
</diag:Desease>
这是我在耶拿的代码:
public void SelectQuesry() {
Query query = QueryFactory.create(queryName);
QueryExecution executeQuery = QueryExecutionFactory.create(query, modelDiag);
org.apache.jena.query.ResultSet res = executeQuery.execSelect();
while (res.hasNext()) {
QuerySolution qs = res.nextSolution();
Literal symp = qs.getLiteral("symptom");
System.out.println(symp.toString());
}
}
第一次查询有结果,第二次没有结果!! 我想了解每种疾病的症状...这很重要...感谢您的帮助。
文字从不 RDF 三元组的主题,这实际上意味着三元组模式
"hyperglycemia" diag:hasSymptom ?symptom
在您的第二个查询中没有匹配任何数据。
您必须使用 RDF 资源的 URI,即http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#hyperglycemia
,作为三重模式的主题:
PREFIX diag: <http://www.hamzadje28/onto/owl_ontologies/diagnostic.owl#>
SELECT ?symptom
WHERE {
diag:hyperglycemia diag:hasSymptom ?symptom
}
作为评论(我想我上次已经告诉过你):使用 N-Triples 语法而不是 RDF/XML 查看你的数据。这直接反映了 SPARQL 查询中的模式。
此外,在您的数据中,除疾病外,所有内容都是字符串文字。不知道为什么,但如果你真的在建模 ontology 那么我也会使用 RDF 资源来处理症状 - 至少当你想对症状做一些额外的陈述时。