This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractTriplePatternStore.php
344 lines (302 loc) · 11.4 KB
/
AbstractTriplePatternStore.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
<?php
/*
* This file is part of Saft.
*
* (c) Konrad Abicht <[email protected]>
* (c) Natanael Arndt <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Saft\Store;
use Saft\Rdf\NodeFactory;
use Saft\Rdf\Statement;
use Saft\Rdf\StatementFactory;
use Saft\Rdf\StatementIterator;
use Saft\Rdf\StatementIteratorFactory;
use Saft\Sparql\Query\Query;
use Saft\Sparql\Query\QueryFactory;
use Saft\Sparql\Query\QueryUtils;
/**
* Predefined Pattern-statement Store. The Triple-methods need to be implemented in the specific statement-store.
* The query method is defined in the abstract class and reroute to the triple-methods.
*
* @api
*
* @since 0.1
*/
abstract class AbstractTriplePatternStore implements Store
{
/**
* @var NodeFactory
*/
private $nodeFactory;
/**
* @var QueryFactory
*/
private $queryFactory;
/**
* @var StatementFactory
*/
private $statementFactory;
/**
* @var StatementIteratorFactory
*/
private $statementIteratorFactory;
/**
* Constructor.
*
* @param NodeFactory $nodeFactory instance of NodeFactory
* @param StatementFactory $statementFactory instance of StatementFactory
* @param QueryFactory $queryFactory instance of QueryFactory
* @param StatementIteratorFactory $statementIteratorFactory instance of StatementIteratorFactory
*
* @api
*
* @since 0.1
*/
public function __construct(
NodeFactory $nodeFactory,
StatementFactory $statementFactory,
QueryFactory $queryFactory,
StatementIteratorFactory $statementIteratorFactory
) {
$this->nodeFactory = $nodeFactory;
$this->queryFactory = $queryFactory;
$this->statementFactory = $statementFactory;
$this->statementIteratorFactory = $statementIteratorFactory;
$this->queryUtils = new QueryUtils();
}
/**
* This method sends a SPARQL query to the store.
*
* @param string $query the SPARQL query to send to the store
* @param array $options optional It contains key-value pairs and should provide additional
* introductions for the store and/or its adapter(s)
*
* @return Result Returns result of the query. Its type depends on the type of the query.
*
* @throws \Exception if query is no string, is malformed or an execution error occured
*
* @api
*
* @since 0.1
*/
public function query($query, array $options = [])
{
$queryObject = $this->queryFactory->createInstanceByQueryString($query);
if ('updateQuery' == $this->queryUtils->getQueryType($query)) {
/*
* INSERT or DELETE query
*/
$firstPart = substr($queryObject->getSubType(), 0, 6);
// DELETE DATA query
if ('delete' == $firstPart) {
$statements = $this->getStatements($queryObject);
// if it goes into the loop, there is more than one triple pattern in the DELETE query,
// which is not allowed.
$i = 0;
foreach ($statements as $statement) {
if (1 <= $i++) {
throw new \Exception(
'DELETE query can not contain more than one triple pattern: '.$query
);
}
// if only one Statement is in the list, no exception will be thrown and in
// $statement is that Statement later on and can be used.
}
return $this->deleteMatchingStatements($statement);
// INSERT DATA or INSERT INTO query
} elseif ('insert' == $firstPart) {
return $this->addStatements($this->getStatements($queryObject));
// TODO Support
// WITH ... DELETE ... WHERE queries
// WITH ... DELETE ... INSERT ... WHERE queries
} else {
throw new \Exception(
'Not yet implemented: WITH-DELETE-WHERE and WITH-DELETE-INSERT-WHERE queries are not '.
'supported yet.'
);
}
} elseif ('askQuery' == $this->queryUtils->getQueryType($query)) {
/*
* ASK query
*/
$statement = $this->getStatement($queryObject);
return $this->hasMatchingStatement($statement);
} elseif ('selectQuery' == $this->queryUtils->getQueryType($query)) {
/*
* SELECT query
*/
$statement = $this->getStatement($queryObject);
return $this->getMatchingStatements($statement);
} elseif ('constructQuery' == $this->queryUtils->getQueryType($query)) {
/*
* CONSTRUCT query
* TODO how to handle it here?
*/
$statement = $this->getStatement($queryObject);
return $this->getMatchingStatements($statement);
} else {
/*
* Unsupported query
*/
throw new \Exception('Unsupported query was given: '.$query);
}
}
/**
* Create Statement instance based on a given Query instance.
*
* @param Query $queryObject query object which represents a SPARQL query
*
* @return Statement statement object itself
*
* @throws \Exception if query contains more than one triple pattern
* @throws \Exception if more than one graph was found
*
* @api
*
* @since 0.1
*/
protected function getStatement(Query $queryObject)
{
$queryParts = $queryObject->getQueryParts();
$tupleInformaton = null;
$tupleType = null;
/*
* Use triple pattern
*/
if (true === isset($queryParts['triple_pattern'])) {
$tupleInformation = $queryParts['triple_pattern'];
$tupleType = 'triple';
/*
* Use quad pattern
*/
} elseif (true === isset($queryParts['quad_pattern'])) {
$tupleInformation = $queryParts['quad_pattern'];
$tupleType = 'quad';
/*
* Neither triple nor quad information
*/
} else {
throw new \Exception(
'Neither triple nor quad information available in given query object: '.$queryObject->getQuery()
);
}
if (1 > count($tupleInformation)) {
throw new \Exception('Query contains more than one triple- respectivly quad pattern.');
}
/*
* Triple
*/
if ('triple' == $tupleType) {
$subject = $this->createNodeByValueAndType($tupleInformation[0]['s'], $tupleInformation[0]['s_type']);
$predicate = $this->createNodeByValueAndType($tupleInformation[0]['p'], $tupleInformation[0]['p_type']);
$object = $this->createNodeByValueAndType($tupleInformation[0]['o'], $tupleInformation[0]['o_type']);
$graph = null;
/*
* Quad
*/
} elseif ('quad' == $tupleType) {
$subject = $this->createNodeByValueAndType($tupleInformation[0]['s'], $tupleInformation[0]['s_type']);
$predicate = $this->createNodeByValueAndType($tupleInformation[0]['p'], $tupleInformation[0]['p_type']);
$object = $this->createNodeByValueAndType($tupleInformation[0]['o'], $tupleInformation[0]['o_type']);
$graph = $this->createNodeByValueAndType($tupleInformation[0]['g'], 'uri');
}
// no else neccessary, because otherwise the upper exception would be thrown if tupleType is neither
// quad or triple.
return $this->statementFactory->createStatement($subject, $predicate, $object, $graph);
}
/**
* Create statements from query.
*
* @param Query $queryObject query object which represents a SPARQL query
*
* @return StatementIterator StatementIterator object
*
* @throws \Exception if query contains quads and triples at the same time
* @throws \Exception if query contains neither quads nor triples
*
* @api
*
* @since 0.1
*/
protected function getStatements(Query $queryObject)
{
$queryParts = $queryObject->getQueryParts();
$statementArray = [];
// if only triples, but no quads
if (true === isset($queryParts['triple_pattern'])
&& false === isset($queryParts['quad_pattern'])) {
foreach ($queryParts['triple_pattern'] as $pattern) {
/**
* Create Node instances for S, P and O to build a Statement instance later on.
*/
$s = $this->createNodeByValueAndType($pattern['s'], $pattern['s_type']);
$p = $this->createNodeByValueAndType($pattern['p'], $pattern['p_type']);
$o = $this->createNodeByValueAndType($pattern['o'], $pattern['o_type']);
$g = null;
$statementArray[] = $this->statementFactory->createStatement($s, $p, $o, $g);
}
// if only quads, but not triples
} elseif (false === isset($queryParts['triple_pattern'])
&& true === isset($queryParts['quad_pattern'])) {
foreach ($queryParts['quad_pattern'] as $pattern) {
/**
* Create Node instances for S, P and O to build a Statement instance later on.
*/
$s = $this->createNodeByValueAndType($pattern['s'], $pattern['s_type']);
$p = $this->createNodeByValueAndType($pattern['p'], $pattern['p_type']);
$o = $this->createNodeByValueAndType($pattern['o'], $pattern['o_type']);
$g = $this->createNodeByValueAndType($pattern['g'], $pattern['g_type']);
$statementArray[] = $this->statementFactory->createStatement($s, $p, $o, $g);
}
// found quads and triples
} elseif (true === isset($queryParts['triple_pattern'])
&& true === isset($queryParts['quad_pattern'])) {
throw new \Exception('Query contains quads and triples. That is not supported yet.');
// neither quads nor triples
} else {
throw new \Exception('Query contains neither quads nor triples.');
}
return $this->statementIteratorFactory->createStatementIteratorFromArray($statementArray);
}
/**
* Creates an instance of Node by given $value and $type.
*
* @param mixed $value
* @param string $type
*
* @return Node instance of Node interface
*
* @throws \Exception if an unknown type was given
*
* @api
*
* @since 0.1
*/
protected function createNodeByValueAndType($value, $type)
{
/*
* URI
*/
if ('uri' == $type) {
return $this->nodeFactory->createNamedNode($value);
/*
* Any Pattern
*/
} elseif ('var' == $type) {
return $this->nodeFactory->createAnyPattern();
/*
* Typed Literal or Literal
*/
} elseif ('typed-literal' == $type || 'literal' == $type) {
return $this->nodeFactory->createLiteral($value);
/*
* Unknown type
*/
} else {
throw new \Exception('Unknown type given: '.$type);
}
}
}