英文:
SPARQL query to get an object that is shared (connected to) by two subjects
问题
以下是翻译好的内容:
我有以下图表
```sparql
@prefix : <http://example.org/> .
:JupiterAtmsphere :isComposedOf :Nitrogen.
:JupiterAtmsphere :isComposedOf :Helium.
:EarthAtmosphere :isComposedOf :Nitrogen.
:comet_1 :hasDiscoverer :JohnSmith.
:comet_5 :hasDiscoverer :JohnSmith.
:comet_3 :hasDiscoverer :JaneLee.
我想返回所有由多个主体共享的谓词和对象。该查询试图回答以下问题:哪些“主体”共享了“对象”?我希望查询返回:Nitrogen作为由:JupiterAtmsphere和:EarthAtmsphere共享的对象,以及:comet_1和:comet_5共享的对象:JohnSmith。然而,挑战在于我希望查询是通用的(没有关于图表的主体、谓词或对象的先验知识)。换句话说,我正在设计一个通用的查询,可以应用于不同的本体,而不考虑它们的词汇。
我尝试了以下查询,但没有产生期望的结果:
SELECT ?class1 ?class2 ?p1 ?o
WHERE {
?class1 ?p1 ?o.
?class2 ?p2 ?o.
FILTER (?p1 = ?p2)
<details>
<summary>英文:</summary>
I have the following graph
@prefix : <http://example.org/> .
:JupiterAtmsphere :isComposedOf :Nitrogen.
:JupiterAtmsphere :isComposedOf :Helium.
:EarthAtmosphere :isComposedOf :Nitrogen.
:comet_1 :hasDiscoverer :JohnSmith.
:comet_5 :hasDiscoverer :JohnSmith.
:comet_3 :hasDiscoverer :JaneLee.
I want to return all predicates and objects that are shared by a number of subjects. The query is trying to answer the following question: Which 'subjects' share 'object' in common?. I'd like the query to return :Nitrogen as an object that is shared by both :JupiterAtmspher and :EarthAtmspher, and :JohnSmith as an object that is shared by :comet_1 and :comet_5
However, the challenge is that I want the query to be generic (without prior knowledge of the subjects, predicates or objects of the graph). In other words, I'm desinging a query that is generic and can be applied to different ontologies regardless of their vocabulary.
I tried the following query which did not yeild the desired result:
SELECT ?class1 ?class2 ?p1 ?o
WHERE {
?class1 ?p1 ?o.
?class2 ?p2 ?o.
FILTER (?p1 = ?p2)
</details>
# 答案1
**得分**: 0
你必须声明主语必须不同。您可以在`FILTER`中使用`!=`运算符来实现:
```sparql
SELECT ?s1 ?s2 ?p1 ?o
WHERE {
?s1 ?p1 ?o .
?s2 ?p2 ?o .
FILTER (?s1 != ?s2) .
} ORDER BY ?o ?p1
如果谓词需要相同,将?p1
和?p2
替换为?p
。
(注意:此查询找到至少两个主语共享的对象。)
使用GROUP_CONCAT
,您可以获得每个对象+谓词的一个结果解决方案(例如,带有逗号分隔的主语列表)。
英文:
You have to state that the subjects need to be different. You can do this with the !=
operator in a FILTER
:
SELECT ?s1 ?s2 ?p1 ?o
WHERE {
?s1 ?p1 ?o .
?s2 ?p2 ?o .
FILTER (?s1 != ?s2) .
} ORDER BY ?o ?p1
If the predicates need to be the same, replace ?p1
and ?p2
with ?p
.
(Note: This query finds objects that are shared by at least two subjects.)
With GROUP_CONCAT
, you could get one result solution per object+predicate (e.g., with a comma-separated list of subjects).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论