Quick Start
Here is an example of constructing a PyQL program and then generating an executable SPARQL query.
Question
Was the population growth of Germany higher than that of Japan between 2000 and 2010?
Step-by-step solution of this question
For this question,we need:
Get the population of Germany in 2000
Get the population of Germany in 2010
Calculate the population growth of Germany between 2000 and 2010. It is calculated by subtracting the result of step 2 from that of step 1
Get the population of Japan in 2000
Get the population of Japan in 2010
Calculate the population growth of Japan between 2000 and 2010. It is calculated by subtracting the result of step 5 from that of step 4
Determine whether the population growth of Germany higher than that of Japan between 2000 and 2010. In other words, determine whether the result of step 3 higher than that of step 6
Construction of a PyQL program
First we init a pyql object
a=PyQL()
Then we add an add_quantity function to get the population(P1082)
of Germany(Q183)
in 2000. This population value will be saved in variable x0
.
It is corresponding to the step 1 above.
a.add_quantity('Q183','P1082','x0',2000)
Similarly, add an add_quantity function to get the population(P1082)
of Germany(Q183)
in 2010. This population value will be saved in variable x1
.
It is corresponding to the step 2 above.
a.add_quantity('Q183','P1082','x1',2010)
Next, add a sub function within a bind function to get the population growth of Germany between 2000 and 2010. It will create a calculation of subtracting the population of Germany in 2010 (saved in variable x1
) from the population of Germany in 2000 (saved in variable x0
). The growth value will be saved in variable x4
.
It is corresponding to the step 3 above.
a.add_bind(sub('x1','x0'),'x4')
Similarly, add three functions to get the population growth of Japan(Q17)
between 2000 and 2010. The growth value will be saved in variable x5
.
It is corresponding to the step 4, step 5 and step 6 above.
a.add_quantity('Q17','P1082','x3',2000)
a.add_quantity('Q17','P1082','x2',2010)
a.add_bind(sub('x2','x3'),'x5')
Finally, add a compare function to check whether the population growth of Germany higher than the population growth of Japan. If the population growth of Germany is higher, the return value of the entire query will be 'True'. Otherwise, the return value will be 'False'.
a.add_compare('x4','>','x5')
Ultimately, we constrcut the whole query according to the step-by-step solution above
a=PyQL()
a.add_quantity('Q183','P1082','x0',2010)
a.add_quantity('Q183','P1082','x1',2010)
a.add_bind(sub('x1','x0'),'x4')
a.add_quantity('Q17','P1082','x3',2000)
a.add_quantity('Q17','P1082','x2',2010)
a.add_bind(sub('x2','x3'),'x5')
a.add_compare('x4','>','x5')
SPARQL generated by the program above
SELECT ?answer {
wd:Q183 p:P1082 ?statement_x0.
?statement_x0 psv:P1082 ?value_st_x0.
?value_st_x0 wikibase:quantityAmount ?x0.
?statement_x0 pq:P585 ?time_x0.
FILTER(YEAR(?time_x0) = 2010).
wd:Q183 p:P1082 ?statement_x1.
?statement_x1 psv:P1082 ?value_st_x1.
?value_st_x1 wikibase:quantityAmount ?x1.
?statement_x1 pq:P585 ?time_x1.
FILTER(YEAR(?time_x1) = 2010).
BIND( ((?x1 - ?x0)) AS ?x4 )
wd:Q17 p:P1082 ?statement_x3.
?statement_x3 psv:P1082 ?value_st_x3.
?value_st_x3 wikibase:quantityAmount ?x3.
?statement_x3 pq:P585 ?time_x3.
FILTER(YEAR(?time_x3) = 2000).
wd:Q17 p:P1082 ?statement_x2.
?statement_x2 psv:P1082 ?value_st_x2.
?value_st_x2 wikibase:quantityAmount ?x2.
?statement_x2 pq:P585 ?time_x2.
FILTER(YEAR(?time_x2) = 2010).
BIND( ((?x2 - ?x3)) AS ?x5 )
BIND( (IF(?x4 > ?x5, "TRUE", "FALSE")) AS ?answer )
}
Execution result of this SPARQL query
FALSE
Last updated