Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Sequence.cs
1using System.Collections.Generic;
2using System.Threading.Tasks;
5
7{
11 public class Sequence : ScriptNode
12 {
13 private readonly LinkedList<ScriptNode> statements;
14 private bool isAsync;
15
23 public Sequence(LinkedList<ScriptNode> Statements, int Start, int Length, Expression Expression)
24 : base(Start, Length, Expression)
25 {
26 this.statements = Statements;
27 this.statements?.SetParent(this);
28
29 this.CalcIsAsync();
30 }
31
32 private void CalcIsAsync()
33 {
34 this.isAsync = false;
35
36 foreach (ScriptNode Statement in this.statements)
37 {
38 if (Statement?.IsAsynchronous ?? false)
39 {
40 this.isAsync = true;
41 break;
42 }
43 }
44 }
45
46
50 public LinkedList<ScriptNode> Statements => this.statements;
51
56 public override bool IsAsynchronous => this.isAsync;
57
64 {
65 IElement Result = null;
66
67 foreach (ScriptNode Node in this.statements)
68 Result = Node.Evaluate(Variables);
69
70 return Result;
71 }
72
78 public override async Task<IElement> EvaluateAsync(Variables Variables)
79 {
80 if (!this.isAsync)
81 return this.Evaluate(Variables);
82
83 IElement Result = null;
84
85 foreach (ScriptNode Node in this.statements)
86 Result = await Node.EvaluateAsync(Variables);
87
88 return Result;
89 }
90
98 public override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
99 {
100 LinkedListNode<ScriptNode> Loop;
101
102 if (Order == SearchMethod.DepthFirst)
103 {
104 Loop = this.statements.First;
105
106 while (!(Loop is null))
107 {
108 if (!(Loop.Value?.ForAllChildNodes(Callback, State, Order) ?? true))
109 return false;
110
111 Loop = Loop.Next;
112 }
113 }
114
115 Loop = this.statements.First;
116 bool RecalcIsAsync = false;
117
118 while (!(Loop is null))
119 {
120 ScriptNode Node = Loop.Value;
121 if (!(Node is null))
122 {
123 bool Result = Callback(Node, out ScriptNode NewNode, State);
124 if (!(NewNode is null))
125 {
126 Loop.Value = NewNode;
127 NewNode.SetParent(this);
128 Node = NewNode;
129
130 RecalcIsAsync = true;
131 }
132
133 if (!Result || (Order == SearchMethod.TreeOrder && !Node.ForAllChildNodes(Callback, State, Order)))
134 {
135 if (RecalcIsAsync)
136 this.CalcIsAsync();
137
138 return false;
139 }
140 }
141
142 Loop = Loop.Next;
143 }
144
145 if (RecalcIsAsync)
146 this.CalcIsAsync();
147
148 if (Order == SearchMethod.BreadthFirst)
149 {
150 Loop = this.statements.First;
151
152 while (!(Loop is null))
153 {
154 if (!(Loop.Value?.ForAllChildNodes(Callback, State, Order) ?? true))
155 return false;
156
157 Loop = Loop.Next;
158 }
159 }
160
161 return true;
162 }
163
165 public override bool Equals(object obj)
166 {
167 return obj is Sequence O &&
168 AreEqual(this.statements, O.statements) &&
169 base.Equals(obj);
170 }
171
173 public override int GetHashCode()
174 {
175 int Result = base.GetHashCode();
176 Result ^= Result << 5 ^ GetHashCode(this.statements);
177 return Result;
178 }
179
180 }
181}
Class managing a script expression.
Definition: Expression.cs:39
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, bool DepthFirst)
Calls the callback method for all child nodes.
Definition: ScriptNode.cs:243
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
static bool AreEqual(ScriptNode S1, ScriptNode S2)
Compares if two script nodes are equal.
Definition: ScriptNode.cs:275
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
Represents a sequence of statements.
Definition: Sequence.cs:12
override bool Equals(object obj)
Definition: Sequence.cs:165
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: Sequence.cs:63
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: Sequence.cs:56
override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
Definition: Sequence.cs:98
Sequence(LinkedList< ScriptNode > Statements, int Start, int Length, Expression Expression)
Represents a sequence of statements.
Definition: Sequence.cs:23
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: Sequence.cs:78
LinkedList< ScriptNode > Statements
Statements
Definition: Sequence.cs:50
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