Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Wait.cs
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Threading.Tasks;
5using Waher.Script;
6
8{
12 public class Wait : ActivityNode
13 {
14 private readonly List<ITriggerNode> triggers = new List<ITriggerNode>();
15 private Timeout timeout = null;
16
23 : base(Parent, Model)
24 {
25 }
26
30 public override string LocalName => nameof(Wait);
31
39 {
40 return new Wait(Parent, Model);
41 }
42
47 public void Register(ITriggerNode Node)
48 {
49 if (Node is Timeout Timeout)
50 {
51 if (this.timeout is null)
52 this.timeout = Timeout;
53 else
54 throw new Exception("A timeout has already been defined.");
55 }
56 else
57 this.triggers.Add(Node);
58 }
59
65 public override async Task<LinkedListNode<IActivityNode>> Execute(Variables Variables)
66 {
67 int i, c = this.triggers.Count;
68 if (c == 0)
69 throw new Exception("No triggers defined.");
70
71 int d = c;
72
73 if (!(this.timeout is null))
74 d++;
75
76 Task[] Tasks = new Task[d];
77
78 for (i = 0; i < c; i++)
79 Tasks[i] = this.triggers[i].GetTask();
80
81 if (!(this.timeout is null))
82 Tasks[c] = this.timeout.GetTask();
83
84 Task Result = await Task.WhenAny(Tasks);
85 i = Array.IndexOf<Task>(Tasks, Result);
86
87 if (i < 0)
88 throw new Exception("Unexpected error.");
89
90 if (i < c)
91 await this.triggers[i].Execute(Variables);
92 else
93 await this.timeout.Execute(Variables);
94
95 return null;
96 }
97
104 public override void ExportPlantUml(StreamWriter Output, int Indentation, char QuoteChar)
105 {
106 Indent(Output, Indentation);
107 Output.WriteLine(":Wait for first:;");
108 Indent(Output, Indentation);
109
110 bool First = true;
111
112 foreach (ITriggerNode Node in this.triggers)
113 {
114 Node.ExportPlantUml(Output, Indentation, First, QuoteChar);
115 First = false;
116 }
117
118 if (!First)
119 {
120 Indent(Output, Indentation);
121 Output.WriteLine("end split");
122 }
123 }
124
125 }
126}
Root node of a simulation model
Definition: Model.cs:49
Abstract base class for activity nodes
Definition: ActivityNode.cs:15
Adds a timeout limit in a Wait statement.
Definition: Timeout.cs:16
Task GetTask()
Gets a task object.
Definition: Timeout.cs:82
override async Task< LinkedListNode< IActivityNode > > Execute(Variables Variables)
Executes a node.
Definition: Timeout.cs:72
Conditional execution in an activity, based on external events.
Definition: Wait.cs:13
override async Task< LinkedListNode< IActivityNode > > Execute(Variables Variables)
Executes a node.
Definition: Wait.cs:65
override string LocalName
Local name of XML element defining contents of class.
Definition: Wait.cs:30
void Register(ITriggerNode Node)
Register a trigger node.
Definition: Wait.cs:47
override ISimulationNode Create(ISimulationNode Parent, Model Model)
Creates a new instance of the node.
Definition: Wait.cs:38
override void ExportPlantUml(StreamWriter Output, int Indentation, char QuoteChar)
Exports PlantUML
Definition: Wait.cs:104
Wait(ISimulationNode Parent, Model Model)
Conditional execution in an activity, based on external events.
Definition: Wait.cs:22
static void Indent(StreamWriter Output, int Indentation)
Adds indentation to the current row.
Collection of variables.
Definition: Variables.cs:25
Basic interface for simulator nodes. Implementing this interface allows classes with default contruct...
ISimulationNode Parent
Parent node in the simulation model.
void ExportPlantUml(StreamWriter Output, int Indentation, bool First, char QuoteChar)
Exports PlantUML