Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SimulationNode.cs
1using System;
2using System.IO;
3using System.Threading.Tasks;
4using System.Xml;
6using Waher.Script;
7
9{
13 public abstract class SimulationNode : ISimulationNode
14 {
15 private readonly ISimulationNode parent;
16 private readonly Model model;
17
24 {
25 this.parent = Parent;
26 this.model = Model;
27 }
28
32 public ISimulationNode Parent => this.parent;
33
37 public Model Model
38 {
39 get => this.model;
40 }
41
45 public virtual string Namespace => Model.ComSimNamespace;
46
50 public virtual string SchemaResource => "TAG.Simulator.Schema.ComSim.xsd";
51
55 public abstract string LocalName
56 {
57 get;
58 }
59
67
72 public abstract Task FromXml(XmlElement Definition);
73
79 public virtual Task ForEach(ForEachCallbackMethod Method, bool DepthFirst)
80 {
81 return Method(this);
82 }
83
87 public virtual Task Initialize()
88 {
89 return Task.CompletedTask;
90 }
91
95 public virtual Task Start()
96 {
97 return Task.CompletedTask;
98 }
99
103 public virtual Task Finalize()
104 {
105 return Task.CompletedTask;
106 }
107
112 public virtual Task ExportMarkdown(StreamWriter Output)
113 {
114 return Task.CompletedTask;
115 }
116
121 public virtual Task ExportXml(XmlWriter Output)
122 {
123 return Task.CompletedTask;
124 }
125
131 protected static void Indent(StreamWriter Output, int Indentation)
132 {
133 if (Indentation > 0)
134 Output.Write(new string('\t', Indentation));
135 }
136
145 {
146 string ActorStr = await Actor.GetValueAsync(Variables);
147 object Result;
148
149 if (Variables.TryGetVariable(ActorStr, out Variable v))
150 Result = v.ValueObject;
151 else
152 {
153 Expression Exp;
154
155 lock (this.synchObj)
156 {
157 if (this.lastActorExpression is null || ActorStr != this.lastActor)
158 {
159 this.lastActorExpression = new Expression(ActorStr);
160 this.lastActor = ActorStr;
161 }
162
163 Exp = this.lastActorExpression;
164 }
165
166 Result = await Exp.EvaluateAsync(Variables);
167 }
168
169 if (Result is Actor Actor2)
170 return Actor2.ActivityObject;
171 else
172 return Result;
173 }
174
183 {
184 if (await this.GetActorObjectAsync(Actor, Variables) is IActor ActorRef)
185 return ActorRef;
186 else
187 throw new Exception("Expected an actor: " + Actor);
188 }
189
190 private string lastActor = null;
191 private Expression lastActorExpression = null;
192 private readonly object synchObj = new object();
193 }
194}
Root node of a simulation model
Definition: Model.cs:49
const string ComSimNamespace
http://lab.tagroot.io/Schema/ComSim.xsd
Definition: Model.cs:105
Abstract base class for actors
Definition: Actor.cs:15
Abstract base class for simulation nodes
SimulationNode(ISimulationNode Parent, Model Model)
Abstract base class for simulation nodes
async Task< object > GetActorObjectAsync(StringAttribute Actor, Variables Variables)
Gets an actor object, given a string representation, possibly containing script, of the actor.
virtual Task Finalize()
Finalizes the node after simulation.
static void Indent(StreamWriter Output, int Indentation)
Adds indentation to the current row.
ISimulationNode Parent
Parent node in the simulation model.
abstract string LocalName
Local name of XML element defining contents of class.
Model Model
Model in which the node is defined.
virtual Task ForEach(ForEachCallbackMethod Method, bool DepthFirst)
Evaluates Method on each node in the subtree defined by the current node.
virtual Task Initialize()
Initialized the node before simulation.
virtual Task ExportXml(XmlWriter Output)
Exports XML
virtual string SchemaResource
Points to the embedded XML Schema resource defining the semantics of the XML namespace.
async Task< IActor > GetActorAsync(StringAttribute Actor, Variables Variables)
Gets an actor, given a string representation, possibly containing script, of the actor.
virtual Task ExportMarkdown(StreamWriter Output)
Exports Markdown
abstract ISimulationNode Create(ISimulationNode Parent, Model Model)
Creates a new instance of the node.
abstract Task FromXml(XmlElement Definition)
Sets properties and attributes of class in accordance with XML definition.
virtual Task Start()
Starts the node.
virtual string Namespace
XML Namespace where the element is defined.
Contains the value of a string attribute, possibly with embedded script.
Class managing a script expression.
Definition: Expression.cs:39
async Task< object > EvaluateAsync(Variables Variables)
Evaluates the expression, using the variables provided in the Variables collection....
Definition: Expression.cs:4275
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
Basic interface for simulator nodes. Implementing this interface allows classes with default contruct...
Basic interface for simulator nodes. Implementing this interface allows classes with default contruct...
Definition: IActor.cs:11
delegate Task ForEachCallbackMethod(ISimulationNode Node)
Callback method for iteration across the simulation model.