Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
StochasticEvent.cs
1using System;
2using System.IO;
3using System.Threading.Tasks;
4using System.Xml;
7using Waher.Script;
8
10{
15 {
16 private string distributionId;
17 private IDistribution distribution;
18 private Expression guard;
19 private int guardLimit;
20
27 : base(Parent, Model)
28 {
29 }
30
34 public override string LocalName => nameof(StochasticEvent);
35
39 public string DistributionId => this.distributionId;
40
44 public override IDistribution Distribution => this.distribution;
45
53 {
54 return new StochasticEvent(Parent, Model);
55 }
56
61 public override Task FromXml(XmlElement Definition)
62 {
63 this.distributionId = XML.Attribute(Definition, "distribution");
64 this.guardLimit = XML.Attribute(Definition, "guardLimit", 1000);
65
66 if (Definition.HasAttribute("guard"))
67 this.guard = new Expression(Definition.GetAttribute("guard"));
68 else
69 this.guard = null;
70
71 return base.FromXml(Definition);
72 }
73
77 public override Task Initialize()
78 {
79 if (!this.Model.TryGetDistribution(this.distributionId, out this.distribution))
80 throw new Exception("Distribution not found: " + this.distributionId);
81
82 return base.Initialize();
83 }
84
91 public void CheckTrigger(double t1, double t2, int NrCycles)
92 {
93 int n = this.distribution.CheckTrigger(t1, t2, NrCycles);
94 while (n > 0)
95 {
96 Task.Run(() => this.Trigger(this.Model.GetEventVariables(null), this.guard, this.guardLimit));
97 n--;
98 }
99 }
100
104 public override string UseCaseLinkName => this.distributionId;
105
111 public override void ExportUseCaseData(StreamWriter Output, int Index)
112 {
113 base.ExportUseCaseData(Output, Index);
114
115 if (!(this.guard is null))
116 {
117 Output.Write("note left of UC");
118 Output.WriteLine(Index.ToString());
119 Output.Write(this.distributionId);
120 Output.WriteLine(" is subject to conditions defined by script");
121 Output.WriteLine("end note");
122 }
123 }
124
125 }
126}
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 TryGetDistribution(string Id, out IDistribution Distribution)
Tries to get a registered distribution from the model.
Definition: Model.cs:313
Abstract base class for distributions
Definition: Distribution.cs:13
Abstract base class for events
Definition: Event.cs:19
async Task Trigger(Variables Variables, Expression Guard=null, int GuardLimit=int.MaxValue)
Triggers the event.
Definition: Event.cs:118
override Task Initialize()
Initialized the node before simulation.
void CheckTrigger(double t1, double t2, int NrCycles)
Check if event is triggered during a time period.
override Task FromXml(XmlElement Definition)
Sets properties and attributes of class in accordance with XML definition.
StochasticEvent(ISimulationNode Parent, Model Model)
Stochastic Event
override string UseCaseLinkName
Name of use case association.
override ISimulationNode Create(ISimulationNode Parent, Model Model)
Creates a new instance of the node.
override void ExportUseCaseData(StreamWriter Output, int Index)
Exports use case diagram data.
override string LocalName
Local name of XML element defining contents of class.
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
Class managing a script expression.
Definition: Expression.cs:39
Basic interface for simulator nodes. Implementing this interface allows classes with default contruct...
ISimulationNode Parent
Parent node in the simulation model.
int CheckTrigger(double t1, double t2, int NrCycles)
Check if distribution has a sample within the time period.
Interface for events that can be triggered by elapsed time.