"和"在sparql中执行时间

时间:2014-12-03 10:08:39

标签: rdf sparql

我必须检查有关所述人口的数据集的一致性; L级所有行政单位A的人口总和必须等于所有行政单位A所属的L + 1级行政单位B的人口。这只能在一个查询中。我尝试过以下代码,但结果一无所获(条件似乎没有效果)。我想没有结果因为"总和"时间执行。我该怎么做才能解决这个问题?

prefix ns:   <http://geo.linkedopendata.gr/gag/ontology/>

SELECT ?condition WHERE {
  ?x  ns:has_official_name  ?decentralized . 
  ?x  ns:has_population  ?decentralized_population . 
  ?y  ns:has_official_name  ?region . 
  ?y  ns:has_population  ?region_population . 
  ?z  ns:has_official_name  ?regional_unit . 
  ?z  ns:has_population  ?regional_unit_population .
  ?z  ns:belongs_to  ?y . 
  ?y  ns:belongs_to  ?x . 
  FILTER regex(str(?decentralized), \"DECENTRALIZED\") .
  FILTER regex(str(?region), \"REGION\") .
  FILTER regex(str(?regional_unit), \"REGIONAL UNIT\") .
  FILTER (!regex(str(?region), \"REGIONAL\")) . 
  BIND(SUM(?decentralized_population) AS ?sum_decentralized_population) .
  BIND(SUM(?region_population) AS ?sum_region_population) .
  BIND(SUM(?regional_unit_population) AS ?sum_regional_unit_population) .
  OPTIONAL {
    BIND( IF(?sum_decentralized_population = ?sum_region_population &&
             ?sum_region_population = ?sum_regional_unit_population, 'TRUE', 'FALSE')
           AS ?condition) .
  }
}
GROUP BY ?condition

@Joshua,假设我们在L级有7个单位。这些是:

答:人口:100

B:人口:150

C:人口:300

D:人口:200

E:人口:250

F:人口:150

G:人口:150

在L + 1级,我们有13个单位,其中:

unit_1:人口30,属于A

unit_2:人口70,属于A

unit_3:人口80,属于B

unit_4:人口70,属于B

unit_5:人口200,属于C

unit_6:人口100,属于C

unit_7:人口130,属于D

unit_8:人口70,属于D

unit_9:人口180,属于E

unit_10:人口70,属于E

unit_11:人口150,属于F

unit_12:人口100,属于G

unit_13:人口50,属于G

在L + 2级,我们有74个单位,其中:

unit_a:人口15,属于unit_1

unit_b:人口5,属于unit_1

unit_c:人口3,属于unit_1

unit_d:人口7,属于unit_1

unit_e:人口40,属于unit_1

unit_f:人口20,属于unit_1

unit_g:人口10,属于unit_1

等......(人口数量不是真实的)

现在,我必须检查单位A(L级为100)的人口是否等于:

  1. unit_1和unit_2的人口总和(L + 1级为30 + 70)

  2. unit_a,unit_b,unit_c,unit_d,unit_e,unit_f和unit_g(15 + 5 + 3 + 7 + 40 + 20 + 10等级L + 2)的总和

  3. .....................

    1. L + 5级所有单位的总人口数
    2. 我在eclipse中使用sesame,我的查询是:

      String queryStringAndy =&#34; PREFIX rdf:http://www.w3.org/1999/02/22-rdf-syntax-ns#&#34; +                          &#34; PREFIX gag:http://geo.linkedopendata.gr/gag/ontology/&#34; +

      &#34; SELECT DISTINCT?dec_pop(SUM(?reg_pop)AS?sum_reg_pop)(SUM(?reg_unit_pop)AS?sum_reg_unit_pop)(SUM(?mun_pop)AS?sum_mun_pop)&#34; +

      &#34;在哪里{?x rdf:type gag:Decentralized_Unit。&#34; +

      &#34; ?x gag:has_official_name?分散。 &#34; +

      &#34; ?x gag:has_population?dec_pop。 &#34; +

      &#34; ?rdf:type gag:Region。&#34; +

      &#34; ?gag:has_official_name?region。 &#34; +

      &#34; ?gag:has_population?reg_pop。 &#34; +

      &#34; ?z rdf:type gag:Regional_Unit。&#34; +

      &#34; ?z gag:has_official_name?regional_unit。 &#34; +

      &#34; ?z gag:has_population?reg_unit_pop。 &#34; +

      &#34; ?wddf:type gag:Municipality。&#34; +

      &#34; ?w gag:has_official_name?municipality。 &#34; +

      &#34; ?w gag:has_population?mun_pop。 &#34; +

      &#34; ?gag:belongs_to?x。&#34; +

      &#34; ?z gag:belongs_to?y。&#34; +

      &#34; ?w gag:belongs_to?z。}&#34; +

      &#34; GROUP BY?dec_pop&#34; ;

      dec_pop:L级单位的数量

      reg_pop:L + 1级单位的数量

      reg_unit_pop:L + 2级单位的数量

      mun_pop:L + 3级单位的人口

      奇怪的是,当我必须将?dec_pop与一个和表达式进行比较时,结果是正确的。但是当我输入更多的金额时,只有最后一个计算级别的单位总和等于?dec_pop。我希望现在更清楚了。

1 个答案:

答案 0 :(得分:3)

您写的查询不合法SPARQL(例如,您不应该逃避引号)并且您没有向我们展示数据,因此很难说您的查询中实际出现了什么问题。但是,您尝试执行的操作在SPARQL中是可以实现的。例如,假设你已经得到了这些数据,其中的东西可能有孩子,事情可能有重要性:

@prefix : <urn:ex:>

:a :count 23 ;
   :hasChild :b, :c .

:b :count 10 .

:c :count 13 ;
   :hasChild :d, :e .

:d :count 6 .
:e :count 6 .

现在,首先要检索所需的数据。您可以使用以下查询检索带有子项的每个节点,其声明的计数以及子项计数的总和:

prefix : <urn:ex:>

select ?x ?count (sum(?_subcount) as ?subcount) where {
  ?x :count ?count ;
     :hasChild/:count ?_subcount
}
group by ?x ?count
-------------------------
| x  | count | subcount |
=========================
| :a | 23    | 23       |
| :c | 13    | 12       |
-------------------------

声明的值:a是正确的,但是:c是错误的。您可以通过拥有

进行过滤来选择不一致的值
prefix : <urn:ex:>

select ?x ?count (sum(?_subcount) as ?subcount) where {
  ?x :count ?count ;
     :hasChild/:count ?_subcount
}
group by ?x ?count
having (?count != ?subcount)
-------------------------
| x  | count | subcount |
=========================
| :c | 13    | 12       |
-------------------------

将此示例更新为您的实际数据应该不会太难。

相关问题