Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ObjectExNihilo.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
8
10{
15 {
16 private readonly LinkedList<KeyValuePair<string, ScriptNode>> members;
17 private Dictionary<string, ScriptNode> quick = null;
18 private bool isAsync;
19
27 public ObjectExNihilo(LinkedList<KeyValuePair<string, ScriptNode>> Members, int Start, int Length, Expression Expression)
28 : base(Start, Length, Expression)
29 {
30 this.members = Members;
31
32 foreach (KeyValuePair<string, ScriptNode> P in Members)
33 P.Value?.SetParent(this);
34
35 this.CalcIsAsync();
36 }
37
38 private void CalcIsAsync()
39 {
40 this.isAsync = false;
41
42 foreach (KeyValuePair<string, ScriptNode> P in this.members)
43 {
44 if (P.Value?.IsAsynchronous ?? false)
45 {
46 this.isAsync = true;
47 break;
48 }
49 }
50 }
51
55 public LinkedList<KeyValuePair<string, ScriptNode>> Members => this.members;
56
61 public override bool IsAsynchronous => this.isAsync;
62
69 {
70 Dictionary<string, IElement> Result = new Dictionary<string, IElement>();
71
72 foreach (KeyValuePair<string, ScriptNode> P in this.members)
73 Result[P.Key] = P.Value.Evaluate(Variables);
74
75 return new ObjectValue(Result);
76 }
77
83 public override async Task<IElement> EvaluateAsync(Variables Variables)
84 {
85 if (!this.isAsync)
86 return this.Evaluate(Variables);
87
88 Dictionary<string, IElement> Result = new Dictionary<string, IElement>();
89
90 foreach (KeyValuePair<string, ScriptNode> P in this.members)
91 Result[P.Key] = await P.Value.EvaluateAsync(Variables);
92
93 return new ObjectValue(Result);
94 }
95
103 public override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
104 {
105 LinkedListNode<KeyValuePair<string, ScriptNode>> Loop;
106
107 if (Order == SearchMethod.DepthFirst)
108 {
109 Loop = this.members.First;
110
111 while (!(Loop is null))
112 {
113 if (!(Loop.Value.Value?.ForAllChildNodes(Callback, State, Order) ?? true))
114 return false;
115
116 Loop = Loop.Next;
117 }
118 }
119
120 Loop = this.members.First;
121
122 ScriptNode Node;
123 bool RecalcIsAsync = false;
124
125 while (!(Loop is null))
126 {
127 Node = Loop.Value.Value;
128 if (!(Node is null))
129 {
130 bool Result = Callback(Node, out ScriptNode NewNode, State);
131 if (!(NewNode is null))
132 {
133 Loop.Value = new KeyValuePair<string, ScriptNode>(Loop.Value.Key, NewNode);
134 NewNode.SetParent(this);
135 Node = NewNode;
136
137 RecalcIsAsync = true;
138 }
139
140 if (!Result || (Order == SearchMethod.TreeOrder && !Node.ForAllChildNodes(Callback, State, Order)))
141 {
142 if (RecalcIsAsync)
143 this.CalcIsAsync();
144
145 return false;
146 }
147 }
148
149 Loop = Loop.Next;
150 }
151
152 if (RecalcIsAsync)
153 this.CalcIsAsync();
154
155 if (Order == SearchMethod.BreadthFirst)
156 {
157 Loop = this.members.First;
158
159 while (!(Loop is null))
160 {
161 if (!(Loop.Value.Value?.ForAllChildNodes(Callback, State, Order) ?? true))
162 return false;
163
164 Loop = Loop.Next;
165 }
166 }
167
168 return true;
169 }
170
172 public override bool Equals(object obj)
173 {
174 if (!(obj is ObjectExNihilo O) || !base.Equals(obj))
175 return false;
176
177 LinkedList<KeyValuePair<string, ScriptNode>>.Enumerator e1 = this.members.GetEnumerator();
178 LinkedList<KeyValuePair<string, ScriptNode>>.Enumerator e2 = O.members.GetEnumerator();
179
180 while (true)
181 {
182 bool b1 = e1.MoveNext();
183 bool b2 = e2.MoveNext();
184
185 if (b1 ^ b2)
186 return false;
187
188 if (!b1)
189 return true;
190
191 KeyValuePair<string, ScriptNode> Item1 = e1.Current;
192 KeyValuePair<string, ScriptNode> Item2 = e2.Current;
193
194 if (!Item1.Key.Equals(Item2.Key) ||
195 !Item1.Value.Equals(Item2.Value))
196 {
197 return false;
198 }
199 }
200 }
201
203 public override int GetHashCode()
204 {
205 int Result = base.GetHashCode();
206
207 foreach (KeyValuePair<string, ScriptNode> P in this.members)
208 {
209 Result ^= Result << 5 ^ P.Key.GetHashCode();
210 Result ^= Result << 5 ^ P.Value.GetHashCode();
211 }
212
213 return Result;
214 }
215
222 public override PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary<string, IElement> AlreadyFound)
223 {
224 PatternMatchResult Result;
225
226 if (CheckAgainst.AssociatedObjectValue is IDictionary<string, IElement> Object)
227 {
228 this.CheckQuick();
229
230 foreach (KeyValuePair<string, IElement> P in Object)
231 {
232 if (!this.quick.ContainsKey(P.Key))
233 return PatternMatchResult.NoMatch;
234 }
235
236 foreach (KeyValuePair<string, ScriptNode> P in this.members)
237 {
238 if (Object.TryGetValue(P.Key, out IElement E))
239 Result = P.Value.PatternMatch(E, AlreadyFound);
240 else
241 Result = P.Value.PatternMatch(ObjectValue.Null, AlreadyFound);
242
243 if (Result != PatternMatchResult.Match)
244 return Result;
245 }
246 }
247 else if (CheckAgainst.AssociatedObjectValue is IDictionary<string, object> Object2)
248 {
249 this.CheckQuick();
250
251 foreach (KeyValuePair<string, object> P in Object2)
252 {
253 if (!this.quick.ContainsKey(P.Key))
254 return PatternMatchResult.NoMatch;
255 }
256
257 foreach (KeyValuePair<string, ScriptNode> P in this.members)
258 {
259 if (Object2.TryGetValue(P.Key, out object E))
260 {
261 if (E is Array A)
262 Result = P.Value.PatternMatch(VectorDefinition.Encapsulate(A, false, this), AlreadyFound);
263 else
264 Result = P.Value.PatternMatch(Expression.Encapsulate(E), AlreadyFound);
265 }
266 else
267 Result = P.Value.PatternMatch(ObjectValue.Null, AlreadyFound);
268
269 if (Result != PatternMatchResult.Match)
270 return Result;
271 }
272 }
273 else if (CheckAgainst.AssociatedObjectValue is IDictionary<string, string> Object3)
274 {
275 this.CheckQuick();
276
277 foreach (KeyValuePair<string, string> P in Object3)
278 {
279 if (!this.quick.ContainsKey(P.Key))
280 return PatternMatchResult.NoMatch;
281 }
282
283 foreach (KeyValuePair<string, ScriptNode> P in this.members)
284 {
285 if (Object3.TryGetValue(P.Key, out string s))
286 Result = P.Value.PatternMatch(new StringValue(s), AlreadyFound);
287 else
288 Result = P.Value.PatternMatch(ObjectValue.Null, AlreadyFound);
289
290 if (Result != PatternMatchResult.Match)
291 return Result;
292 }
293 }
294 else
295 return PatternMatchResult.NoMatch;
296
297 return PatternMatchResult.Match;
298 }
299
300 private void CheckQuick()
301 {
302 if (this.quick is null)
303 {
304 Dictionary<string, ScriptNode> Quick = new Dictionary<string, ScriptNode>();
305
306 foreach (KeyValuePair<string, ScriptNode> N in this.members)
307 Quick[N.Key] = N.Value;
308
309 this.quick = Quick;
310 }
311 }
312
313 }
314}
Class managing a script expression.
Definition: Expression.cs:39
static IElement Encapsulate(object Value)
Encapsulates an object.
Definition: Expression.cs:4955
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, bool DepthFirst)
Calls the callback method for all child nodes.
Definition: ScriptNode.cs:243
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
static readonly ObjectValue Null
Null value.
Definition: ObjectValue.cs:86
Creates an object from nothing.
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
override bool Equals(object obj)
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
ObjectExNihilo(LinkedList< KeyValuePair< string, ScriptNode > > Members, int Start, int Length, Expression Expression)
Creates an object from nothing.
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
override PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary< string, IElement > AlreadyFound)
Performs a pattern match operation.
LinkedList< KeyValuePair< string, ScriptNode > > Members
Members, in order of definition.
static IElement Encapsulate(Array Elements, bool CanEncapsulateAsMatrix, ScriptNode Node)
Encapsulates the elements of a vector.
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33
delegate bool ScriptNodeEventHandler(ScriptNode Node, out ScriptNode NewNode, object State)
Delegate for ScriptNode callback methods.
PatternMatchResult
Status result of a pattern matching operation.
Definition: ScriptNode.cs:17
SearchMethod
Method to traverse the expression structure
Definition: ScriptNode.cs:38