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