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;
2using System.Threading.Tasks;
4
5namespace Waher.Script.Model
6{
10 public abstract class UnaryOperator : ScriptNode
11 {
15 protected ScriptNode op;
16
20 protected bool isAsync;
21
30 : base(Start, Length, Expression)
31 {
32 this.op = Operand;
33 this.op?.SetParent(this);
34
35 this.isAsync = Operand?.IsAsynchronous ?? false;
36 }
37
41 public ScriptNode Operand => this.op;
42
46 public virtual string DefaultVariableName
47 {
48 get
49 {
50 if (this.op is IDifferentiable Differentiable)
51 return Differentiable.DefaultVariableName;
52 else
53 return null;
54 }
55 }
56
61 public override bool IsAsynchronous => this.isAsync;
62
69 {
71
72 return this.Evaluate(Operand, Variables);
73 }
74
80 public override async Task<IElement> EvaluateAsync(Variables Variables)
81 {
82 if (!this.isAsync)
83 return this.Evaluate(Variables);
84
85 IElement Operand = await this.op.EvaluateAsync(Variables);
86 return await this.EvaluateAsync(Operand, Variables);
87 }
88
96
103 public virtual Task<IElement> EvaluateAsync(IElement Operand, Variables Variables)
104 {
105 return Task.FromResult<IElement>(this.Evaluate(Operand, Variables));
106 }
107
115 public override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
116 {
117 if (Order == SearchMethod.DepthFirst)
118 {
119 if (!(this.op?.ForAllChildNodes(Callback, State, Order) ?? true))
120 return false;
121 }
122
123 if (!(this.op is null))
124 {
125 bool b = !Callback(this.op, out ScriptNode NewNode, State);
126 if (!(NewNode is null))
127 {
128 this.op = NewNode;
129 this.op.SetParent(this);
130
131 this.isAsync = NewNode.IsAsynchronous;
132 }
133
134 if (b || (Order == SearchMethod.TreeOrder && !this.op.ForAllChildNodes(Callback, State, Order)))
135 return false;
136 }
137
138 if (Order == SearchMethod.BreadthFirst)
139 {
140 if (!(this.op?.ForAllChildNodes(Callback, State, Order) ?? true))
141 return false;
142 }
143
144 return true;
145 }
146
148 public override bool Equals(object obj)
149 {
150 return obj is UnaryOperator O &&
151 this.op.Equals(O.op) &&
152 base.Equals(obj);
153 }
154
156 public override int GetHashCode()
157 {
158 int Result = base.GetHashCode();
159 Result ^= Result << 5 ^ this.op.GetHashCode();
160 return Result;
161 }
162
163 }
164}
Class managing a script expression.
Definition: Expression.cs:39
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
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:20
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