Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SparqlRegularPattern.cs
1using System.Collections.Generic;
2using System.Threading.Tasks;
7
9{
14 {
15 private LinkedList<SemanticQueryTriple> triples = null;
16 private LinkedList<KeyValuePair<ScriptNode, ScriptNode>> boundVariables = null;
17 private LinkedList<IFilterNode> filter = null;
18
23 {
24 }
25
29 public bool IsEmpty => !(this.HasTriples || this.HasBoundVariables || this.HasFilter);
30
34 public IEnumerable<SemanticQueryTriple> Triples => this.triples;
35
39 public IEnumerable<KeyValuePair<ScriptNode, ScriptNode>> BoundVariables => this.boundVariables;
40
44 public IEnumerable<IFilterNode> Filter => this.filter;
45
49 public bool HasTriples => !(this.triples is null);
50
54 public bool HasBoundVariables => !(this.boundVariables is null);
55
59 public bool HasFilter => !(this.filter is null);
60
65 public void AddTriple(SemanticQueryTriple Triple)
66 {
67 if (this.triples is null)
68 this.triples = new LinkedList<SemanticQueryTriple>();
69
70 this.triples.AddLast(Triple);
71 }
72
79 {
80 if (this.boundVariables is null)
81 this.boundVariables = new LinkedList<KeyValuePair<ScriptNode, ScriptNode>>();
82
83 this.boundVariables.AddLast(new KeyValuePair<ScriptNode, ScriptNode>(Value, Variable));
84 }
85
91 {
92 if (this.filter is null)
93 this.filter = new LinkedList<IFilterNode>();
94
95 if (Filter is IFilterNode FilterNode)
96 this.filter.AddLast(FilterNode);
97 else
98 this.filter.AddLast(new FilterScriptNode(Filter));
99 }
100
108 public Task<IEnumerable<Possibility>> Search(ISemanticCube Cube, Variables Variables,
109 SparqlQuery Query)
110 {
111 return this.Search(Cube, Variables, null, Query);
112 }
113
122 public async Task<IEnumerable<Possibility>> Search(ISemanticCube Cube,
123 Variables Variables, IEnumerable<Possibility> ExistingMatches,
124 SparqlQuery Query)
125 {
126 if (this.HasTriples)
127 {
128 foreach (SemanticQueryTriple T in this.triples)
129 {
130 switch (T.Type)
131 {
132 case QueryTripleType.Constant:
134 return null;
135 break;
136
137 case QueryTripleType.SubjectVariable:
138 ExistingMatches = await this.CrossPossibilitiesOneVariable(ExistingMatches, T, 0, 1, 2, Cube);
139 break;
140
141 case QueryTripleType.PredicateVariable:
142 ExistingMatches = await this.CrossPossibilitiesOneVariable(ExistingMatches, T, 1, 0, 2, Cube);
143 break;
144
145 case QueryTripleType.ObjectVariable:
146 ExistingMatches = await this.CrossPossibilitiesOneVariable(ExistingMatches, T, 2, 0, 1, Cube);
147 break;
148
149 case QueryTripleType.SubjectPredicateVariables:
150 ExistingMatches = await this.CrossPossibilitiesTwoVariables(ExistingMatches, T, 0, 1, 2, Cube);
151 break;
152
153 case QueryTripleType.SubjectObjectVariable:
154 ExistingMatches = await this.CrossPossibilitiesTwoVariables(ExistingMatches, T, 0, 2, 1, Cube);
155 break;
156
157 case QueryTripleType.PredicateObjectVariable:
158 ExistingMatches = await this.CrossPossibilitiesTwoVariables(ExistingMatches, T, 1, 2, 0, Cube);
159 break;
160
161 case QueryTripleType.SubjectPredicateObjectVariable:
162 ExistingMatches = await this.CrossPossibilitiesThreeVariables(ExistingMatches, T, Cube);
163 break;
164 }
165
166 if (ExistingMatches is null || !ExistingMatches.GetEnumerator().MoveNext())
167 return null;
168 }
169 }
170
171 if (this.HasBoundVariables && !(ExistingMatches is null))
172 {
173 LinkedList<Possibility> NewMatches = new LinkedList<Possibility>();
174 ObjectProperties RecordVariables = null;
175 Possibility P;
176 string Name;
177
178 foreach (Possibility Possibility in ExistingMatches)
179 {
180 P = Possibility;
181
182 foreach (KeyValuePair<ScriptNode, ScriptNode> P2 in this.boundVariables)
183 {
184 if (RecordVariables is null)
185 RecordVariables = new ObjectProperties(P, Variables);
186 else
187 RecordVariables.Object = P;
188
189 if (P2.Value is VariableReference Ref)
190 Name = Ref.VariableName;
191 else
192 {
193 object Obj = await SparqlQuery.EvaluateValue(RecordVariables, P2.Value);
194 if (Obj is null)
195 continue;
196
197 Name = Obj.ToString();
198 if (string.IsNullOrEmpty(Name))
199 continue;
200 }
201
202 ISemanticElement Literal = await Query.EvaluateSemanticElement(RecordVariables, P2.Key);
203 if (!(Literal is null))
204 P = new Possibility(Name, Literal, P);
205 }
206
207 NewMatches.AddLast(P);
208 }
209
210 ExistingMatches = NewMatches;
211 }
212
213 if (this.HasFilter && !(ExistingMatches is null))
214 {
215 LinkedList<Possibility> Filtered = null;
216 ObjectProperties RecordVariables = null;
217 bool Pass;
218
219 foreach (Possibility P in ExistingMatches)
220 {
221 if (RecordVariables is null)
222 RecordVariables = new ObjectProperties(P, Variables);
223 else
224 RecordVariables.Object = P;
225
226 Pass = true;
227
228 foreach (IFilterNode Filter in this.filter)
229 {
230 object Value = await SparqlQuery.EvaluateValue(RecordVariables, Filter, Cube, Query, P);
231 if (!(Value is bool b) || !b)
232 {
233 Pass = false;
234 break;
235 }
236 }
237
238 if (Pass)
239 {
240 if (Filtered is null)
241 Filtered = new LinkedList<Possibility>();
242
243 Filtered.AddLast(P);
244 }
245 }
246
247 ExistingMatches = Filtered;
248 }
249
250 return ExistingMatches;
251 }
252
253 private async Task<IEnumerable<Possibility>> CrossPossibilitiesOneVariable(
254 IEnumerable<Possibility> Possibilities, SemanticQueryTriple T, int VariableIndex,
255 int ValueIndex1, int ValueIndex2, ISemanticCube Cube)
256 {
257 LinkedList<Possibility> NewPossibilities = null;
258 string Name = T.VariableName(VariableIndex);
259 ISemanticElement Value;
260
261 if (Possibilities is null)
262 {
263 IEnumerable<ISemanticTriple> NewTriples = await Cube.GetTriples(
264 T[ValueIndex1], ValueIndex1, T[ValueIndex2], ValueIndex2);
265
266 if (NewTriples is null)
267 return null;
268
269 NewPossibilities = new LinkedList<Possibility>();
270
271 foreach (ISemanticTriple T2 in NewTriples)
272 NewPossibilities.AddLast(new Possibility(Name, T2[VariableIndex]));
273 }
274 else
275 {
276 foreach (Possibility P in Possibilities)
277 {
278 Value = P.GetValue(Name);
279 if (Value is null)
280 {
281 IEnumerable<ISemanticTriple> NewTriples = await Cube.GetTriples(
282 T[ValueIndex1], ValueIndex1, T[ValueIndex2], ValueIndex2);
283
284 if (NewTriples is null)
285 continue;
286
287 if (NewPossibilities is null)
288 NewPossibilities = new LinkedList<Possibility>();
289
290 foreach (ISemanticTriple T2 in NewTriples)
291 NewPossibilities.AddLast(new Possibility(Name, T2[VariableIndex], P));
292 }
293 else
294 {
295 if (await Cube.GetTriplesBySubjectAndPredicateAndObject(Value, T.Predicate, T.Object) is null)
296 continue;
297
298 if (NewPossibilities is null)
299 NewPossibilities = new LinkedList<Possibility>();
300
301 NewPossibilities.AddLast(P);
302 }
303 }
304 }
305
306 return NewPossibilities;
307 }
308
309 private async Task<IEnumerable<Possibility>> CrossPossibilitiesTwoVariables(
310 IEnumerable<Possibility> Possibilities, SemanticQueryTriple T, int VariableIndex1,
311 int VariableIndex2, int ValueIndex, ISemanticCube Cube)
312 {
313 LinkedList<Possibility> NewPossibilities = null;
314 string Name = T.VariableName(VariableIndex1);
315 string Name2 = T.VariableName(VariableIndex2);
316 ISemanticElement Value;
317 ISemanticElement Value2;
318 bool SameName = Name == Name2;
319
320 if (Possibilities is null)
321 {
322 IEnumerable<ISemanticTriple> Triples = await Cube.GetTriples(T[ValueIndex], ValueIndex);
323 if (Triples is null)
324 return null;
325
326 foreach (ISemanticTriple T2 in Triples)
327 {
328 if (SameName)
329 {
330 ISemanticElement E = T2[VariableIndex1];
331
332 if (!E.Equals(T2[VariableIndex2]))
333 continue;
334
335 if (NewPossibilities is null)
336 NewPossibilities = new LinkedList<Possibility>();
337
338 NewPossibilities.AddLast(new Possibility(Name, E));
339 }
340 else
341 {
342 if (NewPossibilities is null)
343 NewPossibilities = new LinkedList<Possibility>();
344
345 NewPossibilities.AddLast(
346 new Possibility(Name, T2[VariableIndex1],
347 new Possibility(Name2, T2[VariableIndex2])));
348 }
349 }
350 }
351 else
352 {
353 foreach (Possibility P in Possibilities)
354 {
355 Value = P.GetValue(Name);
356
357 if (SameName)
358 Value2 = Value;
359 else
360 Value2 = P.GetValue(Name2);
361
362 bool IsProcessed = !(Value is null);
363 bool IsProcessed2 = !(Value2 is null);
364
365 if (IsProcessed && IsProcessed2)
366 {
367 if (await Cube.GetTriplesBySubjectAndPredicateAndObject(Value, Value2, T[ValueIndex]) is null)
368 continue;
369
370 if (NewPossibilities is null)
371 NewPossibilities = new LinkedList<Possibility>();
372
373 NewPossibilities.AddLast(P);
374 }
375 else if (IsProcessed)
376 {
377 this.CrossPossibilities(
378 await Cube.GetTriples(Value, VariableIndex1, T[ValueIndex], ValueIndex),
379 P, Name2, VariableIndex2, ref NewPossibilities);
380 }
381 else if (IsProcessed2)
382 {
383 this.CrossPossibilities(
384 await Cube.GetTriples(Value2, VariableIndex2, T[ValueIndex], ValueIndex),
385 P, Name, VariableIndex1, ref NewPossibilities);
386 }
387 else
388 {
389 IEnumerable<ISemanticTriple> Triples = await Cube.GetTriples(T[ValueIndex], ValueIndex);
390 if (Triples is null)
391 return null;
392
393 foreach (ISemanticTriple T2 in Triples)
394 {
395 if (SameName)
396 {
397 ISemanticElement E = T2[VariableIndex1];
398
399 if (!E.Equals(T2[VariableIndex2]))
400 continue;
401
402 if (NewPossibilities is null)
403 NewPossibilities = new LinkedList<Possibility>();
404
405 NewPossibilities.AddLast(
406 new Possibility(Name, E, P));
407 }
408 else
409 {
410 if (NewPossibilities is null)
411 NewPossibilities = new LinkedList<Possibility>();
412
413 NewPossibilities.AddLast(
414 new Possibility(Name, T2[VariableIndex1],
415 new Possibility(Name2, T2[VariableIndex2], P)));
416 }
417 }
418 }
419
420 }
421 }
422
423 return NewPossibilities;
424 }
425
426 private async Task<IEnumerable<Possibility>> CrossPossibilitiesThreeVariables(
427 IEnumerable<Possibility> Possibilities, SemanticQueryTriple T, ISemanticCube Cube)
428 {
429 LinkedList<Possibility> NewPossibilities = null;
430 string Name = T.SubjectVariable;
431 string Name2 = T.PredicateVariable;
432 string Name3 = T.ObjectVariable;
433 bool SameName = Name == Name2;
434 bool SameName2 = Name == Name3;
435 bool SameName3 = Name2 == Name3;
436
437 if (Possibilities is null)
438 {
439 foreach (ISemanticTriple T2 in Cube)
440 {
441 if (SameName && !T2.Subject.Equals(T2.Predicate))
442 continue;
443
444 if (SameName2 && !T2.Subject.Equals(T2.Object))
445 continue;
446
447 if (SameName3 && !T2.Predicate.Equals(T2.Object))
448 continue;
449
450 Possibility NewPossibility = new Possibility(Name, T2.Subject);
451
452 if (!SameName)
453 NewPossibility = new Possibility(Name2, T2.Predicate, NewPossibility);
454
455 if (!SameName2 && !SameName3)
456 NewPossibility = new Possibility(Name3, T2.Object, NewPossibility);
457
458 if (NewPossibilities is null)
459 NewPossibilities = new LinkedList<Possibility>();
460
461 NewPossibilities.AddLast(NewPossibility);
462 }
463 }
464 else
465 {
466 foreach (Possibility P in Possibilities)
467 {
468 ISemanticElement Value = P.GetValue(Name);
469 ISemanticElement Value2 = SameName ? Value : P.GetValue(Name2);
470 ISemanticElement Value3 = SameName2 ? Value : SameName3 ? Value2 : P.GetValue(Name3);
471 bool IsProcessed = !(Value is null);
472 bool IsProcessed2 = !(Value2 is null);
473 bool IsProcessed3 = !(Value3 is null);
474
475 if (IsProcessed && IsProcessed2 && IsProcessed3)
476 {
477 if (await Cube.GetTriplesBySubjectAndPredicateAndObject(Value, Value2, Value3) is null)
478 continue;
479
480 if (NewPossibilities is null)
481 NewPossibilities = new LinkedList<Possibility>();
482
483 NewPossibilities.AddLast(P);
484 }
485 else if (IsProcessed && IsProcessed2) // Subject & Predicate variables already processed
486 {
487 this.CrossPossibilities(
488 await Cube.GetTriplesBySubjectAndPredicate(Value, Value2),
489 P, Name3, 2, ref NewPossibilities);
490 }
491 else if (IsProcessed && IsProcessed3) // Subject & Object variables already processed
492 {
493 this.CrossPossibilities(
494 await Cube.GetTriplesBySubjectAndObject(Value, Value3),
495 P, Name2, 1, ref NewPossibilities);
496 }
497 else if (IsProcessed2 && IsProcessed3) // Predicate & Object variables already processed
498 {
499 this.CrossPossibilities(
500 await Cube.GetTriplesByPredicateAndObject(Value2, Value3),
501 P, Name, 0, ref NewPossibilities);
502 }
503 else if (IsProcessed) // Subject variable processed
504 {
505 ISemanticPlane Plane = await Cube.GetTriplesBySubject(Value);
506 if (Plane is null)
507 continue;
508
509 foreach (ISemanticTriple T2 in Plane)
510 {
511 if (SameName3)
512 {
513 if (!T2.Predicate.Equals(T2.Object))
514 continue;
515
516 if (NewPossibilities is null)
517 NewPossibilities = new LinkedList<Possibility>();
518
519 NewPossibilities.AddLast(
520 new Possibility(Name2, T2.Predicate, P));
521 }
522 else
523 {
524 if (NewPossibilities is null)
525 NewPossibilities = new LinkedList<Possibility>();
526
527 NewPossibilities.AddLast(
528 new Possibility(Name2, T2.Predicate,
529 new Possibility(Name3, T2.Object, P)));
530 }
531 }
532 }
533 else if (IsProcessed2) // Predicate variable processed
534 {
535 ISemanticPlane Plane = await Cube.GetTriplesByPredicate(Value2);
536 if (Plane is null)
537 continue;
538
539 foreach (ISemanticTriple T2 in Plane)
540 {
541 if (SameName2)
542 {
543 if (!T2.Subject.Equals(T2.Object))
544 continue;
545
546 if (NewPossibilities is null)
547 NewPossibilities = new LinkedList<Possibility>();
548
549 NewPossibilities.AddLast(
550 new Possibility(Name, T2.Subject, P));
551 }
552 else
553 {
554 if (NewPossibilities is null)
555 NewPossibilities = new LinkedList<Possibility>();
556
557 NewPossibilities.AddLast(
558 new Possibility(Name, T2.Subject,
559 new Possibility(Name3, T2.Object, P)));
560 }
561 }
562 }
563 else if (IsProcessed3) // Object variable processed
564 {
565 ISemanticPlane Plane = await Cube.GetTriplesByObject(Value3);
566 if (Plane is null)
567 continue;
568
569 foreach (ISemanticTriple T2 in Plane)
570 {
571 if (SameName)
572 {
573 if (!T2.Subject.Equals(T2.Predicate))
574 continue;
575
576 if (NewPossibilities is null)
577 NewPossibilities = new LinkedList<Possibility>();
578
579 NewPossibilities.AddLast(
580 new Possibility(Name, T2.Subject, P));
581 }
582 else
583 {
584 if (NewPossibilities is null)
585 NewPossibilities = new LinkedList<Possibility>();
586
587 NewPossibilities.AddLast(
588 new Possibility(Name, T2.Subject,
589 new Possibility(Name2, T2.Predicate, P)));
590 }
591 }
592 }
593 else
594 {
595 foreach (ISemanticTriple T2 in Cube)
596 {
597 if (SameName && !T2.Subject.Equals(T2.Predicate))
598 continue;
599
600 if (SameName2 && !T2.Subject.Equals(T2.Object))
601 continue;
602
603 if (SameName3 && !T2.Predicate.Equals(T2.Object))
604 continue;
605
606 Possibility NewPossibility = new Possibility(Name, T2.Subject, P);
607
608 if (!SameName)
609 NewPossibility = new Possibility(Name2, T2.Predicate, NewPossibility);
610
611 if (!SameName2 && !SameName3)
612 NewPossibility = new Possibility(Name3, T2.Object, NewPossibility);
613
614 if (NewPossibilities is null)
615 NewPossibilities = new LinkedList<Possibility>();
616
617 NewPossibilities.AddLast(NewPossibility);
618 }
619 }
620 }
621 }
622
623 return NewPossibilities;
624 }
625
626 private void CrossPossibilities(IEnumerable<ISemanticTriple> NewTriples,
627 Possibility Possibility, string Name, int Index, ref LinkedList<Possibility> NewPossibilities)
628 {
629 if (NewTriples is null)
630 return;
631
632 if (NewPossibilities is null)
633 NewPossibilities = new LinkedList<Possibility>();
634
635 foreach (ISemanticTriple T2 in NewTriples)
636 NewPossibilities.AddLast(new Possibility(Name, T2[Index], Possibility));
637 }
638
646 public bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
647 {
648 if (Order == SearchMethod.DepthFirst)
649 {
650 if (!(this.triples is null))
651 {
652 foreach (ISemanticTriple T in this.triples)
653 {
655 {
656 if (!S.Node.ForAllChildNodes(Callback, State, Order))
657 return false;
658 }
659
661 {
662 if (!P.Node.ForAllChildNodes(Callback, State, Order))
663 return false;
664 }
665
666 if (T.Object is SemanticScriptElement O)
667 {
668 if (!O.Node.ForAllChildNodes(Callback, State, Order))
669 return false;
670 }
671 }
672 }
673
674 if (!(this.boundVariables is null))
675 {
676 foreach (KeyValuePair<ScriptNode, ScriptNode> P in this.boundVariables)
677 {
678 if (!P.Key.ForAllChildNodes(Callback, State, Order))
679 return false;
680
681 if (!P.Value.ForAllChildNodes(Callback, State, Order))
682 return false;
683 }
684 }
685
686 if (!(this.filter is null))
687 {
688 foreach (IFilterNode P in this.filter)
689 {
690 if (!P.ScriptNode.ForAllChildNodes(Callback, State, Order))
691 return false;
692 }
693 }
694 }
695
696 this.ForAll(Callback, State, Order);
697
698 if (Order == SearchMethod.BreadthFirst)
699 {
700 if (!(this.triples is null))
701 {
702 foreach (ISemanticTriple T in this.triples)
703 {
705 {
706 if (!S.Node.ForAllChildNodes(Callback, State, Order))
707 return false;
708 }
709
711 {
712 if (!P.Node.ForAllChildNodes(Callback, State, Order))
713 return false;
714 }
715
716 if (T.Object is SemanticScriptElement O)
717 {
718 if (!O.Node.ForAllChildNodes(Callback, State, Order))
719 return false;
720 }
721 }
722 }
723
724 if (!(this.boundVariables is null))
725 {
726 foreach (KeyValuePair<ScriptNode, ScriptNode> P in this.boundVariables)
727 {
728 if (!P.Key.ForAllChildNodes(Callback, State, Order))
729 return false;
730
731 if (!P.Value.ForAllChildNodes(Callback, State, Order))
732 return false;
733 }
734 }
735
736 if (!(this.filter is null))
737 {
738 foreach (IFilterNode P in this.filter)
739 {
740 if (!P.ScriptNode.ForAllChildNodes(Callback, State, Order))
741 return false;
742 }
743 }
744 }
745
746 return true;
747 }
748
756 public bool ForAll(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
757 {
758 if (!(this.triples is null))
759 {
760 foreach (ISemanticTriple T in this.triples)
761 {
763 {
764 if (!S.ForAll(Callback, State, Order))
765 return false;
766 }
767
769 {
770 if (!P.ForAll(Callback, State, Order))
771 return false;
772 }
773
774 if (T.Object is SemanticScriptElement O)
775 {
776 if (!O.ForAll(Callback, State, Order))
777 return false;
778 }
779 }
780 }
781
782 if (!(this.boundVariables is null))
783 {
784 LinkedListNode<KeyValuePair<ScriptNode, ScriptNode>> Loop = this.boundVariables.First;
785
786 while (!(Loop is null))
787 {
788 if (!Callback(Loop.Value.Key, out ScriptNode NewKey, State))
789 return false;
790
791 if (!Callback(Loop.Value.Value, out ScriptNode NewValue, State))
792 return false;
793
794 if (!(NewKey is null) || !(NewValue is null))
795 {
796 Loop.Value = new KeyValuePair<ScriptNode, ScriptNode>(
797 NewKey ?? Loop.Value.Key, NewValue ?? Loop.Value.Value);
798 }
799
800 Loop = Loop.Next;
801 }
802 }
803
804 if (!(this.filter is null))
805 {
806 LinkedListNode<IFilterNode> Loop = this.filter.First;
807
808 while (!(Loop is null))
809 {
810 if (!Callback(Loop.Value.ScriptNode, out ScriptNode NewValue, State))
811 return false;
812
813 if (!(NewValue is null))
814 {
815 if (NewValue is IFilterNode NewFilterNode)
816 Loop.Value = NewFilterNode;
817 else
818 Loop.Value = new FilterScriptNode(NewValue);
819 }
820
821 Loop = Loop.Next;
822 }
823 }
824
825 return true;
826 }
827
829 public override bool Equals(object obj)
830 {
831 if (!(obj is SparqlRegularPattern Typed) ||
832 this.triples is null ^ Typed.triples is null ||
833 this.boundVariables is null ^ Typed.boundVariables is null ||
834 this.filter is null ^ Typed.filter is null)
835 {
836 return false;
837 }
838
839 if (!(this.boundVariables is null))
840 {
841 IEnumerator<KeyValuePair<ScriptNode, ScriptNode>> e1 = this.boundVariables.GetEnumerator();
842 IEnumerator<KeyValuePair<ScriptNode, ScriptNode>> e2 = Typed.boundVariables.GetEnumerator();
843 bool b1 = e1.MoveNext();
844 bool b2 = e2.MoveNext();
845
846 while (b1 && b2)
847 {
848 if (!e1.Current.Equals(e2.Current))
849 return false;
850
851 b1 = e1.MoveNext();
852 b2 = e2.MoveNext();
853 }
854
855 if (b1 || b2)
856 return false;
857 }
858
859 if (!(this.filter is null))
860 {
861 IEnumerator<IFilterNode> e1 = this.filter.GetEnumerator();
862 IEnumerator<IFilterNode> e2 = Typed.filter.GetEnumerator();
863 bool b1 = e1.MoveNext();
864 bool b2 = e2.MoveNext();
865
866 while (b1 && b2)
867 {
868 if (!e1.Current.Equals(e2.Current))
869 return false;
870
871 b1 = e1.MoveNext();
872 b2 = e2.MoveNext();
873 }
874
875 if (b1 || b2)
876 return false;
877 }
878
879 return true;
880 }
881
883 public override int GetHashCode()
884 {
885 int Result = base.GetHashCode();
886
887 if (!(this.triples is null))
888 {
889 foreach (SemanticQueryTriple T in this.triples)
890 Result ^= Result << 5 ^ T.GetHashCode();
891 }
892
893 if (!(this.boundVariables is null))
894 {
895 foreach (KeyValuePair<ScriptNode, ScriptNode> P in this.boundVariables)
896 {
897 Result ^= Result << 5 ^ P.Key.GetHashCode();
898 Result ^= Result << 5 ^ P.Value.GetHashCode();
899 }
900 }
901
902 if (!(this.filter is null))
903 {
904 foreach (IFilterNode N in this.filter)
905 Result ^= Result << 5 ^ N.GetHashCode();
906 }
907
908 return Result;
909 }
910
915 public void SetParent(ScriptNode Parent)
916 {
917 if (!(this.triples is null))
918 {
919 foreach (ISemanticTriple T in this.triples)
920 {
922 S.Node.SetParent(Parent);
923
925 P.Node.SetParent(Parent);
926
927 if (T.Object is SemanticScriptElement O)
928 O.Node.SetParent(Parent);
929 }
930 }
931
932 if (!(this.boundVariables is null))
933 {
934 foreach (KeyValuePair<ScriptNode, ScriptNode> P in this.boundVariables)
935 {
936 P.Key.SetParent(Parent);
937 P.Value.SetParent(Parent);
938 }
939 }
940
941 if (!(this.filter is null))
942 {
943 foreach (IFilterNode P in this.filter)
944 P.ScriptNode.SetParent(Parent);
945 }
946 }
947 }
948}
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
Represents a variable reference.
void AddFilter(ScriptNode Filter)
Adds a filter to the pattern.
void SetParent(ScriptNode Parent)
Sets the parent node. Can only be used when expression is being parsed.
void AddTriple(SemanticQueryTriple Triple)
Adds a triple to the pattern
bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
SparqlRegularPattern()
Represents a pattern in a SPARQL query.
Task< IEnumerable< Possibility > > Search(ISemanticCube Cube, Variables Variables, SparqlQuery Query)
Searches for the pattern on information in a semantic cube.
async Task< IEnumerable< Possibility > > Search(ISemanticCube Cube, Variables Variables, IEnumerable< Possibility > ExistingMatches, SparqlQuery Query)
Searches for the pattern on information in a semantic cube.
IEnumerable< SemanticQueryTriple > Triples
Triples, null if none.
bool ForAll(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
void AddVariableBinding(ScriptNode Value, ScriptNode Variable)
Adds a variable binding to the pattern.
IEnumerable< IFilterNode > Filter
Filter, null if none.
IEnumerable< KeyValuePair< ScriptNode, ScriptNode > > BoundVariables
Bound variables, null if none.
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
string VariableName(int Index)
Gets a variable name, given the axis index: 0=Subject, 1=Predicate, 2=Object.
string PredicateVariable
Predicate element variable name, if any
string ObjectVariable
Object element variable name, if any
string SubjectVariable
Subject element variable name, if any
Contains information about a variable.
Definition: Variable.cs:10
Collection of variables.
Definition: Variables.cs:25
Interface for semantic cubes.
Task< IEnumerable< ISemanticTriple > > GetTriples(ISemanticElement Value, int AxisIndex)
Gets available triples in the cube, having a given value, along a given axis.
Task< IEnumerable< ISemanticTriple > > GetTriplesBySubjectAndPredicateAndObject(ISemanticElement Subject, ISemanticElement Predicate, ISemanticElement Object)
Gets available triples in the cube, having a given subject, predicate and object.
Interface for semantic nodes.
Interface for semantic planes.
Interface for semantic triples.
ISemanticElement Object
Object element
ISemanticElement Predicate
Predicate element
ISemanticElement Subject
Subject element
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