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;
12
14{
18 public class OnEvent : ActionReference
19 {
20 private ScriptableStringAttribute newState;
21 private ScriptableStringAttribute failureState;
22 private ScriptableBooleanAttribute suppressSample;
23 private EventNode @event;
24
28 public OnEvent()
29 : base()
30 {
31 }
32
36 [DefaultValueNull]
37 public string NewStateDefinition
38 {
39 get => this.newState?.Definition;
40 set => this.newState = new ScriptableStringAttribute(value, this);
41 }
42
46 [DefaultValueNull]
48 {
49 get => this.failureState?.Definition;
50 set => this.failureState = new ScriptableStringAttribute(value, this);
51 }
52
56 [DefaultValueNull]
58 {
59 get => this.suppressSample?.Definition;
60 set => this.suppressSample = new ScriptableBooleanAttribute(value, this);
61 }
62
66 public EventNode Event => this.@event;
67
71 public override string LocalName => nameof(OnEvent);
72
77 public override IStateMachineNode Create()
78 {
79 return new OnEvent();
80 }
81
86 public override Task Parse(XmlElement Xml)
87 {
88 this.newState = new ScriptableStringAttribute(XML.Attribute(Xml, "newState"), this);
89 this.failureState = new ScriptableStringAttribute(XML.Attribute(Xml, "failureState"), this);
90 this.suppressSample = new ScriptableBooleanAttribute(XML.Attribute(Xml, "suppressSample"), this);
91
92 return base.Parse(Xml);
93 }
94
98 protected override void OnChildNodesUpdated()
99 {
100 base.OnChildNodesUpdated();
101
102 this.@event = this.GetChildElement<EventNode>(true);
103 }
104
109 public override void CheckReferences(StateMachine Machine, Token Token)
110 {
111 if (this.newState.IsConstant && !this.newState.IsEmpty && !Machine.TryGetState(this.newState.Definition, out _))
112 throw new StateMachineException(Machine, "State not found: " + this.newState.Definition);
113
114 if (this.failureState.IsConstant && !this.failureState.IsEmpty && !Machine.TryGetState(this.failureState.Definition, out _))
115 throw new StateMachineException(Machine, "State not found: " + this.failureState.Definition);
116
117 base.CheckReferences(Machine, Token);
118 }
119
125 public async Task<string> GetNewState(EvaluationArguments Arguments)
126 {
127 if (this.newState?.IsEmpty ?? true)
128 return null;
129
130 return await this.newState.Evaluate(Arguments.Variables);
131 }
132
138 public async Task<string> GetFailureState(EvaluationArguments Arguments)
139 {
140 if (this.failureState?.IsEmpty ?? true)
141 return null;
142
143 return await this.failureState.Evaluate(Arguments.Variables);
144 }
145
151 public async Task<bool> GetSuppressSample(EvaluationArguments Arguments)
152 {
153 if (this.suppressSample?.IsEmpty ?? true)
154 return false;
155
156 return await this.suppressSample.Evaluate(Arguments.Variables);
157 }
158
162 public bool HasNewState => !(this.newState is null) && !this.newState.IsEmpty;
163
167 public bool HasNewStateConstant => this.newState?.IsConstant ?? false;
168
172 public bool HasNewStateExpression => this.newState?.IsExpression ?? false;
173
177 public bool HasFailureState => !(this.failureState is null) && !this.failureState.IsEmpty;
178
182 public bool HasFailureStateConstant => this.failureState?.IsConstant ?? false;
183
187 public bool HasFailureStateExpression => this.failureState?.IsExpression ?? false;
188
197 public async Task<EventHandlerReference> Register(int EventIndex, EvaluationArguments Arguments)
198 {
199 if (this.@event is null)
200 return null;
201
202 return await this.@event.Register(EventIndex, Arguments, this);
203 }
204
210 public Task Unregister(int EventIndex, EvaluationArguments Arguments)
211 {
212 return this.@event?.Unregister(EventIndex, Arguments) ?? Task.CompletedTask;
213 }
214
221 public async Task<EventHandlerReference> StateUpdated(EvaluationArguments Arguments)
222 {
223 if (this.@event is null)
224 return null;
225
226 return await this.@event.StateUpdated(Arguments);
227 }
228
234 public virtual Task<bool> Applies(EvaluationArguments Arguments)
235 {
236 if (this.@event is null)
237 return Task.FromResult(true);
238 else
239 return this.@event.Applies(Arguments);
240 }
241 }
242}
Helps with common XML-related tasks.
Definition: XML.cs:21
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:1062
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:11
Action executed when entering a state.
Definition: OnEvent.cs:19
bool HasFailureStateExpression
If the failure state is defined by an expression
Definition: OnEvent.cs:187
bool HasFailureStateConstant
If the failure state is defined by a constant
Definition: OnEvent.cs:182
Task Unregister(int EventIndex, EvaluationArguments Arguments)
Registers the event
Definition: OnEvent.cs:210
override IStateMachineNode Create()
Creates a new node of the corresponding type.
Definition: OnEvent.cs:77
override void OnChildNodesUpdated()
Method called whenever ChildNodes is updated.
Definition: OnEvent.cs:98
async Task< bool > GetSuppressSample(EvaluationArguments Arguments)
Gets a Boolean value indicating if samples should be suppressed for the event.
Definition: OnEvent.cs:151
bool HasNewStateConstant
If the new state is defined by a constant
Definition: OnEvent.cs:167
string FailureStateDefinition
Failure State Definition
Definition: OnEvent.cs:48
bool HasFailureState
If the event defines a failure state.
Definition: OnEvent.cs:177
bool HasNewStateExpression
If the new state is defined by an expression
Definition: OnEvent.cs:172
async Task< EventHandlerReference > Register(int EventIndex, EvaluationArguments Arguments)
Registers the event
Definition: OnEvent.cs:197
override Task Parse(XmlElement Xml)
Parses the State-machine node.
Definition: OnEvent.cs:86
virtual Task< bool > Applies(EvaluationArguments Arguments)
If the event node applies to the current context.
Definition: OnEvent.cs:234
async Task< string > GetFailureState(EvaluationArguments Arguments)
Gets the failure state ID when the event is triggered.
Definition: OnEvent.cs:138
async Task< EventHandlerReference > StateUpdated(EvaluationArguments Arguments)
Method called when the internal state of the state-machine has been updated.
Definition: OnEvent.cs:221
OnEvent()
Action executed when entering a state.
Definition: OnEvent.cs:28
override void CheckReferences(StateMachine Machine, Token Token)
Checks references in the node.
Definition: OnEvent.cs:109
bool HasNewState
If the event defines a new state.
Definition: OnEvent.cs:162
string SuppressSampleDefinition
Suppress Sample Definition
Definition: OnEvent.cs:58
async Task< string > GetNewState(EvaluationArguments Arguments)
Gets the new state ID when the event is triggered.
Definition: OnEvent.cs:125
Class representing a state machine.
Definition: StateMachine.cs:43
bool TryGetState(string Id, out State State)
Tries to get a state.
Definition: ImplTypes.g.cs:58