Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
HandlerNode.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Xml;
11using Waher.Script;
12
14{
18 public abstract class HandlerNode : SimulationNode, IExternalEvent
19 {
20 private IEvent @event;
21 private IActor actor;
22 private string name;
23 private string @namespace;
24 private string eventId;
25 private string actorName;
26 private string eventArgs;
27
34 : base(Parent, Model)
35 {
36 }
37
41 public IEvent Event => this.@event;
42
46 public string Name => this.name;
47
51 public string HandlerNamespace => this.@namespace;
52
56 public string EventId => this.eventId;
57
61 public string ActorName => this.actorName;
62
66 public IActor Actor => this.actor;
67
71 public IExternalEventsNode Events => this.actor;
72
76 public string EventArgs => this.eventArgs;
77
81 public override string SchemaResource => XmppActor.XmppSchema;
82
86 public override string Namespace => XmppActor.XmppNamespace;
87
92 public override Task FromXml(XmlElement Definition)
93 {
94 this.name = XML.Attribute(Definition, "name");
95 this.@namespace = XML.Attribute(Definition, "namespace");
96 this.eventId = XML.Attribute(Definition, "event");
97 this.actorName = XML.Attribute(Definition, "actorName");
98 this.eventArgs = XML.Attribute(Definition, "eventArgs");
99
100 return Task.CompletedTask;
101 }
102
106 public override Task Initialize()
107 {
108 this.actor = this.Parent as IActor;
109 if (!(this.actor is null))
110 this.actor.Register(this);
111 else
112 throw new Exception("External event registered on a node that is not an actor.");
113
114 return base.Initialize();
115 }
116
120 public override Task Start()
121 {
122 if (!this.Model.TryGetEvent(this.eventId, out this.@event))
123 throw new Exception("Event not found: " + this.eventId);
124
125 this.@event.Register(this);
126
127 return base.Start();
128 }
129
136 public virtual void Trigger(IActor Source, params KeyValuePair<string, object>[] Arguments)
137 {
139
140 if (!string.IsNullOrEmpty(this.actorName))
141 Variables[this.actorName] = Source.ActivityObject;
142
143 if (!(Arguments is null))
144 {
145 foreach (KeyValuePair<string, object> Argument in Arguments)
146 Variables[Argument.Key] = Argument.Value;
147 }
148
149 this.@event?.Trigger(Variables);
150 }
151
155 public IEnumerable<Parameter> Parameters
156 {
157 get
158 {
159 return new Parameter[] { new Parameter(this, this.Model, "e", string.IsNullOrEmpty(this.eventArgs) ? "e" : this.eventArgs) };
160 }
161 }
162
168 public abstract void RegisterHandlers(IActor Actor, XmppClient Client);
169 }
170}
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 actors
Definition: Actor.cs:15
Abstract base class for events
Definition: Event.cs:19
Sets a variable to the value of an event parameter.
Definition: Parameter.cs:11
Abstract base class for simulation nodes
ISimulationNode Parent
Parent node in the simulation model.
Abstract base class for XMPP actors.
Definition: XmppActor.cs:23
const string XmppSchema
TAG.Simulator.XMPP.Schema.ComSimXmpp.xsd
Definition: XmppActor.cs:32
const string XmppNamespace
http://lab.tagroot.io/Schema/ComSim/XMPP.xsd
Definition: XmppActor.cs:27
Abstract base class for handler nodes
Definition: HandlerNode.cs:19
abstract void RegisterHandlers(IActor Actor, XmppClient Client)
Registers handlers on the XMPP Client.
string EventArgs
Variable name for the event arguments
Definition: HandlerNode.cs:76
override Task Start()
Starts the node.
Definition: HandlerNode.cs:120
string EventId
Event to trigger
Definition: HandlerNode.cs:56
IEnumerable< Parameter > Parameters
Parameters
Definition: HandlerNode.cs:156
override Task FromXml(XmlElement Definition)
Sets properties and attributes of class in accordance with XML definition.
Definition: HandlerNode.cs:92
HandlerNode(ISimulationNode Parent, Model Model)
Abstract base class for handler nodes
Definition: HandlerNode.cs:33
string ActorName
Variable name of the actor
Definition: HandlerNode.cs:61
string HandlerNamespace
Namespace for the handler
Definition: HandlerNode.cs:51
virtual void Trigger(IActor Source, params KeyValuePair< string, object >[] Arguments)
Method called when an external event has been received.
Definition: HandlerNode.cs:136
override string SchemaResource
Points to the embedded XML Schema resource defining the semantics of the XML namespace.
Definition: HandlerNode.cs:81
override string Namespace
XML Namespace where the element is defined.
Definition: HandlerNode.cs:86
override Task Initialize()
Initialized the node before simulation.
Definition: HandlerNode.cs:106
string Name
Local element name for the handler
Definition: HandlerNode.cs:46
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
Manages an XMPP client connection. Implements XMPP, as defined in https://tools.ietf....
Definition: XmppClient.cs:59
Collection of variables.
Definition: Variables.cs:25
Basic interface for simulator nodes. Implementing this interface allows classes with default contruct...
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
Interface for nodes that can register external events.
void Register(IExternalEvent ExternalEvent)
Registers an external event on the actor.