Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ReportAttribute.cs
1using System.Text;
2using System.Threading.Tasks;
3using System.Xml;
5using Waher.Script;
7
9{
14 public abstract class ReportAttribute<T>
15 {
16 private readonly string definition;
17 private T constant;
18 private Expression expression;
19 private readonly bool isExpression;
20 private bool first = true;
21
27 public ReportAttribute(XmlElement Xml, string AttributeName)
28 {
29 if (string.IsNullOrEmpty(AttributeName))
30 this.definition = Xml.InnerText;
31 else
32 this.definition = XML.Attribute(Xml, AttributeName);
33
34 this.isExpression = this.definition.StartsWith('{') && this.definition.EndsWith('}');
35 }
36
40 public string Definition => this.definition;
41
45 public bool IsConstant => !this.isExpression;
46
50 public bool IsExpression => this.isExpression;
51
55 public bool IsEmpty => string.IsNullOrEmpty(this.definition);
56
62 public abstract T ParseValue(string s);
63
71 public Task<T> Evaluate(Variables Variables, T DefaultValue)
72 {
73 if (this.IsEmpty)
74 return Task.FromResult(DefaultValue);
75 else
76 return this.Evaluate(Variables);
77 }
78
84 public async Task<T> Evaluate(Variables Variables)
85 {
86 if (this.first)
87 {
88 if (this.isExpression)
89 this.expression = new Expression(this.definition[1..^1]);
90 else
91 this.constant = this.ParseValue(this.definition);
92
93 this.first = false;
94 }
95
96 if (this.isExpression)
97 {
98 object Result = await this.expression.EvaluateAsync(Variables);
99
100 if (Result is T TypedResult)
101 return TypedResult;
102 else if (Result is null)
103 return default;
104 else
105 {
106 StringBuilder sb = new StringBuilder();
107
108 sb.Append("Expected a value of type ");
109 sb.Append(typeof(T).FullName);
110 sb.Append(" but got a value of type ");
111 sb.Append(Result.GetType().FullName ?? "null");
112 sb.Append('.');
113
114 throw new ScriptRuntimeException(sb.ToString(), this.expression.Root);
115 }
116 }
117 else
118 return this.constant;
119 }
120 }
121}
Helps with common XML-related tasks.
Definition: XML.cs:21
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:1062
Abstract base class for report attributes.
bool IsConstant
If the attribute value is a constant.
bool IsEmpty
If the definition is empty.
Task< T > Evaluate(Variables Variables, T DefaultValue)
Evaluates the attribute, if defined, or returns the DefaultValue if attribute is not defined.
abstract T ParseValue(string s)
Parses a string representation of a value.
async Task< T > Evaluate(Variables Variables)
Evaluates the attribute
ReportAttribute(XmlElement Xml, string AttributeName)
Abstract base class for report attributes.
bool IsExpression
If the attribute value is an expression.
Class managing a script expression.
Definition: Expression.cs:41
async Task< object > EvaluateAsync(Variables Variables)
Evaluates the expression, using the variables provided in the Variables collection....
Definition: Expression.cs:4461
Collection of variables.
Definition: Variables.cs:25