Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
EventReference.cs
1using System;
2using System.Threading.Tasks;
3using System.Xml;
9
11{
16 {
17 private ScriptableStringAttribute eventRef;
18
23 : base()
24 {
25 }
26
30 [DefaultValueNull]
32 {
33 get => this.eventRef?.Definition;
34 set => this.eventRef = new ScriptableStringAttribute(value, this);
35 }
36
41 public override Task Parse(XmlElement Xml)
42 {
43 this.eventRef = new ScriptableStringAttribute(XML.Attribute(Xml, "eventRef"), this);
44
45 return base.Parse(Xml);
46 }
47
51 public override string LocalName => nameof(EventReference);
52
57 public override IStateMachineNode Create()
58 {
59 return new EventReference();
60 }
61
67 public override void CheckReferences(StateMachine Machine, Token Token)
68 {
69 if (this.eventRef.IsConstant && !Machine.TryGetEvent(this.eventRef.Definition, out _))
70 throw new Exception("Event not found: " + this.eventRef.Definition);
71
72 base.CheckReferences(Machine, Token);
73 }
74
80 public async Task<Event> GetReference(EvaluationArguments Arguments)
81 {
82 if (this.eventRef is null)
83 throw new Exception("No event id defined.");
84
85 string EventId = await this.eventRef.Evaluate(Arguments.Variables);
86 if (!Arguments.Machine.TryGetEvent(EventId, out Event Event))
87 throw new Exception("Event not found: " + Event.Id);
88
89 return Event;
90 }
91
99 public override async Task<bool> Register(int EventIndex, EvaluationArguments Arguments, OnEvent Event)
100 {
101 Event Event2 = await this.GetReference(Arguments);
102 return await Event2.Register(EventIndex, Arguments, Event);
103 }
104
110 public override async Task Unregister(int EventIndex, EvaluationArguments Arguments)
111 {
112 Event Event = await this.GetReference(Arguments);
113 await Event.Unregister(EventIndex, Arguments);
114 }
115
119 public override string Label => this.eventRef.Definition;
120
121 }
122}
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
async Task< T > Evaluate(Variables Variables)
Evaluates the attribute
Contains information required for evaluating script in a state-machine.
StateMachine Machine
Reference to state-machine definition.
async Task< bool > Register(int EventIndex, EvaluationArguments Arguments, OnEvent Event)
Registers the event
Definition: Event.cs:88
Task Unregister(int EventIndex, EvaluationArguments Arguments)
Registers the event
Definition: Event.cs:101
Abstract base class for State-Machine event nodes.
Definition: EventNode.cs:10
override void CheckReferences(StateMachine Machine, Token Token)
Checks references in the node.
override Task Parse(XmlElement Xml)
Parses the State-machine node.
override async Task< bool > Register(int EventIndex, EvaluationArguments Arguments, OnEvent Event)
Registers the event
override async Task Unregister(int EventIndex, EvaluationArguments Arguments)
Registers the event
override IStateMachineNode Create()
Creates a new node of the corresponding type.
async Task< Event > GetReference(EvaluationArguments Arguments)
Evaluates the reference.
Action executed when entering a state.
Definition: OnEvent.cs:17
Class representing a state machine.
Definition: StateMachine.cs:37
bool TryGetEvent(string Id, out Model.Events.Event Event)
Tries to get an event.