Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Like.cs
1using System.Collections.Generic;
2using System.Text.RegularExpressions;
7
9{
15 public delegate string ExpressionTransform(string Expression);
16
21 {
31 : base(Left, Right, Start, Length, Expression)
32 {
33 }
34
39
48 {
49 if (!(Left.AssociatedObjectValue is string sl))
50 throw new ScriptRuntimeException("String values expected.", this);
51
52 if (!(Right.AssociatedObjectValue is string sr))
53 throw new ScriptRuntimeException("String values expected.", this);
54
55 Match M;
56
58 if (!(h is null))
59 sr = h(sr);
60
61 M = this.Matches(sl, sr, out string[] GroupNames);
62
63 if (M.Success)
64 {
65 if (!(GroupNames is null))
66 {
67 foreach (string GroupName in GroupNames)
68 {
69 Group G = M.Groups[GroupName];
70 if (G.Success)
71 {
72 string Value = G.Value;
73
74 if (Expression.TryParse(Value, out double d))
75 Variables[GroupName] = d;
76 else
77 Variables[GroupName] = Value;
78
79 Variables[GroupName + "_STR"] = Value;
80 Variables[GroupName + "_POS"] = G.Index;
81 Variables[GroupName + "_LEN"] = G.Length;
82 }
83 }
84 }
85
86 if (this.partialMatch || (M.Index == 0 && M.Length == sl.Length))
87 return BooleanValue.True;
88 }
89
90 return BooleanValue.False;
91 }
92
100 protected Match Matches(string Value, string Expression, out string[] GroupNames)
101 {
102 lock (this.synchObject)
103 {
104 if (this.lastExpression is null || Expression != this.lastExpression)
105 {
106 this.lastExpression = Expression;
107 this.regex = new Regex(Expression, this.options);
108
109 List<string> Names = null;
110
111 foreach (string s in this.regex.GetGroupNames())
112 {
113 if (!int.TryParse(s, out int _))
114 {
115 if (Names is null)
116 Names = new List<string>();
117
118 Names.Add(s);
119 }
120 }
121
122 if (Names is null)
123 this.groupNames = null;
124 else
125 this.groupNames = Names.ToArray();
126 }
127
128 GroupNames = this.groupNames;
129
130 return this.regex.Match(Value);
131 }
132 }
133
134 private Regex regex = null;
135 private string[] groupNames = null;
136 private string lastExpression = null;
137 private RegexOptions options = RegexOptions.Singleline;
138 private bool partialMatch = false;
139 private readonly object synchObject = new object();
140
144 public RegexOptions Options
145 {
146 get => this.options;
147 set
148 {
149 lock (this.synchObject)
150 {
151 this.options = value;
152 this.lastExpression = null;
153 }
154 }
155 }
156
160 public bool PartialMatch
161 {
162 get => this.partialMatch;
163 set => this.partialMatch = value;
164 }
165
172 public override PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary<string, IElement> AlreadyFound)
173 {
174 if (!(CheckAgainst.AssociatedObjectValue is string sl))
175 return PatternMatchResult.NoMatch;
176
177 if (this.right is ConstantElement RightConstant &&
178 RightConstant.Constant.AssociatedObjectValue is string sr)
179 {
181 if (!(h is null))
182 sr = h(sr);
183
184 Match M = this.Matches(sl, sr, out string[] GroupNames);
185
186 if (M.Success)
187 {
188 if (!(GroupNames is null))
189 {
190 foreach (string GroupName in GroupNames)
191 {
192 Group G = M.Groups[GroupName];
193 if (G.Success)
194 {
195 string Value = G.Value;
196 object ObjValue;
197
198 if (Expression.TryParse(Value, out double d))
199 ObjValue = d;
200 else
201 ObjValue = Value;
202
203 if (AlreadyFound.TryGetValue(GroupName, out IElement E) &&
204 !E.AssociatedObjectValue.Equals(ObjValue))
205 {
206 return PatternMatchResult.NoMatch;
207 }
208
209 AlreadyFound[GroupName] = Expression.Encapsulate(ObjValue);
210 }
211 }
212 }
213
214 return this.left.PatternMatch(CheckAgainst, AlreadyFound);
215 }
216 }
217
218 return PatternMatchResult.NoMatch;
219 }
220
221 }
222}
Class managing a script expression.
Definition: Expression.cs:39
static IElement Encapsulate(object Value)
Encapsulates an object.
Definition: Expression.cs:4955
static bool TryParse(string s, out double Value)
Tries to parse a double-precision floating-point value.
Definition: Expression.cs:4517
ScriptNode right
Right operand.
ScriptNode left
Left operand.
Base class for binary scalar operators.
Represents a constant element value.
IElement Constant
Constant value.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
virtual PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary< string, IElement > AlreadyFound)
Performs a pattern match operation.
Definition: ScriptNode.cs:169
Expression Expression
Expression of which the node is a part.
Definition: ScriptNode.cs:177
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Boolean-valued number.
Definition: BooleanValue.cs:12
static readonly BooleanValue False
Constant false value.
static readonly BooleanValue True
Constant true value.
RegexOptions Options
Options for regular expression. Default is RegexOptions.Singleline.
Definition: Like.cs:145
ExpressionTransform TransformExpression
Event raised before performing comparison. Can be used to transform expression.
Definition: Like.cs:38
bool PartialMatch
If a partial match is sufficient for operator to return true. (Default=false)
Definition: Like.cs:161
override PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary< string, IElement > AlreadyFound)
Performs a pattern match operation.
Definition: Like.cs:172
Match Matches(string Value, string Expression, out string[] GroupNames)
Checks a value against a regular expression.
Definition: Like.cs:100
override IElement EvaluateScalar(IElement Left, IElement Right, Variables Variables)
Evaluates the operator on scalar operands.
Definition: Like.cs:47
Like(ScriptNode Left, ScriptNode Right, int Start, int Length, Expression Expression)
Like
Definition: Like.cs:30
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33
PatternMatchResult
Status result of a pattern matching operation.
Definition: ScriptNode.cs:17
delegate string ExpressionTransform(string Expression)
Delegate for expression transforms.