Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CurrentState.cs
1using System;
2using System.Collections.Generic;
3using Waher.Content;
7using Waher.Script;
9
11{
15 [CollectionName("StateMachineCurrentStates")]
16 [TypeName(TypeNameSerialization.None)]
17 [ArchivingTime(nameof(ArchiveDays))]
18 [Index("StateMachineId")]
19 public class CurrentState
20 {
21 private readonly object synchObj = new object();
22 private Dictionary<string, CurrentStateVariable> variablesByName;
23 private Dictionary<CaseInsensitiveString, bool> sourcesByName;
24
28 public CurrentState()
29 {
30 }
31
35 [ObjectId]
36 public string ObjectId { get; set; }
37
41 public string StateMachineId { get; set; }
42
47 public string State { get; set; }
48
52 public bool HasEnded => string.IsNullOrEmpty(this.State);
53
57 public bool IsRunning => !string.IsNullOrEmpty(this.State);
58
62 public CurrentStateVariable[] VariableValues { get; set; }
63
67 public CaseInsensitiveString[] Sources { get; set; }
68
72 public DateTime Expires { get; set; }
73
77 [DefaultValueNull]
78 public Duration? ArchiveRequired { get; set; }
79
83 [DefaultValueNull]
84 public Duration? ArchiveOptional { get; set; }
85
90
91 #region Variables
92
98 public void SetVariable(string Name, object Value)
99 {
100 lock (this.synchObj)
101 {
102 if (this.VariableValues is null)
103 {
104 this.VariableValues = new CurrentStateVariable[]
105 {
106 new CurrentStateVariable(Name, Value, null)
107 };
108
109 this.variablesByName = null;
110 }
111 else
112 {
113 if (this.variablesByName is null)
114 this.SortVariablesLocked();
115
116 if (this.variablesByName.TryGetValue(Name, out CurrentStateVariable V))
117 {
118 V.Value = Value;
119 return;
120 }
121
122 int c = this.VariableValues.Length;
123
124 CurrentStateVariable[] Updated = new CurrentStateVariable[c + 1];
125 Array.Copy(this.VariableValues, 0, Updated, 0, c);
126 Updated[c] = V = new CurrentStateVariable(Name, Value, null);
127
128 this.VariableValues = Updated;
129 this.variablesByName[Name] = V;
130 }
131 }
132 }
133
140 {
141 lock (this.synchObj)
142 {
144 Variables.ContextVariables = Machine;
145 Variables["CurrentState"] = this;
146
147 if (!(this.VariableValues is null))
148 {
150 Variables[Variable.Name] = Variable.Value;
151 }
152
153 return Variables;
154 }
155 }
156
163 public bool TryGetVariable(string Name, out CurrentStateVariable Variable)
164 {
165 lock (this.synchObj)
166 {
167 if (this.variablesByName is null)
168 this.SortVariablesLocked();
169
170 return this.variablesByName.TryGetValue(Name, out Variable);
171 }
172 }
173
174 private void SortVariablesLocked()
175 {
176 Dictionary<string, CurrentStateVariable> Sorted = new Dictionary<string, CurrentStateVariable>();
177
178 if (!(this.VariableValues is null))
179 {
180 foreach (CurrentStateVariable Variable2 in this.VariableValues)
181 Sorted[Variable2.Name] = Variable2;
182 }
183
184 this.variablesByName = Sorted;
185 }
186
187 #endregion
188
189 #region Sources
190
197 {
198 lock (this.synchObj)
199 {
200 if (this.Sources is null)
201 {
202 this.Sources = new CaseInsensitiveString[] { Name };
203 this.sourcesByName = null;
204
205 return true;
206 }
207 else
208 {
209 if (this.sourcesByName is null)
210 this.SortSourcesLocked();
211
212 if (this.sourcesByName.ContainsKey(Name))
213 return false;
214
215 int c = this.Sources.Length;
216
217 CaseInsensitiveString[] Updated = new CaseInsensitiveString[c + 1];
218 Array.Copy(this.Sources, 0, Updated, 0, c);
219 Updated[c] = Name;
220
221 this.Sources = Updated;
222 this.sourcesByName[Name] = true;
223
224 return true;
225 }
226 }
227 }
228
235 {
236 lock (this.synchObj)
237 {
238 if (this.Sources is null)
239 return false;
240
241 if (!(this.sourcesByName is null) && !this.sourcesByName.Remove(Name))
242 return false;
243
244 int i = Array.IndexOf(this.Sources, Name);
245 if (i < 0)
246 return false;
247
248 int c = this.Sources.Length;
249
250 CaseInsensitiveString[] Updated = new CaseInsensitiveString[c - 1];
251
252 if (i > 0)
253 Array.Copy(this.Sources, 0, Updated, 0, i);
254
255 if (i < c - 1)
256 Array.Copy(this.Sources, i + 1, Updated, i, c - i - 1);
257
258 this.Sources = Updated;
259
260 return true;
261 }
262 }
263
270 {
271 lock (this.synchObj)
272 {
273 if (this.sourcesByName is null)
274 this.SortSourcesLocked();
275
276 return this.sourcesByName.ContainsKey(Name);
277 }
278 }
279
280 private void SortSourcesLocked()
281 {
282 Dictionary<CaseInsensitiveString, bool> Sorted = new Dictionary<CaseInsensitiveString, bool>();
283
284 if (!(this.Sources is null))
285 {
286 foreach (CaseInsensitiveString Source2 in this.Sources)
287 Sorted[Source2] = true;
288 }
289
290 this.sourcesByName = Sorted;
291 }
292
293 #endregion
294
295 }
296}
Implements an HTTP server.
Definition: HttpServer.cs:36
static Variables CreateVariables()
Creates a new collection of variables, that contains access to the global set of variables.
Definition: HttpServer.cs:1604
Represents a case-insensitive string.
int Length
Gets the number of characters in the current CaseInsensitiveString object.
Contains information about a variable.
Definition: Variable.cs:10
string Name
Name of variable.
Definition: Variable.cs:78
Collection of variables.
Definition: Variables.cs:25
static int CalcArchiveDays(DateTime Expires, Duration? ArchiveReq, Duration? ArchiveOpt)
Calculates the number of days an object should be archived in the ledger.
Definition: Token.cs:582
Class representing the current state of a state machine.
Definition: CurrentState.cs:20
bool ContainsSource(CaseInsensitiveString Name)
Checks if a source exists.
string ObjectId
Object ID of current state.
Definition: CurrentState.cs:36
bool IsRunning
If state-machine is running.
Definition: CurrentState.cs:57
CurrentStateVariable[] VariableValues
Current variable values.
Definition: CurrentState.cs:62
Duration? ArchiveRequired
Duration after which token expires, the token is required to be archived.
Definition: CurrentState.cs:78
Duration? ArchiveOptional
Duration after which token expires, and the required archiving time, the token can optionally be arch...
Definition: CurrentState.cs:84
CaseInsensitiveString[] Sources
Approved sources.
Definition: CurrentState.cs:67
int ArchiveDays
Number of days to archive field.
Definition: CurrentState.cs:89
bool HasEnded
If state-machine has ended.
Definition: CurrentState.cs:52
DateTime Expires
When state-machine expires
Definition: CurrentState.cs:72
bool TryGetVariable(string Name, out CurrentStateVariable Variable)
Tries to get a state variable, given its name.
void SetVariable(string Name, object Value)
Sets a variable
Definition: CurrentState.cs:98
Variables GetVariables(StateMachine Machine)
Gets a new variable collection containing the current state variables.
bool AddSource(CaseInsensitiveString Name)
Adds a source
string State
ID of current state in state-machine. Empty State = State-machine has ended.
Definition: CurrentState.cs:47
bool RemoveSource(CaseInsensitiveString Name)
Removes a source
CurrentState()
Class representing the current state of a state machine.
Definition: CurrentState.cs:28
Class representing a persisted state-machine variable value.
Class representing a state machine.
Definition: StateMachine.cs:37
TypeNameSerialization
How the type name should be serialized.
Represents a duration value, as defined by the xsd:duration data type: http://www....
Definition: Duration.cs:13