Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
OnEvent.cs
1using System;
2using System.Threading.Tasks;
3using System.Xml;
10
12{
16 public class OnEvent : ActionReference
17 {
18 private ScriptableStringAttribute newState;
19 private ScriptableStringAttribute failureState;
20 private EventNode @event;
21
25 public OnEvent()
26 : base()
27 {
28 }
29
33 [DefaultValueNull]
34 public string NewStateDefinition
35 {
36 get => this.newState?.Definition;
37 set => this.newState = new ScriptableStringAttribute(value, this);
38 }
39
43 [DefaultValueNull]
45 {
46 get => this.failureState?.Definition;
47 set => this.failureState = new ScriptableStringAttribute(value, this);
48 }
49
53 public EventNode Event => this.@event;
54
58 public override string LocalName => nameof(OnEvent);
59
64 public override IStateMachineNode Create()
65 {
66 return new OnEvent();
67 }
68
73 public override Task Parse(XmlElement Xml)
74 {
75 this.newState = new ScriptableStringAttribute(XML.Attribute(Xml, "newState"), this);
76 this.failureState = new ScriptableStringAttribute(XML.Attribute(Xml, "failureState"), this);
77
78 return base.Parse(Xml);
79 }
80
84 protected override void OnChildNodesUpdated()
85 {
86 base.OnChildNodesUpdated();
87
88 this.@event = this.GetChildElement<EventNode>(true);
89 }
90
95 public override void CheckReferences(StateMachine Machine, Token Token)
96 {
97 if (this.newState.IsConstant && !this.newState.IsEmpty && !Machine.TryGetState(this.newState.Definition, out _))
98 throw new Exception("State not found: " + this.newState.Definition);
99
100 if (this.failureState.IsConstant && !this.failureState.IsEmpty && !Machine.TryGetState(this.failureState.Definition, out _))
101 throw new Exception("State not found: " + this.failureState.Definition);
102
103 base.CheckReferences(Machine, Token);
104 }
105
111 public async Task<string> GetNewState(EvaluationArguments Arguments)
112 {
113 if (this.newState is null)
114 return null;
115
116 return await this.newState.Evaluate(Arguments.Variables);
117 }
118
124 public async Task<string> GetFailureState(EvaluationArguments Arguments)
125 {
126 if (this.failureState is null)
127 return null;
128
129 return await this.failureState.Evaluate(Arguments.Variables);
130 }
131
135 public bool HasNewState => !(this.newState is null) && !this.newState.IsEmpty;
136
140 public bool HasNewStateConstant => this.newState?.IsConstant ?? false;
141
145 public bool HasNewStateExpression => this.newState?.IsExpression ?? false;
146
150 public bool HasFailureState => !(this.failureState is null) && !this.failureState.IsEmpty;
151
155 public bool HasFailureStateConstant => this.failureState?.IsConstant ?? false;
156
160 public bool HasFailureStateExpression => this.failureState?.IsExpression ?? false;
161
168 public async Task<bool> Register(int EventIndex, EvaluationArguments Arguments)
169 {
170 if (this.@event is null)
171 return false;
172
173 return await this.@event.Register(EventIndex, Arguments, this);
174 }
175
181 public Task Unregister(int EventIndex, EvaluationArguments Arguments)
182 {
183 return this.@event?.Unregister(EventIndex, Arguments) ?? Task.CompletedTask;
184 }
185
191 public async Task<bool> StateUpdated(EvaluationArguments Arguments)
192 {
193 if (this.@event is null)
194 return false;
195
196 return await this.@event.StateUpdated(Arguments);
197 }
198 }
199}
Helps with common XML-related tasks.
Definition: XML.cs:19
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:914
Abstract base class for nodes referencing an action.
async Task< T > Evaluate(Variables Variables)
Evaluates the attribute
Contains information required for evaluating script in a state-machine.
Abstract base class for State-Machine event nodes.
Definition: EventNode.cs:10
Action executed when entering a state.
Definition: OnEvent.cs:17
bool HasFailureStateExpression
If the failure state is defined by an expression
Definition: OnEvent.cs:160
bool HasFailureStateConstant
If the failure state is defined by a constant
Definition: OnEvent.cs:155
Task Unregister(int EventIndex, EvaluationArguments Arguments)
Registers the event
Definition: OnEvent.cs:181
override IStateMachineNode Create()
Creates a new node of the corresponding type.
Definition: OnEvent.cs:64
override void OnChildNodesUpdated()
Method called whenever ChildNodes is updated.
Definition: OnEvent.cs:84
bool HasNewStateConstant
If the new state is defined by a constant
Definition: OnEvent.cs:140
async Task< bool > Register(int EventIndex, EvaluationArguments Arguments)
Registers the event
Definition: OnEvent.cs:168
string FailureStateDefinition
Failure State Definition
Definition: OnEvent.cs:45
bool HasFailureState
If the event defines a failure state.
Definition: OnEvent.cs:150
bool HasNewStateExpression
If the new state is defined by an expression
Definition: OnEvent.cs:145
override Task Parse(XmlElement Xml)
Parses the State-machine node.
Definition: OnEvent.cs:73
async Task< string > GetFailureState(EvaluationArguments Arguments)
Gets the failure state ID when the event is triggered.
Definition: OnEvent.cs:124
OnEvent()
Action executed when entering a state.
Definition: OnEvent.cs:25
override void CheckReferences(StateMachine Machine, Token Token)
Checks references in the node.
Definition: OnEvent.cs:95
bool HasNewState
If the event defines a new state.
Definition: OnEvent.cs:135
async Task< bool > StateUpdated(EvaluationArguments Arguments)
Method called when the internal state of the state-machine has been updated.
Definition: OnEvent.cs:191
async Task< string > GetNewState(EvaluationArguments Arguments)
Gets the new state ID when the event is triggered.
Definition: OnEvent.cs:111
Class representing a state machine.
Definition: StateMachine.cs:37
bool TryGetState(string Id, out State State)
Tries to get a state.