Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GroupObject.cs
1using System;
2using System.Collections.Generic;
3using System.Numerics;
6
8{
12 public class GroupObject
13 {
14 private readonly object[] objects;
15 private readonly Variables variables;
16 private readonly Dictionary<string, object> groupedValues = new Dictionary<string, object>();
17 private readonly int objectCount;
18 private readonly int groupCount;
19 private ObjectProperties properties = null;
20
28 public GroupObject(object[] Objects, object[] GroupByValues, ScriptNode[] GroupNames, Variables Variables)
29 {
30 this.objects = Objects;
31 this.variables = Variables;
32
33 int i;
34
35 this.objectCount = this.objects.Length;
36 this.groupCount = GroupNames.Length;
37 if (GroupByValues.Length != this.groupCount)
38 throw new ArgumentException("Length mismatch.", nameof(GroupByValues));
39
40 string Name;
41 ScriptNode Node;
42 IElement E;
43
44 for (i = 0; i < this.groupCount; i++)
45 {
46 Node = GroupNames[i];
47 if (Node is null)
48 continue;
49
50 if (Node is VariableReference Ref)
51 Name = Ref.VariableName;
52 else
53 {
54 E = Node.Evaluate(Variables);
55 Name = E.AssociatedObjectValue?.ToString();
56 }
57
58 if (!string.IsNullOrEmpty(Name))
59 this.groupedValues[Name] = GroupByValues[i];
60 }
61 }
62
68 public object this[string Name]
69 {
70 get
71 {
72 if (this.groupedValues.TryGetValue(Name, out object Value))
73 return Value;
74
75 if (this.objectCount == 1)
76 {
77 object Result;
78 object Item = this.objects[0];
79
80 if (Item is null)
81 Result = null;
82 else
83 {
84 if (this.properties is null)
85 this.properties = new ObjectProperties(Item, this.variables);
86 else
87 this.properties.Object = Item;
88
89 if (this.properties.TryGetValue(Name, out Value))
90 Result = Value;
91 else
92 Result = null;
93 }
94
95 this.groupedValues[Name] = Result;
96
97 return Result;
98 }
99 else
100 {
101 Type ItemType = null;
102 Type T;
103 object[] Result = new object[this.objectCount];
104 object Item;
105 int i;
106
107 for (i = 0; i < this.objectCount; i++)
108 {
109 Item = this.objects[i];
110
111 if (Item is null)
112 {
113 Result[i] = null;
114 ItemType = null;
115 }
116 else
117 {
118 if (this.properties is null)
119 this.properties = new ObjectProperties(Item, this.variables);
120 else
121 this.properties.Object = Item;
122
123 if (this.properties.TryGetValue(Name, out Value) && !(Value is null))
124 {
125 T = Value.GetType();
126 if (i == 0)
127 ItemType = T;
128 else if (T != ItemType)
129 ItemType = null;
130
131 Result[i] = Value;
132 }
133 else
134 {
135 Result[i] = null;
136 ItemType = null;
137 }
138 }
139 }
140
141 if (ItemType == typeof(double))
142 {
143 double[] TypedArray = new double[this.objectCount];
144 Array.Copy(Result, 0, TypedArray, 0, this.objectCount);
145 return TypedArray;
146 }
147 else if (ItemType == typeof(bool))
148 {
149 bool[] TypedArray = new bool[this.objectCount];
150 Array.Copy(Result, 0, TypedArray, 0, this.objectCount);
151 return TypedArray;
152 }
153 else if (ItemType == typeof(string))
154 {
155 string[] TypedArray = new string[this.objectCount];
156 Array.Copy(Result, 0, TypedArray, 0, this.objectCount);
157 return TypedArray;
158 }
159 else if (ItemType == typeof(DateTime))
160 {
161 DateTime[] TypedArray = new DateTime[this.objectCount];
162 Array.Copy(Result, 0, TypedArray, 0, this.objectCount);
163 return TypedArray;
164 }
165 else if (ItemType == typeof(Complex))
166 {
167 Complex[] TypedArray = new Complex[this.objectCount];
168 Array.Copy(Result, 0, TypedArray, 0, this.objectCount);
169 return TypedArray;
170 }
171 else
172 {
173 this.groupedValues[Name] = Result;
174 return Result;
175 }
176 }
177 }
178
179 set
180 {
181 this.groupedValues[Name] = value;
182 }
183 }
184
188 public int Count => this.objects.Length;
189
193 public object[] Objects => this.objects;
194
195 }
196}
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
abstract IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection. This method should be ...
Represents a variable reference.
Represents a collection of objects grouped together useing a GROUP BY construct.
Definition: GroupObject.cs:13
GroupObject(object[] Objects, object[] GroupByValues, ScriptNode[] GroupNames, Variables Variables)
Represents a collection of objects grouped together useing a GROUP BY construct.
Definition: GroupObject.cs:28
int Count
Number of objects in group.
Definition: GroupObject.cs:188
bool TryGetValue(string Name, out object Value)
Tries to get the value, given its variable name.
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33