Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
VectorForEachDefinition.cs
3using System.Threading.Tasks;
9
11{
16 {
17 private readonly string variableName;
18
27 : base(Elements.LeftOperand, Elements.RightOperand, Start, Length, Expression)
28 {
29 this.variableName = Elements.VariableName;
30 }
31
35 public string VariableName => this.variableName;
36
43 {
44 IElement S = this.left.Evaluate(Variables);
46
47 if (!(S is ICollection<IElement> Elements))
48 {
49 if (S is IVector Vector)
50 Elements = Vector.VectorElements;
51 else if (!S.IsScalar)
52 Elements = S.ChildElements;
53 else if (S.AssociatedObjectValue is IEnumerable Enumerable)
54 {
55 foreach (object Item in Enumerable)
56 {
57 Variables[this.variableName] = Item;
58 Elements2.Add(this.right.Evaluate(Variables));
59 }
60
61 return this.Encapsulate(Elements2);
62 }
63 else
64 Elements = new IElement[] { S };
65 }
66
67 foreach (IElement Element in Elements)
68 {
69 try
70 {
71 Variables[this.variableName] = Element;
72 Elements2.Add(this.right.Evaluate(Variables));
73 }
75 {
76 if (ex.HasLoopValue)
77 Elements2.Add(ex.LoopValue);
78
79 //ScriptBreakLoopException.Reuse(ex);
80 break;
81 }
83 {
84 if (ex.HasLoopValue)
85 Elements2.Add(ex.LoopValue);
86
87 //ScriptContinueLoopException.Reuse(ex);
88 }
89 }
90
91 return this.Encapsulate(Elements2);
92 }
93
99 public override async Task<IElement> EvaluateAsync(Variables Variables)
100 {
101 if (!this.isAsync)
102 return this.Evaluate(Variables);
103
104 IElement S = await this.left.EvaluateAsync(Variables);
106
107 if (!(S is ICollection<IElement> Elements))
108 {
109 if (S is IVector Vector)
110 Elements = Vector.VectorElements;
111 else if (!S.IsScalar)
112 Elements = S.ChildElements;
113 else if (S.AssociatedObjectValue is IEnumerable Enumerable)
114 {
115 foreach (object Item in Enumerable)
116 {
117 Variables[this.variableName] = Item;
118 Elements2.Add(await this.right.EvaluateAsync(Variables));
119 }
120
121 return this.Encapsulate(Elements2);
122 }
123 else
124 Elements = new IElement[] { S };
125 }
126
127 foreach (IElement Element in Elements)
128 {
129 try
130 {
131 Variables[this.variableName] = Element;
132 Elements2.Add(await this.right.EvaluateAsync(Variables));
133 }
134 catch (ScriptBreakLoopException ex)
135 {
136 if (ex.HasLoopValue)
137 Elements2.Add(ex.LoopValue);
138
139 //ScriptBreakLoopException.Reuse(ex);
140 break;
141 }
143 {
144 if (ex.HasLoopValue)
145 Elements2.Add(ex.LoopValue);
146
147 //ScriptContinueLoopException.Reuse(ex);
148 continue;
149 }
150 }
151
152 return this.Encapsulate(Elements2);
153 }
154
160 protected virtual IElement Encapsulate(ChunkedList<IElement> Elements)
161 {
162 return VectorDefinition.Encapsulate(Elements, true, this);
163 }
164
165 }
166}
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
void Add(T Item)
Adds an item to the collection.
Definition: ChunkedList.cs:272
Base class for all types of elements.
Definition: Element.cs:14
IElement LoopValue
Value to include in the loop's result.
bool HasLoopValue
If LoopValue contains a value that should be included in the loop's result.
Class managing a script expression.
Definition: Expression.cs:41
Base class for all binary operators.
ScriptNode RightOperand
Right operand.
ScriptNode LeftOperand
Left operand.
ScriptNode right
Right operand.
ScriptNode left
Left operand.
bool isAsync
If subtree is asynchroneous.
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
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
static IElement Encapsulate(Array Elements, bool CanEncapsulateAsMatrix, ScriptNode Node)
Encapsulates the elements of a vector.
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
virtual IElement Encapsulate(ChunkedList< IElement > Elements)
Encapsulates the calculated elements.
VectorForEachDefinition(ForEach Elements, int Start, int Length, Expression Expression)
Creates a vector using a FOREACH statement.
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:21
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:34
ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
Definition: IElement.cs:50
bool IsScalar
If the element represents a scalar value.
Definition: IElement.cs:42
Basic interface for vectors.
Definition: IVector.cs:9