Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GroupResultSet.cs
1using System;
2using System.Collections.Generic;
7
9{
13 public class GroupResultSet : IComparer<ISparqlResultRecord>
14 {
15 private readonly string[] variables;
16 private readonly string[] alias;
17 private readonly ScriptNode[] expression;
18 private readonly ScriptNode[] aliasExpression;
19 private readonly bool[] calculated;
20 private readonly bool[] hasAlias;
21 private readonly bool[] hasCalculatedAlias;
22 private readonly int count;
23
30 public GroupResultSet(ScriptNode[] GroupBy, ScriptNode[] Names)
31 {
32 int i;
33
34 this.count = GroupBy.Length;
35 if (Names.Length != this.count)
36 throw new ArgumentException("Array size mismatch.", nameof(Names));
37
38 this.expression = GroupBy;
39 this.aliasExpression = Names;
40
41 this.variables = new string[this.count];
42 this.alias = new string[this.count];
43 this.calculated = new bool[this.count];
44 this.hasAlias = new bool[this.count];
45 this.hasCalculatedAlias = new bool[this.count];
46
47 for (i = 0; i < this.count; i++)
48 {
49 if (this.expression[i] is VariableReference Ref)
50 {
51 this.variables[i] = Ref.VariableName;
52 this.calculated[i] = false;
53 }
54 else
55 this.calculated[i] = true;
56
57 if (this.aliasExpression[i] is null)
58 this.hasAlias[i] = false;
59 else if (this.aliasExpression[i] is VariableReference Ref2)
60 {
61 this.alias[i] = Ref2.VariableName;
62 this.hasCalculatedAlias[i] = false;
63 }
64 else
65 this.hasCalculatedAlias[i] = true;
66 }
67 }
68
80 {
81 ISemanticElement e1, e2;
82 int i, j;
83
84 for (i = 0; i < this.count; i++)
85 {
86 e1 = this.GetValue(x, i);
87 e2 = this.GetValue(y, i);
88
89 if (e1 is null)
90 {
91 if (!(e2 is null))
92 return -1;
93 }
94 else if (e2 is null)
95 return 1;
96 else
97 {
98 j = e1.CompareTo(e2);
99 if (j != 0)
100 return j;
101 }
102 }
103
104 return 0;
105 }
106
107 private ISemanticElement GetValue(ISparqlResultRecord Record, int Index)
108 {
109 if (!this.calculated[Index])
110 return Record[this.variables[Index]];
111
113 string Alias = null;
114 ISemanticElement Result;
115
116 if (this.hasAlias[Index])
117 {
118 if (this.hasCalculatedAlias[Index])
119 {
120 if (ObjectProperties is null)
122
123 Alias = EvaluateValue(this.aliasExpression[Index], ObjectProperties)?.ToString();
124 }
125 else
126 Alias = this.alias[Index];
127
128 if (!(Alias is null))
129 {
130 Result = Record[Alias];
131 if (!(Result is null))
132 return Result;
133 }
134 }
135
136 if (ObjectProperties is null)
138
139 Result = OrderResultSet.EvaluateElement(this.expression[Index], ObjectProperties);
140 Record[Alias] = Result;
141
142 return Result;
143 }
144
145 private static object EvaluateValue(ScriptNode Expression, Variables Variables)
146 {
147 try
148 {
149 return Expression.Evaluate(Variables)?.AssociatedObjectValue;
150 }
152 {
153 return ex.ReturnValue.AssociatedObjectValue;
154 }
155 }
156 }
157}
Class managing a script expression.
Definition: Expression.cs:39
object Evaluate(Variables Variables)
Evaluates the expression, using the variables provided in the Variables collection....
Definition: Expression.cs:4248
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
Represents a variable reference.
Comparer for grouping a SPARQL result set.
int Compare(ISparqlResultRecord x, ISparqlResultRecord y)
Compares two records.
GroupResultSet(ScriptNode[] GroupBy, ScriptNode[] Names)
Comparer for grouping a SPARQL result set.
Represents one record.
Definition: Record.cs:9
Collection of variables.
Definition: Variables.cs:25
Interface for semantic nodes.
Interface for result records of a SPARQL query.