Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
LikeWithOptions.cs
1using System.Collections.Generic;
2using System.Text.RegularExpressions;
3using System.Threading.Tasks;
12
14{
19 {
20 private ScriptNode options;
21
31 public LikeWithOptions(ScriptNode Argument1, ScriptNode Argument2, ScriptNode Argument3,
33 : base(Argument1, Argument2, Start, Length, Expression)
34 {
35 this.options = Argument3;
36 this.PartialMatch = true;
37
38 this.CalcIsAsync();
39 }
40
44 protected override void CalcIsAsync()
45 {
46 base.CalcIsAsync();
47
48 if (this.options?.IsAsynchronous ?? false)
49 this.isAsync = true;
50 }
51
58 {
59 if (!(this.options is null))
60 this.SetOptions(this.options.Evaluate(Variables));
61
62 IElement Left = this.left.Evaluate(Variables);
63 IElement Right = this.right.Evaluate(Variables);
64
65 return this.EvaluateScalar(Left, Right, Variables);
66 }
67
73 public override async Task<IElement> EvaluateAsync(Variables Variables)
74 {
75 if (!(this.options is null))
76 this.SetOptions(await this.options.EvaluateAsync(Variables));
77
78 IElement Left = await this.left.EvaluateAsync(Variables);
79 IElement Right = await this.right.EvaluateAsync(Variables);
80
81 return await this.EvaluateScalarAsync(Left, Right, Variables);
82 }
83
92 public Task<IElement> EvaluateAsync(Variables Variables, ISemanticCube Cube,
94 {
95 return this.EvaluateAsync(Variables);
96 }
97
101 public ScriptNode ScriptNode => this;
102
103 private void SetOptions(IElement Options)
104 {
105 if (!(Options.AssociatedObjectValue is string s))
106 throw new ScriptRuntimeException("Options argument to regex function must be a string.", this);
107
108 this.Options = Replace.GetOptions(s, this);
109 }
110
118 public override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
119 {
120 if (Order == SearchMethod.DepthFirst)
121 {
122 if (!(this.left?.ForAllChildNodes(Callback, State, Order) ?? true))
123 return false;
124
125 if (!(this.right?.ForAllChildNodes(Callback, State, Order) ?? true))
126 return false;
127
128 if (!(this.options?.ForAllChildNodes(Callback, State, Order) ?? true))
129 return false;
130 }
131
132 ScriptNode NewNode;
133 bool RecalcIsAsync = false;
134 bool b;
135
136 if (!(this.left is null))
137 {
138 b = !Callback(this.left, out NewNode, State);
139 if (!(NewNode is null))
140 {
141 this.left = NewNode;
142 this.left.SetParent(this);
143
144 RecalcIsAsync = true;
145 }
146
147 if (b || Order == SearchMethod.TreeOrder && !this.left.ForAllChildNodes(Callback, State, Order))
148 {
149 if (RecalcIsAsync)
150 this.CalcIsAsync();
151
152 return false;
153 }
154 }
155
156 if (!(this.right is null))
157 {
158 b = !Callback(this.right, out NewNode, State);
159 if (!(NewNode is null))
160 {
161 this.right = NewNode;
162 this.right.SetParent(this);
163
164 RecalcIsAsync = true;
165 }
166
167 if (b || Order == SearchMethod.TreeOrder && !this.right.ForAllChildNodes(Callback, State, Order))
168 {
169 if (RecalcIsAsync)
170 this.CalcIsAsync();
171
172 return false;
173 }
174 }
175
176 if (!(this.options is null))
177 {
178 b = !Callback(this.options, out NewNode, State);
179 if (!(NewNode is null))
180 {
181 this.options = NewNode;
182 this.options.SetParent(this);
183
184 RecalcIsAsync = true;
185 }
186
187 if (b || Order == SearchMethod.TreeOrder && !this.options.ForAllChildNodes(Callback, State, Order))
188 {
189 if (RecalcIsAsync)
190 this.CalcIsAsync();
191
192 return false;
193 }
194 }
195
196 if (RecalcIsAsync)
197 this.CalcIsAsync();
198
199 if (Order == SearchMethod.BreadthFirst)
200 {
201 if (!(this.left?.ForAllChildNodes(Callback, State, Order) ?? true))
202 return false;
203
204 if (!(this.right?.ForAllChildNodes(Callback, State, Order) ?? true))
205 return false;
206
207 if (!(this.options?.ForAllChildNodes(Callback, State, Order) ?? true))
208 return false;
209 }
210
211 return true;
212 }
213
215 public override bool Equals(object obj)
216 {
217 return obj is LikeWithOptions O &&
218 AreEqual(this.left, O.left) &&
219 AreEqual(this.right, O.right) &&
220 AreEqual(this.options, O.options) &&
221 base.Equals(obj);
222 }
223
225 public override int GetHashCode()
226 {
227 int Result = base.GetHashCode();
228 Result ^= Result << 5 ^ GetHashCode(this.left);
229 Result ^= Result << 5 ^ GetHashCode(this.right);
230 Result ^= Result << 5 ^ GetHashCode(this.options);
231 return Result;
232 }
233 }
234}
Class managing a script expression.
Definition: Expression.cs:39
Replace(String,From,To)
Definition: Replace.cs:13
static RegexOptions GetOptions(string Options, ScriptNode Node)
Converts a string-representation of regex options into an enumeration value.
Definition: Replace.cs:115
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
ScriptNode right
Right operand.
ScriptNode left
Left operand.
virtual Task< IElement > EvaluateScalarAsync(IElement Left, IElement Right, Variables Variables)
Evaluates the operator on scalar operands.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
static bool AreEqual(ScriptNode S1, ScriptNode S2)
Compares if two script nodes are equal.
Definition: ScriptNode.cs:275
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
void SetParent(ScriptNode Parent)
Sets the parent node. Can only be used when expression is being parsed.
Definition: ScriptNode.cs:132
abstract IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection. This method should be ...
virtual Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection. This method should be ...
Definition: ScriptNode.cs:158
RegexOptions Options
Options for regular expression. Default is RegexOptions.Singleline.
Definition: Like.cs:145
override IElement EvaluateScalar(IElement Left, IElement Right, Variables Variables)
Evaluates the operator on scalar operands.
Definition: Like.cs:47
Extension of the Like operator, that allows the script to set options.
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Task< IElement > EvaluateAsync(Variables Variables, ISemanticCube Cube, SparqlQuery Query, Possibility Possibility)
Evaluates the node, using the variables provided in the Variables collection.
override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
LikeWithOptions(ScriptNode Argument1, ScriptNode Argument2, ScriptNode Argument3, int Start, int Length, Expression Expression)
Extension of the Like operator, that allows the script to set options.
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
override void CalcIsAsync()
Recalculates if operator is asynchronous or not.
Represents a possible solution during SPARQL evaluation.
Definition: Possibility.cs:13
Collection of variables.
Definition: Variables.cs:25
Interface for semantic cubes.
Basic interface for all types of elements.
Definition: IElement.cs:20
delegate bool ScriptNodeEventHandler(ScriptNode Node, out ScriptNode NewNode, object State)
Delegate for ScriptNode callback methods.
SearchMethod
Method to traverse the expression structure
Definition: ScriptNode.cs:38