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

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

(1) 我有 some ontology with the next structure: 每个人都有一个 "Data Property" 名称为“value_of_individual”和 literal.
例如,individualA1value_of_individualliteral valueA1individualB2value_of_individualliteral valueB2等等

(2) 我想创建下一个查询:在所有 类 的所有对象中查找文字匹配。如果有巧合 - return true,如果没有巧合 - return false

(3) 我发现我需要使用 ASK 查询。例如:

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

(4) 我的问题:
我如何编写第 2 节中描述的查询并将其与第 3 节中描述的查询组合?

编辑:
我输入了单词,例如"MyLiteral"。我想知道 ClassA、ClassB、ClassC 中是否有个人在数据 属性.

中的文字为 "MyLiteral"

(我仍然不确定我是否正确理解了你的问题,特别是因为你写了 "find a literal match in all objects of all classes""all objects" 令人困惑...)

您必须反转以下查询的结果才能得到您原来问题的答案,我只是将其重写为:

"Is there a class that doesn't contain at least one individual with "MyLiteral" as value of the property :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
}
}

更新

根据, 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

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

最终解决方案

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 
}