Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
DecCounter.cs
1using System.Threading.Tasks;
2using Waher.Script;
7
9{
14 {
23 : base(new ScriptNode[] { Key }, argumentTypes1Scalar, Start, Length, Expression)
24 {
25 }
26
36 : base(new ScriptNode[] { Key, Amount }, argumentTypes2Scalar, Start, Length, Expression)
37 {
38 }
39
43 public override string FunctionName => nameof(DecCounter);
44
48 public override string[] DefaultArgumentNames => new string[] { "Key", "Amount" };
49
54 public override bool IsAsynchronous => true;
55
63 {
64 return this.EvaluateAsync(Arguments, Variables).Result;
65 }
66
73 public override async Task<IElement> EvaluateAsync(IElement[] Arguments, Variables Variables)
74 {
75 long Count;
76
77 if (!(Arguments[0].AssociatedObjectValue is string Key))
78 throw new ScriptRuntimeException("Key must be a string.", this);
79
80 if (Arguments.Length == 2)
81 {
82 double N = Expression.ToDouble(Arguments[1].AssociatedObjectValue);
83 long Amount = (long)N;
84
85 if (Amount != N)
86 throw new ScriptRuntimeException("Amounts must be integers.", this);
87
88 Count = await Runtime.Counters.RuntimeCounters.DecrementCounter(Key, Amount);
89 }
90 else
91 {
92 Count = await Runtime.Counters.RuntimeCounters.DecrementCounter(Key);
93 }
94
95 return new DoubleNumber(Count);
96 }
97 }
98}
override async Task< IElement > EvaluateAsync(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: DecCounter.cs:73
override string FunctionName
Name of the function
Definition: DecCounter.cs:43
DecCounter(ScriptNode Key, int Start, int Length, Expression Expression)
Decrements a counter
Definition: DecCounter.cs:22
override IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the function.
Definition: DecCounter.cs:62
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: DecCounter.cs:54
override string[] DefaultArgumentNames
Default Argument names
Definition: DecCounter.cs:48
DecCounter(ScriptNode Key, ScriptNode Amount, int Start, int Length, Expression Expression)
Decrements a counter
Definition: DecCounter.cs:35
Class managing a script expression.
Definition: Expression.cs:39
static double ToDouble(object Object)
Converts an object to a double value.
Definition: Expression.cs:4824
Base class for multivariate funcions.
ScriptNode[] Arguments
Function arguments.
static readonly ArgumentType[] argumentTypes2Scalar
Two 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
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
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20