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