Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ActionReference.cs
1using System;
2using System.Threading.Tasks;
3using System.Xml;
11
13{
17 public abstract class ActionReference : StateMachineNode
18 {
19 private ScriptableStringAttribute actionRef;
20 private ExpressionAttribute beforeActionScript;
21
26 : base()
27 {
28 }
29
33 [DefaultValueNull]
35 {
36 get => this.actionRef?.Definition;
37 set => this.actionRef = new ScriptableStringAttribute(value, this);
38 }
39
43 [DefaultValueNull]
44 public string BeforeActionScript
45 {
46 get => this.beforeActionScript?.Expression;
47 set => this.beforeActionScript = new ExpressionAttribute(value, false, this);
48 }
49
53 public bool HasActionReference => !(this.actionRef?.IsEmpty ?? true);
54
58 public bool HasConstantActionReference => this.actionRef?.IsConstant ?? false;
59
63 public bool HasExpressionActionReference => this.actionRef?.IsExpression ?? false;
64
69 public override Task Parse(XmlElement Xml)
70 {
71 this.actionRef = new ScriptableStringAttribute(XML.Attribute(Xml, "actionRef"), this);
72 this.beforeActionScript = new ExpressionAttribute(XML.Attribute(Xml, "beforeActionScript"), false, this);
73
74 return base.Parse(Xml);
75 }
76
82 public override void CheckReferences(StateMachine Machine, Token Token)
83 {
84 if (this.actionRef.IsConstant && !this.actionRef.IsEmpty && !Machine.TryGetAction(this.actionRef.Definition, out _))
85 throw new StateMachineException(Machine, "Action not found: " + this.actionRef.Definition);
86
87 base.CheckReferences(Machine, Token);
88 }
89
95 public Task<string> GetReference(EvaluationArguments Arguments)
96 {
97 return this.actionRef?.Evaluate(Arguments.Variables) ?? Task.FromResult<string>(null);
98 }
99
106 public async Task<TimeSpan> ExecuteLog(EvaluationArguments Arguments, bool SuppressSample)
107 {
108 string ActionId = await this.GetReference(Arguments);
109 if (string.IsNullOrEmpty(ActionId))
110 return TimeSpan.Zero;
111
112 DateTime Start = DateTime.UtcNow;
113 await this.ExecuteNoLog(ActionId, Arguments, SuppressSample);
114 TimeSpan Elapsed = DateTime.UtcNow.Subtract(Start);
115
116 if (SuppressSample)
117 return Elapsed;
118
120 {
121 Variable = "<" + ActionId + ">",
122 Timestamp = Start,
123 Value = Elapsed,
124 StateMachineId = Arguments.Machine.StateMachineId,
125 Expires = Arguments.Machine.Expires,
126 ArchiveOptional = Arguments.Machine.ArchiveOptional,
127 ArchiveRequired = Arguments.Machine.ArchiveRequired
128 });
129
130 return Elapsed;
131 }
132
138 public async Task ExecuteNoLog(EvaluationArguments Arguments, bool SuppressProfiling)
139 {
140 string ActionId = await this.GetReference(Arguments);
141 await this.ExecuteNoLog(ActionId, Arguments, SuppressProfiling);
142 }
143
150 public async Task ExecuteNoLog(string ActionId, EvaluationArguments Arguments, bool SuppressProfiling)
151 {
152 if (!string.IsNullOrEmpty(ActionId))
153 {
154 if (!Arguments.Machine.TryGetAction(ActionId, out Action Action))
155 throw new StateMachineException(Arguments.Machine, "Action not found: " + ActionId);
156
157 ProfilerThread Thread = SuppressProfiling ? null : Arguments.Profiler?.GetThread(ActionId, ProfilerThreadType.Binary);
158 Thread?.High();
159
160 try
161 {
162 if (this.beforeActionScript?.HasExpreession ?? false)
163 await this.beforeActionScript.Evaluate(Arguments.Variables);
164
165 await Action.Execute(Arguments);
166 }
167 finally
168 {
169 Thread?.Low();
170 }
171 }
172 }
173 }
174}
Helps with common XML-related tasks.
Definition: XML.cs:21
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:1062
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:21
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
Definition: Database.cs:97
ProfilerThread GetThread(string Name, ProfilerThreadType Type)
Gets a profiler thread. If none is available, a new is created.
Definition: Profiler.cs:145
Class that keeps track of events and timing for one thread.
void Low()
Sets the (binary) state to "low".
void High()
Sets the (binary) state to "high".
override async Task Execute(EvaluationArguments Arguments)
Evaluates the action node
Abstract base class for nodes referencing an action.
async Task ExecuteNoLog(EvaluationArguments Arguments, bool SuppressProfiling)
Evaluates an action, without logging the time the action took as a sample value.
override Task Parse(XmlElement Xml)
Parses the State-machine node.
ActionReference()
Abstract base class for nodes referencing an action.
bool HasExpressionActionReference
If event has an action reference defined in an expression.
Task< string > GetReference(EvaluationArguments Arguments)
Evaluates the reference.
override void CheckReferences(StateMachine Machine, Token Token)
Checks references in the node.
async Task ExecuteNoLog(string ActionId, EvaluationArguments Arguments, bool SuppressProfiling)
Evaluates an action, without logging the time the action took as a sample value.
async Task< TimeSpan > ExecuteLog(EvaluationArguments Arguments, bool SuppressSample)
Evaluates an action, and logs the time the action took as a sample value.
bool HasConstantActionReference
If event has a constant action reference.
async Task< object > Evaluate(Variables Variables)
Evaluates the attribute
async Task< T > Evaluate(Variables Variables)
Evaluates the attribute
Contains information required for evaluating script in a state-machine.
StateMachine Machine
Reference to state-machine definition.
Abstract base class for State-Machine nodes.
Variable definition in a State-Machine
Definition: Variable.cs:14
Class representing a state machine.
Definition: StateMachine.cs:43
bool TryGetAction(string Id, out Model.Actions.Action Action)
Tries to get an action.
CaseInsensitiveString StateMachineId
ID of State Machine.
Definition: StateMachine.cs:57
Duration? ArchiveOptional
Duration after which token expires, and the required archiving time, the token can optionally be arch...
DateTime Expires
When state-machine expires
Definition: StateMachine.cs:72
Class representing a sample of a state machine variable over time.
Definition: ImplTypes.g.cs:58
ProfilerThreadType
Type of profiler thread.