Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
FieldAggregatorProcessor.cs
1using System;
3using System.Threading.Tasks;
9
11{
15 public class FieldAggregatorProcessor : IProcessor<object>
16 {
17 private readonly KeyValuePair<string, ScriptNode>[] additionalFields;
18 private readonly bool[] additionalFieldAsynchronous;
19 private readonly IProcessor<object> processor;
20 private readonly Variables variables;
21 private readonly int count;
22 private readonly bool isAsynchronous;
23 private ObjectProperties objectVariables = null;
24
33 KeyValuePair<string, int>[] AdditionalFields, ScriptNode[] Columns)
34 {
35 this.processor = Processor;
36 this.variables = Variables;
37
38 this.count = AdditionalFields.Length;
39
40 this.additionalFieldAsynchronous = new bool[this.count];
41 this.additionalFields = new KeyValuePair<string, ScriptNode>[this.count];
42 this.isAsynchronous = Processor.IsAsynchronous;
43
44 for (int i = 0; i < this.count; i++)
45 {
46 KeyValuePair<string, int> P = AdditionalFields[i];
47 ScriptNode Node = Columns[P.Value];
48
49 this.additionalFields[i] = new KeyValuePair<string, ScriptNode>(P.Key, Node);
50
51 if (this.additionalFieldAsynchronous[i] = Node.IsAsynchronous)
52 this.isAsynchronous = true;
53 }
54 }
55
59 public bool IsAsynchronous => this.isAsynchronous;
60
66 public bool Process(object Object)
67 {
68 KeyValuePair<string, ScriptNode> P;
69 int i;
70
71 if (this.objectVariables is null)
72 this.objectVariables = new ObjectProperties(Object, this.variables);
73 else
74 this.objectVariables.Object = Object;
75
76 if (Object is GenericObject GenObj)
77 {
78 for (i = 0; i < this.count; i++)
79 {
80 P = this.additionalFields[i];
81 GenObj[P.Key] = P.Value.Evaluate(this.objectVariables);
82 }
83 }
84 else if (Object is GroupObject GroupObj)
85 {
86 for (i = 0; i < this.count; i++)
87 {
88 P = this.additionalFields[i];
89 GroupObj[P.Key] = P.Value.Evaluate(this.objectVariables);
90 }
91 }
92 else
93 {
94 GroupObject Obj = new GroupObject(Array.Empty<object>(), Array.Empty<IElement>(),
95 Array.Empty<ScriptNode>(), this.objectVariables);
96
97 for (i = 0; i < this.count; i++)
98 {
99 P = this.additionalFields[i];
100 Obj[P.Key] = P.Value.Evaluate(this.objectVariables);
101 }
102
103 Object = Obj;
104 }
105
106 return this.processor.Process(Object);
107 }
108
114 public async Task<bool> ProcessAsync(object Object)
115 {
116 KeyValuePair<string, ScriptNode> P;
117 int i;
118
119 if (this.objectVariables is null)
120 this.objectVariables = new ObjectProperties(Object, this.variables);
121 else
122 this.objectVariables.Object = Object;
123
124 if (Object is GenericObject GenObj)
125 {
126 for (i = 0; i < this.count; i++)
127 {
128 P = this.additionalFields[i];
129
130 if (this.additionalFieldAsynchronous[i])
131 GenObj[P.Key] = await P.Value.EvaluateAsync(this.objectVariables);
132 else
133 GenObj[P.Key] = P.Value.Evaluate(this.objectVariables);
134 }
135 }
136 else if (Object is GroupObject GroupObj)
137 {
138 for (i = 0; i < this.count; i++)
139 {
140 P = this.additionalFields[i];
141
142 if (this.additionalFieldAsynchronous[i])
143 GroupObj[P.Key] = await P.Value.EvaluateAsync(this.objectVariables);
144 else
145 GroupObj[P.Key] = P.Value.Evaluate(this.objectVariables);
146 }
147 }
148 else
149 {
150 GroupObject Obj = new GroupObject(Array.Empty<object>(), Array.Empty<IElement>(),
151 Array.Empty<ScriptNode>(), this.objectVariables);
152
153 for (i = 0; i < this.count; i++)
154 {
155 P = this.additionalFields[i];
156
157 if (this.additionalFieldAsynchronous[i])
158 Obj[P.Key] = await P.Value.EvaluateAsync(this.objectVariables);
159 else
160 Obj[P.Key] = P.Value.Evaluate(this.objectVariables);
161 }
162
163 Object = Obj;
164 }
165
166 return await this.processor.ProcessAsync(Object);
167 }
168
173 public bool Flush() => this.processor.Flush();
174
179 public Task<bool> FlushAsync() => this.processor.FlushAsync();
180 }
181}
Generic object. Contains a sequence of properties.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
virtual bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: ScriptNode.cs:142
Represents a collection of objects grouped together useing a GROUP BY construct.
Definition: GroupObject.cs:12
bool Flush()
Called at the end of processing, to allow for flushing of buffers, etc.
Task< bool > FlushAsync()
Called at the end of processing, to allow for flushing of buffers, etc.
FieldAggregatorProcessor(IProcessor< object > Processor, Variables Variables, KeyValuePair< string, int >[] AdditionalFields, ScriptNode[] Columns)
Processor that adds fields to processed items.
async Task< bool > ProcessAsync(object Object)
Processes an object asynchronously.
bool Process(object Object)
Processes an object synchronously.
Collection of variables.
Definition: Variables.cs:25
Interface for processors of objects.
Definition: IProcessor.cs:9
bool IsAsynchronous
If the processor operates asynchronously.
Definition: IProcessor.cs:13
Basic interface for all types of elements.
Definition: IElement.cs:21