-
Notifications
You must be signed in to change notification settings - Fork 87
/
SparqlEvaluationContext.cs
375 lines (351 loc) · 12 KB
/
SparqlEvaluationContext.cs
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
/*
// <copyright>
// dotNetRDF is free and open source software licensed under the MIT License
// -------------------------------------------------------------------------
//
// Copyright (c) 2009-2021 dotNetRDF Project (http://dotnetrdf.org/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is furnished
// to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using VDS.RDF.Query.Algebra;
using VDS.RDF.Query.Datasets;
namespace VDS.RDF.Query
{
/// <summary>
/// Stores information about the Evaluation of a Query during it's evaluation.
/// </summary>
public class SparqlEvaluationContext
{
private ISparqlQueryAlgebraProcessor<BaseMultiset, SparqlEvaluationContext> _processor;
private BaseMultiset _inputSet, _outputSet;
private bool _trimTemporaries = true;//, _rigorous = Options.RigorousQueryEvaluation;
private ISparqlDataset _data;
private SparqlQuery _query;
private SparqlResultBinder _binder;
private Stopwatch _timer = new Stopwatch();
private Dictionary<String, Object> _functionContexts = new Dictionary<string, object>();
private long _timeout;
/// <summary>
/// Creates a new Evaluation Context for the given Query over the given Dataset.
/// </summary>
/// <param name="q">Query.</param>
/// <param name="data">Dataset.</param>
public SparqlEvaluationContext(SparqlQuery q, ISparqlDataset data)
{
_query = q;
_data = data;
_inputSet = new IdentityMultiset();
_binder = new LeviathanResultBinder(this);
CalculateTimeout();
}
/// <summary>
/// Creates a new Evaluation Context for the given Query over the given Dataset using a specific processor.
/// </summary>
/// <param name="q">Query.</param>
/// <param name="data">Dataset.</param>
/// <param name="processor">Query Processor.</param>
public SparqlEvaluationContext(SparqlQuery q, ISparqlDataset data, ISparqlQueryAlgebraProcessor<BaseMultiset, SparqlEvaluationContext> processor)
: this(q, data)
{
_processor = processor;
}
/// <summary>
/// Creates a new Evaluation Context which is a Container for the given Result Binder.
/// </summary>
/// <param name="binder"></param>
public SparqlEvaluationContext(SparqlResultBinder binder)
{
_binder = binder;
}
private void CalculateTimeout()
{
if (_query != null)
{
if (_query.Timeout > 0)
{
if (Options.QueryExecutionTimeout == 0 || (_query.Timeout <= Options.QueryExecutionTimeout && Options.QueryExecutionTimeout > 0))
{
// Query Timeout is used provided it is less than global timeout unless global timeout is zero
_timeout = _query.Timeout;
}
else
{
// Query Timeout cannot be set higher than global timeout
_timeout = Options.QueryExecutionTimeout;
}
}
else
{
// If Query Timeout set to zero (i.e. no timeout) then global timeout is used
_timeout = Options.QueryExecutionTimeout;
}
}
else
{
// If no query then global timeout is used
_timeout = Options.QueryExecutionTimeout;
}
}
/// <summary>
/// Gets the Query that is being evaluated.
/// </summary>
public SparqlQuery Query
{
get
{
return _query;
}
}
/// <summary>
/// Gets the Dataset the query is over.
/// </summary>
public ISparqlDataset Data
{
get
{
return _data;
}
}
/// <summary>
/// Gets the custom query processor that is in use (if any).
/// </summary>
public ISparqlQueryAlgebraProcessor<BaseMultiset, SparqlEvaluationContext> Processor
{
get
{
return _processor;
}
}
/// <summary>
/// Gets/Sets the Input Multiset.
/// </summary>
public BaseMultiset InputMultiset
{
get
{
return _inputSet;
}
set
{
_inputSet = value;
}
}
/// <summary>
/// Gets/Sets the Output Multiset.
/// </summary>
public BaseMultiset OutputMultiset
{
get
{
return _outputSet;
}
set
{
_outputSet = value;
}
}
/// <summary>
/// Gets/Sets the Results Binder.
/// </summary>
public SparqlResultBinder Binder
{
get
{
return _binder;
}
set
{
_binder = value;
}
}
/// <summary>
/// Gets/Sets whether BGPs should trim temporary variables.
/// </summary>
public bool TrimTemporaryVariables
{
get
{
return _trimTemporaries;
}
set
{
_trimTemporaries = value;
}
}
///// <summary>
///// Gets/Sets whether additional checks should be made during evaluation
///// </summary>
// public bool RigorousEvaluation
// {
// get
// {
// return this._rigorous;
// }
// set
// {
// this._rigorous = value;
// }
// }
/// <summary>
/// Starts the Execution Timer.
/// </summary>
public void StartExecution()
{
CalculateTimeout();
_timer.Start();
}
/// <summary>
/// Ends the Execution Timer.
/// </summary>
public void EndExecution()
{
_timer.Stop();
}
/// <summary>
/// Checks whether Execution should Time out.
/// </summary>
/// <exception cref="RdfQueryTimeoutException">Thrown if the Query has exceeded the Execution Timeout.</exception>
public void CheckTimeout()
{
if (_timeout > 0)
{
if (_timer.ElapsedMilliseconds > _timeout)
{
_timer.Stop();
throw new RdfQueryTimeoutException("Query Execution Time exceeded the Timeout of " + _timeout + "ms, query aborted after " + _timer.ElapsedMilliseconds + "ms");
}
}
}
/// <summary>
/// Gets the Remaining Timeout i.e. the Timeout taking into account time already elapsed.
/// </summary>
/// <remarks>
/// If there is no timeout then this is always zero, if there is a timeout this is always >= 1 since any operation that wants to respect the timeout must have a non-zero timeout to actually timeout properly.
/// </remarks>
public long RemainingTimeout
{
get
{
if (_timeout <= 0)
{
return 0;
}
else
{
long timeout = _timeout - QueryTime;
if (timeout <= 0)
{
return 1;
}
else
{
return timeout;
}
}
}
}
/// <summary>
/// Gets the Query Timeout used for the Query.
/// </summary>
/// <remarks>
/// <para>
/// This is taken either from the <see cref="SparqlQuery.Timeout">Timeout</see> property of the <see cref="SparqlQuery">SparqlQuery</see> to which this evaluation context pertains (if any) or from the global option <see cref="Options.QueryExecutionTimeout">Options.QueryExecutionTimeout</see>. To set the Timeout to be used set whichever of those is appropriate prior to evaluating the query. If there is a Query present then it's timeout takes precedence unless it is set to zero (no timeout) in which case the global timeout setting is applied. You cannot set the Query Timeout to be higher than the global timeout unless the global timeout is set to zero (i.e. no global timeout).
/// </para>
/// </remarks>
public long QueryTimeout
{
get
{
return _timeout;
}
}
/// <summary>
/// Retrieves the Time in milliseconds the query took to evaluate.
/// </summary>
public long QueryTime
{
get
{
return _timer.ElapsedMilliseconds;
}
}
/// <summary>
/// Retrieves the Time in ticks the query took to evaluate.
/// </summary>
public long QueryTimeTicks
{
get
{
return _timer.ElapsedTicks;
}
}
/// <summary>
/// Gets/Sets a Object that should be persisted over the entire Evaluation Context.
/// </summary>
/// <param name="key">Key.</param>
/// <returns></returns>
/// <remarks>
/// May be used by parts of the Evaluation Process that need to ensure a persistent state across the entire Evaluation Query (e.g. the implementation of the BNODE() function).
/// </remarks>
public Object this[String key]
{
get
{
if (_functionContexts.ContainsKey(key))
{
return _functionContexts[key];
}
else
{
return null;
}
}
set
{
if (_functionContexts.ContainsKey(key))
{
_functionContexts[key] = value;
}
else
{
_functionContexts.Add(key, value);
}
}
}
/// <summary>
/// Evalutes an Algebra Operator in this Context using the current Query Processor (if any) or the default <see cref="ISparqlAlgebra.Evaluate">Evaluate()</see> method.
/// </summary>
/// <param name="algebra">Algebra.</param>
/// <returns></returns>
public BaseMultiset Evaluate(ISparqlAlgebra algebra)
{
if (_processor == null)
{
return algebra.Evaluate(this);
}
else
{
return _processor.ProcessAlgebra(algebra, this);
}
}
}
}