根据查询结果创建新的三联。

问题描述 投票:0回答:1

[![在此输入图片描述][1]][1][1]。https:/i.stack.imgur.comAG7MM.png

给出以下三点

我想得到一个结果:CRR1 :hasPart :part1:CRR2 :hasPart :part3。

我目前已经尝试了下面的查询,但我没有得到预期的结果,我试图在两个查询的UNION上放一个过滤器FILTER (?part = ?part2),但没有得到我需要的响应,我在这里做错了什么?

我使用查询的唯一目的是找到缺失的连接,就像在这种情况下,如果CRR1和CRR2通过Part2连接,那么我想创建一个新的CRR,连接Part1和Part3,取决于谁是DOB,在这个例子中是CRR1。

:crr1 a :XObject .
:crr2 a :XObject .
:crr3 a :XObject .
:crr4 a :XObject .

:part1 a :Part
:part2 a :Part
:part3 a :Part
:part4 a :Part
:part5 a :Part

:crr1 :hasPart :part1 .
:crr1 :hasPart :part2 .
:crr2 :hasPart :part2 .
:crr2 :hasPart :part3 .
:crr3 :hasPart :part3 .
:crr3 :hasPart :part4 .
:crr4 :hasPart :part4 .
:crr4 :hasPart :part5 .

:crr1 :hasType :DOB .



PREFIX : <http://example.com/test#> 
construct {

?o :hasPart ?part .
:crr2 :hasPart ?part2 .
}
where
{
    {
  ?o :hasPart ?part
  {
    select DISTINCT ?o
      where {
      :crr2 (:hasPart/^:hasPart)+ ?o
      FILTER (:crr2 != ?o)
      }
  }
  FILTER EXISTS {?o :hasType :DOB .}
    }
    UNION
    {
        :crr2 :hasPart ?part2 .
    }
 FILTER (?part = ?part2)
}



sparql rdf jena ontology
1个回答
1
投票

我不确定我是否正确理解了你的问题,但让我试着回答一下。

如果你想在:crr1和:part3之间构造一个三联,正如你的问题的第3段所暗示的那样,那么你需要这样的东西。

PREFIX : <http://example.com/test#>

CONSTRUCT {?dob :hasPart ?part}
WHERE {
?dob :hasType :DOB .
?dob :hasPart ?p .
?otherCRR :hasPart ?p.
FILTER(?dob != ?otherCRR)
#The above establishes whether the DOB CRR and another one are connected via any part
?otherCRR :hasPart ?part .
FILTER NOT EXISTS {?dob :hasPart ?part}
#This instead makes sure that there isn't already a connection between the DOB and the part in question.
}

如果你想寻找的是 :crr2中没有与其他CRR共享的部分 在这些部分中存在着一些连接 而其他CRR是一个DOB, 就像你问题的第一段所暗示的那样, 那么你需要这样的东西:

PREFIX : <http://example.com/test#>

CONSTRUCT {
?o :hasPart ?part .
:crr2 :hasPart ?part2 .
}
WHERE{
?o :hasPart ?sharedPart, ?part .
?o :hasType :DOB .
FILTER(?sharedPart != ?part)
:crr2 :hasPart ?sharedPart, ?part2 .
FILTER NOT EXISTS {?o :hasPart ?part2}
FILTER NOT EXISTS {:crr2 :hasPart ?part}
}

注意,在你的过滤器中,你说?part和?part2必须是相同的,所以你不能让它们分别作为:part1和:part3,在你返回的三组中。

你可能还感兴趣的是,你可以通过插入查询直接将你的三对组合插入到图中。只需用insert替换constuct即可。

© www.soinside.com 2019 - 2024. All rights reserved.