Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ValuesPattern.cs
1using System.Collections.Generic;
2using System.Threading.Tasks;
5
7{
12 {
13 private readonly string[] variables;
14 private readonly ISemanticElement[][] records;
15
21 public ValuesPattern(string Variable, ISemanticElement[] Values)
22 : this(new string[] { Variable }, ToRecords(Values))
23 {
24 }
25
26 private static ISemanticElement[][] ToRecords(ISemanticElement[] Values)
27 {
28 List<ISemanticElement[]> Records = new List<ISemanticElement[]>();
29
30 foreach (ISemanticElement Element in Values)
31 Records.Add(new ISemanticElement[] { Element });
32
33 return Records.ToArray();
34 }
35
41 public ValuesPattern(string[] Variables, ISemanticElement[][] Records)
42 {
43 this.variables = Variables;
44 this.records = Records;
45 }
46
50 public bool IsEmpty => false;
51
60 public Task<IEnumerable<Possibility>> Search(ISemanticCube Cube,
61 Variables Variables, IEnumerable<Possibility> ExistingMatches, SparqlQuery Query)
62 {
63 LinkedList<Possibility> Result = new LinkedList<Possibility>();
64 ISemanticElement Element;
65 int i, c = this.variables.Length;
66
67 if (ExistingMatches is null)
68 {
69 foreach (ISemanticElement[] Record in this.records)
70 {
71 for (i = 0; i < c; i++)
72 {
73 Element = Record[i];
74 if (!(Element is null))
75 Result.AddLast(new Possibility(this.variables[i], Element));
76 }
77 }
78 }
79 else
80 {
81 ISemanticElement Element0;
82 string s;
83
84 foreach (Possibility P in ExistingMatches)
85 {
86 foreach (ISemanticElement[] Record in this.records)
87 {
88 Possibility Extended = P;
89
90 for (i = 0; i < c; i++)
91 {
92 Element = Record[i];
93 if (Element is null)
94 continue;
95
96 s = this.variables[i];
97 Element0 = P.GetValue(s);
98 if (Element0 is null)
99 Extended = new Possibility(s, Element, Extended);
100 else if (!Element0.Equals(Element))
101 {
102 Extended = null;
103 break;
104 }
105 }
106
107 if (!(Extended is null))
108 Result.AddLast(Extended);
109 }
110 }
111 }
112
113 return Task.FromResult<IEnumerable<Possibility>>(Result);
114 }
115
120 public void SetParent(ScriptNode Parent)
121 {
122 foreach (ISemanticElement[] Record in this.records)
123 {
124 foreach (ISemanticElement Element in Record)
125 {
126 if (Element is SemanticScriptElement ScriptNode)
127 ScriptNode.Node.SetParent(Parent);
128 }
129 }
130 }
131
139 public bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
140 {
141 if (Order == SearchMethod.DepthFirst)
142 {
143 foreach (ISemanticElement[] Record in this.records)
144 {
145 foreach (ISemanticElement Element in Record)
146 {
147 if (Element is SemanticScriptElement ScriptNode)
148 {
149 if (!ScriptNode.Node.ForAllChildNodes(Callback, State, Order))
150 return false;
151 }
152 }
153 }
154 }
155
156 this.ForAll(Callback, State, Order);
157
158 if (Order == SearchMethod.BreadthFirst)
159 {
160 foreach (ISemanticElement[] Record in this.records)
161 {
162 foreach (ISemanticElement Element in Record)
163 {
164 if (Element is SemanticScriptElement ScriptNode)
165 {
166 if (!ScriptNode.Node.ForAllChildNodes(Callback, State, Order))
167 return false;
168 }
169 }
170 }
171 }
172
173 return true;
174 }
175
183 public bool ForAll(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
184 {
185 int i, c;
186
187 foreach (ISemanticElement[] Record in this.records)
188 {
189 c = Record.Length;
190
191 for (i = 0; i < c; i++)
192 {
193 ISemanticElement Element = Record[i];
194
195 if (Element is SemanticScriptElement ScriptNode)
196 {
197 if (!Callback(ScriptNode.Node, out ScriptNode NewNode, State))
198 return false;
199
200 if (!(NewNode is null))
201 Record[i] = new SemanticScriptElement(NewNode);
202 }
203 }
204 }
205
206 return true;
207 }
208
210 public override bool Equals(object obj)
211 {
212 int c, d;
213
214 if (!(obj is ValuesPattern Typed) ||
215 (c = this.variables.Length) != Typed.variables.Length ||
216 (d = this.records.Length) != Typed.records.Length)
217 {
218 return false;
219 }
220
221 int i, j;
222
223 for (i = 0; i < c; i++)
224 {
225 if (!this.variables[i].Equals(Typed.variables[i]))
226 return false;
227 }
228
229 for (j = 0; j < d; j++)
230 {
231 ISemanticElement[] Rec1 = this.records[j];
232 ISemanticElement[] Rec2 = this.records[j];
233
234 for (i = 0; i < c; i++)
235 {
236 ISemanticElement E1 = Rec1[i];
237 ISemanticElement E2 = Rec2[i];
238
239 if ((E1 is null) ^ (E2 is null))
240 return false;
241
242 if (!(E1 is null) && !Rec1[i].Equals(Rec2[i]))
243 return false;
244 }
245 }
246
247 return true;
248 }
249
251 public override int GetHashCode()
252 {
253 int Result = base.GetHashCode();
254
255 foreach (string s in this.variables)
256 Result ^= Result << 5 ^ s.GetHashCode();
257
258 foreach (ISemanticElement[] Record in this.records)
259 {
260 foreach (ISemanticElement Element in Record)
261 {
262 if (!(Element is null))
263 Result ^= Result << 5 ^ Element.GetHashCode();
264 }
265 }
266
267 return Result;
268 }
269
270 }
271}
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, bool DepthFirst)
Calls the callback method for all child nodes.
Definition: ScriptNode.cs:243
void SetParent(ScriptNode Parent)
Sets the parent node. Can only be used when expression is being parsed.
Definition: ScriptNode.cs:132
void SetParent(ScriptNode Parent)
Sets the parent node. Can only be used when expression is being parsed.
ValuesPattern(string[] Variables, ISemanticElement[][] Records)
A collection of predefined values
ValuesPattern(string Variable, ISemanticElement[] Values)
A collection of predefined values
bool ForAll(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
Task< IEnumerable< Possibility > > Search(ISemanticCube Cube, Variables Variables, IEnumerable< Possibility > ExistingMatches, SparqlQuery Query)
Searches for the pattern on information in a semantic cube.
bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
Represents a possible solution during SPARQL evaluation.
Definition: Possibility.cs:13
ISemanticElement GetValue(string VariableName)
Access to possible variable values, given a variable name.
Definition: Possibility.cs:68
Contains information about a variable.
Definition: Variable.cs:10
Collection of variables.
Definition: Variables.cs:25
Interface for semantic cubes.
Interface for semantic nodes.
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