Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ConditionalEnumerator.cs
1using System;
3using System.Threading.Tasks;
7
9{
14 {
15 private readonly ScriptNode conditions;
16 private readonly Variables variables;
17 private readonly IResultSetEnumerator e;
18 private ObjectProperties properties = null;
19
27 {
28 this.e = ItemEnumerator;
29 this.variables = Variables;
30 this.conditions = Conditions;
31 }
32
36 public object Current => this.e.Current;
37
41 public void Dispose()
42 {
43 this.e.Dispose();
44 }
45
49 public bool MoveNext()
50 {
51 while (this.e.MoveNext())
52 {
53 if (this.MatchesCondition())
54 return true;
55 }
56
57 return false;
58 }
59
63 public async Task<bool> MoveNextAsync()
64 {
65 while (await this.e.MoveNextAsync())
66 {
67 if (await this.MatchesConditionAsync())
68 return true;
69 }
70
71 return false;
72 }
73
74 private bool MatchesCondition()
75 {
76 try
77 {
78 if (this.properties is null)
79 this.properties = new ObjectProperties(this.e.Current, this.variables);
80 else
81 this.properties.Object = this.e.Current;
82
83 IElement E = this.conditions.Evaluate(this.properties); // TODO: Async
84 if (!(E.AssociatedObjectValue is bool B) || !B)
85 return false;
86
87 return true;
88 }
89 catch (Exception)
90 {
91 return false;
92 }
93 }
94
95 private async Task<bool> MatchesConditionAsync()
96 {
97 try
98 {
99 if (this.properties is null)
100 this.properties = new ObjectProperties(this.e.Current, this.variables);
101 else
102 this.properties.Object = this.e.Current;
103
104 IElement E = await this.conditions.EvaluateAsync(this.properties);
105 if (!(E.AssociatedObjectValue is bool B) || !B)
106 return false;
107
108 return true;
109 }
110 catch (Exception)
111 {
112 return false;
113 }
114 }
115
119 public void Reset()
120 {
121 this.e.Reset();
122 }
123 }
124}
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
Enumerator that only returns elements matching a set of conditions.
async Task< bool > MoveNextAsync()
IAsyncEnumerator.MoveNextAsync
ConditionalEnumerator(IResultSetEnumerator ItemEnumerator, Variables Variables, ScriptNode Conditions)
Enumerator that only returns elements matching a set of conditions.
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:21
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:34