gpt4 book ai didi

rdf - SPARQL 查询。可选子查询和具有 MIN MAX 值

转载 作者:行者123 更新时间:2023-12-04 16:49:26 31 4
gpt4 key购买 nike

我在为我的数据库进行 SPARQL 查询时遇到了一些问题。

类( Material 组)中有个人( Material )。每种 Material 都有一些特性,如密度、抗拉强度等,但并非所有 Material 都有。

我需要进行查询,在其中输入每个属性的范围值,如果 Material 具有该属性,将我的值范围与 Material 的值范围进行比较。

我用子查询做到了这一点,因为每个属性都可以有更多的值,由空白节点链接,但它不起作用。

我想要这样的结果:

Material |属性标签 |最小值 |最大值

垫01 |抗拉强度 | 250 | 250

垫02 |抗拉强度 | 255 | 280

垫01 |密度 | 4800 | 4900

垫02 |密度 | 5000 | 5010


我的查询是这样的:

 SELECT DISTINCT ?material ?propertyLabel ?minValue ?maxValue WHERE {

?material rdf:type/rdfs:subClassOf* res:Material . #Get all materials

{ OPTIONAL {
SELECT ?material ?propertyLabel (MIN(?value) AS ?minValue) (MAX(?value) AS ?maxValue) WHERE {
?material pro:hasDensity [ unit:hasValue ?value ; unit:hasUnit ?unit ] .
pro:hasDensity rdfs:label ?propertyLabel .
} GROUP BY ?material ?propertyLabel
HAVING ((MAX(?value) > 4500) && (MIN(?value) < 10000))
} }

{ OPTIONAL {
SELECT ?material ?propertyLabel (MIN(?value) AS ?minValue) (MAX(?value) AS ?maxValue) WHERE {
?material pro:hasUltimateTensileStrength [ unit:hasValue ?value ; unit:hasUnit ?unit ] .
pro:hasUltimateTensileStrength rdfs:label ?propertyLabel .
} GROUP BY ?material ?propertyLabel
HAVING ((MAX(?value) > 50) && (MIN(?value) < 300))
} }

# Other properties...

}

附加信息

好的,这里有更清晰的代码和说明...

是的,这个带有 VALUES 的答案现在有效,但是......

它输出的 Material 甚至对一个属性和那个属性都有值(value),但我需要的是满足他们拥有的所有属性的 Material 。

假设我们有 Material :

res:m1
res:m2
res:m3

和 Material 属性:

pro:hasYieldStrenght
pro:hasMeltingPoint
Pro:hasDensity

属性通过空白节点链接到它们的值和单位(对于一个范围)。

Material 1 有一些属性 1,该属性可以有多个值(1、2、3.. 这就是我使用 MIN 和 MAX 的原因),如下所示:

res:m1 pro:hasYieldStrength 
[ unit:hasValue "100"^^xsd:double ; unit:hasUnit unit:MegaPascal ] ,
[ unit:hasValue "133"^^xsd:double ; unit:hasUnit unit:MegaPascal ] ;

有些 Material 没有某些属性(它们在现实世界中是未知的,或者它们不在数据库中)。

现在,我想要搜索 Material 的方式是假设我想要所有 Material :pro:hasDensity 2000 - 3000pro:hasMeltingPoint 250 - 400

所以我需要我的搜索值和数据库中特定属性的最小值和最大值的范围交集。

这就是为什么我有:

HAVING ((MAX(?value) > ?low) && (MIN(?value) < ?high))

如果我在输入上只设置一个属性,所有这些都有效。

但是当我想搜索多个交叉点(属性)时,结果包含每种 Material 的属性,因此,假设 Material 仅满足其中一个属性,则该属性和 Material 将在结果中。

结果,我需要的是满足所有属性如果他们拥有的 Material 。

