|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Asparagus\Tests\Integration; |
|
4
|
|
|
|
|
5
|
|
|
use Asparagus\QueryBuilder; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @license GNU GPL v2+ |
|
9
|
|
|
* @author Bene* < [email protected] > |
|
10
|
|
|
*/ |
|
11
|
|
|
class WDQSQueryExamplesTest extends \PHPUnit_Framework_TestCase { |
|
12
|
|
|
|
|
13
|
|
|
private static $prefixes = array( |
|
14
|
|
|
'wd' => 'http://www.wikidata.org/entity/', |
|
15
|
|
|
'wdt' => 'http://www.wikidata.org/prop/direct/', |
|
16
|
|
|
'wikibase' => 'http://wikiba.se/ontology#', |
|
17
|
|
|
'p' => 'http://www.wikidata.org/prop/', |
|
18
|
|
|
'v' => 'http://www.wikidata.org/prop/statement/', |
|
19
|
|
|
'q' => 'http://www.wikidata.org/prop/qualifier/', |
|
20
|
|
|
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#', |
|
21
|
|
|
'xsd' => 'http://www.w3.org/2001/XMLSchema#' |
|
22
|
|
|
); |
|
23
|
|
|
|
|
24
|
|
|
public function testUSPresidentsAndSpouses() { |
|
25
|
|
|
$queryBuilder = new QueryBuilder( self::$prefixes ); |
|
26
|
|
|
|
|
27
|
|
|
$queryBuilder->select( '?p', '?w', '?l', '?wl' ) |
|
28
|
|
|
->where( 'wd:Q30', 'p:P6/v:P6', '?p' ) |
|
29
|
|
|
->where( '?p', 'wdt:P26', '?w' ) |
|
30
|
|
|
->optional( |
|
31
|
|
|
$queryBuilder->newSubgraph() |
|
32
|
|
|
->where( '?p', 'rdfs:label', '?l' ) |
|
33
|
|
|
->filter( 'LANG(?l) = "en"' ) |
|
34
|
|
|
) |
|
35
|
|
|
->optional( |
|
36
|
|
|
$queryBuilder->newSubgraph() |
|
37
|
|
|
->where( '?w', 'rdfs:label', '?wl' ) |
|
38
|
|
|
->filter( 'LANG(?wl) = "en"' ) |
|
39
|
|
|
); |
|
40
|
|
|
|
|
41
|
|
|
$this->assertIsExpected( 'US_presidents_and_spouses', $queryBuilder->format() ); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function testPresidentsAndCausesOfDeath() { |
|
45
|
|
|
$queryBuilder = new QueryBuilder( self::$prefixes ); |
|
46
|
|
|
|
|
47
|
|
|
$queryBuilder->select( '?h', '?cause', '?hl', '?causel' ) |
|
48
|
|
|
->where( '?h', 'wdt:P39', 'wd:Q11696' ) |
|
49
|
|
|
->also( 'wdt:P509', '?cause' ) |
|
50
|
|
|
->optional( |
|
51
|
|
|
$queryBuilder->newSubgraph() |
|
52
|
|
|
->where( '?h', 'rdfs:label', '?hl' ) |
|
53
|
|
|
->filter( 'LANG(?hl) = "en"' ) |
|
54
|
|
|
) |
|
55
|
|
|
->optional( |
|
56
|
|
|
$queryBuilder->newSubgraph() |
|
57
|
|
|
->where( '?cause', 'rdfs:label', '?causel' ) |
|
58
|
|
|
->filter( 'LANG(?causel) = "en"' ) |
|
59
|
|
|
); |
|
60
|
|
|
|
|
61
|
|
|
$this->assertIsExpected( 'Presidents_and_causes_of_death', $queryBuilder->format() ); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function testPeopleBornBeforeYear1880WithNoDeathDate() { |
|
65
|
|
|
$queryBuilder = new QueryBuilder( self::$prefixes ); |
|
66
|
|
|
|
|
67
|
|
|
$queryBuilder->select( '?h', '?date' ) |
|
68
|
|
|
->where( '?h', 'wdt:P31', 'wd:Q5' ) |
|
69
|
|
|
->also( 'wdt:P569', '?date' ) |
|
70
|
|
|
->optional( '?h', 'wdt:P570', '?d' ) |
|
71
|
|
|
->filter( '?date < "1880-01-01T00:00:00Z"^^xsd:dateTime' ) |
|
72
|
|
|
->filter( '!BOUND(?d)' ) |
|
73
|
|
|
->limit( 100 ); |
|
74
|
|
|
|
|
75
|
|
|
$this->assertIsExpected( 'People_born_before_year_1880_with_no_death_date', $queryBuilder->format() ); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function testLargestCitiesWithFemaleMayor() { |
|
79
|
|
|
$queryBuilder = new QueryBuilder( self::$prefixes ); |
|
80
|
|
|
|
|
81
|
|
|
$queryBuilder->selectDistinct( '?city', '?citylabel', '?mayorlabel' ) |
|
82
|
|
|
->where( '?city', 'wdt:P31/wdt:P279*', 'wd:Q515' ) |
|
83
|
|
|
->also( 'p:P6', '?statement' ) |
|
84
|
|
|
->also( 'wdt:P1082', '?population' ) |
|
85
|
|
|
->where( '?statement', 'v:P6', '?mayor' ) |
|
86
|
|
|
->where( '?mayor', 'wdt:P21', 'wd:Q6581072' ) |
|
87
|
|
|
->filterNotExists( '?statement', 'q:P582', '?x' ) |
|
88
|
|
|
->optional( |
|
89
|
|
|
$queryBuilder->newSubgraph() |
|
90
|
|
|
->where( '?city', 'rdfs:label', '?citylabel' ) |
|
91
|
|
|
->filter( 'LANG(?citylabel) = "en"' ) |
|
92
|
|
|
) |
|
93
|
|
|
->optional( |
|
94
|
|
|
$queryBuilder->newSubgraph() |
|
95
|
|
|
->where( '?mayor', 'rdfs:label', '?mayorlabel' ) |
|
96
|
|
|
->filter( 'LANG(?mayorlabel) = "en"' ) |
|
97
|
|
|
) |
|
98
|
|
|
->orderBy( '?population', 'DESC' ) |
|
99
|
|
|
->limit( 10 ); |
|
100
|
|
|
|
|
101
|
|
|
$this->assertIsExpected( 'Largest_cities_with_female_mayor', $queryBuilder->format() ); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function testListOfCountriesOrderedByTheNumberOfTheirCitiesWithFemaleMayor() { |
|
105
|
|
|
$queryBuilder = new QueryBuilder( self::$prefixes ); |
|
106
|
|
|
|
|
107
|
|
|
$queryBuilder->select( '?country', '?label', '(COUNT(*) AS ?COUNT)' ) |
|
108
|
|
|
->where( '?city', 'wdt:P31/wdt:P279*', 'wd:Q515' ) |
|
109
|
|
|
->also( 'p:P6', '?statement' ) |
|
110
|
|
|
->also( 'wdt:P17', '?country' ) |
|
111
|
|
|
->where( '?statement', 'v:P6', '?mayor' ) |
|
112
|
|
|
->where( '?mayor', 'wdt:P21', 'wd:Q6581072' ) |
|
113
|
|
|
->filterNotExists( '?statement', 'q:P582', '?x' ) |
|
114
|
|
|
->optional( |
|
115
|
|
|
$queryBuilder->newSubgraph() |
|
116
|
|
|
->where( '?country', 'rdfs:label', '?label' ) |
|
117
|
|
|
->filter( 'LANG(?label) = "en"' ) |
|
118
|
|
|
) |
|
119
|
|
|
->groupBy( '?country', '?label' ) |
|
120
|
|
|
->orderBy( '?COUNT', 'DESC' ) |
|
121
|
|
|
->limit( 100 ); |
|
122
|
|
|
|
|
123
|
|
|
$this->assertIsExpected( 'List_of_countries_ordered_by_the_number_of_their_cities_with_female_mayor', $queryBuilder->format() ); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function testsHowManyStatesThisUSStateBorders() { |
|
127
|
|
|
$queryBuilder = new QueryBuilder( self::$prefixes ); |
|
128
|
|
|
|
|
129
|
|
|
$queryBuilder->select( '?state', '?stateL', '?borders' ) |
|
130
|
|
|
->subquery( |
|
131
|
|
|
$queryBuilder->newSubquery() |
|
132
|
|
|
->select( '?state', '(COUNT (?otherState) AS ?borders)' ) |
|
133
|
|
|
->where( '?state', 'wdt:P31', 'wd:Q35657' ) |
|
134
|
|
|
->where( '?otherState', 'wdt:P47', '?state' ) |
|
135
|
|
|
->also( 'wdt:P31', 'wd:Q35657' ) |
|
136
|
|
|
->groupBy( '?state' ) |
|
137
|
|
|
) |
|
138
|
|
|
->optional( |
|
139
|
|
|
$queryBuilder->newSubgraph() |
|
140
|
|
|
->where( '?state', 'rdfs:label', '?stateL' ) |
|
141
|
|
|
->filter( 'LANG(?stateL) = "en"' ) |
|
142
|
|
|
) |
|
143
|
|
|
->orderBy( '?borders', 'DESC' ); |
|
144
|
|
|
|
|
145
|
|
|
$this->assertIsExpected( 'How_many_states_this_US_state_borders', $queryBuilder->format() ); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function testWhoseBirthdayIsToday() { |
|
149
|
|
|
$queryBuilder = new QueryBuilder( self::$prefixes ); |
|
150
|
|
|
|
|
151
|
|
|
$queryBuilder->select( '?entity', '(YEAR(?date) AS ?YEAR)' ) |
|
152
|
|
|
->where( '?entityS', 'wdt:P569', '?date' ) |
|
153
|
|
|
->also( 'rdfs:label', '?entity' ) |
|
154
|
|
|
->filter( 'DATATYPE (?date) = xsd:dateTime' ) |
|
155
|
|
|
->filter( 'MONTH (?date) = MONTH (NOW ())' ) |
|
156
|
|
|
->filter( 'DAY (?date) = DAY (NOW ())' ) |
|
157
|
|
|
->limit( 10 ); |
|
158
|
|
|
|
|
159
|
|
|
$this->assertIsExpected( 'Whose_birthday_is_today', $queryBuilder->format() ); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
public function testWhoDiscoveredTheMostAsteroids() { |
|
163
|
|
|
$queryBuilder = new QueryBuilder( self::$prefixes ); |
|
164
|
|
|
|
|
165
|
|
|
$queryBuilder->select( '?discoverer', '?name', '(COUNT (?asteroid) AS ?count)' ) |
|
166
|
|
|
->where( '?asteroid', 'wdt:P31', 'wd:Q3863' ) |
|
167
|
|
|
->also( 'wdt:P61', '?discoverer' ) |
|
168
|
|
|
->optional( |
|
169
|
|
|
$queryBuilder->newSubgraph() |
|
170
|
|
|
->where( '?discoverer', 'rdfs:label', '?name' ) |
|
171
|
|
|
->filter( 'LANG(?name) = "en"' ) |
|
172
|
|
|
) |
|
173
|
|
|
->groupBy( '?discoverer', '?name' ) |
|
174
|
|
|
->orderBy( '?count', 'DESC' ) |
|
175
|
|
|
->limit( 10 ); |
|
176
|
|
|
|
|
177
|
|
|
$this->assertIsExpected( 'Who_discovered_the_most_asteroids', $queryBuilder->format() ); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
public function testWhoDiscoveredTheMostPlanets() { |
|
181
|
|
|
$queryBuilder = new QueryBuilder( self::$prefixes ); |
|
182
|
|
|
|
|
183
|
|
|
$queryBuilder->select( '?discoverer', '?name', '(COUNT (DISTINCT ?planet) AS ?count)' ) |
|
184
|
|
|
->where( '?ppart', 'wdt:P279*', 'wd:Q634' ) |
|
185
|
|
|
->where( '?planet', 'wdt:P31', '?ppart' ) |
|
186
|
|
|
->also( 'wdt:P61', '?discoverer' ) |
|
187
|
|
|
->optional( |
|
188
|
|
|
$queryBuilder->newSubgraph() |
|
189
|
|
|
->where( '?discoverer', 'rdfs:label', '?name' ) |
|
190
|
|
|
->filter( 'LANG(?name) = "en"' ) |
|
191
|
|
|
) |
|
192
|
|
|
->groupBy( '?discoverer', '?name' ) |
|
193
|
|
|
->orderBy( '?count', 'DESC' ) |
|
194
|
|
|
->limit( 10 ); |
|
195
|
|
|
|
|
196
|
|
|
$this->assertIsExpected( 'Who_discovered_the_most_planets', $queryBuilder->format() ); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public function testAmericanUniversitiesFoundedBeforeTheStatesTheyResideInWereCreated() { |
|
200
|
|
|
$queryBuilder = new QueryBuilder( self::$prefixes ); |
|
201
|
|
|
|
|
202
|
|
|
$queryBuilder->select( '?uniName', '?founded', '?stateName', '?stateStart' ) |
|
203
|
|
|
->where( '?uni', 'wdt:P31|wdt:P279/wdt:P31', 'wd:Q3918' ) |
|
204
|
|
|
->also( 'wdt:P131+', '?state' ) |
|
205
|
|
|
->also( 'wdt:P571', '?founded' ) |
|
206
|
|
|
->where( '?state', 'wdt:P31', 'wd:Q35657' ) |
|
207
|
|
|
->also( 'wdt:P571', '?stateStart' ) |
|
208
|
|
|
->filter( '?founded < ?stateStart' ) |
|
209
|
|
|
->optional( |
|
210
|
|
|
$queryBuilder->newSubgraph() |
|
211
|
|
|
->where( '?state', 'rdfs:label', '?stateName' ) |
|
212
|
|
|
->filter( 'LANG(?stateName) = "en"' ) |
|
213
|
|
|
) |
|
214
|
|
|
->optional( |
|
215
|
|
|
$queryBuilder->newSubgraph() |
|
216
|
|
|
->where( '?uni', 'rdfs:label', '?uniName' ) |
|
217
|
|
|
->filter( 'LANG(?uniName) = "en"' ) |
|
218
|
|
|
) |
|
219
|
|
|
->limit( 10 ); |
|
220
|
|
|
|
|
221
|
|
|
$this->assertIsExpected( 'American_universities_founded_before_the_states_they_reside_in_were_created', $queryBuilder->format() ); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
public function testWhatIsTheRelationBetweenTerrellBuckleyAndMiamiDolphins() { |
|
225
|
|
|
$queryBuilder = new QueryBuilder( self::$prefixes ); |
|
226
|
|
|
|
|
227
|
|
|
$queryBuilder->select( '?l' ) |
|
228
|
|
|
->where( 'wd:Q5571382', '?p', 'wd:Q223243' ) |
|
229
|
|
|
->where( '?property', '?ref', '?p' ) |
|
230
|
|
|
->also( 'a', 'wikibase:Property' ) |
|
231
|
|
|
->also( 'rdfs:label', '?l' ) |
|
232
|
|
|
->filter( 'LANG(?l) = "en"' ) |
|
233
|
|
|
->limit( 10 ); |
|
234
|
|
|
|
|
235
|
|
|
$this->assertIsExpected( 'What_is_the_relation_between_Terrell_Buckley_and_Miami_Dolphins', $queryBuilder->format() ); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
public function testAliasesOfPropertiesWhichAreUsedMoreThanOnce() { |
|
239
|
|
|
$queryBuilder = new QueryBuilder( self::$prefixes + array( |
|
240
|
|
|
'skos' => 'http://www.w3.org/2004/02/skos/core#' |
|
241
|
|
|
) ); |
|
242
|
|
|
|
|
243
|
|
|
$queryBuilder->select( '?property', '?alias', '?occurences' ) |
|
244
|
|
|
->subquery( |
|
245
|
|
|
$queryBuilder->newSubquery() |
|
246
|
|
|
->select( '?alias', '(COUNT (?alias) AS ?occurences)' ) |
|
247
|
|
|
->where( '?tmp', 'a', 'wikibase:Property' ) |
|
248
|
|
|
->also( 'skos:altLabel', '?alias' ) |
|
249
|
|
|
->filter( 'LANG (?alias) = "en"' ) |
|
250
|
|
|
->groupBy( '?alias' ) |
|
251
|
|
|
) |
|
252
|
|
|
->where( '?property', 'a', 'wikibase:Property' ) |
|
253
|
|
|
->also( 'skos:altLabel', '?alias' ) |
|
254
|
|
|
->filter( '?occurences > 1' ) |
|
255
|
|
|
->orderBy( '?alias' ); |
|
256
|
|
|
|
|
257
|
|
|
$this->assertIsExpected( 'Aliases_of_properties_which_are_used_more_than_once', $queryBuilder->format() ); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
private function assertIsExpected( $name, $sparql ) { |
|
261
|
|
|
$expected = file_get_contents( __DIR__ . '/../data/wdqs_' . $name . '.rq' ); |
|
262
|
|
|
|
|
263
|
|
|
$this->assertEquals( $expected, $sparql, 'Query didn\'t match the expected content of integration_' . $name . '.rq' ); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
} |
|
267
|
|
|
|