Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Query.cs
1using System;
2using System.Threading.Tasks;
3using Waher.Events;
5
7{
11 public class Query : IObservableLayer
12 {
13 private readonly INode nodeReference;
14 private readonly Language language;
15 private readonly RequestOrigin origin;
16 private readonly string commandId;
17 private readonly string queryId;
18 private readonly object state;
19 private bool isStarted = false;
20 private bool isAborted = false;
21 private bool isDone = false;
22 private int seqNr = 0;
23 private int nrSectionsBegun = 0;
24 private int nrSectionsEnded = 0;
25 private int nrTablesStarted = 0;
26 private int nrTablesCompleted = 0;
27 private int nrObjectsReported = 0;
28 private int nrMessagesReported = 0;
29 private int nrRecordsReported = 0;
30 private bool hasTitle = false;
31
41 public Query(string CommandId, string QueryId, object State, Language Language,
43 {
44 this.nodeReference = NodeReference;
45 this.commandId = CommandId;
46 this.queryId = QueryId;
47 this.state = State;
48 this.language = Language;
49 this.origin = Origin;
50 }
51
55 public string CommandID => this.commandId;
56
60 public string QueryID => this.queryId;
61
65 public object State => this.state;
66
70 public Language Language => this.language;
71
75 public INode NodeReference => this.nodeReference;
76
80 public RequestOrigin Origin => this.origin;
81
85 public bool IsStarted => this.isStarted;
86
90 public bool IsAborted => this.isAborted;
91
95 public bool IsDone => this.isDone;
96
100 public int SequenceNumber => this.seqNr;
101
105 public int NrSectionsBegun => this.nrSectionsBegun;
106
110 public int NrSectionsEnded => this.nrSectionsEnded;
111
115 public int NrTablesStarted => this.nrTablesStarted;
116
120 public int NrTablesCompleted => this.nrTablesCompleted;
121
125 public int NrObjectsReported => this.nrObjectsReported;
126
130 public int NrMessagesReported => this.nrMessagesReported;
131
135 public int NrRecordsReported => this.nrRecordsReported;
136
141 {
142 get
143 {
144 int Result = this.nrSectionsBegun;
145 Result += this.nrSectionsEnded;
146 Result += this.NrTablesStarted;
147 Result += this.nrTablesCompleted;
148 Result += this.nrObjectsReported;
149 Result += this.nrMessagesReported;
150 Result += this.nrRecordsReported;
151 return Result;
152 }
153 }
154
158 public bool HasTitle => this.hasTitle;
159
163 public bool HasReported => this.NrTotalItemsReported > 0;
164
170 {
171 return ++this.seqNr;
172 }
173
177 public virtual async Task Abort()
178 {
179 if (!this.isAborted)
180 {
181 this.isAborted = true;
182 await this.Raise(this.OnAborted, new QueryEventArgs(this), false);
183 }
184 }
185
186 internal Task Abort(QueryEventArgs e)
187 {
188 if (!this.isAborted)
189 {
190 this.isAborted = true;
191 return this.Raise(this.OnAborted, e, false);
192 }
193 else
194 return Task.CompletedTask;
195 }
196
197 private async Task Raise(EventHandlerAsync<QueryEventArgs> Callback, QueryEventArgs e, bool CheckTerminated)
198 {
199 if (CheckTerminated && (this.isAborted || this.isDone))
200 return;
201
202 await Callback.Raise(this, e);
203 }
204
208 public event EventHandlerAsync<QueryEventArgs> OnAborted = null;
209
213 public Task Start()
214 {
215 if (this.isStarted)
216 return Task.CompletedTask;
217
218 this.isStarted = true;
219 return this.Raise(this.OnStarted, new QueryEventArgs(this), false);
220 }
221
222 internal Task Start(QueryEventArgs e)
223 {
224 if (!this.isStarted)
225 {
226 this.isStarted = true;
227 return this.Raise(this.OnStarted, e, false);
228 }
229 else
230 return Task.CompletedTask;
231 }
232
236 public event EventHandlerAsync<QueryEventArgs> OnStarted = null;
237
241 public async Task Done()
242 {
243 if (!this.isDone)
244 {
245 this.isDone = true;
246 await this.Raise(this.OnDone, new QueryEventArgs(this), false);
247 }
248 }
249
250 internal Task Done(QueryEventArgs e)
251 {
252 if (!this.isDone)
253 {
254 this.isDone = true;
255 return this.Raise(this.OnDone, e, false);
256 }
257 else
258 return Task.CompletedTask;
259 }
260
264 public event EventHandlerAsync<QueryEventArgs> OnDone = null;
265
272 public Task NewTable(string TableId, string TableName, params Column[] Columns)
273 {
274 this.nrTablesStarted++;
275 return this.Raise(this.OnNewTable, new QueryNewTableEventArgs(this, TableId, TableName, Columns));
276 }
277
278 internal Task NewTable(QueryNewTableEventArgs e)
279 {
280 this.nrTablesStarted++;
281 return this.Raise(this.OnNewTable, e);
282 }
283
284 private async Task Raise(EventHandlerAsync<QueryNewTableEventArgs> Callback, QueryNewTableEventArgs e)
285 {
286 if (!this.isAborted && !this.isDone)
287 await Callback.Raise(this, e);
288 }
289
293 public event EventHandlerAsync<QueryNewTableEventArgs> OnNewTable = null;
294
300 public Task NewRecords(string TableId, params Record[] Records)
301 {
302 this.nrRecordsReported += Records.Length;
303 return this.Raise(this.OnNewRecords, new QueryNewRecordsEventArgs(this, TableId, Records));
304 }
305
306 internal Task NewRecords(QueryNewRecordsEventArgs e)
307 {
308 this.nrRecordsReported += e.Records.Length;
309 return this.Raise(this.OnNewRecords, e);
310 }
311
312 private async Task Raise(EventHandlerAsync<QueryNewRecordsEventArgs> Callback, QueryNewRecordsEventArgs e)
313 {
314 if (!this.isAborted && !this.isDone)
315 await Callback.Raise(this, e);
316 }
317
321 public event EventHandlerAsync<QueryNewRecordsEventArgs> OnNewRecords = null;
322
327 public Task TableDone(string TableId)
328 {
329 this.nrTablesCompleted++;
330 return this.Raise(this.OnTableDone, new QueryTableEventArgs(this, TableId));
331 }
332
333 internal Task TableDone(QueryTableEventArgs e)
334 {
335 this.nrTablesCompleted++;
336 return this.Raise(this.OnTableDone, e);
337 }
338
339 private async Task Raise(EventHandlerAsync<QueryTableEventArgs> Callback, QueryTableEventArgs e)
340 {
341 if (!this.isAborted && !this.isDone)
342 await Callback.Raise(this, e);
343 }
344
348 public event EventHandlerAsync<QueryTableEventArgs> OnTableDone = null;
349
354 public Task NewObject(object Object)
355 {
356 this.nrObjectsReported++;
357 return this.Raise(this.OnNewObject, new QueryObjectEventArgs(this, Object));
358 }
359
360 internal Task NewObject(QueryObjectEventArgs e)
361 {
362 this.nrObjectsReported++;
363 return this.Raise(this.OnNewObject, e);
364 }
365
366 private async Task Raise(EventHandlerAsync<QueryObjectEventArgs> Callback, QueryObjectEventArgs e)
367 {
368 if (!this.isAborted && !this.isDone)
369 await Callback.Raise(this, e);
370 }
371
375 public event EventHandlerAsync<QueryObjectEventArgs> OnNewObject = null;
376
381 public Task LogMessage(Exception Exception)
382 {
383 return this.LogMessage(QueryEventLevel.Major, Exception);
384 }
385
391 public Task LogMessage(QueryEventLevel Level, Exception Exception)
392 {
393 Exception = Log.UnnestException(Exception);
394 return this.LogMessage(QueryEventType.Exception, Level, Exception.Message);
395 }
396
403 public Task LogMessage(QueryEventType Type, QueryEventLevel Level, string Body)
404 {
405 this.nrMessagesReported++;
406 return this.Raise(this.OnMessage, new QueryMessageEventArgs(this, Type, Level, Body));
407 }
408
409 internal Task LogMessage(QueryMessageEventArgs e)
410 {
411 this.nrMessagesReported++;
412 return this.Raise(this.OnMessage, e);
413 }
414
415 private async Task Raise(EventHandlerAsync<QueryMessageEventArgs> Callback, QueryMessageEventArgs e)
416 {
417 if (!this.isAborted && !this.isDone)
418 await Callback.Raise(this, e);
419 }
420
424 public event EventHandlerAsync<QueryMessageEventArgs> OnMessage = null;
425
430 public Task SetTitle(string Title)
431 {
432 this.hasTitle = true;
433 return this.Raise(this.OnTitle, new QueryTitleEventArgs(this, Title));
434 }
435
436 internal Task SetTitle(QueryTitleEventArgs e)
437 {
438 this.hasTitle = true;
439 return this.Raise(this.OnTitle, e);
440 }
441
442 private async Task Raise(EventHandlerAsync<QueryTitleEventArgs> Callback, QueryTitleEventArgs e)
443 {
444 if (!this.isAborted && !this.isDone)
445 await Callback.Raise(this, e);
446 }
447
451 public event EventHandlerAsync<QueryTitleEventArgs> OnTitle = null;
452
457 public Task SetStatus(string Status)
458 {
459 return this.Raise(this.OnStatus, new QueryStatusEventArgs(this, Status));
460 }
461
462 internal Task SetStatus(QueryStatusEventArgs e)
463 {
464 return this.Raise(this.OnStatus, e);
465 }
466
467 private async Task Raise(EventHandlerAsync<QueryStatusEventArgs> h, QueryStatusEventArgs e)
468 {
469 if (!this.isAborted && !this.isDone)
470 await h.Raise(this, e);
471 }
472
476 public event EventHandlerAsync<QueryStatusEventArgs> OnStatus = null;
477
483 public Task BeginSection(string Header)
484 {
485 this.nrSectionsBegun++;
486 return this.Raise(this.OnBeginSection, new QueryTitleEventArgs(this, Header));
487 }
488
489 internal Task BeginSection(QueryTitleEventArgs e)
490 {
491 this.nrSectionsBegun++;
492 return this.Raise(this.OnBeginSection, e);
493 }
494
498 public event EventHandlerAsync<QueryTitleEventArgs> OnBeginSection = null;
499
504 public Task EndSection()
505 {
506 this.nrSectionsEnded++;
507 return this.Raise(this.OnEndSection, new QueryEventArgs(this), true);
508 }
509
510 internal Task EndSection(QueryEventArgs e)
511 {
512 this.nrSectionsEnded++;
513 return this.Raise(this.OnEndSection, e, true);
514 }
515
519 public event EventHandlerAsync<QueryEventArgs> OnEndSection = null;
520
521 #region ICommunicationLayer
522
527 public bool DecoupledEvents => false;
528
533 public void Information(string Comment) => this.LogMessage(QueryEventType.Information, QueryEventLevel.Minor, Comment);
534
539 public void Warning(string Warning) => this.LogMessage(QueryEventType.Warning, QueryEventLevel.Minor, Warning);
540
545 public void Error(string Error) => this.LogMessage(QueryEventType.Error, QueryEventLevel.Minor, Error);
546
551 public void Exception(Exception Exception) => this.LogMessage(QueryEventType.Exception, QueryEventLevel.Minor, Exception.Message);
552
557 public void Exception(string Exception) => this.LogMessage(QueryEventType.Exception, QueryEventLevel.Minor, Exception);
558
564 public void Information(DateTime Timestamp, string Comment) => this.LogMessage(QueryEventType.Information, QueryEventLevel.Minor, Comment);
565
571 public void Warning(DateTime Timestamp, string Warning) => this.LogMessage(QueryEventType.Warning, QueryEventLevel.Minor, Warning);
572
578 public void Error(DateTime Timestamp, string Error) => this.LogMessage(QueryEventType.Error, QueryEventLevel.Minor, Error);
579
585 public void Exception(DateTime Timestamp, string Exception) => this.LogMessage(QueryEventType.Exception, QueryEventLevel.Minor, Exception);
586
592 public void Exception(DateTime Timestamp, Exception Exception) => this.LogMessage(QueryEventType.Exception, QueryEventLevel.Minor, Exception.Message);
593
594 #endregion
595 }
596}
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
static Exception UnnestException(Exception Exception)
Unnests an exception, to extract the relevant inner exception.
Definition: Log.cs:828
Contains information about a language.
Definition: Language.cs:17
Defines a column in a table.
Definition: Column.cs:30
Base class for all query-related events.
Class handling the reception of data from a query.
Definition: Query.cs:12
Task TableDone(string TableId)
Reports a table as being complete.
Definition: Query.cs:327
int NrTablesStarted
Number of tables started.
Definition: Query.cs:115
EventHandlerAsync< QueryTableEventArgs > OnTableDone
Event raised when a table is completed.
Definition: Query.cs:348
Task EndSection()
Ends a section. Each call to BeginSection(string) must be followed by a call to EndSection().
Definition: Query.cs:504
int NrSectionsBegun
Number of sectios begun.
Definition: Query.cs:105
bool IsDone
If the query is done.
Definition: Query.cs:95
void Error(string Error)
Called to inform the viewer of an error state.
EventHandlerAsync< QueryMessageEventArgs > OnMessage
Event raised when a new message has been received.
Definition: Query.cs:424
int NrTablesCompleted
Number of tables compeleted.
Definition: Query.cs:120
Task NewObject(object Object)
Reports a new object.
Definition: Query.cs:354
int NrRecordsReported
Number of records reported.
Definition: Query.cs:135
void Error(DateTime Timestamp, string Error)
Called to inform the viewer of an error state.
void Warning(DateTime Timestamp, string Warning)
Called to inform the viewer of a warning state.
Query(string CommandId, string QueryId, object State, Language Language, INode NodeReference, RequestOrigin Origin)
Class handling the reception of data from a query.
Definition: Query.cs:41
int NrObjectsReported
Number of objects reported.
Definition: Query.cs:125
EventHandlerAsync< QueryEventArgs > OnDone
Event raised when query has been completed.
Definition: Query.cs:264
string QueryID
Query ID
Definition: Query.cs:60
void Exception(DateTime Timestamp, Exception Exception)
Called to inform the viewer of an exception state.
EventHandlerAsync< QueryTitleEventArgs > OnBeginSection
Event raised when a new section is created.
Definition: Query.cs:498
EventHandlerAsync< QueryEventArgs > OnEndSection
Event raised when a section is closed.
Definition: Query.cs:519
void Information(string Comment)
Called to inform the viewer of something.
INode NodeReference
Node reference.
Definition: Query.cs:75
bool HasReported
If anything has been reported.
Definition: Query.cs:163
virtual async Task Abort()
Aborts the query.
Definition: Query.cs:177
void Exception(DateTime Timestamp, string Exception)
Called to inform the viewer of an exception state.
bool IsStarted
If the query has been started.
Definition: Query.cs:85
bool HasTitle
If a title has been set
Definition: Query.cs:158
EventHandlerAsync< QueryTitleEventArgs > OnTitle
Event raised when the report title has been set.
Definition: Query.cs:451
int NrTotalItemsReported
Number of items (total) reported.
Definition: Query.cs:141
Language Language
Language of query.
Definition: Query.cs:70
string CommandID
Command ID
Definition: Query.cs:55
void Warning(string Warning)
Called to inform the viewer of a warning state.
EventHandlerAsync< QueryEventArgs > OnStarted
Event raised when the query has been aborted.
Definition: Query.cs:236
EventHandlerAsync< QueryObjectEventArgs > OnNewObject
Event raised when new records are reported for a table.
Definition: Query.cs:375
Task Start()
Starts query execution.
Definition: Query.cs:213
Task NewRecords(string TableId, params Record[] Records)
Reports a new set of records in a table.
Definition: Query.cs:300
int NrSectionsEnded
Number of sectios ended.
Definition: Query.cs:110
RequestOrigin Origin
Origin of request to execute the query.
Definition: Query.cs:80
bool IsAborted
If the query is aborted.
Definition: Query.cs:90
int SequenceNumber
Curernt sequence number counter.
Definition: Query.cs:100
object State
State object.
Definition: Query.cs:65
Task BeginSection(string Header)
Begins a new section. Sections can be nested. Each call to BeginSection(string) must be followed by a...
Definition: Query.cs:483
EventHandlerAsync< QueryStatusEventArgs > OnStatus
Event raised when the current status changes.
Definition: Query.cs:476
Task NewTable(string TableId, string TableName, params Column[] Columns)
Defines a new table in the query output.
Definition: Query.cs:272
Task LogMessage(QueryEventType Type, QueryEventLevel Level, string Body)
Logs a query message.
Definition: Query.cs:403
void Exception(string Exception)
Called to inform the viewer of an exception state.
Task LogMessage(Exception Exception)
Logs an Exception as a query message.
Definition: Query.cs:381
void Information(DateTime Timestamp, string Comment)
Called to inform the viewer of something.
EventHandlerAsync< QueryNewTableEventArgs > OnNewTable
Event raised when a new table has been created.
Definition: Query.cs:293
bool DecoupledEvents
If events raised from the communication layer are decoupled, i.e. executed in parallel with the sourc...
Definition: Query.cs:527
int NrMessagesReported
Number of messages reported.
Definition: Query.cs:130
async Task Done()
Query execution completed.
Definition: Query.cs:241
int NextSequenceNumber()
Gets the next sequence number.
Definition: Query.cs:169
EventHandlerAsync< QueryNewRecordsEventArgs > OnNewRecords
Event raised when new records are reported for a table.
Definition: Query.cs:321
void Exception(Exception Exception)
Called to inform the viewer of an exception state.
EventHandlerAsync< QueryEventArgs > OnAborted
Event raised when the query has been aborted.
Definition: Query.cs:208
Task SetStatus(string Status)
Sets the current status of the query execution.
Definition: Query.cs:457
Task SetTitle(string Title)
Sets the title of the report.
Definition: Query.cs:430
Task LogMessage(QueryEventLevel Level, Exception Exception)
Logs an Exception as a query message.
Definition: Query.cs:391
Base class for all query-related table events.
Base class for all query-related table events.
Base class for all query-related table events.
Event arguments for query title events.
Base class for all query-related table events.
Event arguments for query title events.
Defines a record in a table.
Definition: Record.cs:9
Tokens available in request.
Definition: RequestOrigin.cs:9
Interface for classes that can be observed.
Interface for nodes that are published through the concentrator interface.
Definition: INode.cs:49
QueryEventType
Query event type.