Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GetCounters.cs
1using System;
3using System.Threading.Tasks;
9
11{
18 {
27 : base(Array.Empty<ScriptNode>(), argumentTypes0,
29 {
30 }
31
41 : base(new ScriptNode[] { KeyPrefix }, argumentTypes1Scalar,
43 {
44 }
45
56 public GetCounters(ScriptNode KeyPrefix, ScriptNode Delimiter,
58 : base(new ScriptNode[] { KeyPrefix, Delimiter }, argumentTypes2Scalar,
60 {
61 }
62
74 public GetCounters(ScriptNode KeyPrefix, ScriptNode Delimiter, ScriptNode Wildcard,
76 : base(new ScriptNode[] { KeyPrefix, Delimiter, Wildcard }, argumentTypes3Scalar,
78 {
79 }
80
84 public override string FunctionName => nameof(GetCounters);
85
89 public override string[] DefaultArgumentNames => new string[] { "KeyPrefix", "Delimiter", "Wildcard" };
90
95 public override bool IsAsynchronous => true;
96
104 {
105 return this.EvaluateAsync(Arguments, Variables).Result;
106 }
107
114 public override async Task<IElement> EvaluateAsync(IElement[] Arguments, Variables Variables)
115 {
116 string KeyPrefix = Arguments.Length > 0 ? ToString(Arguments[0]) : null;
117 string Delimiter = Arguments.Length > 1 ? ToString(Arguments[1]) : null;
118 string Wildcard = Arguments.Length > 2 ? ToString(Arguments[2]) : null;
119 Dictionary<string, long> Counters;
120
121 if (string.IsNullOrEmpty(KeyPrefix))
122 Counters = await RuntimeCounters.GetWhereAsync();
123 else
124 {
125 if (string.IsNullOrEmpty(Wildcard))
126 {
127 Wildcard = "*";
128 while (KeyPrefix.Contains(Wildcard))
129 Wildcard += "*";
130
131 KeyPrefix += Wildcard;
132 }
133
134 Counters = await RuntimeCounters.GetWhereKeyLikeAsync(KeyPrefix, Wildcard);
135 }
136
137 Dictionary<string, IElement> Result = new Dictionary<string, IElement>();
138
139 if (string.IsNullOrEmpty(Delimiter))
140 {
141 foreach (KeyValuePair<string, long> P in Counters)
142 Result[P.Key] = new DoubleNumber(P.Value);
143 }
144 else
145 {
146 string[] Delimiters = new string[] { Delimiter };
147
148 foreach (KeyValuePair<string, long> P in Counters)
149 {
150 Dictionary<string, IElement> Next, Loop = Result;
151 string Key = P.Key;
152 string[] Parts = Key.Split(Delimiters, StringSplitOptions.None);
153 int i, c = Parts.Length;
154
155 for (i = 0; i < c; i++)
156 {
157 Key = Parts[i];
158
159 if (Loop.TryGetValue(Key, out IElement E))
160 {
161 if (i < c - 1)
162 {
163 if (E.AssociatedObjectValue is Dictionary<string, IElement> Next2)
164 Next = Next2;
165 else
166 {
167 Next = new Dictionary<string, IElement>()
168 {
169 { string.Empty, E }
170 };
171
172 Loop[Key] = new ObjectValue(Next);
173 }
174 }
175 else
176 {
177 E = Add.EvaluateAddition(E, new DoubleNumber(P.Value), this);
178 Loop[Key] = E;
179 Next = null;
180 }
181 }
182 else
183 {
184 if (i < c - 1)
185 {
186 Next = new Dictionary<string, IElement>();
187 E = new ObjectValue(Next);
188 }
189 else
190 {
191 E = new DoubleNumber(P.Value);
192 Next = null;
193 }
194
195 Loop[Key] = E;
196 }
197
198 Loop = Next;
199 }
200 }
201 }
202
203 return new ObjectValue(Result);
204 }
205 }
206}
Static class managing persistent counters.
static Task< Dictionary< string, long > > GetWhereKeyLikeAsync(string Key, string Wildcard)
Gets available counters, matching a search filter.
static async Task< Dictionary< string, long > > GetWhereAsync()
Gets available counters.
Class managing a script expression.
Definition: Expression.cs:41
Base class for multivariate funcions.
ScriptNode[] Arguments
Function arguments.
static readonly ArgumentType[] argumentTypes2Scalar
Two scalar parameters.
static readonly ArgumentType[] argumentTypes0
Zero parameters.
static readonly ArgumentType[] argumentTypes3Scalar
Three scalar parameters.
static readonly ArgumentType[] argumentTypes1Scalar
One scalar parameter.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
override string ToString()
Definition: ScriptNode.cs:359
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
static IElement EvaluateAddition(IElement Left, IElement Right, ScriptNode Node)
Adds two operands.
Definition: Add.cs:65
Gets a set of counters and returns an object with keys as properties and counter values the values....
Definition: GetCounters.cs:18
GetCounters(int Start, int Length, Expression Expression)
Gets a set of counters and returns an object with keys as properties and counter values the values.
Definition: GetCounters.cs:26
override string FunctionName
Name of the function
Definition: GetCounters.cs:84
GetCounters(ScriptNode KeyPrefix, ScriptNode Delimiter, ScriptNode Wildcard, int Start, int Length, Expression Expression)
Gets a set of counters and returns an object with keys as properties and counter values the values....
Definition: GetCounters.cs:74
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: GetCounters.cs:95
GetCounters(ScriptNode KeyPrefix, int Start, int Length, Expression Expression)
Gets a set of counters and returns an object with keys as properties and counter values the values.
Definition: GetCounters.cs:40
override string[] DefaultArgumentNames
Default Argument names
Definition: GetCounters.cs:89
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: GetCounters.cs:103
GetCounters(ScriptNode KeyPrefix, ScriptNode Delimiter, int Start, int Length, Expression Expression)
Gets a set of counters and returns an object with keys as properties and counter values the values....
Definition: GetCounters.cs:56
override async Task< IElement > EvaluateAsync(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: GetCounters.cs:114
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:21