Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
VectorForDefinition.cs
1using System.Threading.Tasks;
8
10{
15 {
16 private readonly string variableName;
17
26 : base(Elements.LeftOperand, Elements.MiddleOperand, Elements.Middle2Operand, Elements.RightOperand, Start, Length, Expression)
27 {
28 this.variableName = Elements.VariableName;
29 }
30
34 public string VariableName => this.variableName;
35
42 {
43 if (!(this.left.Evaluate(Variables) is ICommutativeRingWithIdentityElement From))
44 throw new ScriptRuntimeException("Invalid range.", this);
45
46 if (!(this.middle.Evaluate(Variables) is ICommutativeRingWithIdentityElement To))
47 throw new ScriptRuntimeException("Invalid range.", this);
48
49 if (!(From.AssociatedSet is IOrderedSet S))
50 throw new ScriptRuntimeException("Cannot compare range.", this);
51
52 IElement Step;
53 int Direction = S.Compare(From, To);
54 bool Done;
55
56 if (!(this.middle2 is null))
57 {
58 Step = this.middle2.Evaluate(Variables);
59
60 if (Direction < 0)
61 {
62 if (S.Compare(Step, From.Zero) <= 0)
63 throw new ScriptRuntimeException("Invalid step size for corresponding range.", this);
64 }
65 else if (Direction > 0)
66 {
67 if (S.Compare(Step, From.Zero) >= 0)
68 throw new ScriptRuntimeException("Invalid step size for corresponding range.", this);
69 }
70 }
71 else
72 {
73 if (Direction <= 0)
74 Step = From.One;
75 else
76 Step = From.One.Negate();
77 }
78
80
81 do
82 {
83 try
84 {
85 Variables[this.variableName] = From;
86 Elements.Add(this.right.Evaluate(Variables));
87 }
89 {
90 if (ex.HasLoopValue)
91 Elements.Add(ex.LoopValue);
92
93 //ScriptBreakLoopException.Reuse(ex);
94 break;
95 }
97 {
98 if (ex.HasLoopValue)
99 Elements.Add(ex.LoopValue);
100
101 //ScriptContinueLoopException.Reuse(ex);
102 }
103
104 if (Direction == 0)
105 Done = true;
106 else
107 {
108 From = Arithmetics.Add.EvaluateAddition(From, Step, this) as ICommutativeRingWithIdentityElement;
109 if (From is null)
110 throw new ScriptRuntimeException("Invalid step size.", this);
111
112 if (Direction > 0)
113 Done = S.Compare(From, To) < 0;
114 else
115 Done = S.Compare(From, To) > 0;
116 }
117 }
118 while (!Done);
119
120 return this.Encapsulate(Elements);
121 }
122
128 public override async Task<IElement> EvaluateAsync(Variables Variables)
129 {
130 if (!this.isAsync)
131 return this.Evaluate(Variables);
132
133 if (!(await this.left.EvaluateAsync(Variables) is ICommutativeRingWithIdentityElement From))
134 throw new ScriptRuntimeException("Invalid range.", this);
135
136 if (!(await this.middle.EvaluateAsync(Variables) is ICommutativeRingWithIdentityElement To))
137 throw new ScriptRuntimeException("Invalid range.", this);
138
139 if (!(From.AssociatedSet is IOrderedSet S))
140 throw new ScriptRuntimeException("Cannot compare range.", this);
141
142 IElement Step;
143 int Direction = S.Compare(From, To);
144 bool Done;
145
146 if (!(this.middle2 is null))
147 {
148 Step = await this.middle2.EvaluateAsync(Variables);
149
150 if (Direction < 0)
151 {
152 if (S.Compare(Step, From.Zero) <= 0)
153 throw new ScriptRuntimeException("Invalid step size for corresponding range.", this);
154 }
155 else if (Direction > 0)
156 {
157 if (S.Compare(Step, From.Zero) >= 0)
158 throw new ScriptRuntimeException("Invalid step size for corresponding range.", this);
159 }
160 }
161 else
162 {
163 if (Direction <= 0)
164 Step = From.One;
165 else
166 Step = From.One.Negate();
167 }
168
170
171 do
172 {
173 try
174 {
175 Variables[this.variableName] = From;
176 Elements.Add(await this.right.EvaluateAsync(Variables));
177 }
178 catch (ScriptBreakLoopException ex)
179 {
180 if (ex.HasLoopValue)
181 Elements.Add(ex.LoopValue);
182
183 //ScriptBreakLoopException.Reuse(ex);
184 break;
185 }
187 {
188 if (ex.HasLoopValue)
189 Elements.Add(ex.LoopValue);
190
191 //ScriptContinueLoopException.Reuse(ex);
192 }
193
194 if (Direction == 0)
195 Done = true;
196 else
197 {
198 From = Arithmetics.Add.EvaluateAddition(From, Step, this) as ICommutativeRingWithIdentityElement;
199 if (From is null)
200 throw new ScriptRuntimeException("Invalid step size.", this);
201
202 if (Direction > 0)
203 Done = S.Compare(From, To) < 0;
204 else
205 Done = S.Compare(From, To) > 0;
206 }
207 }
208 while (!Done);
209
210 return this.Encapsulate(Elements);
211 }
212
218 protected virtual IElement Encapsulate(ChunkedList<IElement> Elements)
219 {
220 return VectorDefinition.Encapsulate(Elements, true, this);
221 }
222
223 }
224}
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
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
ScriptNode RightOperand
Right operand.
ScriptNode LeftOperand
Left operand.
ScriptNode right
Right operand.
ScriptNode left
Left operand.
bool isAsync
If subtree is asynchroneous.
Base class for all quaternary operators.
ScriptNode Middle2Operand
Second middle operand.
ScriptNode middle2
Second Middle operand.
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
ScriptNode middle
Middle operand.
ScriptNode MiddleOperand
Middle operand.
string VariableName
Variable Name.
Definition: For.cs:37
static IElement Encapsulate(Array Elements, bool CanEncapsulateAsMatrix, ScriptNode Node)
Encapsulates the elements of a vector.
Creates a vector using a FOR statement.
virtual IElement Encapsulate(ChunkedList< IElement > Elements)
Encapsulates the calculated elements.
VectorForDefinition(For Elements, int Start, int Length, Expression Expression)
Creates a vector using a FOR statement.
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of commutative ring with identity elements.
Basic interface for all types of elements.
Definition: IElement.cs:21
Basic interface for ordered sets.
Definition: IOrderedSet.cs:11