Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
OrderResultSet.cs
1using System;
2using System.Collections.Generic;
9
11{
15 public class OrderResultSet : IComparer<ISparqlResultRecord>
16 {
17 private readonly string[] variables;
18 private readonly ScriptNode[] expression;
19 private readonly bool[] ascending;
20 private readonly bool[] calculated;
21 private readonly int count;
22
29 public OrderResultSet(KeyValuePair<ScriptNode, bool>[] OrderBy)
30 {
31 ScriptNode Node;
32 int i;
33
34 this.count = OrderBy.Length;
35 this.variables = new string[this.count];
36 this.expression = new ScriptNode[this.count];
37 this.ascending = new bool[this.count];
38 this.calculated = new bool[this.count];
39
40 for (i = 0; i < this.count; i++)
41 {
42 this.expression[i] = Node = OrderBy[i].Key;
43 this.ascending[i] = OrderBy[i].Value;
44
45 if (Node is VariableReference Ref)
46 {
47 this.variables[i] = Ref.VariableName;
48 this.calculated[i] = false;
49 }
50 else
51 this.calculated[i] = true;
52 }
53 }
54
66 {
67 ISemanticElement e1, e2;
68 int i, j;
69
70 for (i = 0; i < this.count; i++)
71 {
72 if (this.calculated[i])
73 {
74 Variables v = new Variables();
77
78 e1 = EvaluateElement(this.expression[i], v1);
79 e2 = EvaluateElement(this.expression[i], v2);
80 }
81 else
82 {
83 e1 = x[this.variables[i]];
84 e2 = y[this.variables[i]];
85 }
86
87 if (e1 is null)
88 {
89 if (!(e2 is null))
90 return -1;
91 }
92 else if (e2 is null)
93 return 1;
94 else
95 {
96 j = e1.CompareTo(e2);
97 if (j != 0)
98 return this.ascending[i] ? j : -j;
99 }
100 }
101
102 return 0;
103 }
104
105 internal static ISemanticElement EvaluateElement(ScriptNode Expression, Variables Variables)
106 {
107 object Obj;
108
109 try
110 {
111 Obj = Expression.Evaluate(Variables)?.AssociatedObjectValue;
112 }
114 {
115 Obj = ex.ReturnValue.AssociatedObjectValue;
116 }
117 catch (Exception ex)
118 {
119 return new StringLiteral(ex.Message);
120 }
121
122 return SemanticElements.Encapsulate(Obj);
123 }
124 }
125}
static ISemanticElement Encapsulate(object Value)
Encapsulates an object as a semantic element.
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 ordering a SPARQL result set.
int Compare(ISparqlResultRecord x, ISparqlResultRecord y)
Compares two records.
OrderResultSet(KeyValuePair< ScriptNode, bool >[] OrderBy)
Comparer for ordering a SPARQL result set.
Collection of variables.
Definition: Variables.cs:25
Interface for semantic nodes.
Interface for result records of a SPARQL query.