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;
10
12{
16 public abstract class ActionReference : StateMachineNode
17 {
18 private ScriptableStringAttribute actionRef;
19 private ExpressionAttribute beforeActionScript;
20
25 : base()
26 {
27 }
28
32 [DefaultValueNull]
34 {
35 get => this.actionRef?.Definition;
36 set => this.actionRef = new ScriptableStringAttribute(value, this);
37 }
38
42 [DefaultValueNull]
43 public string BeforeActionScript
44 {
45 get => this.beforeActionScript?.Expression;
46 set => this.beforeActionScript = new ExpressionAttribute(value, false, this);
47 }
48
52 public bool HasActionReference => !(this.actionRef?.IsEmpty ?? true);
53
57 public bool HasConstantActionReference => this.actionRef?.IsConstant ?? false;
58
62 public bool HasExpressionActionReference => this.actionRef?.IsExpression ?? false;
63
68 public override Task Parse(XmlElement Xml)
69 {
70 this.actionRef = new ScriptableStringAttribute(XML.Attribute(Xml, "actionRef"), this);
71 this.beforeActionScript = new ExpressionAttribute(XML.Attribute(Xml, "beforeActionScript"), false, this);
72
73 return base.Parse(Xml);
74 }
75
81 public override void CheckReferences(StateMachine Machine, Token Token)
82 {
83 if (this.actionRef.IsConstant && !this.actionRef.IsEmpty && !Machine.TryGetAction(this.actionRef.Definition, out _))
84 throw new Exception("Action not found: " + this.actionRef.Definition);
85
86 base.CheckReferences(Machine, Token);
87 }
88
94 public Task<string> GetReference(EvaluationArguments Arguments)
95 {
96 return this.actionRef?.Evaluate(Arguments.Variables) ?? Task.FromResult<string>(null);
97 }
98
104 public async Task<TimeSpan> ExecuteLog(EvaluationArguments Arguments)
105 {
106 string ActionId = await this.GetReference(Arguments);
107 if (string.IsNullOrEmpty(ActionId))
108 return TimeSpan.Zero;
109
110 DateTime Start = DateTime.UtcNow;
111 await this.ExecuteNoLog(ActionId, Arguments);
112 TimeSpan Elapsed = DateTime.UtcNow.Subtract(Start);
113
115 {
116 Variable = "<" + ActionId + ">",
117 Timestamp = Start,
118 Value = Elapsed,
119 StateMachineId = Arguments.Machine.StateMachineId,
120 Expires = Arguments.Machine.Expires,
121 ArchiveOptional = Arguments.Machine.ArchiveOptional,
122 ArchiveRequired = Arguments.Machine.ArchiveRequired
123 });
124
125 return Elapsed;
126 }
127
132 public async Task ExecuteNoLog(EvaluationArguments Arguments)
133 {
134 string ActionId = await this.GetReference(Arguments);
135 await this.ExecuteNoLog(ActionId, Arguments);
136 }
137
143 public async Task ExecuteNoLog(string ActionId, EvaluationArguments Arguments)
144 {
145 if (!string.IsNullOrEmpty(ActionId))
146 {
147 if (!Arguments.Machine.TryGetAction(ActionId, out Action Action))
148 throw new Exception("Action not found: " + ActionId);
149
150 ProfilerThread Thread = Arguments.Profiler?.GetThread(ActionId, ProfilerThreadType.Binary);
151 Thread?.High();
152
153 try
154 {
155 if (this.beforeActionScript?.HasExpreession ?? false)
156 await this.beforeActionScript.Evaluate(Arguments.Variables);
157
158 await Action.Execute(Arguments);
159 }
160 finally
161 {
162 Thread?.Low();
163 }
164 }
165 }
166 }
167}
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
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:19
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
Definition: Database.cs:95
ProfilerThread GetThread(string Name, ProfilerThreadType Type)
Gets a profiler thread. If none is available, a new is created.
Definition: Profiler.cs:144
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< TimeSpan > ExecuteLog(EvaluationArguments Arguments)
Evaluates an action, and logs the time the action took as a sample value.
override Task Parse(XmlElement Xml)
Parses the State-machine node.
async Task ExecuteNoLog(string ActionId, EvaluationArguments Arguments)
Evaluates an action, without logging the time the action took as a sample value.
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(EvaluationArguments Arguments)
Evaluates an action, without logging 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:13
Class representing a state machine.
Definition: StateMachine.cs:37
bool TryGetAction(string Id, out Model.Actions.Action Action)
Tries to get an action.
CaseInsensitiveString StateMachineId
ID of State Machine.
Definition: StateMachine.cs:51
Duration? ArchiveOptional
Duration after which token expires, and the required archiving time, the token can optionally be arch...
Definition: StateMachine.cs:98
DateTime Expires
When state-machine expires
Definition: StateMachine.cs:66
Class representing a sample of a state machine variable over time.
ProfilerThreadType
Type of profiler thread.