Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
TimerActor.cs
1using System;
2using System.IO;
3using System.Threading;
4using System.Xml;
5using System.Threading.Tasks;
6using Waher.Content;
8
10{
14 public class TimerActor : Actor
15 {
16 private Duration period;
17 private bool isPeriodic;
18 private Timer timer;
19
26 : base(Parent, Model)
27 {
28 }
29
39 {
40 }
41
45 public override string LocalName => nameof(Timer);
46
54 {
55 return new TimerActor(Parent, Model);
56 }
57
62 public override Task FromXml(XmlElement Definition)
63 {
64 this.period = XML.Attribute(Definition, "period", Duration.Zero);
65 this.isPeriodic = this.period > Duration.Zero;
66
67 return base.FromXml(Definition);
68 }
69
78 public override Task<Actor> CreateInstanceAsync(int InstanceIndex, string InstanceId)
79 {
80 return Task.FromResult<Actor>(new TimerActor(this, this.Model, InstanceIndex, InstanceId)
81 {
82 period = this.period,
83 isPeriodic = this.isPeriodic
84 });
85 }
86
90 public override Task InitializeInstance()
91 {
92 return Task.CompletedTask;
93 }
94
95 private void TimerCallback(object State)
96 {
97 this.Model.ExternalEvent(this, "Elapsed");
98 }
99
103 public override Task StartInstance()
104 {
105 if (this.isPeriodic)
106 {
107 DateTime Now = DateTime.Now;
108 DateTime TP = Now + this.period;
109 TimeSpan TS = TP - Now;
110
111 this.timer = new Timer(this.TimerCallback, null, TS, TS);
112 }
113
114 return Task.CompletedTask;
115 }
116
120 public override Task FinalizeInstance()
121 {
122 this.timer?.Dispose();
123 this.timer = null;
124
125 return Task.CompletedTask;
126 }
127
133 public override void AnnotateActorUseCaseUml(StreamWriter Output, string Id)
134 {
135 if (this.isPeriodic)
136 {
137 Output.Write("note right of ");
138 Output.Write(Id);
139 Output.Write(" : Elapses periodically every ");
140
141 Values.Duration.ExportText(this.period, Output);
142
143 Output.WriteLine();
144 }
145 }
146
147 }
148}
Root node of a simulation model
Definition: Model.cs:49
bool ExternalEvent(IExternalEventsNode Source, string Name, params KeyValuePair< string, object >[] Arguments)
Method called when an external event has been received.
Definition: Model.cs:889
Abstract base class for actors
Definition: Actor.cs:15
string InstanceId
ID of actor instance.
Definition: Actor.cs:57
int InstanceIndex
Actor instance index.
Definition: Actor.cs:67
override string Id
ID of actor.
Definition: Actor.cs:52
Represents an internal clock.
Definition: TimerActor.cs:15
override Task FinalizeInstance()
Finalizes an instance of an actor.
Definition: TimerActor.cs:120
TimerActor(ISimulationNode Parent, Model Model, int InstanceIndex, string InstanceId)
Represents an internal clock.
Definition: TimerActor.cs:37
TimerActor(ISimulationNode Parent, Model Model)
Represents an internal clock.
Definition: TimerActor.cs:25
override Task FromXml(XmlElement Definition)
Sets properties and attributes of class in accordance with XML definition.
Definition: TimerActor.cs:62
override Task InitializeInstance()
Initializes an instance of an actor.
Definition: TimerActor.cs:90
override Task StartInstance()
Starts an instance of an actor.
Definition: TimerActor.cs:103
override void AnnotateActorUseCaseUml(StreamWriter Output, string Id)
Allows the actor to add notes related to the actor in use case diagrams.
Definition: TimerActor.cs:133
override Task< Actor > CreateInstanceAsync(int InstanceIndex, string InstanceId)
Creates an instance of the actor.
Definition: TimerActor.cs:78
override string LocalName
Local name of XML element defining contents of class.
Definition: TimerActor.cs:45
override ISimulationNode Create(ISimulationNode Parent, Model Model)
Creates a new instance of the node.
Definition: TimerActor.cs:53
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
Basic interface for simulator nodes. Implementing this interface allows classes with default contruct...
ISimulationNode Parent
Parent node in the simulation model.
Represents a duration value, as defined by the xsd:duration data type: http://www....
Definition: Duration.cs:13
static readonly Duration Zero
Zero value
Definition: Duration.cs:532