简体   繁体   English

如何使用Jena编写SPARQL查询以查找所有类的所有对象中的文字匹配?

[英]How to write SPARQL query to find a literal match in all objects of all classes using Jena?

(1) I have some ontology with the next structure: (1)我对下一个结构有一些本体 在此处输入图片说明 Each individual has a "Data Property" with name " value_of_individual " and literal . 每个人都有一个名为“ value_of_individual ”和文字的“数据属性”。
For example, individualA1 has value_of_individual with literal valueA1 ; 例如,individualA1具有value_of_individual文字 valueA1; individualB2 has value_of_individual with literal valueB2 etc. individualB2文字 valueB2value_of_individual

(2) I want to create next query: find a literal match in all objects of all classes . (2)我想创建下一个查询: 在所有类的所有对象中找到一个文字匹配 If there is a coincidence - return true , if there is no coincidence - return false . 如果有巧合,则返回true ;如果没有巧合,则返回false

(3) I found that I need use ASK query . (3)我发现我需要使用ASK查询 For, example: 例如:

QueryExecution queryExecution = QueryExecutionFactory.create(""
            + "ASK { GRAPH ?g { ?s ?p ?o } }"
            + "", dataset);
    boolean res = queryExecution.execAsk();
    System.out.println("The result is " + res);

(4) My question: (4)我的问题:
How do I write the query described in clause 2 and combine it with the query described in clause 3? 如何编写第2节中所述的查询,并将其与第3节中所述的查询组合?

Edit: 编辑:
I have input word, for example, "MyLiteral". 我输入了单词,例如“ MyLiteral”。 I want to know if there are Individuals in the ClassA, ClassB, ClassC that have a literal as a "MyLiteral" in the data property. 我想知道在ClassA,ClassB,ClassC中是否有在data属性中具有“ MyLiteral”字面值的文字。

(I'm still not sure if I understood your question correctly, especially because you wrote "find a literal match in all objects of all classes" and the "all objects" is confusing...) (我仍然不确定我是否正确理解了您的问题,尤其是因为您编写了“在所有类的所有对象中查找文字匹配项” ,并且“所有对象”令人困惑...)

You have to invert the result of the following query to get the answer to your original question which I simply rewrote as: 您必须将以下查询的结果求逆,以获取原始问题的答案,我将其简单地重写为:

"Is there a class that doesn't contain at least one individual with "MyLiteral" as value of the property :value_of_individual ?" “是否存在一个至少包含至少一个以"MyLiteral"作为属性值的个体:value_of_individual :

PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX : <http://www.semanticweb.org/test-ontology#> 
ASK { 
?cls a owl:Class
FILTER NOT EXISTS {
 ?s a ?cls .
 ?s :value_of_individual "MyLiteral"^^xsd:string
}
}

Update 更新

As per comment from @StansilavKralin , if the question is more about to check whether there is "any class with an individual having the given value" the question would be exactly what @StansilavKralin wrote in his other comment : 根据@StansilavKralin的评论 ,如果问题更多地是要检查是否存在“具有给定值的个人的任何班级”,那么问题将正是@StansilavKralin在他的其他评论中写的:

PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> 
PREFIX : <http://www.semanticweb.org/test-ontology#> 
ASK {
  ?s :value_of_individual "MyLiteral"^^xsd:string
}

Final solution 最终解决方案

PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> 
PREFIX test: <http://www.semanticweb.org/test-ontology#> 
ASK {
 VALUES ?cls {test:ClassA test:ClassB test:ClassC} 
 ?s a ?cls .
 ?s test:value_of_individual "valueC3"^^xsd:string 
}

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

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