Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
StateMachineNode.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Xml;
8
10{
17 public delegate bool ForEachCallback(IStateMachineNode Node, object State);
18
22 [TypeName(TypeNameSerialization.FullName)]
23 public abstract class StateMachineNode : IStateMachineNode
24 {
25 private IStateMachineNode[] childNodes;
26 private StateMachine stateMachine;
27
32 {
33 }
34
38 public abstract string LocalName { get; }
39
44
48 public StateMachine StateMachine => this.stateMachine;
49
53 [DefaultValueNull]
55 {
56 get => this.childNodes;
57 set
58 {
59 this.childNodes = value;
61 }
62 }
63
67 protected virtual void OnChildNodesUpdated()
68 {
69 // Do nothing by default.
70 }
71
76 public abstract IStateMachineNode Create();
77
82 public virtual Task Parse(XmlElement Xml)
83 {
84 List<IStateMachineNode> Children = null;
85
86 foreach (XmlNode N in Xml.ChildNodes)
87 {
88 if (N is XmlElement E)
89 {
90 if (Children is null)
91 Children = new List<IStateMachineNode>();
92
93 Children.Add(StateMachineProcessor.Create(E));
94 }
95 }
96
97 this.ChildNodes = Children?.ToArray();
98
99 return Task.CompletedTask;
100 }
101
108 protected T GetChildElement<T>(bool Required)
109 where T : StateMachineNode
110 {
111 T Result = this.GetChildElement<T>();
112 if (!(Result is null))
113 return Result;
114
115 if (Required)
116 throw new Exception("Expected element: " + typeof(T).FullName);
117
118 return default;
119 }
120
126 protected T GetChildElement<T>()
127 where T : IStateMachineNode
128 {
129 if (this.ChildNodes is null)
130 return default;
131
132 foreach (IStateMachineNode Node in this.ChildNodes)
133 {
134 if (Node is T Item)
135 return Item;
136 }
137
138 return default;
139 }
140
146 protected T[] GetChildElements<T>()
147 where T : IStateMachineNode
148 {
149 if (this.ChildNodes is null)
150 return new T[0];
151
152 List<T> Result = null;
153
154 foreach (IStateMachineNode Node in this.ChildNodes)
155 {
156 if (Node is T Item)
157 {
158 if (Result is null)
159 Result = new List<T>();
160
161 Result.Add(Item);
162 }
163 }
164
165 return Result?.ToArray() ?? new T[0];
166 }
167
178 protected void ConvertValueAttributeToElement<ValueType>(XmlElement Xml, bool Required)
179 where ValueType : Value
180 {
181 this.ConvertValueAttributesToElements(Xml, new Type[] { typeof(ValueType) }, new bool[] { Required });
182 }
183
194 protected void ConvertValueAttributesToElements(XmlElement Xml, Type[] ValueTypes, bool[] Required)
195 {
196 Dictionary<Type, bool> Types = new Dictionary<Type, bool>();
197 int i, c = ValueTypes.Length;
198
199 if (c != Required.Length)
200 throw new ArgumentException("Array lengths must match.", nameof(Required));
201
202 for (i = 0; i < c; i++)
203 Types[ValueTypes[i]] = Required[i];
204
205 if (!(this.ChildNodes is null))
206 {
207 foreach (IStateMachineNode Node in this.ChildNodes)
208 Types.Remove(Node.GetType());
209 }
210
211 List<IStateMachineNode> ToAdd = null;
212
213 foreach (KeyValuePair<Type, bool> P in Types)
214 {
215 Value Result = (Value)Activator.CreateInstance(P.Key);
216 string s = Result.LocalName;
217
218 if (!string.IsNullOrEmpty(s))
219 {
220 s = s.Substring(0, 1).ToLower() + s.Substring(1);
221 if (Xml.HasAttribute(s))
222 {
223 if (ToAdd is null)
224 ToAdd = new List<IStateMachineNode>();
225
226 Result.Node = Value.ParseAttributeValue(Xml.GetAttribute(s));
227 ToAdd.Add(Result);
228 }
229 else if (P.Value)
230 throw new Exception("Required child element (or corresponding attribute) missing: " + Result.LocalName);
231 }
232 }
233
234 if (!(ToAdd is null))
235 {
236 if (this.childNodes is null)
237 this.childNodes = ToAdd.ToArray();
238 else
239 {
240 c = this.childNodes.Length;
241 i = c + ToAdd.Count;
242
243 Array.Resize(ref this.childNodes, i);
244 ToAdd.CopyTo(this.childNodes, c);
245 }
246
247 this.OnChildNodesUpdated();
248 }
249 }
250
256 protected T GetValueElement<T>()
257 where T : Value
258 {
259 if (!(this.ChildNodes is null))
260 {
261 foreach (IStateMachineNode Node in this.ChildNodes)
262 {
263 if (Node is T Element)
264 return Element;
265 }
266 }
267
268 return null;
269 }
270
277 public bool ForEach(ForEachCallback Callback, object State)
278 {
279 if (!Callback(this, State))
280 return false;
281
282 if (!(this.ChildNodes is null))
283 {
284 foreach (IStateMachineNode Node in this.ChildNodes)
285 {
286 if (!Node.ForEach(Callback, State))
287 return false;
288 }
289 }
290
291 return true;
292 }
293
298 public virtual void IndexElement(StateMachine Machine)
299 {
300 this.stateMachine = Machine;
301 }
302
307 public virtual void CheckReferences(StateMachine Machine, Token Token)
308 {
309 // Do nothing by default.
310 }
311
312 }
313}
Abstract base class for State-Machine nodes.
bool ForEach(ForEachCallback Callback, object State)
Iterates through th node and all its child nodes.
virtual void CheckReferences(StateMachine Machine, Token Token)
Checks references in the node.
virtual Task Parse(XmlElement Xml)
Parses the State-machine node.
IStateMachineNode[] ChildNodes
Child nodes, if available. Null if no children.
void ConvertValueAttributeToElement< ValueType >(XmlElement Xml, bool Required)
Converts a value attribute to parsed element. The XML definition has to be parsed before,...
abstract IStateMachineNode Create()
Creates a new node of the corresponding type.
virtual void OnChildNodesUpdated()
Method called whenever ChildNodes is updated.
virtual void IndexElement(StateMachine Machine)
Indexes the element in the state-machine.
T GetChildElement< T >()
Gets a child items of a specific type.
T GetValueElement< T >()
Gets a value element from the list of child elements.
StateMachineNode()
Abstract base class for State-Machine nodes.
T[] GetChildElements< T >()
Gets an array of child items of a specific type.
void ConvertValueAttributesToElements(XmlElement Xml, Type[] ValueTypes, bool[] Required)
Converts value attributes to parsed elements. The XML definition has to be parsed before,...
Abstract base class for nodes with a value.
Definition: Value.cs:13
Class representing a state machine.
Definition: StateMachine.cs:37
const string StateMachineNamespace
https://paiwise.tagroot.io/Schema/StateMachines.xsd
bool ForEach(ForEachCallback Callback, object State)
Iterates through th node and all its child nodes.
TypeNameSerialization
How the type name should be serialized.
delegate bool ForEachCallback(IStateMachineNode Node, object State)
Delegate or callback methods when iterating through nodes in a state-machine.