Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CustomOrderProcessor.cs
1using System;
3using System.Threading.Tasks;
9
11{
15 public class CustomOrderProcessor : IProcessor<object>
16 {
17 private readonly IProcessor<object> processor;
18 private readonly bool isAsynchronous;
19 private readonly Dictionary<Type, ObjectProperties> propertiesX = new Dictionary<Type, ObjectProperties>();
20 private readonly Dictionary<Type, ObjectProperties> propertiesY = new Dictionary<Type, ObjectProperties>();
21 private readonly KeyValuePair<ScriptNode, bool>[] order;
22 private readonly List<object> items = new List<object>();
23 private readonly Variables variables;
24
32 Variables Variables, KeyValuePair<ScriptNode, bool>[] Order)
33 {
34 this.processor = Processor;
35 this.order = Order;
36 this.variables = Variables;
37 this.isAsynchronous = Processor.IsAsynchronous;
38
39 foreach (KeyValuePair<ScriptNode, bool> P in Order)
40 {
41 if (P.Key.IsAsynchronous)
42 {
43 this.isAsynchronous = true;
44 break;
45 }
46 }
47 }
48
52 public bool IsAsynchronous => this.isAsynchronous;
53
59 public bool Process(object Object)
60 {
61 this.items.Add(Object);
62 return true;
63 }
64
70 public Task<bool> ProcessAsync(object Object)
71 {
72 this.items.Add(Object);
73 return Task.FromResult(true);
74 }
75
80 public bool Flush()
81 {
82 this.Sort();
83
84 foreach (object Item in this.items)
85 {
86 if (!this.processor.Process(Item))
87 return false;
88 }
89
90 return this.processor.Flush();
91 }
92
93 private void Sort()
94 {
95 this.items.Sort((x, y) =>
96 {
97 if (x is null)
98 {
99 if (y is null)
100 return 0;
101 else
102 return -1;
103 }
104 else if (y is null)
105 return 1;
106
107 Type Tx = x.GetType();
108 Type Ty = y.GetType();
109
110 if (this.propertiesX.TryGetValue(Tx, out ObjectProperties Vx))
111 Vx.Object = x;
112 else
113 {
114 Vx = new ObjectProperties(x, this.variables);
115 this.propertiesX[Tx] = Vx;
116 }
117
118 if (this.propertiesY.TryGetValue(Ty, out ObjectProperties Vy))
119 Vy.Object = y;
120 else
121 {
122 Vy = new ObjectProperties(y, this.variables);
123 this.propertiesY[Ty] = Vy;
124 }
125
126 int i, j, c = this.order.Length;
127 IElement Ex, Ey;
128 ScriptNode Node;
129
130 for (i = 0; i < c; i++)
131 {
132 Node = this.order[i].Key;
133 Ex = Node.Evaluate(Vx); // TODO: Async
134 Ey = Node.Evaluate(Vy); // TODO: Async
135
136 if (!(Ex.AssociatedSet is IOrderedSet S))
137 throw new ScriptRuntimeException("Result not member of an ordered set.", Node);
138
139 j = S.Compare(Ex, Ey);
140 if (j != 0)
141 {
142 if (this.order[i].Value)
143 return j;
144 else
145 return -j;
146 }
147 }
148
149 return 0;
150 });
151 }
152
157 public async Task<bool> FlushAsync()
158 {
159 this.Sort();
160
161 foreach (object Item in this.items)
162 {
163 if (!await this.processor.ProcessAsync(Item))
164 return false;
165 }
166
167 return await this.processor.FlushAsync();
168 }
169 }
170}
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
async Task< bool > FlushAsync()
Called at the end of processing, to allow for flushing of buffers, etc.
bool Process(object Object)
Processes an object synchronously.
Task< bool > ProcessAsync(object Object)
Processes an object asynchronously.
bool IsAsynchronous
If the processor operates asynchronously.
CustomOrderProcessor(IProcessor< object > Processor, Variables Variables, KeyValuePair< ScriptNode, bool >[] Order)
Processor that reorders a sequence of items.
bool Flush()
Called at the end of processing, to allow for flushing of buffers, etc.
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
Basic interface for ordered sets.
Definition: IOrderedSet.cs:11