Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
FunctionTwoVariables.cs
1using System;
2using System.Threading.Tasks;
4
5namespace Waher.Script.Model
6{
10 public abstract class FunctionTwoVariables : Function
11 {
12 private ScriptNode argument1;
13 private ScriptNode argument2;
14 private bool isAsync;
15
25 : base(Start, Length, Expression)
26 {
27 this.argument1 = Argument1;
28 this.argument1?.SetParent(this);
29
30 this.argument2 = Argument2;
31 this.argument2?.SetParent(this);
32
33 this.CalcIsAsync();
34 }
35
36 private void CalcIsAsync()
37 {
38 this.isAsync =
39 (this.argument1?.IsAsynchronous ?? false) ||
40 (this.argument2?.IsAsynchronous ?? false);
41 }
42
46 public ScriptNode Argument1 => this.argument1;
47
51 public ScriptNode Argument2 => this.argument2;
52
56 public override string[] DefaultArgumentNames => new string[] { "x", "y" };
57
62 public override bool IsAsynchronous => this.isAsync;
63
70 {
71 IElement Arg1 = this.argument1.Evaluate(Variables);
72 IElement Arg2 = this.argument2.Evaluate(Variables);
73
74 return this.Evaluate(Arg1, Arg2, Variables);
75 }
76
82 public override async Task<IElement> EvaluateAsync(Variables Variables)
83 {
84 if (!this.IsAsynchronous)
85 return this.Evaluate(Variables);
86
87 IElement Arg1 = await this.argument1.EvaluateAsync(Variables);
88 IElement Arg2 = await this.argument2.EvaluateAsync(Variables);
89
90 return await this.EvaluateAsync(Arg1, Arg2, Variables);
91 }
92
101
110 {
111 return Task.FromResult(this.Evaluate(Argument1, Argument2, Variables));
112 }
113
121 public override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
122 {
123 if (Order == SearchMethod.DepthFirst)
124 {
125 if (!(this.argument1?.ForAllChildNodes(Callback, State, Order) ?? true))
126 return false;
127
128 if (!(this.argument2?.ForAllChildNodes(Callback, State, Order) ?? true))
129 return false;
130 }
131
132 ScriptNode NewNode;
133 bool RecalcIsAsync = false;
134 bool b;
135
136 if (!(this.argument1 is null))
137 {
138 b = !Callback(this.argument1, out NewNode, State);
139 if (!(NewNode is null))
140 {
141 this.argument1 = NewNode;
142 this.argument1.SetParent(this);
143
144 RecalcIsAsync = true;
145 }
146
147 if (b || (Order == SearchMethod.TreeOrder && !this.argument1.ForAllChildNodes(Callback, State, Order)))
148 {
149 if (RecalcIsAsync)
150 this.CalcIsAsync();
151
152 return false;
153 }
154 }
155
156 if (!(this.argument2 is null))
157 {
158 b = !Callback(this.argument2, out NewNode, State);
159 if (!(NewNode is null))
160 {
161 this.argument2 = NewNode;
162 this.argument2.SetParent(this);
163
164 RecalcIsAsync = true;
165 }
166
167 if (b || (Order == SearchMethod.TreeOrder && !this.argument2.ForAllChildNodes(Callback, State, Order)))
168 {
169 if (RecalcIsAsync)
170 this.CalcIsAsync();
171
172 return false;
173 }
174 }
175
176 if (RecalcIsAsync)
177 this.CalcIsAsync();
178
179 if (Order == SearchMethod.BreadthFirst)
180 {
181 if (!(this.argument1?.ForAllChildNodes(Callback, State, Order) ?? true))
182 return false;
183
184 if (!(this.argument2?.ForAllChildNodes(Callback, State, Order) ?? true))
185 return false;
186 }
187
188 return true;
189 }
190
192 public override bool Equals(object obj)
193 {
194 return obj is FunctionTwoVariables O &&
195 this.argument1.Equals(O.argument1) &&
196 this.argument2.Equals(O.argument2) &&
197 base.Equals(obj);
198 }
199
201 public override int GetHashCode()
202 {
203 int Result = base.GetHashCode();
204 Result ^= Result << 5 ^ this.argument1.GetHashCode();
205 Result ^= Result << 5 ^ this.argument2.GetHashCode();
206 return Result;
207 }
208
209 }
210}
Class managing a script expression.
Definition: Expression.cs:39
Base class for all funcions.
Definition: Function.cs:9
Base class for funcions of one variable.
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
FunctionTwoVariables(ScriptNode Argument1, ScriptNode Argument2, int Start, int Length, Expression Expression)
Base class for funcions of one variable.
override string[] DefaultArgumentNames
Default Argument names
override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
ScriptNode Argument2
Function argument 2.
abstract IElement Evaluate(IElement Argument1, IElement Argument2, Variables Variables)
Evaluates the function.
ScriptNode Argument1
Function argument 1.
virtual Task< IElement > EvaluateAsync(IElement Argument1, IElement Argument2, Variables Variables)
Evaluates the function.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
override bool Equals(object obj)
Definition: ScriptNode.cs:258
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
virtual bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: ScriptNode.cs:142
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
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
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