Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ExternalEvent.cs
1using System;
2using System.Collections.Generic;
3using System.Net.NetworkInformation;
4using System.Threading.Tasks;
5using System.Xml;
8using Waher.Script;
9
11{
16 {
17 private Dictionary<string, Parameter> parameters = null;
18 private IEvent eventReference;
19 private IExternalEventsNode events;
20 private string name;
21 private string eventId;
22 private string actorName;
23
30 : base(Parent, Model)
31 {
32 }
33
37 public string Name => this.name;
38
42 public string EventId => this.eventId;
43
47 public string ActorName => this.actorName;
48
52 public IExternalEventsNode Events => this.events;
53
57 public IEvent Event => this.eventReference;
58
62 public override string LocalName => nameof(ExternalEvent);
63
67 public IEnumerable<Parameter> Parameters => this.parameters?.Values;
68
76 {
77 return new ExternalEvent(Parent, Model);
78 }
79
84 public override Task FromXml(XmlElement Definition)
85 {
86 this.name = XML.Attribute(Definition, "name");
87 this.eventId = XML.Attribute(Definition, "event");
88 this.actorName = XML.Attribute(Definition, "actorName");
89
90 return base.FromXml(Definition);
91 }
92
96 public override Task Initialize()
97 {
98 ISimulationNode Loop = this.Parent;
99
100 while (!(Loop is null))
101 {
102 if (Loop is IExternalEventsNode Events)
103 {
104 this.events = Events;
105 this.events.Register(this);
106 break;
107 }
108 else
109 Loop = Loop.Parent;
110 }
111
112 if (Loop is null)
113 throw new Exception("External event registered on a node that is not hosted by an actor.");
114
115 return Task.CompletedTask;
116 }
117
121 public override Task Start()
122 {
123 if (!this.Model.TryGetEvent(this.eventId, out this.eventReference))
124 throw new Exception("Event node not found: " + this.eventId);
125
126 this.eventReference.Register(this);
127
128 return base.Start();
129 }
130
136 {
137 string Name = Parameter.Name;
138
139 if (this.parameters is null)
140 this.parameters = new Dictionary<string, Parameter>();
141
142 if (this.parameters.ContainsKey(Name))
143 throw new Exception("A parameter named " + Name + " has already been registered for external event " + this.eventId);
144
145 this.parameters[Name] = Parameter;
146 }
147
154 public void Trigger(IActor Source, params KeyValuePair<string, object>[] Arguments)
155 {
157
158 if (!string.IsNullOrEmpty(this.actorName))
159 Variables[this.actorName] = Source.ActivityObject;
160
161 if (!(this.parameters is null))
162 {
163 string Name;
164
165 foreach (KeyValuePair<string, object> P in Arguments)
166 {
167 if (this.parameters.TryGetValue(P.Key, out Parameter P2))
168 {
169 Name = P2.Variable;
170 if (string.IsNullOrEmpty(Name))
171 Name = P2.Name;
172
173 Variables[Name] = P.Value;
174 }
175 }
176 }
177
178 this.eventReference.Trigger(Variables);
179 }
180
181 }
182}
Root node of a simulation model
Definition: Model.cs:49
Variables GetEventVariables(IActor Actor)
Gets a collection of variables for a new event.
Definition: Model.cs:1202
bool TryGetEvent(string Id, out IEvent Event)
Tries to get a registered event from the model.
Definition: Model.cs:365
Abstract base class for events
Definition: Event.cs:19
Handles an incoming event raised from an external source
override Task Initialize()
Initialized the node before simulation.
string EventId
Optional name for variable, if different from Name
string ActorName
Optional name for actor variable reference of sender of external event.
override Task FromXml(XmlElement Definition)
Sets properties and attributes of class in accordance with XML definition.
override ISimulationNode Create(ISimulationNode Parent, Model Model)
Creates a new instance of the node.
void Trigger(IActor Source, params KeyValuePair< string, object >[] Arguments)
Method called when an external event has been received.
ExternalEvent(ISimulationNode Parent, Model Model)
Handles an incoming event raised from an external source
void Register(Parameter Parameter)
Registers a parameter with the external event.
IExternalEventsNode Events
Reference to collection of external events.
IEnumerable< Parameter > Parameters
Parameters
override Task Start()
Starts the node.
override string LocalName
Local name of XML element defining contents of class.
Sets a variable to the value of an event parameter.
Definition: Parameter.cs:11
string Name
Name of external event
Definition: Parameter.cs:47
Abstract base class for simulation nodes with children
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
Collection of variables.
Definition: Variables.cs:25
Basic interface for simulator nodes. Implementing this interface allows classes with default contruct...
ISimulationNode Parent
Parent node in the simulation model.
Basic interface for simulator nodes. Implementing this interface allows classes with default contruct...
Definition: IActor.cs:11
object ActivityObject
Returns the object that will be used by the actor for actions during an activity.
Definition: IActor.cs:54
Task Trigger(Variables Variables, Expression Guard=null, int GuardLimit=int.MaxValue)
Triggers the event.
void Register(IEventPreparation PreparationNode)
Registers an event preparation node.
Interface for nodes that can register external events.
void Register(IExternalEvent ExternalEvent)
Registers an external event on the actor.