Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ExportToVariableTable.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
9
11{
16 {
17 private readonly LinkedList<KeyValuePair<string, IElement>> collectionProperties = new LinkedList<KeyValuePair<string, IElement>>();
18 private readonly LinkedList<KeyValuePair<string, IElement>> blockProperties = new LinkedList<KeyValuePair<string, IElement>>();
19 private readonly LinkedList<KeyValuePair<string, IElement>> entryProperties = new LinkedList<KeyValuePair<string, IElement>>();
20 private readonly LinkedList<IElement[]> rows = new LinkedList<IElement[]>();
21 private readonly List<IElement> currentRow = new List<IElement>();
22 private readonly List<string> headers = new List<string>();
23 private readonly Dictionary<string, int> headerOrder = new Dictionary<string, int>();
24 private int width;
25 private int height;
26 private Variables variables;
27
32 {
33 }
34
39 {
40 get => this.variables;
41 internal set => this.variables = value;
42 }
43
49 {
50 LinkedList<IElement> Elements = new LinkedList<IElement>();
51
52 foreach (IElement[] Row in this.rows)
53 {
54 foreach (IElement E in Row)
55 Elements.AddLast(E ?? ObjectValue.Null);
56
57 int Padding = this.width - Row.Length;
58 while (Padding > 0)
59 {
60 Elements.AddLast(ObjectValue.Null);
61 Padding--;
62 }
63 }
64
65 return new ObjectMatrix(this.height, this.width, Elements)
66 {
67 ColumnNames = this.headers.ToArray()
68 };
69 }
70
75 public Task<bool> StartLedger()
76 {
77 this.height = 0;
78 this.width = 0;
79
80 this.rows.Clear();
81 this.headerOrder.Clear();
82 this.headers.Clear();
83
84 return Task.FromResult(true);
85 }
86
91 public Task<bool> EndLedger()
92 {
93 return Task.FromResult(true);
94 }
95
101 public Task<bool> StartCollection(string CollectionName)
102 {
103 this.collectionProperties.Clear();
104 this.collectionProperties.AddLast(new KeyValuePair<string, IElement>("Collection", new StringValue(CollectionName)));
105
106 return Task.FromResult(true);
107 }
108
113 public Task<bool> EndCollection()
114 {
115 return Task.FromResult(true);
116 }
117
123 public Task<bool> StartBlock(string BlockID)
124 {
125 this.blockProperties.Clear();
126 this.blockProperties.AddLast(new KeyValuePair<string, IElement>("BlockId", new StringValue(BlockID)));
127
128 return Task.FromResult(true);
129 }
130
137 public Task<bool> BlockMetaData(string Key, object Value)
138 {
139 this.blockProperties.AddLast(new KeyValuePair<string, IElement>(Key, Expression.Encapsulate(Value)));
140 return Task.FromResult(true);
141 }
142
147 public Task<bool> EndBlock()
148 {
149 return Task.FromResult(true);
150 }
151
160 public Task<bool> StartEntry(string ObjectId, string TypeName, EntryType EntryType, DateTimeOffset EntryTimestamp)
161 {
162 this.entryProperties.Clear();
163 this.entryProperties.AddLast(new KeyValuePair<string, IElement>("ObjectId", new StringValue(ObjectId)));
164 this.entryProperties.AddLast(new KeyValuePair<string, IElement>("TypeName", new StringValue(TypeName)));
165 this.entryProperties.AddLast(new KeyValuePair<string, IElement>("EntryType", new ObjectValue(EntryType)));
166 this.entryProperties.AddLast(new KeyValuePair<string, IElement>("Timestamp", new ObjectValue(EntryTimestamp)));
167
168 return Task.FromResult(true);
169 }
170
177 public Task<bool> ReportProperty(string PropertyName, object PropertyValue)
178 {
179 this.entryProperties.AddLast(new KeyValuePair<string, IElement>(PropertyName, Expression.Encapsulate(PropertyValue)));
180 return Task.FromResult(true);
181 }
182
187 public Task<bool> EndEntry()
188 {
189 if (this.variables is null)
190 return Task.FromResult(false);
191
192 this.currentRow.Clear();
193
194 foreach (KeyValuePair<string, IElement> P in this.collectionProperties)
195 this.AddPropertyToRow(P.Key, P.Value);
196
197 foreach (KeyValuePair<string, IElement> P in this.blockProperties)
198 this.AddPropertyToRow(P.Key, P.Value);
199
200 foreach (KeyValuePair<string, IElement> P in this.entryProperties)
201 this.AddPropertyToRow(P.Key, P.Value);
202
203 this.rows.AddLast(this.currentRow.ToArray());
204 this.height++;
205
206 int c = this.currentRow.Count;
207 if (c > this.width)
208 this.width = c;
209
210 return Task.FromResult(true);
211 }
212
213 private void AddPropertyToRow(string Name, IElement Value)
214 {
215 if (!this.headerOrder.TryGetValue(Name, out int i))
216 {
217 i = this.headers.Count;
218 this.headerOrder[Name] = i;
219 this.headers.Add(Name);
220 }
221
222 int c = this.currentRow.Count;
223
224 if (c <= i)
225 {
226 while (c < i - 1)
227 {
228 this.currentRow.Add(null);
229 c++;
230 }
231
232 this.currentRow.Add(Value);
233 }
234 else
235 this.currentRow[i] = Value;
236 }
237
243 public Task<bool> CollectionCleared(DateTimeOffset EntryTimestamp)
244 {
245 return Task.FromResult(true);
246 }
247
253 public Task<bool> ReportError(string Message)
254 {
255 return Task.FromResult(true);
256 }
257
263 public Task<bool> ReportException(Exception Exception)
264 {
265 return Task.FromResult(true);
266 }
267 }
268}
Class managing a script expression.
Definition: Expression.cs:39
static IElement Encapsulate(object Value)
Encapsulates an object.
Definition: Expression.cs:4955
static readonly ObjectValue Null
Null value.
Definition: ObjectValue.cs:86
Export is serialized into table form, with an undefined number of columns.
Task< bool > ReportProperty(string PropertyName, object PropertyValue)
Is called when a property is reported.
Task< bool > ReportException(Exception Exception)
Is called when an exception has occurred.
Task< bool > CollectionCleared(DateTimeOffset EntryTimestamp)
Is called when the collection has been cleared.
Task< bool > EndEntry()
Is called when an entry is finished.
Task< bool > StartLedger()
Is called when export of ledger is started.
Task< bool > EndBlock()
Is called when a block in a collection is finished.
Task< bool > StartEntry(string ObjectId, string TypeName, EntryType EntryType, DateTimeOffset EntryTimestamp)
Is called when an entry is started.
ExportToVariableTable()
Export is serialized into table form, with an undefined number of columns.
Task< bool > EndLedger()
Is called when export of ledger is finished.
Task< bool > ReportError(string Message)
Is called when an error is reported.
Task< bool > EndCollection()
Is called when a collection is finished.
Task< bool > BlockMetaData(string Key, object Value)
Reports block meta-data.
Task< bool > StartBlock(string BlockID)
Is called when a block in a collection is started.
ObjectMatrix ToMatrix()
Converts the exported information into an object matrix with column headers.
Task< bool > StartCollection(string CollectionName)
Is called when a collection is started.
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
EntryType
Ledger entry type.
Definition: ILedgerEntry.cs:9