Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ActorReference.cs
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Threading.Tasks;
5using System.Xml;
8using Waher.Script;
9
11{
16 {
17 private readonly List<IActor> actors = new List<IActor>();
18 private IActor[] actorsStat;
19 private int count;
20 private string name;
21 private string name2;
22 private bool exclusive;
23
30 : base(Parent, Model)
31 {
32 }
33
37 public override string LocalName => nameof(ActorReference);
38
42 public string Name => this.name;
43
47 public bool Exclusive => this.exclusive;
48
56 {
57 return new ActorReference(Parent, Model);
58 }
59
64 public override Task FromXml(XmlElement Definition)
65 {
66 this.name = XML.Attribute(Definition, "name");
67 this.exclusive = XML.Attribute(Definition, "exclusive", true);
68
69 return base.FromXml(Definition);
70 }
71
76 public void Register(IActor Actor)
77 {
78 this.actors.Add(Actor);
79 }
80
84 public override Task Start()
85 {
86 this.actorsStat = this.actors.ToArray();
87 this.count = this.actorsStat.Length;
88 this.name2 = this.name + " Actor";
89
90 return base.Start();
91 }
92
98 public override Task Prepare(Variables Variables, List<KeyValuePair<string, object>> Tags)
99 {
101 int[] P = new int[this.count];
102 int i, j;
103
104 lock (this.Model)
105 {
106 for (i = j = 0; i < this.count; i++)
107 {
108 j += this.actorsStat[i].FreeCount;
109 P[i] = j;
110 }
111
112 if (j <= 0)
113 throw new Exception("No free individual available in population.");
114
115 j = this.Model.GetRandomInteger(j);
116 i = 0;
117
118 while (P[i] <= j)
119 i++;
120
121 if (i > 0)
122 j -= P[i - 1];
123
124 Actor = this.actorsStat[i].GetFreeIndividual(j, this.exclusive);
125 }
126
127 Variables[this.name2] = Actor;
128 Variables[this.name] = Actor.ActivityObject;
129
130 Tags.Add(new KeyValuePair<string, object>(this.name, Actor.InstanceId));
131
132 return Task.CompletedTask;
133 }
134
139 public override void Release(Variables Variables)
140 {
141 if (this.exclusive &&
142 Variables.TryGetVariable(this.name2, out Variable v) &&
143 v.ValueObject is IActor InstanceActor &&
144 InstanceActor.Parent is IActor ActorPopulation)
145 {
146 lock (this.Model)
147 {
148 ActorPopulation.ReturnIndividual(InstanceActor);
149 }
150
151 Variables.Remove(this.name2);
152 }
153 }
154
161 public override void ExportPlantUml(StreamWriter Output, string Name, int Index)
162 {
163 int i = 0;
164
165 foreach (IActor Actor in this.actors)
166 {
167 string s = this.name + "_" + Index.ToString() + "_A" + i.ToString();
168
169 Output.Write("actor \"");
170 Output.Write(this.name);
171 Output.Write("\" as ");
172 Output.Write(s);
173 Output.Write(" <<");
174 Output.Write(Actor.Id);
175 Output.WriteLine(">>");
176
178
179 Output.Write(s);
180 Output.Write(" --> UC");
181 Output.Write(Index.ToString());
182
183 if (!string.IsNullOrEmpty(Name))
184 {
185 Output.Write(" : ");
186 Output.Write(Name);
187 }
188
189 Output.WriteLine();
190
191 i++;
192 }
193 }
194
195 }
196}
Root node of a simulation model
Definition: Model.cs:49
int GetRandomInteger(int MaxValueExclusive)
Generates a new random integer between 0 (inclusive) and MaxValueExclusive (exclusive).
Definition: Model.cs:798
Abstract base class for actors
Definition: Actor.cs:15
string InstanceId
ID of actor instance.
Definition: Actor.cs:57
override string Id
ID of actor.
Definition: Actor.cs:52
virtual object ActivityObject
Returns the object that will be used by the actor for actions during an activity.
Definition: Actor.cs:223
References a population of actors.
bool Exclusive
If the actor is referenced for exclusive use in the event (i.e. cannot participate in another event a...
string Name
Name of actor within the scope of the event.
override ISimulationNode Create(ISimulationNode Parent, Model Model)
Creates a new instance of the node.
override Task Prepare(Variables Variables, List< KeyValuePair< string, object > > Tags)
Prepares Variables for the execution of an event.
void Register(IActor Actor)
Registers an actor with the collection of actors.
override Task Start()
Starts the node.
override void ExportPlantUml(StreamWriter Output, string Name, int Index)
Exports the node to PlantUML script in a markdown document.
override void Release(Variables Variables)
Releases resources at the end of an event.
ActorReference(ISimulationNode Parent, Model Model)
References a population of actors.
override string LocalName
Local name of XML element defining contents of class.
override Task FromXml(XmlElement Definition)
Sets properties and attributes of class in accordance with XML definition.
Abstract base class for event preparation nodes (with children).
virtual void AnnotateActorUseCaseUml(StreamWriter Output, string Id)
Allows the actor to add notes related to the actor in use case diagrams.
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
Contains information about a variable.
Definition: Variable.cs:10
Collection of variables.
Definition: Variables.cs:25
virtual bool TryGetVariable(string Name, out Variable Variable)
Tries to get a variable object, given its name.
Definition: Variables.cs:52
virtual bool Remove(string VariableName)
Removes a varaiable from the collection.
Definition: Variables.cs:147
Basic interface for simulator nodes. Implementing this interface allows classes with default contruct...
Task Start()
Starts the node.
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
int FreeCount
Number of individuals in population that are free.
Definition: IActor.cs:32
IActor GetFreeIndividual(int Index, bool Exclusive)
Gets a free individual instance from the population.
Interface for collections of actors.
Definition: IActors.cs:7