Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
RecordProcessor.cs
1using System;
2using System.Threading.Tasks;
3using Waher.Events;
9
11{
15 public class RecordProcessor : IProcessor<object>
16 {
17 private readonly ChunkedList<IElement[]> items = new ChunkedList<IElement[]>();
18 private readonly ScriptNode[] columns;
19 private readonly bool[] columnAsynchronous;
20 private readonly Variables variables;
21 private readonly int count;
22 private readonly bool isAsynchronous;
23 private ObjectProperties properties = null;
24
29 {
30 this.columns = Columns;
31 this.variables = Variables;
32 this.count = this.columns?.Length ?? 0;
33 this.columnAsynchronous = new bool[this.count];
34
35 for (int i = 0; i < this.count; i++)
36 {
37 bool b = this.columnAsynchronous[i] = this.columns[i].IsAsynchronous;
38 this.isAsynchronous |= b;
39 }
40 }
41
45 public bool IsAsynchronous => this.isAsynchronous;
46
50 public int Count => this.items.Count;
51
57 {
58 return this.items.ToArray();
59 }
60
66 public bool TryGetSingleElement(out IElement Result)
67 {
68 if (!this.items.HasFirstItem)
69 {
70 Result = ObjectValue.Null;
71 return true;
72 }
73 else if (this.items.Count == 1 && this.items.FirstItem.Length == 1)
74 {
75 Result = this.items.FirstItem[0];
76 return true;
77 }
78 else
79 {
80 Result = null;
81 return false;
82 }
83 }
84
90 public bool Process(object Object)
91 {
92 IElement Element = Object as IElement;
94 int i;
95
96 if (this.properties is null)
97 this.properties = new ObjectProperties(Element?.AssociatedObjectValue ?? Object, this.variables);
98 else
99 this.properties.Object = Element?.AssociatedObjectValue ?? Object;
100
101 if (this.columns is null)
102 Record = new IElement[1] { Element ?? Expression.Encapsulate(Object) };
103 else
104 {
105 Record = new IElement[this.count];
106
107 for (i = 0; i < this.count; i++)
108 {
109 try
110 {
111 Record[i] = this.columns[i].Evaluate(this.properties);
112 }
113 catch (Exception ex)
114 {
115 ex = Log.UnnestException(ex);
117 }
118 }
119 }
120
121 this.ProcessRecord(Record);
122
123 return true;
124 }
125
130 protected virtual void ProcessRecord(IElement[] Record)
131 {
132 this.items.Add(Record);
133 }
134
140 public async Task<bool> ProcessAsync(object Object)
141 {
142 IElement Element = Object as IElement;
144 int i;
145
146 if (this.properties is null)
147 this.properties = new ObjectProperties(Element?.AssociatedObjectValue ?? Object, this.variables);
148 else
149 this.properties.Object = Element?.AssociatedObjectValue ?? Object;
150
151 if (this.columns is null)
152 Record = new IElement[1] { Element ?? Expression.Encapsulate(Object) };
153 else
154 {
155 Record = new IElement[this.count];
156
157 for (i = 0; i < this.count; i++)
158 {
159 try
160 {
161 if (this.columnAsynchronous[i])
162 Record[i] = await this.columns[i].EvaluateAsync(this.properties);
163 else
164 Record[i] = this.columns[i].Evaluate(this.properties);
165 }
166 catch (Exception ex)
167 {
168 ex = Log.UnnestException(ex);
170 }
171 }
172 }
173
174 this.ProcessRecord(Record);
175
176 return true;
177 }
178
183 public bool Flush()
184 {
185 return true;
186 }
187
192 public Task<bool> FlushAsync()
193 {
194 return Task.FromResult(true);
195 }
196 }
197}
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
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
Base class for all types of elements.
Definition: Element.cs:14
abstract object AssociatedObjectValue
Associated object value.
Definition: Element.cs:43
Class managing a script expression.
Definition: Expression.cs:41
static IElement Encapsulate(object Value)
Encapsulates an object.
Definition: Expression.cs:5241
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
static readonly ObjectValue Null
Null value.
Definition: ObjectValue.cs:88
Processor that generates a tabular record set.
bool IsAsynchronous
If the processor operates asynchronously.
RecordProcessor(ScriptNode[] Columns, Variables Variables)
Processor that generates a tabular record set.
IElement[][] GetRecordSet()
Gets the generated record set.
bool TryGetSingleElement(out IElement Result)
Tries to get a single element result.
bool Flush()
Called at the end of processing, to allow for flushing of buffers, etc.
async Task< bool > ProcessAsync(object Object)
Processes an object asynchronously.
virtual void ProcessRecord(IElement[] Record)
Processes a record.
Task< bool > FlushAsync()
Called at the end of processing, to allow for flushing of buffers, etc.
bool Process(object Object)
Processes an object synchronously.
Represents one record.
Definition: Record.cs:9
Collection of variables.
Definition: Variables.cs:25
Interface for processors of objects.
Definition: IProcessor.cs:9
Basic interface for all types of elements.
Definition: IElement.cs:21