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;
3using System.Threading.Tasks;
10
12{
17 {
21 private readonly ChunkedList<IElement[]> rows = new ChunkedList<IElement[]>();
22 private readonly ChunkedList<IElement> currentRow = new ChunkedList<IElement>();
23 private readonly ChunkedList<string> headers = new ChunkedList<string>();
24 private readonly Dictionary<string, int> headerOrder = new Dictionary<string, int>();
25 private int width;
26 private int height;
27 private Variables variables;
28
33 {
34 }
35
40 {
41 get => this.variables;
42 internal set => this.variables = value;
43 }
44
50 {
52
53 foreach (IElement[] Row in this.rows)
54 {
55 foreach (IElement E in Row)
56 Elements.Add(E ?? ObjectValue.Null);
57
58 int Padding = this.width - Row.Length;
59
60 while (Padding > 0)
61 {
62 Elements.Add(ObjectValue.Null);
63 Padding--;
64 }
65 }
66
67 return new ObjectMatrix(this.height, this.width, Elements)
68 {
69 ColumnNames = this.headers.ToArray()
70 };
71 }
72
78 public Task<bool> StartLedger(ILedgerProvider Provider)
79 {
80 this.height = 0;
81 this.width = 0;
82
83 this.rows.Clear();
84 this.headerOrder.Clear();
85 this.headers.Clear();
86
87 return Task.FromResult(true);
88 }
89
94 public Task<bool> EndLedger()
95 {
96 return Task.FromResult(true);
97 }
98
104 public Task<bool> StartCollection(string CollectionName)
105 {
106 this.collectionProperties.Clear();
107 this.collectionProperties.Add(new KeyValuePair<string, IElement>("Collection", new StringValue(CollectionName)));
108
109 return Task.FromResult(true);
110 }
111
116 public Task<bool> EndCollection()
117 {
118 return Task.FromResult(true);
119 }
120
126 public Task<bool> StartBlock(string BlockID)
127 {
128 this.blockProperties.Clear();
129 this.blockProperties.Add(new KeyValuePair<string, IElement>("BlockId", new StringValue(BlockID)));
130
131 return Task.FromResult(true);
132 }
133
140 public Task<bool> BlockMetaData(string Key, object Value)
141 {
142 this.blockProperties.Add(new KeyValuePair<string, IElement>(Key, Expression.Encapsulate(Value)));
143 return Task.FromResult(true);
144 }
145
150 public Task<bool> EndBlock()
151 {
152 return Task.FromResult(true);
153 }
154
163 public Task<bool> StartEntry(string ObjectId, string TypeName, EntryType EntryType, DateTimeOffset EntryTimestamp)
164 {
165 this.entryProperties.Clear();
166 this.entryProperties.Add(new KeyValuePair<string, IElement>("ObjectId", new StringValue(ObjectId)));
167 this.entryProperties.Add(new KeyValuePair<string, IElement>("TypeName", new StringValue(TypeName)));
168 this.entryProperties.Add(new KeyValuePair<string, IElement>("EntryType", new ObjectValue(EntryType)));
169 this.entryProperties.Add(new KeyValuePair<string, IElement>("Timestamp", new ObjectValue(EntryTimestamp)));
170
171 return Task.FromResult(true);
172 }
173
180 public Task<bool> ReportProperty(string PropertyName, object PropertyValue)
181 {
182 this.entryProperties.Add(new KeyValuePair<string, IElement>(PropertyName, Expression.Encapsulate(PropertyValue)));
183 return Task.FromResult(true);
184 }
185
190 public Task<bool> EndEntry()
191 {
192 if (this.variables is null)
193 return Task.FromResult(false);
194
195 this.currentRow.Clear();
196
197 foreach (KeyValuePair<string, IElement> P in this.collectionProperties)
198 this.AddPropertyToRow(P.Key, P.Value);
199
200 foreach (KeyValuePair<string, IElement> P in this.blockProperties)
201 this.AddPropertyToRow(P.Key, P.Value);
202
203 foreach (KeyValuePair<string, IElement> P in this.entryProperties)
204 this.AddPropertyToRow(P.Key, P.Value);
205
206 this.rows.Add(this.currentRow.ToArray());
207 this.height++;
208
209 int c = this.currentRow.Count;
210 if (c > this.width)
211 this.width = c;
212
213 return Task.FromResult(true);
214 }
215
216 private void AddPropertyToRow(string Name, IElement Value)
217 {
218 if (!this.headerOrder.TryGetValue(Name, out int i))
219 {
220 i = this.headers.Count;
221 this.headerOrder[Name] = i;
222 this.headers.Add(Name);
223 }
224
225 int c = this.currentRow.Count;
226
227 if (c <= i)
228 {
229 while (c < i - 1)
230 {
231 this.currentRow.Add(null);
232 c++;
233 }
234
235 this.currentRow.Add(Value);
236 }
237 else
238 this.currentRow[i] = Value;
239 }
240
246 public Task<bool> CollectionCleared(DateTimeOffset EntryTimestamp)
247 {
248 return Task.FromResult(true);
249 }
250
256 public Task<bool> ReportError(string Message)
257 {
258 return Task.FromResult(true);
259 }
260
266 public Task<bool> ReportException(Exception Exception)
267 {
268 return Task.FromResult(true);
269 }
270 }
271}
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
void Clear()
Clears the collection.
Definition: ChunkedList.cs:306
void Add(T Item)
Adds an item to the collection.
Definition: ChunkedList.cs:272
Class managing a script expression.
Definition: Expression.cs:41
static IElement Encapsulate(object Value)
Encapsulates an object.
Definition: Expression.cs:5241
static readonly ObjectValue Null
Null value.
Definition: ObjectValue.cs:88
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(ILedgerProvider Provider)
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
Interface for ledger providers that can be plugged into the static Ledger class.
Basic interface for all types of elements.
Definition: IElement.cs:21
EntryType
Ledger entry type.
Definition: ILedgerEntry.cs:9