Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SparqlQuery.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
18using Waher.Things;
19
21{
25 public enum QueryType
26 {
30 Select,
31
35 Ask,
36
40 Construct
41 }
42
47 {
48 private readonly ScriptNode[] columns;
49 private readonly ScriptNode[] columnNames;
50 private readonly ScriptNode[] groupBy;
51 private readonly ScriptNode[] groupByNames;
52 private readonly ScriptNode[] from;
53 private readonly ISparqlPattern where;
54 private readonly ScriptNode having;
55 private readonly KeyValuePair<ScriptNode, bool>[] orderBy;
56 private readonly SparqlRegularPattern construct;
57 private readonly QueryType queryType;
58 private readonly int? limit;
59 private readonly int? offset;
60 private readonly bool distinct;
61 private readonly bool reduced;
62 private Dictionary<UriNode, ISemanticCube> namedGraphs;
63 private UriNode[] namedGraphNames;
64
86 public SparqlQuery(QueryType QueryType, bool Distinct, bool Reduced, ScriptNode[] Columns,
87 ScriptNode[] ColumnNames, ScriptNode[] From, Dictionary<UriNode, ISemanticCube> NamedGraphs,
88 ISparqlPattern Where, ScriptNode[] GroupBy, ScriptNode[] GroupByNames, ScriptNode Having,
89 KeyValuePair<ScriptNode, bool>[] OrderBy, int? Limit, int? Offset,
91 : base(Start, Length, Expression)
92 {
93 this.queryType = QueryType;
94 this.distinct = Distinct;
95 this.reduced = Reduced;
96 this.limit = Limit;
97 this.offset = Offset;
98 this.construct = Construct;
99
100 this.columns = Columns;
101 this.columns?.SetParent(this);
102
103 this.columnNames = ColumnNames;
104 this.columnNames?.SetParent(this);
105
106 this.from = From;
107 this.from?.SetParent(this);
108 this.namedGraphs = NamedGraphs;
109
110 this.namedGraphNames = new UriNode[NamedGraphs?.Count ?? 0];
111 NamedGraphs?.Keys.CopyTo(this.namedGraphNames, 0);
112
113 this.where = Where;
114 this.where?.SetParent(this);
115
116 this.groupBy = GroupBy;
117 this.groupBy?.SetParent(this);
118
119 this.groupByNames = GroupByNames;
120 this.groupByNames?.SetParent(this);
121
122 this.having = Having;
123 this.having?.SetParent(this);
124
125 this.orderBy = OrderBy;
126
127 if (!(this.orderBy is null))
128 {
129 foreach (KeyValuePair<ScriptNode, bool> P in this.orderBy)
130 P.Key.SetParent(this);
131 }
132 }
133
138 public override bool IsAsynchronous => true;
139
143 public UriNode[] NamedGraphNames => this.namedGraphNames;
144
151 {
152 return this.EvaluateAsync(Variables).Result;
153 }
154
161 public override Task<IElement> EvaluateAsync(Variables Variables)
162 {
163 return this.EvaluateAsync(Variables, null);
164 }
165
173 public async Task<IElement> EvaluateAsync(Variables Variables,
174 IEnumerable<Possibility> ExistingMatches)
175 {
176 SemanticDataSet DataSet = new SemanticDataSet();
177 object From;
178
179 if (this.from is null)
180 {
181 if (Variables.TryGetVariable(" Default Graph ", out Variable v))
182 From = v.ValueObject;
183 else
184 throw new ScriptRuntimeException("Default graph not defined.", this);
185
186 DataSet.Add(await this.GetDataSource(From, Variables, false));
187 }
188 else
189 {
190 foreach (ScriptNode Source in this.from)
191 {
192 From = (await Source.EvaluateAsync(Variables)).AssociatedObjectValue;
193 DataSet.Add(await this.GetDataSource(From, Variables, false));
194 }
195 }
196
197 IEnumerable<ISparqlResultRecord> Possibilities;
198
199 if (this.where is null)
200 Possibilities = ExistingMatches;
201 else
202 Possibilities = await this.where.Search(DataSet, Variables, ExistingMatches, this);
203
204 if (!(this.groupBy is null) && !(Possibilities is null))
205 {
206 Dictionary<string, bool> VectorProperties = null;
207
208 if (!(this.columns is null))
209 {
210 foreach (ScriptNode Node in this.columns)
211 {
212 if (!(Node is VariableReference))
213 {
214 Node.ForAllChildNodes((ScriptNode Descendant, out ScriptNode NewNode, object State) =>
215 {
216 if (Descendant is VariableReference Ref)
217 {
218 if (VectorProperties is null)
219 VectorProperties = new Dictionary<string, bool>();
220
221 VectorProperties[Ref.VariableName] = true;
222 }
223
224 NewNode = null;
225 return true;
226 }, null, SearchMethod.TreeOrder);
227 }
228 }
229 }
230
231 GroupResultSet GroupComparer = new GroupResultSet(this.groupBy, this.groupByNames);
232 SortedDictionary<ISparqlResultRecord, KeyValuePair<ISparqlResultRecord, LinkedList<ISparqlResultRecord>>> Groups =
233 new SortedDictionary<ISparqlResultRecord, KeyValuePair<ISparqlResultRecord, LinkedList<ISparqlResultRecord>>>(GroupComparer);
234 LinkedList<ISparqlResultRecord> LastList = null;
235 ISparqlResultRecord LastRecord = null;
236 bool First = false;
237
238 foreach (ISparqlResultRecord P in Possibilities)
239 {
240 if (LastRecord is null || GroupComparer.Compare(LastRecord, P) != 0)
241 {
242 if (Groups.TryGetValue(P, out KeyValuePair<ISparqlResultRecord, LinkedList<ISparqlResultRecord>> P2))
243 {
244 LastRecord = P2.Key;
245 LastList = P2.Value;
246 First = false;
247 }
248 else
249 {
250 LastList = new LinkedList<ISparqlResultRecord>();
251 Groups[P] = new KeyValuePair<ISparqlResultRecord, LinkedList<ISparqlResultRecord>>(P, LastList);
252 LastRecord = P;
253 First = true;
254 }
255 }
256
257 LastList.AddLast(P);
258
259 if (!(VectorProperties is null))
260 {
261 foreach (string VectorProperty in VectorProperties.Keys)
262 {
263 ISemanticElement Element = LastRecord[VectorProperty];
264 if (!(Element is SemanticElementVector Vector))
265 {
266 Vector = new SemanticElementVector();
267 LastRecord[VectorProperty] = Vector;
268 }
269
270 if (First)
271 Vector.Add(Element);
272 else
273 Vector.Add(P[VectorProperty]);
274 }
275 }
276
277 First = false;
278 }
279
280 Possibilities = Groups.Keys;
281
282 if (!(this.having is null))
283 {
284 LinkedList<ISparqlResultRecord> Filtered = new LinkedList<ISparqlResultRecord>();
285 ObjectProperties RecordVariables = null;
286
287 foreach (ISparqlResultRecord Record in Possibilities)
288 {
289 try
290 {
291 if (RecordVariables is null)
292 RecordVariables = new ObjectProperties(Record, Variables);
293 else
294 RecordVariables.Object = Record;
295
296 object Value = await EvaluateValue(RecordVariables, this.having);
297 if (Value is bool b && b)
298 Filtered.AddLast(Record);
299 }
300 catch (Exception)
301 {
302 // Ignore record
303 }
304 }
305
306 Possibilities = Filtered;
307 }
308 }
309
310 switch (this.queryType)
311 {
312 case QueryType.Ask:
313 if (!(Possibilities is null))
314 return new ObjectValue(new SparqlResultSet(Possibilities.GetEnumerator().MoveNext()));
315
316 return new ObjectValue(new SparqlResultSet(false));
317
318 case QueryType.Select:
319 Dictionary<string, int> ColumnVariables = new Dictionary<string, int>();
320 LinkedList<KeyValuePair<ScriptNode, int>> ColumnScript = null;
321 List<string> ColumnNames = new List<string>();
322 string Name;
323 int i, c;
324 bool AllNames;
325
326 if (this.columns is null)
327 AllNames = true;
328 else
329 {
330 AllNames = false;
331
332 int Columns = this.columns.Length;
333
334 c = this.columnNames?.Length ?? 0;
335
336 for (i = 0; i < Columns; i++)
337 {
338 if (i < c && !(this.columnNames[i] is null))
339 {
340 if (this.columnNames[i] is VariableReference Ref2)
341 Name = Ref2.VariableName;
342 else
343 Name = (await this.columnNames[i].EvaluateAsync(Variables)).AssociatedObjectValue?.ToString();
344
345 ColumnVariables[Name] = i;
346 ColumnNames.Add(Name);
347 }
348 else
349 Name = null;
350
351 if (this.columns[i] is VariableReference Ref)
352 {
353 if (Name is null)
354 {
355 Name = Ref.VariableName;
356
357 ColumnVariables[Name] = i;
358 ColumnNames.Add(Name);
359 }
360 else
361 {
362 if (ColumnScript is null)
363 ColumnScript = new LinkedList<KeyValuePair<ScriptNode, int>>();
364
365 ColumnScript.AddLast(new KeyValuePair<ScriptNode, int>(Ref, i));
366 }
367 }
368 else
369 {
370 if (ColumnScript is null)
371 ColumnScript = new LinkedList<KeyValuePair<ScriptNode, int>>();
372
373 ColumnScript.AddLast(new KeyValuePair<ScriptNode, int>(this.columns[i], i));
374
375 if (Name is null)
376 {
377 Name = " c" + i.ToString();
378 ColumnNames.Add(Name);
379 }
380 }
381 }
382 }
383
384 List<ISparqlResultRecord> Records = new List<ISparqlResultRecord>();
385 bool MakeUnique = this.distinct || this.reduced;
386 Dictionary<string, bool> Distinct = MakeUnique ? new Dictionary<string, bool>() : null;
387 StringBuilder sb = MakeUnique ? new StringBuilder() : null;
388 ObjectProperties RecordVariables = null;
389
390 if (!(Possibilities is null))
391 {
392 foreach (ISparqlResultRecord P in Possibilities)
393 {
394 Dictionary<string, ISparqlResultItem> Record = new Dictionary<string, ISparqlResultItem>();
395
396 foreach (ISparqlResultItem Loop in P)
397 {
398 Name = Loop.Name;
399
400 if (ColumnVariables.TryGetValue(Name, out i))
401 Record[Name] = new SparqlResultItem(Name, Loop.Value, i);
402 else if (AllNames)
403 {
404 i = ColumnNames.Count;
405 ColumnNames.Add(Name);
406 ColumnVariables[Name] = i;
407
408 Record[Name] = new SparqlResultItem(Name, Loop.Value, i);
409 }
410 }
411
412 if (!(ColumnScript is null))
413 {
414 if (RecordVariables is null)
415 RecordVariables = new ObjectProperties(P, Variables);
416 else
417 RecordVariables.Object = P;
418
419 foreach (KeyValuePair<ScriptNode, int> P2 in ColumnScript)
420 {
421 Name = ColumnNames[P2.Value];
422 ISemanticElement Literal = await this.EvaluateSemanticElement(RecordVariables, P2.Key);
423
424 if (!(Literal is null))
425 {
426 Record[Name] = new SparqlResultItem(Name, Literal, P2.Value);
427 P[Name] = Literal;
428 }
429 }
430 }
431
432 if (MakeUnique)
433 {
434 bool First = true;
435
436 sb.Clear();
437
438 foreach (ISparqlResultItem Value in Record.Values)
439 {
440 if (First)
441 First = false;
442 else
443 sb.Append(';');
444
445 sb.Append(Value.Name);
446 sb.Append('=');
447 sb.Append(Value.Value?.ToString());
448 }
449
450 string Key = sb.ToString();
451
452 if (Distinct.ContainsKey(Key))
453 continue;
454
455 Distinct[Key] = true;
456 }
457
458 Records.Add(new SparqlPatternResultRecord(Record));
459 }
460 }
461
462 if (!(this.orderBy is null))
463 Records.Sort(new OrderResultSet(this.orderBy));
464
465 if (this.offset.HasValue || this.limit.HasValue)
466 {
467 int Offset = this.offset ?? 0;
468 int MaxCount = this.limit ?? int.MaxValue;
469 int Count = Records.Count;
470
471 while (Offset > 0 && Count > 0)
472 {
473 Records.RemoveAt(0);
474 Count--;
475 Offset--;
476 }
477
478 while (Count > MaxCount)
479 {
480 Records.RemoveAt(MaxCount);
481 Count--;
482 }
483 }
484
485 return new ObjectValue(new SparqlResultSet(ColumnNames.ToArray(), new Uri[0],
486 Records.ToArray()));
487
488 case QueryType.Construct:
489 Dictionary<string, string> BlankNodeDictionary = null;
490 LinkedList<SemanticTriple> Construction = new LinkedList<SemanticTriple>();
491 IEnumerable<ISparqlResultRecord> Items = Possibilities;
492
493 RecordVariables = null;
494
495 if (!(Items is null))
496 {
497 if (!(this.orderBy is null))
498 {
499 List<ISparqlResultRecord> Ordered = new List<ISparqlResultRecord>();
500
501 foreach (ISparqlResultRecord Record in Items)
502 Ordered.Add(Record);
503
504 Ordered.Sort(new OrderResultSet(this.orderBy));
505 Items = Ordered;
506 }
507
508 int Offset = this.offset ?? 0;
509 int MaxCount = this.limit ?? int.MaxValue;
510
511 foreach (ISparqlResultRecord P in Items)
512 {
513 if (Offset > 0)
514 {
515 Offset--;
516 continue;
517 }
518
519 if (--MaxCount < 0)
520 break;
521
522 BlankNodeDictionary?.Clear();
523
524 if (RecordVariables is null)
525 RecordVariables = new ObjectProperties(P, Variables);
526 else
527 RecordVariables.Object = P;
528
529 foreach (ISemanticTriple T in this.construct.Triples)
530 {
531 ISemanticElement Subject = await this.EvaluateSemanticElement(RecordVariables, T.Subject);
532 if (Subject is null)
533 continue;
534 else if (Subject is BlankNode BnS)
535 {
536 if (BlankNodeDictionary is null)
537 BlankNodeDictionary = new Dictionary<string, string>();
538
539 if (!BlankNodeDictionary.TryGetValue(BnS.NodeId, out string NewLabel))
540 {
541 NewLabel = "n" + Guid.NewGuid().ToString();
542 BlankNodeDictionary[BnS.NodeId] = NewLabel;
543 }
544
545 Subject = new BlankNode(NewLabel);
546 }
547
548 ISemanticElement Predicate = await this.EvaluateSemanticElement(RecordVariables, T.Predicate);
549 if (Predicate is null)
550 continue;
551 else if (Predicate is BlankNode BnP)
552 {
553 if (BlankNodeDictionary is null)
554 BlankNodeDictionary = new Dictionary<string, string>();
555
556 if (!BlankNodeDictionary.TryGetValue(BnP.NodeId, out string NewLabel))
557 {
558 NewLabel = "n" + Guid.NewGuid().ToString();
559 BlankNodeDictionary[BnP.NodeId] = NewLabel;
560 }
561
562 Predicate = new BlankNode(NewLabel);
563 }
564
565 ISemanticElement Object = await this.EvaluateSemanticElement(RecordVariables, T.Object);
566 if (Object is null)
567 continue;
568 else if (Object is BlankNode BnO)
569 {
570 if (BlankNodeDictionary is null)
571 BlankNodeDictionary = new Dictionary<string, string>();
572
573 if (!BlankNodeDictionary.TryGetValue(BnO.NodeId, out string NewLabel))
574 {
575 NewLabel = "n" + Guid.NewGuid().ToString();
576 BlankNodeDictionary[BnO.NodeId] = NewLabel;
577 }
578
579 Object = new BlankNode(NewLabel);
580 }
581
582 Construction.AddLast(new SemanticTriple(Subject, Predicate, Object));
583 }
584 }
585 }
586
587 return new ObjectValue(new InMemorySemanticModel(Construction));
588
589 default:
590 throw new ScriptRuntimeException("Query type not supported.", this);
591 }
592 }
593
594 private async Task<ISemanticCube> GetDataSource(object From, Variables Variables,
595 bool NullIfNotFound)
596 {
597 if (From is UriNode UriNode)
598 return await this.LoadGraph(UriNode.Uri, Variables, NullIfNotFound);
599 else if (From is Uri Uri)
600 return await this.LoadGraph(Uri, Variables, NullIfNotFound);
601 else if (From is string s)
602 return await this.LoadGraph(new Uri(s, UriKind.RelativeOrAbsolute), Variables, NullIfNotFound);
603 else if (From is ISemanticCube Cube)
604 return Cube;
605 else if (From is ISemanticModel Model)
606 return await InMemorySemanticCube.Create(Model);
607
608 if (NullIfNotFound)
609 return null;
610 else
611 throw new ScriptRuntimeException("Graph not a semantic cube or semantic model.", this);
612 }
613
614 internal static async Task<object> EvaluateValue(Variables RecordVariables, ScriptNode Node)
615 {
616 try
617 {
618 return (await Node.EvaluateAsync(RecordVariables)).AssociatedObjectValue;
619 }
621 {
623 }
624 catch (Exception)
625 {
626 return null;
627 }
628 }
629
630 internal static async Task<object> EvaluateValue(Variables RecordVariables,
632 {
633 try
634 {
635 return (await Node.EvaluateAsync(RecordVariables, Cube, Query, P)).AssociatedObjectValue;
636 }
638 {
640 }
641 catch (Exception ex)
642 {
643 return ex;
644 }
645 }
646
647 internal Task<ISemanticElement> EvaluateSemanticElement(Variables RecordVariables, ISemanticElement Element)
648 {
649 if (Element is SemanticScriptElement ScriptElement)
650 return this.EvaluateSemanticElement(RecordVariables, ScriptElement.Node);
651 else
652 return Task.FromResult(Element);
653 }
654
655 internal async Task<ISemanticElement> EvaluateSemanticElement(Variables RecordVariables, ScriptNode Node)
656 {
657 object Value = await EvaluateValue(RecordVariables, Node);
658 if (Value is null)
659 return null;
660 else
661 return SemanticElements.Encapsulate(Value);
662 }
663
671 public override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
672 {
673 if (Order == SearchMethod.DepthFirst)
674 {
675 if (!this.columns.ForAllChildNodes(Callback, State, Order))
676 return false;
677
678 if (!(this.where is null) && !this.where.ForAllChildNodes(Callback, State, Order))
679 return false;
680
681 if (!(this.construct is null) && !this.construct.ForAllChildNodes(Callback, State, Order))
682 return false;
683 }
684
685 if (!this.columns.ForAll(Callback, this, State, Order == SearchMethod.TreeOrder))
686 return false;
687
688 if (!(this.where is null) && !this.where.ForAll(Callback, State, Order))
689 return false;
690
691 if (!(this.construct is null) && !this.construct.ForAll(Callback, State, Order))
692 return false;
693
694 if (Order == SearchMethod.BreadthFirst)
695 {
696 if (!this.columns.ForAllChildNodes(Callback, State, Order))
697 return false;
698
699 if (!(this.where is null) && !this.where.ForAllChildNodes(Callback, State, Order))
700 return false;
701
702 if (!(this.construct is null) && !this.construct.ForAllChildNodes(Callback, State, Order))
703 return false;
704 }
705
706 return true;
707 }
708
710 public override bool Equals(object obj)
711 {
712 if (!(obj is SparqlQuery O) ||
713 !AreEqual(this.columns, O.columns) ||
714 ((this.where is null) ^ (O.where is null)) ||
715 ((this.construct is null) ^ (O.construct is null)) ||
716 this.distinct != O.distinct ||
717 this.reduced != O.reduced ||
718 !base.Equals(obj))
719 {
720 return false;
721 }
722
723 if (!(this.where is null) && !this.where.Equals(O.where))
724 return false;
725
726 if (!(this.construct is null) && !this.construct.Equals(O.construct))
727 return false;
728
729 return true;
730 }
731
733 public override int GetHashCode()
734 {
735 int Result = base.GetHashCode();
736
737 Result ^= Result << 5 ^ GetHashCode(this.columns);
738 Result ^= Result << 5 ^ this.distinct.GetHashCode();
739 Result ^= Result << 5 ^ this.reduced.GetHashCode();
740
741 if (!(this.where is null))
742 Result ^= Result << 5 ^ this.where.GetHashCode();
743
744 if (!(this.construct is null))
745 Result ^= Result << 5 ^ this.construct.GetHashCode();
746
747 return Result;
748 }
749
750 private async Task<ISemanticCube> LoadGraph(Uri Uri, Variables Variables, bool NullIfNotFound)
751 {
752 if (Variables.TryGetVariable(" " + Uri.ToString() + " ", out Variable v) &&
753 v.ValueObject is ISemanticCube Cube)
754 {
755 return Cube;
756 }
757
758 IGraphSource Source = await GetSourceHandler(Uri, NullIfNotFound);
759 if (Source is null)
760 return null;
761
762 if (Variables.TryGetVariable("QuickLoginUser", out v) &&
763 v.ValueObject is IRequestOrigin Caller)
764 {
765 return await Source.LoadGraph(Uri, this, NullIfNotFound, await Caller.GetOrigin());
766 }
767 else if (Variables.TryGetVariable("User", out v) &&
768 v.ValueObject is IRequestOrigin Caller2)
769 {
770 return await Source.LoadGraph(Uri, this, NullIfNotFound, await Caller2.GetOrigin());
771 }
772 else
773 return await Source.LoadGraph(Uri, this, NullIfNotFound, new RequestOrigin(string.Empty, null, null, null));
774 }
775
783 public static async Task<IGraphSource> GetSourceHandler(Uri Uri, bool NullIfNotFound)
784 {
785 GraphReference Ref = await Database.FindFirstIgnoreRest<GraphReference>(
786 new FilterFieldEqualTo("GraphUri", Uri.AbsoluteUri));
787
788 if (!(Ref is null))
789 return await Ref.GetGraphSource();
790
791 IGraphSource Source = Types.FindBest<IGraphSource, Uri>(Uri);
792
793 if (Source is null && !NullIfNotFound)
794 throw new InvalidOperationException("Unable to get access to graph source: " + Uri.ToString());
795
796 return Source;
797 }
798
805 internal Task<ISemanticCube> GetNamedGraph(object Name, Variables Variables)
806 {
807 if (Name is UriNode UriNode)
808 return this.GetNamedGraph(UriNode, Variables);
809 else if (Name is Uri Uri)
810 return this.GetNamedGraph(new UriNode(Uri, Uri.ToString()), Variables);
811 else if (Name is string s && System.Uri.TryCreate(s, UriKind.RelativeOrAbsolute, out Uri))
812 return this.GetNamedGraph(new UriNode(Uri, s), Variables);
813 else
814 return null;
815 }
816
823 internal async Task<ISemanticCube> GetNamedGraph(UriNode Uri, Variables Variables)
824 {
825 if (!(this.namedGraphs is null) &&
826 this.namedGraphs.TryGetValue(Uri, out ISemanticCube Cube) &&
827 !(Cube is null))
828 {
829 return Cube;
830 }
831
832 Cube = await this.GetDataSource(Uri, Variables, true);
833 if (Cube is null)
834 Cube = new InMemorySemanticCube();
835
836 if (this.namedGraphs is null)
837 this.namedGraphs = new Dictionary<UriNode, ISemanticCube>();
838
839 this.namedGraphs[Uri] = Cube;
840
841 return Cube;
842 }
843
849 public void RegisterNamedGraph(params string[] Names)
850 {
851 int i, c = Names.Length;
852 UriNode[] Nodes = new UriNode[Names.Length];
853
854 for (i = 0; i < c; i++)
855 {
856 if (!Uri.TryCreate(Names[i], UriKind.RelativeOrAbsolute, out Uri Name))
857 throw new ArgumentException("Not a valid URI.", nameof(Names));
858
859 Nodes[i] = new UriNode(Name, Names[i]);
860 }
861
862 this.RegisterNamedGraph(Nodes);
863 }
864
870 public void RegisterNamedGraph(params Uri[] Names)
871 {
872 int i, c = Names.Length;
873 UriNode[] Nodes = new UriNode[Names.Length];
874
875 for (i = 0; i < c; i++)
876 Nodes[i] = new UriNode(Names[i], Names[i].ToString());
877
878 this.RegisterNamedGraph(Nodes);
879 }
880
886 public void RegisterNamedGraph(params UriNode[] Names)
887 {
888 if (this.namedGraphs is null)
889 this.namedGraphs = new Dictionary<UriNode, ISemanticCube>();
890
891 foreach (UriNode Name in Names)
892 {
893 if (!this.namedGraphs.ContainsKey(Name))
894 this.namedGraphs[Name] = null;
895 }
896
897 this.namedGraphNames = new UriNode[this.namedGraphs.Count];
898 this.namedGraphs.Keys.CopyTo(this.namedGraphNames, 0);
899 }
900
901 }
902}
static async Task< InMemorySemanticCube > Create(ISemanticModel Model)
Creates an in-memory semantic cube from a semantic model.
Represents a blank node
Definition: BlankNode.cs:7
static ISemanticElement Encapsulate(object Value)
Encapsulates an object as a semantic element.
void Add(ISemanticCube Source)
Adds a source to the data set.
Contains a record from the results of a SPARQL query.
Contains an item in a record from the results of a SPARQL query.
Contains the results of a SPARQL query. https://www.w3.org/TR/2023/WD-sparql12-results-xml-20230516/ ...
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:19
This filter selects objects that have a named field equal to a given value.
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:14
Base class for all types of elements.
Definition: Element.cs:13
Class managing a script expression.
Definition: Expression.cs:39
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
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
override string ToString()
Definition: ScriptNode.cs:359
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
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
Represents a variable reference.
Comparer for grouping a SPARQL result set.
Comparer for ordering a SPARQL result set.
Represents a possible solution during SPARQL evaluation.
Definition: Possibility.cs:13
Contains a reference to a graph in the graph store.
async Task< IGraphSource > GetGraphSource()
Gets a Graph Source object corresponding to the graph referenced by the object.
async Task< IElement > EvaluateAsync(Variables Variables, IEnumerable< Possibility > ExistingMatches)
Evaluates the node asynchronously, using the variables provided in the Variables collection.
Definition: SparqlQuery.cs:173
static async Task< IGraphSource > GetSourceHandler(Uri Uri, bool NullIfNotFound)
Gets a graph source handler, given the Graph URI
Definition: SparqlQuery.cs:783
void RegisterNamedGraph(params UriNode[] Names)
Registers implicitly defined named graphs, that may be used by GRAPH patterns, even if they are not n...
Definition: SparqlQuery.cs:886
override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
Definition: SparqlQuery.cs:671
override Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node asynchronously, using the variables provided in the Variables collection.
Definition: SparqlQuery.cs:161
void RegisterNamedGraph(params string[] Names)
Registers implicitly defined named graphs, that may be used by GRAPH patterns, even if they are not n...
Definition: SparqlQuery.cs:849
void RegisterNamedGraph(params Uri[] Names)
Registers implicitly defined named graphs, that may be used by GRAPH patterns, even if they are not n...
Definition: SparqlQuery.cs:870
SparqlQuery(QueryType QueryType, bool Distinct, bool Reduced, ScriptNode[] Columns, ScriptNode[] ColumnNames, ScriptNode[] From, Dictionary< UriNode, ISemanticCube > NamedGraphs, ISparqlPattern Where, ScriptNode[] GroupBy, ScriptNode[] GroupByNames, ScriptNode Having, KeyValuePair< ScriptNode, bool >[] OrderBy, int? Limit, int? Offset, SparqlRegularPattern Construct, int Start, int Length, Expression Expression)
Executes a SPARQL query.
Definition: SparqlQuery.cs:86
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: SparqlQuery.cs:150
UriNode[] NamedGraphNames
Names of named graphs, may be null.
Definition: SparqlQuery.cs:143
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: SparqlQuery.cs:138
Represents one record.
Definition: Record.cs:9
Executes a SELECT statement against the object database.
Definition: Select.cs:20
Contains information about a variable.
Definition: Variable.cs:10
Collection of variables.
Definition: Variables.cs:25
virtual bool TryGetVariable(string Name, out Variable Variable)
Tries to get a variable object, given its name.
Definition: Variables.cs:52
Tokens available in request.
Definition: RequestOrigin.cs:9
Interface for semantic cubes.
Interface for semantic nodes.
Interface for semantic models.
Interface for semantic triples.
ISemanticElement Object
Object element
ISemanticElement Predicate
Predicate element
ISemanticElement Subject
Subject element
Interface for items in a record from the results of a SPARQL query.
string Name
Name of item in record.
ISemanticElement Value
Value of item in record.
Interface for result records of a SPARQL query.
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33
Interface for script nodes with asynchronous evaluation
Task< IElement > EvaluateAsync(Variables Variables, ISemanticCube Cube, SparqlQuery Query, Possibility Possibility)
Evaluates the node, using the variables provided in the Variables collection.
Task< ISemanticCube > LoadGraph(Uri Source, ScriptNode Node, bool NullIfNotFound, RequestOrigin Caller)
Loads the graph
void SetParent(ScriptNode Parent)
Sets the parent node. Can only be used when expression is being parsed.
Interface for requestors that can act as an origin for distributed requests.
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
QueryType
SPARQL query type.
Definition: SparqlQuery.cs:26