Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ScriptableAttribute.cs
1using System;
2using System.Threading.Tasks;
4using Waher.Script;
6
8{
13 public abstract class ScriptableAttribute<T>
14 {
15 private readonly StateMachineNode node;
16 private readonly string definition;
17 private T constant;
18 private Expression expression;
19 private readonly bool isExpression;
20 private bool first = true;
21
28 {
29 this.node = Node;
30 this.definition = Definition;
31 this.isExpression = this.definition.StartsWith("{") && this.definition.EndsWith("}");
32 }
33
37 public string Definition => this.definition;
38
42 public bool IsConstant => !this.isExpression;
43
47 public bool IsExpression => this.isExpression;
48
52 public bool IsEmpty => string.IsNullOrEmpty(this.definition);
53
59 public abstract T ParseValue(string s);
60
66 public async Task<T> Evaluate(Variables Variables)
67 {
68 if (this.first)
69 {
70 if (this.isExpression)
71 {
72 string s = this.definition.Substring(1, this.definition.Length - 2);
73 Expression Exp = new Expression(s);
74 if (!XmppServer.CheckExpressionSafe(Exp, true, true, false, out ScriptNode Prohibited))
75 throw new UnauthorizedAccessException("Expression not permitted: " + Prohibited?.SubExpression);
76
77 this.expression = Exp;
78 }
79 else
80 this.constant = this.ParseValue(this.definition);
81
82 this.first = false;
83 }
84
85 if (this.isExpression)
86 {
88 try
89 {
90 Variables.ContextVariables = this.node.StateMachine;
91
92 return (T)await this.expression.EvaluateAsync(Variables);
93 }
94 finally
95 {
96 Variables.ContextVariables = Bak;
97 }
98 }
99 else
100 return this.constant;
101 }
102 }
103}
static bool CheckExpressionSafe(Expression Expression, out ScriptNode Prohibited)
Checks if an expression is safe to execute (if it comes from an external source).
Definition: XmppServer.cs:6733
Class managing a script expression.
Definition: Expression.cs:39
async Task< object > EvaluateAsync(Variables Variables)
Evaluates the expression, using the variables provided in the Variables collection....
Definition: Expression.cs:4275
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
Collection of variables.
Definition: Variables.cs:25
IContextVariables ContextVariables
Variables available during the current context.
Definition: Variables.cs:228
abstract T ParseValue(string s)
Parses a string representation of a value.
async Task< T > Evaluate(Variables Variables)
Evaluates the attribute
ScriptableAttribute(string Definition, StateMachineNode Node)
Abstract base class for scriptable attributes.
Abstract base class for State-Machine nodes.
Variables available in a specific context.