如果 Material 对某些属性没有值(value)(假设没有人测量它)并且满足所有其他属性,则应在该属性上有或没有空值的结果中考虑它,但如果 Material 甚至不能满足一个属性,则不应在结果中考虑它,不仅是针对该属性,而且一般而言,该 Material 。

这就是为什么我在考虑 OPTIONAL,但我已经放弃了它,因为我花了两天时间发现了那个带有值的 bug:p

这是新代码:

SELECT ?m ?p (MAX(?value) AS ?maxValue) (MIN(?value) AS ?minValue) WHERE {
VALUES (?p ?low ?high)
{
( pro:hasDensity 1 300 )
( pro:hasYieldStrength 30 300 )
( pro:hasMeltingPoint 100 350 )
}
?m ?p [ unit:hasValue ?value ; unit:hasUnit ?unit ]
} GROUP BY ?m ?p ?low ?high
HAVING ((MAX(?value) > ?low) && (MIN(?value) < ?high))

好像它甚至没有考虑密度,因为它们都没有这么低的密度,这是结果:

m   p   maxValue    minValue
#m74653522 #hasYieldStrength 110.0 100.0
#m36767231 #hasYieldStrength 262.0 220.0
#Phosphorus #hasMeltingPoint 317.0 317.0

另一方面,有些 Material 具有那么低的密度,但由于其他属性不满足,因此不考虑它们。 (好像这是我不知道的第一行属性的问题..)

最佳答案

一些数据,请...

在没有看到您的数据的情况下,很难确切地说出问题所在,但听起来您的数据类似于以下内容,其中每种 Material 的几个属性都有多个值。在这种情况下,两种 Material m1m2 分别具有多个属性 p1p2(其中也有标签):

@prefix : <urn:ex:>
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

:p1 rdfs:label "P one" .
:p2 rdfs:label "P two" .

:m1 :p1 10, 12, 14, 16 ;
:p2 90, 100, 110, 120 .
:m2 :p1 11, 13, 15, 17 ;
:p2 95, 105, 115, 125 .

查询

听起来你想为每个属性指定下限和上限,然后为每个属性和一对界限,检索每个实例的界限内的属性的所有值,并记录最大和最小值每种 Material 的属性。以下查询通过在 values block 中指定属性和边界来实现。 (如果您不熟悉值 block ,请参阅附录。)结果如下。

prefix : <urn:ex:>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

select ?material
?pLabel
(min(?value) as ?min)
(max(?value) as ?max)
where {
values (?p ?lowerBound ?upperBound) {
(:p1 12 15)
(:p2 97 116)
}
?material ?p ?value .
?p rdfs:label ?pLabel .
filter ( ?lowerBound <= ?value && ?value <= ?upperBound )
}
group by ?material ?pLabel
order by ?pLabel ?material
----------------------------------
| material | pLabel | min | max |
==================================
| :m1 | "P one" | 12 | 14 |
| :m2 | "P one" | 13 | 15 |
| :m1 | "P two" | 100 | 110 |
| :m2 | "P two" | 105 | 115 |
----------------------------------

附录 A:使用值来简化联合

在 SPARQL 1.1 中,值 block 允许您替换,例如,

{ ?s :p1 :o1 }
union
{ ?s :p2 :o2 }

values (?p ?o) {
(:p1 :o1)
(:p2 :o2)
}
?s ?p ?o

附录 B:如果您不能使用

这是使用联合而不是值的等效查询:

prefix : <urn:ex:>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>

select ?material
?pLabel
(min(?value) as ?min)
(max(?value) as ?max)
where {
{
?material :p1 ?value .
:p1 rdfs:label ?pLabel .
filter ( 12 <= ?value && ?value <= 15 )
}
union
{
?material :p2 ?value .
:p2 rdfs:label ?pLabel .
filter ( 97 <= ?value && ?value <= 116 )
}
}
group by ?material ?pLabel
order by ?pLabel ?material

关于rdf - SPARQL 查询。可选子查询和具有 MIN MAX 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26740682/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com