forked from AKSW/Erfurt
/
Constraint.php
414 lines (374 loc) · 14.1 KB
/
Constraint.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
<?php
/**
* This file is part of the {@link http://erfurt-framework.org Erfurt} project.
*
* @copyright Copyright (c) 2012, {@link http://aksw.org AKSW}
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
*/
/**
* Represents a constraint. A value constraint is a boolean- valued expression
* of variables and RDF Terms.
*
* This class was originally adopted from rdfapi-php (@link http://sourceforge.net/projects/rdfapi-php/).
* It was modified and extended in order to fit into Erfurt.
*
* @package Erfurt_Sparql
* @author Tobias Gauss <tobias.gauss@web.de>
* @author Philipp Frischmuth <pfrischmuth@googlemail.com>
* @license http://www.gnu.org/licenses/lgpl.html LGPL
*/
class Erfurt_Sparql_Constraint
{
// ------------------------------------------------------------------------
// --- Protected properties -----------------------------------------------
// ------------------------------------------------------------------------
/**
* The expression string.
*
* @var string
*/
protected $_expression;
/**
* True if it is an outer filter, false if not.
*
* @var boolean
*/
protected $_outer;
/**
* The expression tree
*
* @var array
*/
protected $_tree = null;
/**
* Contains all variables that are used in the constraint expression (recursivly).
* This array is calculated once, if the tree is set in order to avoid multiple calculation.^
*
* @var array
*/
protected $_usedVars = null;
protected $_tokens = null;
// ------------------------------------------------------------------------
// --- Public methods -----------------------------------------------------
// ------------------------------------------------------------------------
/**
* Adds an expression string.
*
* @param String $exp the expression String
* @return void
*/
public function addExpression($exp)
{
$this->_expression = $exp;
}
/**
* Returns the expression string.
*
* @return String the expression String
*/
public function getExpression()
{
return $this->_expression;
}
public function getTree()
{
return $this->_tree;
}
public function getUsedVars()
{
if (null === $this->_usedVars) {
$this->_usedVars = array_unique($this->_resolveUsedVarsRecursive($this->_tree));
}
return $this->_usedVars;
}
/**
* Returns true if this constraint is an outer filter- false if not.
*
* @return boolean
*/
public function isOuterFilter()
{
return $this->_outer;
}
/**
* Sets the filter type to outer or inner filter.
* True for outer false for inner.
*
* @param boolean $boolean
* @return void
*/
public function setOuterFilter($boolean)
{
$this->_outer = $boolean;
}
public function setTree($tree)
{
$this->_tree = $tree;
// If the tree is set or reset, the used variables need to be resolved again.
$this->_usedVars = null;
}
public function parse()
{
$this->_tokens = Erfurt_Sparql_Parser::tokenize($this->_expression);
$this->setOuterFilter(true);
$this->setTree($this->_parseConstraintTree());
$this->_tokens = null;
}
// ------------------------------------------------------------------------
// --- Protected methods --------------------------------------------------
// ------------------------------------------------------------------------
protected function _resolveUsedVarsRecursive($tree)
{
$usedVars = array();
if ($tree['type'] === 'function') {
foreach ($tree['parameter'] as $paramArray) {
if ($paramArray['type'] === 'value') {
if (
is_string($paramArray['value']) &&
($paramArray['value'][0] === '?' || $paramArray['value'][0] === '$')
) {
$usedVars[] = $paramArray['value'];
}
} else if ($paramArray['type'] === 'function') {
foreach ($paramArray['parameter'] as $p) {
if (is_string($p['value']) &&
($p['value'][0] === '?' || $p['value'][0] === '$')) {
$usedVars[] = $p['value'];
}
}
}
}
} else if ($tree['type'] === 'value') {
if (strlen($tree['value']) && ($tree['value'][0] === '?' || $tree['value'][0] === '$')) {
$usedVars[] = $tree['value'];
}
} else {
$usedVars = array_merge($usedVars, $this->_resolveUsedVarsRecursive($tree['operand1']));
$usedVars = array_merge($usedVars, $this->_resolveUsedVarsRecursive($tree['operand2']));
}
return $usedVars;
}
protected function _parseConstraintTree($nLevel = 0, $bParameter = false)
{
$tree = array();
$part = array();
$chQuotes = null;
$litQuotes = null;
$strQuoted = '';
$parens = false;
while ($tok = next($this->_tokens)) {
if ($chQuotes !== null && $tok != $chQuotes) {
$strQuoted .= $tok;
continue;
} else if ($litQuotes !== null) {
$strQuoted .= $tok;
if ($tok[strlen($tok) - 1] === '>') {
$tok = '>';
} else {
continue;
}
} else if ($tok === ')' || $tok === '}' || $tok === '.') {
break;
} else if (strtolower($tok) === 'filter' || strtolower($tok) === 'optional') {
break;
}
switch ($tok) {
case '"':
case '\'':
if ($chQuotes === null) {
$chQuotes = $tok;
$strQuoted = '';
} else {
$chQuotes = null;
$part[] = array(
'type' => 'value',
'value' => $strQuoted,
'quoted'=> true
);
}
continue 2;
break;
case '>':
$litQuotes = null;
$part[] = array(
'type' => 'value',
'value' => $strQuoted,
'quoted'=> false
);
continue 2;
break;
case '(':
$parens = true;
$bFunc1 = isset($part[0]['type']) && $part[0]['type'] === 'value';
$bFunc2 = isset($tree['type']) && $tree['type'] === 'equation'
&& isset($tree['operand2']) && isset($tree['operand2']['value']);
$part[] = $this->_parseConstraintTree(
$nLevel + 1,
$bFunc1 || $bFunc2
);
if ($bFunc1) {
$tree['type'] = 'function';
$tree['name'] = $part[0]['value'];
Erfurt_Sparql_Parser::fixNegationInFuncName($tree);
if (isset($part[1]['type'])) {
$part[1] = array($part[1]);
}
$tree['parameter'] = $part[1];
$part = array();
} else if ($bFunc2) {
$tree['operand2']['type'] = 'function';
$tree['operand2']['name'] = $tree['operand2']['value'];
Erfurt_Sparql_Parser::fixNegationInFuncName($tree['operand2']);
$tree['operand2']['parameter'] = $part[0];
unset($tree['operand2']['value']);
unset($tree['operand2']['quoted']);
$part = array();
}
if (current($this->_tokens) === ')') {
if (substr(next($this->_tokens), 0, 2) === '_:') {
// filter ends here
prev($this->_tokens);
break 2;
} else {
prev($this->_tokens);
}
}
continue 2;
break;
case ' ':
case "\t":
continue 2;
case '=':
case '>':
case '<':
case '<=':
case '>=':
case '!=':
case '&&':
case '||':
if (isset($tree['type']) && $tree['type'] === 'equation' && isset($tree['operand2'])) {
//previous equation open
$part = array($tree);
} else if (isset($tree['type']) && $tree['type'] !== 'equation') {
$part = array($tree);
$tree = array();
}
$tree['type'] = 'equation';
$tree['level'] = $nLevel;
$tree['operator'] = $tok;
$tree['operand1'] = $part[0];
unset($tree['operand2']);
$part = array();
continue 2;
break;
case '!':
if ($tree != array()) {
require_once 'Erfurt/Sparql/ParserException.php';
throw new Erfurt_Sparql_ParserException(
'Unexpected "!" negation in constraint.', -1, current($this->_tokens)
);
}
$tree['negated'] = true;
continue 2;
case ',':
//parameter separator
if (count($part) == 0 && !isset($tree['type'])) {
throw new SparqlParserException(
'Unexpected comma'
);
}
$bParameter = true;
if (count($part) === 0) {
$part[] = $tree;
$tree = array();
}
continue 2;
default:
break;
}
if ($this->_varCheck($tok)) {
if (!$parens && $nLevel === 0) {
// Variables need parenthesizes first
require_once 'Erfurt/Sparql/ParserException.php';
throw new Erfurt_Sparql_ParserException(
'FILTER expressions that start with a variable need parenthesizes.',
-1,
current($this->_tokens)
);
}
$part[] = array(
'type' => 'value',
'value' => $tok,
'quoted' => false
);
} else if (substr($tok, 0, 2) === '_:') {
// syntactic blank nodes not allowed in filter
require_once 'Erfurt/Sparql/ParserException.php';
throw new Erfurt_Sparql_ParserException(
'Syntactic Blanknodes not allowed in FILTER.', -1, current($this->_tokens)
);
} else if (substr($tok, 0, 2) === '^^') {
$part[count($part) - 1]['datatype'] = $this->_query->getFullUri(substr($tok, 2));
} else if ($tok[0] === '@') {
$part[count($part) - 1]['language'] = substr($tok, 1);
} else if ($tok[0] === '<') {
if ($tok[strlen($tok) - 1] === '>') {
//single-tokenized <> uris
$part[] = array(
'type' => 'value',
'value' => $tok,
'quoted' => false
);
} else {
//iris split over several tokens
$strQuoted = $tok;
$litQuotes = true;
}
} else if ($tok === 'true' || $tok === 'false') {
$part[] = array(
'type' => 'value',
'value' => $tok,
'quoted' => false,
'datatype' => 'http://www.w3.org/2001/XMLSchema#boolean'
);
} else {
$part[] = array(
'type' => 'value',
'value' => $tok,
'quoted' => false
);
}
if (isset($tree['type']) && $tree['type'] === 'equation' && isset($part[0])) {
$tree['operand2'] = $part[0];
Erfurt_Sparql_Parser::balanceTree($tree);
$part = array();
}
}
if (!isset($tree['type']) && $bParameter) {
return $part;
} else if (isset($tree['type']) && $tree['type'] === 'equation'
&& isset($tree['operand1']) && !isset($tree['operand2'])
&& isset($part[0])) {
$tree['operand2'] = $part[0];
Erfurt_Sparql_Parser::balanceTree($tree);
}
if ((count($tree) === 0) && (count($part) > 1)) {
require_once 'Erfurt/Sparql/ParserException.php';
throw new Erfurt_Sparql_ParserException('Failed to parse constraint.', -1, current($this->_tokens));
}
if (!isset($tree['type']) && isset($part[0])) {
if (isset($tree['negated'])) {
$part[0]['negated'] = true;
}
return $part[0];
}
return $tree;
}
protected function _varCheck($token)
{
if (isset($token[0]) && ($token{0} == '$' || $token{0} == '?')) {
return true;
}
return false;
}
}