Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Activity.cs
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Text;
5using System.Threading.Tasks;
6using System.Xml;
9using Waher.Script;
10
12{
17 {
18 private LinkedList<IActivityNode> activityNodes = null;
19 private LinkedList<IEvent> events = null;
20 private string id;
21 private int executionCount = 0;
22 private bool logStart;
23 private bool logEnd;
24
31 : base(Parent, Model)
32 {
33 }
34
38 public string Id => this.id;
39
43 public int ExecutionCount => this.executionCount;
44
48 public bool LogStart => this.logStart;
49
53 public bool LogEnd => this.logEnd;
54
58 public override string LocalName => nameof(Activity);
59
67 {
68 return new Activity(Parent, Model);
69 }
70
75 public override Task FromXml(XmlElement Definition)
76 {
77 this.id = XML.Attribute(Definition, "id");
78 this.logStart = XML.Attribute(Definition, "logStart", true);
79 this.logEnd = XML.Attribute(Definition, "logEnd", true);
80
81 return base.FromXml(Definition);
82 }
83
87 public override Task Initialize()
88 {
89 this.Model.Register(this);
90 return base.Initialize();
91 }
92
97 public void Register(IActivityNode Node)
98 {
99 if (this.activityNodes is null)
100 this.activityNodes = new LinkedList<IActivityNode>();
101
102 this.Model.Register(this.activityNodes.AddLast(Node));
103 }
104
109 public void Register(IEvent Event)
110 {
111 if (this.events is null)
112 this.events = new LinkedList<IEvent>();
113
114 this.events.AddLast(Event);
115 }
116
121 public virtual async Task ExecuteTask(Variables Variables)
122 {
123 this.executionCount++;
124
125 if (!(this.activityNodes is null))
126 {
127 try
128 {
129 await ExecuteActivity(Variables, this.activityNodes.First);
130 }
131 catch (FinishedException)
132 {
133 // Execution finished.
134 }
135 }
136 }
137
143 public static async Task ExecuteActivity(Variables Variables, LinkedListNode<IActivityNode> Start)
144 {
145 LinkedListNode<IActivityNode> Next;
146
147 while (!(Start is null))
148 {
149 Next = await Start.Value.Execute(Variables);
150 if (Next is null)
151 Next = Start.Next;
152
153 Start = Next;
154 }
155 }
156
161 public override async Task ExportMarkdown(StreamWriter Output)
162 {
163 Output.WriteLine(this.id);
164 Output.WriteLine(new string('-', this.id.Length + 3));
165 Output.WriteLine();
166
167 await base.ExportMarkdown(Output);
168
169 Output.WriteLine("```uml: Use Case chart for " + this.id);
170 Output.WriteLine("@startuml");
171
172 int Index = 0;
173
174 if (!(this.events is null))
175 {
176 foreach (IEvent Event in this.events)
177 {
178 string Desc = Event.Description;
179
180 Output.Write("usecase UC");
181 Output.Write((++Index).ToString());
182 Output.Write(" as \"");
183 Output.Write(this.id);
184
185 if (!string.IsNullOrEmpty(Desc))
186 {
187 Output.WriteLine();
188 Output.WriteLine("==");
189 Output.Write(BreakWords(Desc, 25));
190 }
191
192 Output.WriteLine("\"");
193
194 Event.ExportUseCaseData(Output, Index);
195 }
196 }
197
198 if (Index == 0)
199 {
200 Output.Write("usecase \"");
201 Output.Write(this.id);
202 Output.WriteLine("\" as UC1");
203 }
204
205 Output.WriteLine("@enduml");
206 Output.WriteLine("```");
207 Output.WriteLine();
208
209
210 Output.WriteLine("```uml: Activity chart for " + this.id);
211 Output.WriteLine("@startuml");
212
213 foreach (IActivityNode Node in this.activityNodes)
214 Node.ExportPlantUml(Output, 0, '"');
215
216 Output.WriteLine("@enduml");
217 Output.WriteLine("```");
218 Output.WriteLine();
219
220 this.Model.ExportActivityCharts(this.id, Output, this.events);
221 }
222
223 private static string BreakWords(string s, int Width)
224 {
225 StringBuilder sb = new StringBuilder();
226 int c, l = 0;
227
228 foreach (string Word in s.Split(new char[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries))
229 {
230 c = Word.Length;
231
232 if (l > 0)
233 {
234 if (l + c < Width)
235 {
236 sb.Append(' ');
237 l++;
238 }
239 else
240 {
241 sb.AppendLine();
242 l = 0;
243 }
244 }
245
246 sb.Append(Word);
247 l += c;
248 }
249
250 return sb.ToString();
251 }
252
253 }
254}
Root node of a simulation model
Definition: Model.cs:49
void Register(IDistribution Distribution)
Registers a distribution with the runtime environment of the model.
Definition: Model.cs:299
Represents an activity that can be executed as the result of triggered events.
Definition: Activity.cs:17
override string LocalName
Local name of XML element defining contents of class.
Definition: Activity.cs:58
override Task FromXml(XmlElement Definition)
Sets properties and attributes of class in accordance with XML definition.
Definition: Activity.cs:75
void Register(IEvent Event)
Registers an event that calls the activity.
Definition: Activity.cs:109
bool LogEnd
If event should be logged at the end of each activity.
Definition: Activity.cs:53
Activity(ISimulationNode Parent, Model Model)
Represents an activity that can be executed as the result of triggered events.
Definition: Activity.cs:30
virtual async Task ExecuteTask(Variables Variables)
Executes the activity.
Definition: Activity.cs:121
override async Task ExportMarkdown(StreamWriter Output)
Exports PlantUML
Definition: Activity.cs:161
override Task Initialize()
Initialized the node before simulation.
Definition: Activity.cs:87
void Register(IActivityNode Node)
Registers a child activity node.
Definition: Activity.cs:97
static async Task ExecuteActivity(Variables Variables, LinkedListNode< IActivityNode > Start)
Executes an activity by executing a possibly branching sequence of nodes.
Definition: Activity.cs:143
override ISimulationNode Create(ISimulationNode Parent, Model Model)
Creates a new instance of the node.
Definition: Activity.cs:66
bool LogStart
If event should be logged at each start of the activity.
Definition: Activity.cs:48
Exceptions thrown when the execution of an activity is completed.
Represents the starting point of the activity.
Definition: Start.cs:13
override Task< LinkedListNode< IActivityNode > > Execute(Variables Variables)
Executes a node.
Definition: Start.cs:45
Abstract base class for events
Definition: Event.cs:19
string Description
Event description
Definition: Event.cs:227
virtual void ExportUseCaseData(StreamWriter Output, int Index)
Exports use case diagram data.
Definition: Event.cs:245
Abstract base class for simulation nodes with children
ISimulationNode Parent
Parent node in the simulation model.
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...
void ExportPlantUml(StreamWriter Output, int Indentation, char QuoteChar)
Exports PlantUML