Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ImplicitSetDefinition.cs
1using System.Collections.Generic;
2using System.Threading.Tasks;
9
11{
16 {
17 private readonly In[] setConditions;
18 private readonly ScriptNode[] otherConditions;
19 private readonly bool doubleColon;
20
31 public ImplicitSetDefinition(ScriptNode Pattern, ScriptNode SuperSet, ScriptNode[] Conditions, bool DoubleColon,
33 : base(Pattern, SuperSet, Start, Length, Expression)
34 {
35 this.doubleColon = DoubleColon;
36
37 Conditions?.SetParent(this);
38
39 SeparateConditions(Conditions, out this.setConditions, out this.otherConditions);
40 }
41
48 public static void SeparateConditions(ScriptNode[] Conditions, out In[] SetConditions, out ScriptNode[] OtherConditions)
49 {
50 List<In> SetConditionList = null;
51 List<ScriptNode> OtherConditionList = null;
52 int i, j, c = Conditions.Length;
53
54 for (i = 0; i < c; i++)
55 {
56 ScriptNode Condition = Conditions[i];
57
58 if (Condition is In In)
59 {
60 if (SetConditionList is null)
61 {
62 SetConditionList = new List<In>();
63
64 if (i > 0)
65 {
66 OtherConditionList = new List<ScriptNode>();
67
68 for (j = 0; j < i; j++)
69 OtherConditionList.Add(Conditions[j]);
70 }
71 }
72
73 SetConditionList.Add(In);
74 }
75 else if (!(SetConditionList is null))
76 {
77 if (OtherConditionList is null)
78 OtherConditionList = new List<ScriptNode>();
79
80 OtherConditionList.Add(Condition);
81 }
82 }
83
84 if (!(SetConditionList is null))
85 {
86 OtherConditions = OtherConditionList?.ToArray();
87 SetConditions = SetConditionList.ToArray();
88 }
89 else
90 {
91 OtherConditions = Conditions;
92 SetConditions = null;
93 }
94 }
95
102 {
103 ISet SuperSet;
104
105 if (this.right is null)
106 SuperSet = null;
107 else
108 {
109 IElement E = this.right.Evaluate(Variables);
110 SuperSet = Set.ToSet(E);
111 if (SuperSet is null)
112 throw new ScriptRuntimeException("Unable to evaluate superset into a set.", this.right);
113 }
114
115 return new ImplicitSet(this.left, SuperSet, this.setConditions, this.otherConditions, Variables, this.doubleColon);
116 }
117
123 public override async Task<IElement> EvaluateAsync(Variables Variables)
124 {
125 if (!this.isAsync)
126 return this.Evaluate(Variables);
127
128 ISet SuperSet;
129
130 if (this.right is null)
131 SuperSet = null;
132 else
133 {
134 IElement E = await this.right.EvaluateAsync(Variables);
135 SuperSet = Set.ToSet(E);
136 if (SuperSet is null)
137 throw new ScriptRuntimeException("Unable to evaluate superset into a set.", this.right);
138 }
139
140 return new ImplicitSet(this.left, SuperSet, this.setConditions, this.otherConditions, Variables, this.doubleColon);
141 }
142
150 public override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
151 {
152 if (Order == SearchMethod.DepthFirst)
153 {
154 if (!(this.left?.ForAllChildNodes(Callback, State, Order) ?? true))
155 return false;
156
157 if (!(this.right?.ForAllChildNodes(Callback, State, Order) ?? true))
158 return false;
159
160 if (!(this.setConditions?.ForAllChildNodes(Callback, State, Order) ?? true))
161 return false;
162
163 if (!(this.otherConditions?.ForAllChildNodes(Callback, State, Order) ?? true))
164 return false;
165 }
166
167 ScriptNode Node;
168 ScriptNode NewNode;
169 int i, c;
170 bool RecalcIsAsync = false;
171 bool b;
172
173 if (!(this.left is null))
174 {
175 b = !Callback(this.left, out NewNode, State);
176 if (!(NewNode is null))
177 {
178 this.left = NewNode;
179 this.left.SetParent(this);
180
181 RecalcIsAsync = true;
182 }
183
184 if (b || (Order == SearchMethod.TreeOrder && !this.left.ForAllChildNodes(Callback, State, Order)))
185 {
186 if (RecalcIsAsync)
187 this.CalcIsAsync();
188
189 return false;
190 }
191 }
192
193 if (!(this.right is null))
194 {
195 b = !Callback(this.right, out NewNode, State);
196 if (!(NewNode is null))
197 {
198 this.right = NewNode;
199 this.right.SetParent(this);
200
201 RecalcIsAsync = true;
202 }
203
204 if (b || (Order == SearchMethod.TreeOrder && !this.right.ForAllChildNodes(Callback, State, Order)))
205 {
206 if (RecalcIsAsync)
207 this.CalcIsAsync();
208
209 return false;
210 }
211 }
212
213 if (!(this.setConditions is null))
214 {
215 for (i = 0, c = this.setConditions.Length; i < c; i++)
216 {
217 Node = this.setConditions[i];
218 if (!(Node is null))
219 {
220 b = !Callback(Node, out NewNode, State);
221 if (!(NewNode is null) && NewNode is In NewIn)
222 {
223 this.setConditions[i] = NewIn;
224 NewIn.SetParent(this);
225 Node = NewNode;
226
227 RecalcIsAsync = true;
228 }
229
230 if (b || (Order == SearchMethod.TreeOrder && !Node.ForAllChildNodes(Callback, State, Order)))
231 {
232 if (RecalcIsAsync)
233 this.CalcIsAsync();
234
235 return false;
236 }
237 }
238 }
239 }
240
241 if (!(this.otherConditions is null))
242 {
243 for (i = 0, c = this.otherConditions.Length; i < c; i++)
244 {
245 Node = this.otherConditions[i];
246 if (!(Node is null))
247 {
248 b = !Callback(Node, out NewNode, State);
249 if (!(NewNode is null))
250 {
251 this.otherConditions[i] = NewNode;
252 NewNode.SetParent(this);
253 Node = NewNode;
254
255 RecalcIsAsync = true;
256 }
257
258 if (b || (Order == SearchMethod.TreeOrder && !Node.ForAllChildNodes(Callback, State, Order)))
259 {
260 if (RecalcIsAsync)
261 this.CalcIsAsync();
262
263 return false;
264 }
265 }
266 }
267 }
268
269 if (RecalcIsAsync)
270 this.CalcIsAsync();
271
272 if (Order == SearchMethod.BreadthFirst)
273 {
274 if (!(this.left?.ForAllChildNodes(Callback, State, Order) ?? true))
275 return false;
276
277 if (!(this.right?.ForAllChildNodes(Callback, State, Order) ?? true))
278 return false;
279
280 if (!(this.setConditions?.ForAllChildNodes(Callback, State, Order) ?? true))
281 return false;
282
283 if (!(this.otherConditions?.ForAllChildNodes(Callback, State, Order) ?? true))
284 return false;
285 }
286
287 return true;
288 }
289
291 public override bool Equals(object obj)
292 {
293 return obj is ImplicitSetDefinition O &&
294 this.doubleColon.Equals(O.doubleColon) &&
295 AreEqual(this.setConditions, O.setConditions) &&
296 AreEqual(this.otherConditions, O.otherConditions) &&
297 base.Equals(obj);
298 }
299
301 public override int GetHashCode()
302 {
303 int Result = base.GetHashCode();
304 Result ^= Result << 5 ^ this.doubleColon.GetHashCode();
305 Result ^= Result << 5 ^ GetHashCode(this.setConditions);
306 Result ^= Result << 5 ^ GetHashCode(this.otherConditions);
307 return Result;
308 }
309 }
310}
Base class for all types of sets.
Definition: Set.cs:14
static ISet ToSet(IElement E)
Converts (if necessary) the element E into a set.
Definition: Set.cs:101
Class managing a script expression.
Definition: Expression.cs:39
Base class for all binary operators.
virtual void CalcIsAsync()
Recalculates if operator is asynchronous or not.
ScriptNode right
Right operand.
ScriptNode left
Left operand.
bool isAsync
If subtree is asynchroneous.
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
static bool AreEqual(ScriptNode S1, ScriptNode S2)
Compares if two script nodes are equal.
Definition: ScriptNode.cs:275
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
void SetParent(ScriptNode Parent)
Sets the parent node. Can only be used when expression is being parsed.
Definition: ScriptNode.cs:132
abstract IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection. This method should be ...
virtual Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection. This method should be ...
Definition: ScriptNode.cs:158
Represents an implicitly defined set
Definition: ImplicitSet.cs:17
Defines a set, by implicitly limiting its members to members of an optional superset,...
ImplicitSetDefinition(ScriptNode Pattern, ScriptNode SuperSet, ScriptNode[] Conditions, bool DoubleColon, int Start, int Length, Expression Expression)
Defines a set, by implicitly limiting its members to members of an optional superset,...
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
static void SeparateConditions(ScriptNode[] Conditions, out In[] SetConditions, out ScriptNode[] OtherConditions)
Separates conditions into set membership conditions and other types of conditions.
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
Basic interface for all types of sets.
Definition: ISet.cs:10
delegate bool ScriptNodeEventHandler(ScriptNode Node, out ScriptNode NewNode, object State)
Delegate for ScriptNode callback methods.
SearchMethod
Method to traverse the expression structure
Definition: ScriptNode.cs:38