Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ImplicitVectorDefinition.cs
1using System.Collections.Generic;
2using System.Threading.Tasks;
8
10{
15 {
16 private readonly In[] setConditions;
17 private readonly ScriptNode[] otherConditions;
18
28 public ImplicitVectorDefinition(ScriptNode Pattern, ScriptNode Vector, ScriptNode[] Conditions,
30 : base(Pattern, Vector, Start, Length, Expression)
31 {
32 Conditions?.SetParent(this);
33
34 Sets.ImplicitSetDefinition.SeparateConditions(Conditions, out this.setConditions, out this.otherConditions);
35 }
36
43 {
44 IEnumerable<IElement> VectorElements;
45 bool CanEncapsulateAsMatrix;
46
47 if (this.right is null)
48 {
49 VectorElements = null;
50 CanEncapsulateAsMatrix = true;
51 }
52 else
53 {
54 IElement E = this.right.Evaluate(Variables);
55 CanEncapsulateAsMatrix = (E is IMatrix);
56 VectorElements = ImplicitSet.GetSetMembers(E);
57
58 if (VectorElements is null)
59 throw new ScriptRuntimeException("Unable to evaluate vector elements.", this.right);
60 }
61
62 IEnumerable<IElement> Elements = ImplicitSet.CalculateElements(this.left, VectorElements,
63 this.setConditions, this.otherConditions, Variables);
64
65 if (!(Elements is ICollection<IElement> Elements2))
66 {
67 Elements2 = new List<IElement>();
68
69 foreach (IElement E in Elements)
70 Elements2.Add(E);
71 }
72
73 return VectorDefinition.Encapsulate(Elements2, CanEncapsulateAsMatrix, this);
74 }
75
81 public override async Task<IElement> EvaluateAsync(Variables Variables)
82 {
83 if (!this.isAsync)
84 return this.Evaluate(Variables);
85
86 IEnumerable<IElement> VectorElements;
87 bool CanEncapsulateAsMatrix;
88
89 if (this.right is null)
90 {
91 VectorElements = null;
92 CanEncapsulateAsMatrix = true;
93 }
94 else
95 {
96 IElement E = await this.right.EvaluateAsync(Variables);
97 CanEncapsulateAsMatrix = (E is IMatrix);
98 VectorElements = ImplicitSet.GetSetMembers(E);
99
100 if (VectorElements is null)
101 throw new ScriptRuntimeException("Unable to evaluate vector elements.", this.right);
102 }
103
104 IEnumerable<IElement> Elements = await ImplicitSet.CalculateElementsAsync(this.left, VectorElements,
105 this.setConditions, this.otherConditions, Variables);
106
107 if (!(Elements is ICollection<IElement> Elements2))
108 {
109 Elements2 = new List<IElement>();
110
111 foreach (IElement E in Elements)
112 Elements2.Add(E);
113 }
114
115 return VectorDefinition.Encapsulate(Elements2, CanEncapsulateAsMatrix, this);
116 }
117
125 public override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
126 {
127 if (Order == SearchMethod.DepthFirst)
128 {
129 if (!(this.left?.ForAllChildNodes(Callback, State, Order) ?? true))
130 return false;
131
132 if (!(this.right?.ForAllChildNodes(Callback, State, Order) ?? true))
133 return false;
134
135 if (!(this.setConditions?.ForAllChildNodes(Callback, State, Order) ?? true))
136 return false;
137
138 if (!(this.otherConditions?.ForAllChildNodes(Callback, State, Order) ?? true))
139 return false;
140 }
141
142 ScriptNode Node;
143 ScriptNode NewNode;
144 int i, c;
145 bool RecalcIsAsync = false;
146 bool b;
147
148 if (!(this.left is null))
149 {
150 b = !Callback(this.left, out NewNode, State);
151 if (!(NewNode is null))
152 {
153 this.left = NewNode;
154 this.left.SetParent(this);
155
156 RecalcIsAsync = true;
157 }
158
159 if (b || (Order == SearchMethod.TreeOrder && !this.left.ForAllChildNodes(Callback, State, Order)))
160 {
161 if (RecalcIsAsync)
162 this.CalcIsAsync();
163
164 return false;
165 }
166 }
167
168 if (!(this.right is null))
169 {
170 b = !Callback(this.right, out NewNode, State);
171 if (!(NewNode is null))
172 {
173 this.right = NewNode;
174 this.right.SetParent(this);
175
176 RecalcIsAsync = true;
177 }
178
179 if (b || (Order == SearchMethod.TreeOrder && !this.right.ForAllChildNodes(Callback, State, Order)))
180 {
181 if (RecalcIsAsync)
182 this.CalcIsAsync();
183
184 return false;
185 }
186 }
187
188 if (!(this.setConditions is null))
189 {
190 for (i = 0, c = this.setConditions.Length; i < c; i++)
191 {
192 Node = this.setConditions[i];
193 if (!(Node is null))
194 {
195 b = !Callback(Node, out NewNode, State);
196 if (!(NewNode is null) && NewNode is In NewIn)
197 {
198 this.setConditions[i] = NewIn;
199 NewIn.SetParent(this);
200 Node = NewNode;
201
202 RecalcIsAsync = true;
203 }
204
205 if (b || (Order == SearchMethod.TreeOrder && !Node.ForAllChildNodes(Callback, State, Order)))
206 {
207 if (RecalcIsAsync)
208 this.CalcIsAsync();
209
210 return false;
211 }
212 }
213 }
214 }
215
216 if (!(this.otherConditions is null))
217 {
218 for (i = 0, c = this.otherConditions.Length; i < c; i++)
219 {
220 Node = this.otherConditions[i];
221 if (!(Node is null))
222 {
223 b = !Callback(Node, out NewNode, State);
224 if (!(NewNode is null))
225 {
226 this.otherConditions[i] = NewNode;
227 NewNode.SetParent(this);
228 Node = NewNode;
229
230 RecalcIsAsync = true;
231 }
232
233 if (b || (Order == SearchMethod.TreeOrder && !Node.ForAllChildNodes(Callback, State, Order)))
234 {
235 if (RecalcIsAsync)
236 this.CalcIsAsync();
237
238 return false;
239 }
240 }
241 }
242 }
243
244 if (RecalcIsAsync)
245 this.CalcIsAsync();
246
247 if (Order == SearchMethod.BreadthFirst)
248 {
249 if (!(this.left?.ForAllChildNodes(Callback, State, Order) ?? true))
250 return false;
251
252 if (!(this.right?.ForAllChildNodes(Callback, State, Order) ?? true))
253 return false;
254
255 if (!(this.setConditions?.ForAllChildNodes(Callback, State, Order) ?? true))
256 return false;
257
258 if (!(this.otherConditions?.ForAllChildNodes(Callback, State, Order) ?? true))
259 return false;
260 }
261
262 return true;
263 }
264
266 public override bool Equals(object obj)
267 {
268 return obj is ImplicitVectorDefinition O &&
269 AreEqual(this.setConditions, O.setConditions) &&
270 AreEqual(this.otherConditions, O.otherConditions) &&
271 base.Equals(obj);
272 }
273
275 public override int GetHashCode()
276 {
277 int Result = base.GetHashCode();
278 Result ^= Result << 5 ^ GetHashCode(this.setConditions);
279 Result ^= Result << 5 ^ GetHashCode(this.otherConditions);
280 return Result;
281 }
282 }
283}
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
static async Task< IEnumerable< IElement > > CalculateElementsAsync(ScriptNode Pattern, IEnumerable< IElement > SuperSetElements, In[] SetConditions, ScriptNode[] OtherConditions, Variables Variables)
Calculates elements specified using implicit notation.
Definition: ImplicitSet.cs:415
static IEnumerable< IElement > GetSetMembers(IElement E)
Gets the elements of a (supposed) set.
Definition: ImplicitSet.cs:604
static IEnumerable< IElement > CalculateElements(ScriptNode Pattern, IEnumerable< IElement > SuperSetElements, In[] SetConditions, ScriptNode[] OtherConditions, Variables Variables)
Calculates elements specified using implicit notation.
Definition: ImplicitSet.cs:222
Defines a vector, by implicitly limiting its members to members of an optional vector,...
override async Task< IElement > EvaluateAsync(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 IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
ImplicitVectorDefinition(ScriptNode Pattern, ScriptNode Vector, ScriptNode[] Conditions, int Start, int Length, Expression Expression)
Defines a vector, by implicitly limiting its members to members of an optional vector,...
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
Basic interface for matrices.
Definition: IMatrix.cs:9
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