-
Notifications
You must be signed in to change notification settings - Fork 6
/
WDQ.php
363 lines (334 loc) · 11.6 KB
/
WDQ.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<?php
require_once __DIR__.'/vendor/autoload.php';
use ParserGenerator\SyntaxTreeNode;
use ParserGenerator\Parser;
use Sparql\AndClause;
use Sparql\ExactItem;
use Sparql\Qualifiers;
use Sparql\Subquery;
use Sparql\Item;
use Sparql\Union;
use Sparql\Claim;
use Sparql\NoClaim;
use Sparql\StringLiteral;
use Sparql\Between;
use Sparql\Quantity;
use Sparql\Tree;
use Sparql\Unknown;
use Sparql\AnyItem;
use Sparql\Link;
use Sparql\NoLink;
use Sparql\GeoAround;
/**
* Main WDQ parser class
*/
class WDQParser {
private $grammar = <<<ENDG
start :=> Expression .
Expression
:=> ExpressionPart
:=> Expression "AND" ExpressionPart
:=> Expression "OR" ExpressionPart
.
ExpressionPart
:=> Clause
:=> "(" Expression ")" .
Clause :=> ( Claim | NoClaim | String | Between | Quantity | Tree | Web | Link | NoLink | Around | Items ) .
Claim :=> "CLAIM[" Propvalue+"," "]" ( "{" Expression "}" )?.
NoClaim :=> "NOCLAIM[" Propvalue+"," "]" .
Propvalue :=> Number ( ":" Item )? .
Item :=> (Number | "(" Expression ")" ) .
String :=> "STRING[" Number ":" LiteralString+"," "]" ( "{" Expression "}" )?.
Between :=> "BETWEEN[" Number "," BetweenParams "]" ( "{" Expression "}" )?.
BetweenParams
:=> Date
:=> "," Date
:=> Date "," Date .
Around :=> "AROUND[" Number "," Float "," Float "," Float "]" .
Quantity :=> "QUANTITY[" Number ":" Number ("," Number)? "]" ( "{" Expression "}" )?.
Tree :=> "TREE[" Item+"," "][" PropList? "]" ("[" PropList? "]")? .
Web :=> "WEB[" Item+"," "][" PropList? "]" .
Link :=> "LINK[" /\w+/ "]" .
NoLink :=> "NOLINK[" /\w+/ "]" .
Items :=> "ITEMS[" Number+"," "]" .
PropList :=> Number+"," .
Number :=> /\d+/ .
LiteralString :=> /"[^"]*?"/ .
Date :=> /[+-]?\d+(-\d\d?(-\d\d?(T\d{2}:\d{2}:\d{2}Z)?)?)?/ .
Float :=> /\d+(\.\d+)?/
ENDG;
/**
* Sub-items counter
* @var int
*/
private $counter = 0;
public function __construct()
{
$this->parser = new Parser($this->grammar,
array("ignoreWhitespaces" => true, 'caseInsensitive' => true)
);
}
protected function generateItem(SyntaxTreeNode\Branch $item) {
$sub = $item->getSubnode(0);
$left = $sub->getSubnode(0);
if($left && !$left->isBranch() && $left->getContent() == "(") {
// we've got subquery
$subvarName = "?sub".$this->counter++;
$subexp = $this->generate($sub->getSubnode(1), $subvarName);
return new Subquery($subvarName, $subexp);
} else {
// simple item
$itemno = $item->getLeftLeaf()->getContent();
if($itemno == '4294967294') {
// Unknown item - special handling
$subvarName = "?unk".$this->counter++;
return new Unknown($subvarName);
}
return new Item($itemno);
}
}
protected function getQualifiers(SyntaxTreeNode\Branch $tree, $itemName, $position, &$qname)
{
if($tree->getSubnode($position)->isBranch()) {
$qname = $itemName."_st".$this->counter++;
return $this->generate($tree->getSubnode($position)->getSubnode(1), $qname);
}
return null;
}
/**
* @param SyntaxTreeNode\Branch $tree
* @param $itemName
* @return \Sparql\Expression
* @throws Exception
*/
public function generate(SyntaxTreeNode\Branch $tree, $itemName)
{
$res = "";
if(!$tree) {
throw new Exception("Tree missing!");
}
if(!$tree->isBranch()) {
// FIXME: this should never happen
var_dump($tree);
throw new Exception("Unexpected leaf!");
}
switch(strtolower($tree->getType())) {
case 'clause':
case 'start':
return $this->generate($tree->getSubnode(0), $itemName);
case 'expressionpart':
if(!$tree->getSubnode(0)->isBranch()) {
// ( case
return $this->generate($tree->getSubnode(1), $itemName);
}
return $this->generate($tree->getSubnode(0), $itemName);
case 'expression':
$op = $tree->getSubnode(1);
if($op && !$op->isBranch()) {
$left = $this->generate($tree->getSubnode(0), $itemName);
$right = $this->generate($tree->getSubnode(2), $itemName);
if( strtoupper($op->getContent()) == "OR") {
return Union::addTwo($left, $right);
} else {
// AND
return AndClause::addTwo($left, $right);
}
}
foreach($tree->getSubnodes() as $subnode) {
if(!$subnode->isBranch()) {
continue;
}
return $this->generate($subnode, $itemName);
}
break;
case 'claim':
$items = array();
$qualifiers = $this->getQualifiers($tree, $itemName, 3, $qname );
foreach($tree->getSubnode(1)->findAll('Propvalue') as $prop) {
$pid = $prop->getSubnode(0)->getLeftLeaf()->getContent();
$item = $prop->getSubnode(1);
if(!$item->isBranch()) {
$newItem = new Claim($itemName, $pid, new Subquery("[]") );
} else {
$item = $item->getSubnode(1);
$newItem = new Claim($itemName, $pid, $this->generateItem($item) );
}
if(!empty($qualifiers)) {
$qualifierExpr = new Qualifiers($itemName, $pid, $qname, $qualifiers);
$newItem->setQualifiers($qualifierExpr);
}
$items[] = $newItem;
}
if(!$items) {
throw new Exception("No items found for claim");
}
if(count($items) == 1) {
return $items[0];
}
return new Union($items);
case 'noclaim':
$items = array();
foreach($tree->getSubnode(1)->findAll('Propvalue') as $prop) {
$pid = $prop->getSubnode(0)->getLeftLeaf()->getContent();
$item = $prop->getSubnode(1);
if(!$item->isBranch()) {
$items[] = new NoClaim($itemName, $pid, new Subquery("?dummy".$this->counter++) );
} else {
$item = $item->getSubnode(1);
$items[] = new NoClaim($itemName, $pid, $this->generateItem($item) );
}
}
if(!$items) {
throw new Exception("No items found for claim");
}
if(count($items) == 1) {
return $items[0];
}
array_unshift($items, new AnyItem($itemName));
return new AndClause($items);
case 'string':
$pid = $tree->getSubnode(1)->getLeftLeaf()->getContent();
$qualifiers = $this->getQualifiers($tree, $itemName, 5, $qname );
if(!empty($qualifiers)) {
$qualifierExpr = new Qualifiers($itemName, $pid, $qname, $qualifiers);
}
$items = array();
foreach($tree->findAll('LiteralString') as $item) {
$newItem = new StringLiteral($itemName, $pid, $item->getLeftLeaf()->getContent());
if(!empty($qualifierExpr)) {
$newItem->setQualifiers($qualifierExpr);
}
$items[] = $newItem;
}
if(count($items) == 1) {
return $items[0];
}
return new Union($items);
case 'between':
$pid = $tree->getSubnode(1)->getLeftLeaf()->getContent();
$qualifiers = $this->getQualifiers($tree, $itemName, 5, $qname );
if(!empty($qualifiers)) {
$qualifierExpr = new Qualifiers($itemName, $pid, $qname, $qualifiers);
}
$numbers = $tree->getSubnode(3);
$subnumbers = $numbers->getSubnodes();
switch(count($subnumbers)) {
case 1:
$newItem = new Between($itemName, $pid, $subnumbers[0]->getLeftLeaf()->getContent() );
break;
case 2:
$newItem = new Between($itemName, $pid, null, $subnumbers[1]->getLeftLeaf()->getContent() );
break;
case 3:
$newItem = new Between($itemName, $pid, $subnumbers[0]->getLeftLeaf()->getContent(),
$subnumbers[2]->getLeftLeaf()->getContent() );
break;
default:
throw new Exception("Weird number of args for Between");
}
if(!empty($qualifierExpr)) {
$newItem->setQualifiers($qualifierExpr);
}
return $newItem;
break;
case 'quantity':
$pid = $tree->getSubnode(1)->getLeftLeaf()->getContent();
$low = $tree->getSubnode(3)->getLeftLeaf()->getContent();
$sub = $tree->getSubnode(4);
if($sub && $sub->isBranch()) {
$high = $sub->getSubnode(1)->getLeftLeaf()->getContent();
} else {
$high = null;
}
$qualifiers = $this->getQualifiers($tree, $itemName, 6, $qname );
if(!empty($qualifiers)) {
$qualifierExpr = new Qualifiers($itemName, $pid, $qname, $qualifiers);
}
$newItem = new Quantity($itemName, $pid, $low, $high);
if(!empty($qualifierExpr)) {
$newItem->setQualifiers($qualifierExpr);
}
return $newItem;
case 'tree':
$extract = function ($it) { return $it->getLeftLeaf()->getContent(); };
if($tree->getSubnode(3)->isBranch()) {
$forward = array_map($extract, $tree->getSubnode(3)->findAll('Number'));
} else {
$forward = array();
}
$back = $tree->getSubnode(5);
if($back && $back->isBranch()) {
$backward = array_map($extract, $back->findAll('Number'));
} else {
$backward = array();
}
$items = array_map(
function ($it) use ($forward, $backward, $itemName) {
return new Tree($itemName, $it->getLeftLeaf()->getContent(), $forward, $backward );
},
$tree->findAll('Item')
);
if(count($items) == 1) {
return $items[0];
}
return new Union($items);
case 'web':
$extract = function ($it) { return $it->getLeftLeaf()->getContent(); };
$props = array_map($extract, $tree->getSubnode(3)->findAll('Number'));
$items = array_map(
function ($it) use ($props, $itemName) {
return new Tree($itemName, $it->getLeftLeaf()->getContent(), $props, $props );
},
$tree->findAll('Item')
);
if(count($items) == 1) {
return $items[0];
}
return new Union($items);
case 'link':
$wiki = $tree->getSubnode(1)->getLeftLeaf()->getContent();
return new Link($itemName, $wiki);
case 'nolink':
$wiki = $tree->getSubnode(1)->getLeftLeaf()->getContent();
return new NoLink($itemName, $wiki);
case 'around':
$pid = $tree->getSubnode(1)->getLeftLeaf()->getContent();
$lat = $tree->getSubnode(3)->getLeftLeaf()->getContent();
$lon = $tree->getSubnode(5)->getLeftLeaf()->getContent();
$radius = $tree->getSubnode(7)->getLeftLeaf()->getContent();
return new GeoAround($itemName, $pid, $lat, $lon, $radius);
case 'items':
$items = array();
foreach($tree->getSubnode(1)->findAll('Number') as $id) {
$items[] = new ExactItem($itemName, $id);
}
return new Union($items);
default:
throw new Exception("Unknown type {$tree->getType()}");
}
return "";
}
public function parse($str) {
return $this->parser->parse($str);
}
}
// match("CLAIM[31:5]");
// match("(CLAIM[31:5])");
// match("CLAIM[31:5] AND CLAIM[27:801]");
// match("CLAIM[31:5] AND CLAIM[27:801] AND NOCLAIM[21]");
// match("CLAIM[31:5] AND CLAIM[27:801] AND NOCLAIM[21:34,56]");
// match("CLAIM[31:5] OR CLAIM[27:801] AND CLAIM[45:37]");
// match("CLAIM[31:5,67] OR CLAIM[27:801] AND NOCLAIM[45]");
// match("CLAIM[31:5] AND (CLAIM[27:801] OR NOCLAIM[1:23,45])");
// match("CLAIM[31:5] AND CLAIM[27:801] OR NOCLAIM[1:23,45]");
// match('CLAIM[31:5] AND STRING[5:"TEST"]');
// match('CLAIM[31:5] OR QUANTITY[5:10,20]');
// match('CLAIM[31:5] AND QUANTITY[5:42]');
// match('CLAIM[31:5] OR BETWEEN[5,1880,1990-05]');
// match('CLAIM[31:5] AND BETWEEN[5,+00000001861-03-17T00:00:00Z]');
// match('CLAIM[31:5] AND BETWEEN[5,,-10000861-03-17]');
// match("TREE[30][150][17,131]");
// match("CLAIM[138:676555] AND NOCLAIM[31:515]");
// match("(TREE[30][150][17,131] AND CLAIM[138:676555])");
// match("TREE[4504][171,273,75,76,77,70,71,74,89]");
// match("WEB[9682][25,22,40,26,7,9,1038]");