Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
UnaryOperator.cs
1using System.Threading.Tasks;
3
4namespace Waher.Script.Model
5{
9 public abstract class UnaryOperator : ScriptNode
10 {
14 protected ScriptNode op;
15
19 protected bool isAsync;
20
29 : base(Start, Length, Expression)
30 {
31 this.op = Operand;
32 this.op?.SetParent(this);
33
34 this.isAsync = Operand?.IsAsynchronous ?? false;
35 }
36
40 public ScriptNode Operand => this.op;
41
45 public virtual string DefaultVariableName
46 {
47 get
48 {
49 if (this.op is IDifferentiable Differentiable)
50 return Differentiable.DefaultVariableName;
51 else
52 return null;
53 }
54 }
55
60 public override bool IsAsynchronous => this.isAsync;
61
68 {
70
71 return this.Evaluate(Operand, Variables);
72 }
73
79 public override async Task<IElement> EvaluateAsync(Variables Variables)
80 {
81 if (!this.isAsync)
82 return this.Evaluate(Variables);
83
84 IElement Operand = await this.op.EvaluateAsync(Variables);
85 return await this.EvaluateAsync(Operand, Variables);
86 }
87
95
102 public virtual Task<IElement> EvaluateAsync(IElement Operand, Variables Variables)
103 {
104 return Task.FromResult<IElement>(this.Evaluate(Operand, Variables));
105 }
106
114 public override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
115 {
116 if (Order == SearchMethod.DepthFirst)
117 {
118 if (!(this.op?.ForAllChildNodes(Callback, State, Order) ?? true))
119 return false;
120 }
121
122 if (!(this.op is null))
123 {
124 bool b = !Callback(this.op, out ScriptNode NewNode, State);
125 if (!(NewNode is null))
126 {
127 this.op = NewNode;
128 this.op.SetParent(this);
129
130 this.isAsync = NewNode.IsAsynchronous;
131 }
132
133 if (b || (Order == SearchMethod.TreeOrder && !this.op.ForAllChildNodes(Callback, State, Order)))
134 return false;
135 }
136
137 if (Order == SearchMethod.BreadthFirst)
138 {
139 if (!(this.op?.ForAllChildNodes(Callback, State, Order) ?? true))
140 return false;
141 }
142
143 return true;
144 }
145
147 public override bool Equals(object obj)
148 {
149 return obj is UnaryOperator O &&
150 this.op.Equals(O.op) &&
151 base.Equals(obj);
152 }
153
155 public override int GetHashCode()
156 {
157 int Result = base.GetHashCode();
158 Result ^= Result << 5 ^ this.op.GetHashCode();
159 return Result;
160 }
161
162 }
163}
Class managing a script expression.
Definition: Expression.cs:41
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 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
Base class for all unary operators.
abstract IElement Evaluate(IElement Operand, Variables Variables)
Evaluates the operator.
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
virtual string DefaultVariableName
Default variable name, if any, null otherwise.
override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
UnaryOperator(ScriptNode Operand, int Start, int Length, Expression Expression)
Base class for all unary operators.
bool isAsync
If subtree is asynchroneous.
override bool Equals(object obj)
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
virtual Task< IElement > EvaluateAsync(IElement Operand, Variables Variables)
Evaluates the operator.
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:21
Base interface for lambda expressions.
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