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 string commandId;
16 private readonly string queryId;
17 private readonly object state;
18 private bool isStarted = false;
19 private bool isAborted = false;
20 private bool isDone = false;
21 private int seqNr = 0;
22 private int nrSectionsBegun = 0;
23 private int nrSectionsEnded = 0;
24 private int nrTablesStarted = 0;
25 private int nrTablesCompleted = 0;
26 private int nrObjectsReported = 0;
27 private int nrMessagesReported = 0;
28 private int nrRecordsReported = 0;
29 private bool hasTitle = false;
30
39 public Query(string CommandId, string QueryId, object State, Language Language, INode NodeReference)
40 {
41 this.nodeReference = NodeReference;
42 this.commandId = CommandId;
43 this.queryId = QueryId;
44 this.state = State;
45 this.language = Language;
46 }
47
51 public string CommandID => this.commandId;
52
56 public string QueryID => this.queryId;
57
61 public object State => this.state;
62
66 public Language Language => this.language;
67
71 public INode NodeReference => this.nodeReference;
72
76 public bool IsStarted => this.isStarted;
77
81 public bool IsAborted => this.isAborted;
82
86 public bool IsDone => this.isDone;
87
91 public int SequenceNumber => this.seqNr;
92
96 public int NrSectionsBegun => this.nrSectionsBegun;
97
101 public int NrSectionsEnded => this.nrSectionsEnded;
102
106 public int NrTablesStarted => this.nrTablesStarted;
107
111 public int NrTablesCompleted => this.nrTablesCompleted;
112
116 public int NrObjectsReported => this.nrObjectsReported;
117
121 public int NrMessagesReported => this.nrMessagesReported;
122
126 public int NrRecordsReported => this.nrRecordsReported;
127
132 {
133 get
134 {
135 int Result = this.nrSectionsBegun;
136 Result += this.nrSectionsEnded;
137 Result += this.NrTablesStarted;
138 Result += this.nrTablesCompleted;
139 Result += this.nrObjectsReported;
140 Result += this.nrMessagesReported;
141 Result += this.nrRecordsReported;
142 return Result;
143 }
144 }
145
149 public bool HasTitle => this.hasTitle;
150
154 public bool HasReported => this.NrTotalItemsReported > 0;
155
161 {
162 return ++this.seqNr;
163 }
164
168 public virtual async Task Abort()
169 {
170 if (!this.isAborted)
171 {
172 this.isAborted = true;
173 await this.Raise(this.OnAborted, new QueryEventArgs(this), false);
174 }
175 }
176
177 internal Task Abort(QueryEventArgs e)
178 {
179 if (!this.isAborted)
180 {
181 this.isAborted = true;
182 return this.Raise(this.OnAborted, e, false);
183 }
184 else
185 return Task.CompletedTask;
186 }
187
188 private async Task Raise(EventHandlerAsync<QueryEventArgs> Callback, QueryEventArgs e, bool CheckTerminated)
189 {
190 if (CheckTerminated && (this.isAborted || this.isDone))
191 return;
192
193 await Callback.Raise(this, e);
194 }
195
199 public event EventHandlerAsync<QueryEventArgs> OnAborted = null;
200
204 public Task Start()
205 {
206 if (this.isStarted)
207 return Task.CompletedTask;
208
209 this.isStarted = true;
210 return this.Raise(this.OnStarted, new QueryEventArgs(this), false);
211 }
212
213 internal Task Start(QueryEventArgs e)
214 {
215 if (!this.isStarted)
216 {
217 this.isStarted = true;
218 return this.Raise(this.OnStarted, e, false);
219 }
220 else
221 return Task.CompletedTask;
222 }
223
227 public event EventHandlerAsync<QueryEventArgs> OnStarted = null;
228
232 public async Task Done()
233 {
234 if (!this.isDone)
235 {
236 this.isDone = true;
237 await this.Raise(this.OnDone, new QueryEventArgs(this), false);
238 }
239 }
240
241 internal Task Done(QueryEventArgs e)
242 {
243 if (!this.isDone)
244 {
245 this.isDone = true;
246 return this.Raise(this.OnDone, e, false);
247 }
248 else
249 return Task.CompletedTask;
250 }
251
255 public event EventHandlerAsync<QueryEventArgs> OnDone = null;
256
263 public Task NewTable(string TableId, string TableName, params Column[] Columns)
264 {
265 this.nrTablesStarted++;
266 return this.Raise(this.OnNewTable, new QueryNewTableEventArgs(this, TableId, TableName, Columns));
267 }
268
269 internal Task NewTable(QueryNewTableEventArgs e)
270 {
271 this.nrTablesStarted++;
272 return this.Raise(this.OnNewTable, e);
273 }
274
275 private async Task Raise(EventHandlerAsync<QueryNewTableEventArgs> Callback, QueryNewTableEventArgs e)
276 {
277 if (!this.isAborted && !this.isDone)
278 await Callback.Raise(this, e);
279 }
280
284 public event EventHandlerAsync<QueryNewTableEventArgs> OnNewTable = null;
285
291 public Task NewRecords(string TableId, params Record[] Records)
292 {
293 this.nrRecordsReported += Records.Length;
294 return this.Raise(this.OnNewRecords, new QueryNewRecordsEventArgs(this, TableId, Records));
295 }
296
297 internal Task NewRecords(QueryNewRecordsEventArgs e)
298 {
299 this.nrRecordsReported += e.Records.Length;
300 return this.Raise(this.OnNewRecords, e);
301 }
302
303 private async Task Raise(EventHandlerAsync<QueryNewRecordsEventArgs> Callback, QueryNewRecordsEventArgs e)
304 {
305 if (!this.isAborted && !this.isDone)
306 await Callback.Raise(this, e);
307 }
308
312 public event EventHandlerAsync<QueryNewRecordsEventArgs> OnNewRecords = null;
313
318 public Task TableDone(string TableId)
319 {
320 this.nrTablesCompleted++;
321 return this.Raise(this.OnTableDone, new QueryTableEventArgs(this, TableId));
322 }
323
324 internal Task TableDone(QueryTableEventArgs e)
325 {
326 this.nrTablesCompleted++;
327 return this.Raise(this.OnTableDone, e);
328 }
329
330 private async Task Raise(EventHandlerAsync<QueryTableEventArgs> Callback, QueryTableEventArgs e)
331 {
332 if (!this.isAborted && !this.isDone)
333 await Callback.Raise(this, e);
334 }
335
339 public event EventHandlerAsync<QueryTableEventArgs> OnTableDone = null;
340
345 public Task NewObject(object Object)
346 {
347 this.nrObjectsReported++;
348 return this.Raise(this.OnNewObject, new QueryObjectEventArgs(this, Object));
349 }
350
351 internal Task NewObject(QueryObjectEventArgs e)
352 {
353 this.nrObjectsReported++;
354 return this.Raise(this.OnNewObject, e);
355 }
356
357 private async Task Raise(EventHandlerAsync<QueryObjectEventArgs> Callback, QueryObjectEventArgs e)
358 {
359 if (!this.isAborted && !this.isDone)
360 await Callback.Raise(this, e);
361 }
362
366 public event EventHandlerAsync<QueryObjectEventArgs> OnNewObject = null;
367
372 public Task LogMessage(Exception Exception)
373 {
374 return this.LogMessage(QueryEventLevel.Major, Exception);
375 }
376
382 public Task LogMessage(QueryEventLevel Level, Exception Exception)
383 {
384 Exception = Log.UnnestException(Exception);
385 return this.LogMessage(QueryEventType.Exception, Level, Exception.Message);
386 }
387
394 public Task LogMessage(QueryEventType Type, QueryEventLevel Level, string Body)
395 {
396 this.nrMessagesReported++;
397 return this.Raise(this.OnMessage, new QueryMessageEventArgs(this, Type, Level, Body));
398 }
399
400 internal Task LogMessage(QueryMessageEventArgs e)
401 {
402 this.nrMessagesReported++;
403 return this.Raise(this.OnMessage, e);
404 }
405
406 private async Task Raise(EventHandlerAsync<QueryMessageEventArgs> Callback, QueryMessageEventArgs e)
407 {
408 if (!this.isAborted && !this.isDone)
409 await Callback.Raise(this, e);
410 }
411
415 public event EventHandlerAsync<QueryMessageEventArgs> OnMessage = null;
416
421 public Task SetTitle(string Title)
422 {
423 this.hasTitle = true;
424 return this.Raise(this.OnTitle, new QueryTitleEventArgs(this, Title));
425 }
426
427 internal Task SetTitle(QueryTitleEventArgs e)
428 {
429 this.hasTitle = true;
430 return this.Raise(this.OnTitle, e);
431 }
432
433 private async Task Raise(EventHandlerAsync<QueryTitleEventArgs> Callback, QueryTitleEventArgs e)
434 {
435 if (!this.isAborted && !this.isDone)
436 await Callback.Raise(this, e);
437 }
438
442 public event EventHandlerAsync<QueryTitleEventArgs> OnTitle = null;
443
448 public Task SetStatus(string Status)
449 {
450 return this.Raise(this.OnStatus, new QueryStatusEventArgs(this, Status));
451 }
452
453 internal Task SetStatus(QueryStatusEventArgs e)
454 {
455 return this.Raise(this.OnStatus, e);
456 }
457
458 private async Task Raise(EventHandlerAsync<QueryStatusEventArgs> h, QueryStatusEventArgs e)
459 {
460 if (!this.isAborted && !this.isDone)
461 await h.Raise(this, e);
462 }
463
467 public event EventHandlerAsync<QueryStatusEventArgs> OnStatus = null;
468
474 public Task BeginSection(string Header)
475 {
476 this.nrSectionsBegun++;
477 return this.Raise(this.OnBeginSection, new QueryTitleEventArgs(this, Header));
478 }
479
480 internal Task BeginSection(QueryTitleEventArgs e)
481 {
482 this.nrSectionsBegun++;
483 return this.Raise(this.OnBeginSection, e);
484 }
485
489 public event EventHandlerAsync<QueryTitleEventArgs> OnBeginSection = null;
490
495 public Task EndSection()
496 {
497 this.nrSectionsEnded++;
498 return this.Raise(this.OnEndSection, new QueryEventArgs(this), true);
499 }
500
501 internal Task EndSection(QueryEventArgs e)
502 {
503 this.nrSectionsEnded++;
504 return this.Raise(this.OnEndSection, e, true);
505 }
506
510 public event EventHandlerAsync<QueryEventArgs> OnEndSection = null;
511
512 #region ICommunicationLayer
513
518 public bool DecoupledEvents => false;
519
524 public Task Information(string Comment) => this.LogMessage(QueryEventType.Information, QueryEventLevel.Minor, Comment);
525
530 public Task Warning(string Warning) => this.LogMessage(QueryEventType.Warning, QueryEventLevel.Minor, Warning);
531
536 public Task Error(string Error) => this.LogMessage(QueryEventType.Error, QueryEventLevel.Minor, Error);
537
542 public Task Exception(Exception Exception) => this.LogMessage(QueryEventType.Exception, QueryEventLevel.Minor, Exception.Message);
543
548 public Task Exception(string Exception) => this.LogMessage(QueryEventType.Exception, QueryEventLevel.Minor, Exception);
549
555 public Task Information(DateTime Timestamp, string Comment) => this.LogMessage(QueryEventType.Information, QueryEventLevel.Minor, Comment);
556
562 public Task Warning(DateTime Timestamp, string Warning) => this.LogMessage(QueryEventType.Warning, QueryEventLevel.Minor, Warning);
563
569 public Task Error(DateTime Timestamp, string Error) => this.LogMessage(QueryEventType.Error, QueryEventLevel.Minor, Error);
570
576 public Task Exception(DateTime Timestamp, string Exception) => this.LogMessage(QueryEventType.Exception, QueryEventLevel.Minor, Exception);
577
583 public Task Exception(DateTime Timestamp, Exception Exception) => this.LogMessage(QueryEventType.Exception, QueryEventLevel.Minor, Exception.Message);
584
585 #endregion
586 }
587}
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static Exception UnnestException(Exception Exception)
Unnests an exception, to extract the relevant inner exception.
Definition: Log.cs:818
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:318
int NrTablesStarted
Number of tables started.
Definition: Query.cs:106
EventHandlerAsync< QueryTableEventArgs > OnTableDone
Event raised when a table is completed.
Definition: Query.cs:339
Task Information(DateTime Timestamp, string Comment)
Called to inform the viewer of something.
Task EndSection()
Ends a section. Each call to BeginSection(string) must be followed by a call to EndSection().
Definition: Query.cs:495
int NrSectionsBegun
Number of sectios begun.
Definition: Query.cs:96
bool IsDone
If the query is done.
Definition: Query.cs:86
Task 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:415
int NrTablesCompleted
Number of tables compeleted.
Definition: Query.cs:111
Task NewObject(object Object)
Reports a new object.
Definition: Query.cs:345
int NrRecordsReported
Number of records reported.
Definition: Query.cs:126
int NrObjectsReported
Number of objects reported.
Definition: Query.cs:116
EventHandlerAsync< QueryEventArgs > OnDone
Event raised when query has been completed.
Definition: Query.cs:255
Task Exception(DateTime Timestamp, string Exception)
Called to inform the viewer of an exception state.
string QueryID
Query ID
Definition: Query.cs:56
EventHandlerAsync< QueryTitleEventArgs > OnBeginSection
Event raised when a new section is created.
Definition: Query.cs:489
EventHandlerAsync< QueryEventArgs > OnEndSection
Event raised when a section is closed.
Definition: Query.cs:510
INode NodeReference
Node reference.
Definition: Query.cs:71
bool HasReported
If anything has been reported.
Definition: Query.cs:154
virtual async Task Abort()
Aborts the query.
Definition: Query.cs:168
bool IsStarted
If the query has been started.
Definition: Query.cs:76
Query(string CommandId, string QueryId, object State, Language Language, INode NodeReference)
Class handling the reception of data from a query.
Definition: Query.cs:39
bool HasTitle
If a title has been set
Definition: Query.cs:149
EventHandlerAsync< QueryTitleEventArgs > OnTitle
Event raised when the report title has been set.
Definition: Query.cs:442
int NrTotalItemsReported
Number of items (total) reported.
Definition: Query.cs:132
Language Language
Language of query.
Definition: Query.cs:66
string CommandID
Command ID
Definition: Query.cs:51
Task Exception(string Exception)
Called to inform the viewer of an exception state.
Task Exception(Exception Exception)
Called to inform the viewer of an exception state.
EventHandlerAsync< QueryEventArgs > OnStarted
Event raised when the query has been aborted.
Definition: Query.cs:227
EventHandlerAsync< QueryObjectEventArgs > OnNewObject
Event raised when new records are reported for a table.
Definition: Query.cs:366
Task Start()
Starts query execution.
Definition: Query.cs:204
Task NewRecords(string TableId, params Record[] Records)
Reports a new set of records in a table.
Definition: Query.cs:291
int NrSectionsEnded
Number of sectios ended.
Definition: Query.cs:101
bool IsAborted
If the query is aborted.
Definition: Query.cs:81
int SequenceNumber
Curernt sequence number counter.
Definition: Query.cs:91
Task Warning(DateTime Timestamp, string Warning)
Called to inform the viewer of a warning state.
object State
State object.
Definition: Query.cs:61
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:474
Task Exception(DateTime Timestamp, Exception Exception)
Called to inform the viewer of an exception state.
EventHandlerAsync< QueryStatusEventArgs > OnStatus
Event raised when the current status changes.
Definition: Query.cs:467
Task Warning(string Warning)
Called to inform the viewer of a warning state.
Task NewTable(string TableId, string TableName, params Column[] Columns)
Defines a new table in the query output.
Definition: Query.cs:263
Task LogMessage(QueryEventType Type, QueryEventLevel Level, string Body)
Logs a query message.
Definition: Query.cs:394
Task LogMessage(Exception Exception)
Logs an Exception as a query message.
Definition: Query.cs:372
EventHandlerAsync< QueryNewTableEventArgs > OnNewTable
Event raised when a new table has been created.
Definition: Query.cs:284
bool DecoupledEvents
If events raised from the communication layer are decoupled, i.e. executed in parallel with the sourc...
Definition: Query.cs:518
Task Error(DateTime Timestamp, string Error)
Called to inform the viewer of an error state.
int NrMessagesReported
Number of messages reported.
Definition: Query.cs:121
async Task Done()
Query execution completed.
Definition: Query.cs:232
int NextSequenceNumber()
Gets the next sequence number.
Definition: Query.cs:160
EventHandlerAsync< QueryNewRecordsEventArgs > OnNewRecords
Event raised when new records are reported for a table.
Definition: Query.cs:312
EventHandlerAsync< QueryEventArgs > OnAborted
Event raised when the query has been aborted.
Definition: Query.cs:199
Task SetStatus(string Status)
Sets the current status of the query execution.
Definition: Query.cs:448
Task SetTitle(string Title)
Sets the title of the report.
Definition: Query.cs:421
Task LogMessage(QueryEventLevel Level, Exception Exception)
Logs an Exception as a query message.
Definition: Query.cs:382
Task Information(string Comment)
Called to inform the viewer of something.
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
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.