Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ParameterScriptOptions.cs
3using System.Threading.Tasks;
4using System.Xml;
6using Waher.Script;
9
11{
16 {
17 private readonly string script;
18 private readonly Expression parsed;
19
24 public ParameterScriptOptions(XmlElement Xml)
25 : base()
26 {
27 this.script = Xml.InnerText;
28 this.parsed = new Expression(this.script);
29 }
30
36 public async Task<KeyValuePair<string, string>[]> GetOptions(Variables Variables)
37 {
39 object Obj = await this.parsed.EvaluateAsync(Variables);
40 string s;
41
42 if (Obj is IEnumerable<KeyValuePair<string, object>> Options)
43 {
44 foreach (KeyValuePair<string, object> P in Options)
45 Result.Add(new KeyValuePair<string, string>(P.Key, P.Value?.ToString() ?? string.Empty));
46 }
47 else if (Obj is IEnumerable<KeyValuePair<string, IElement>> Options2)
48 {
49 foreach (KeyValuePair<string, IElement> P in Options2)
50 Result.Add(new KeyValuePair<string, string>(P.Key, ScriptNode.ToString(P.Value) ?? string.Empty));
51 }
52 else if (Obj is IEnumerable Enumerable)
53 {
54 IEnumerator e = Enumerable.GetEnumerator();
55
56 while (e.MoveNext())
57 {
58 object Item = e.Current;
59
60 if (Item is KeyValuePair<string, object> P)
61 Result.Add(new KeyValuePair<string, string>(P.Key, P.Value?.ToString() ?? string.Empty));
62 else if (Item is KeyValuePair<string, string> P2)
63 Result.Add(P2);
64 else if (Item is KeyValuePair<string, IElement> P3)
65 Result.Add(new KeyValuePair<string, string>(P3.Key, ScriptNode.ToString(P3.Value) ?? string.Empty));
66 else
67 {
68 s = Item?.ToString() ?? string.Empty;
69 Result.Add(new KeyValuePair<string, string>(s, s));
70 }
71 }
72 }
73 else
74 {
75 s = Obj?.ToString() ?? string.Empty;
76 Result.Add(new KeyValuePair<string, string>(s, s));
77 }
78
79 return Result.ToArray();
80 }
81
82 }
83}
async Task< KeyValuePair< string, string >[]> GetOptions(Variables Variables)
Gets the options for the parameter.
ParameterScriptOptions(XmlElement Xml)
Represents a string-valued option.
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
void Add(T Item)
Adds an item to the collection.
Definition: ChunkedList.cs:272
T[] ToArray()
Returns an array containing all elements of the collection.
Class managing a script expression.
Definition: Expression.cs:41
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
override string ToString()
Definition: ScriptNode.cs:359
Collection of variables.
Definition: Variables.cs:25
Interface for classes implementing parameter options.