Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ExportToLambda.cs
1using System;
3using System.Threading.Tasks;
9
11{
16 {
17 private readonly ILambdaExpression lambda;
18 private readonly IElement[] arguments = new IElement[] { ObjectValue.Null };
19 private Dictionary<string, IElement> currentCollection = null;
20 private Dictionary<string, IElement> currentBlock = null;
21 private Dictionary<string, IElement> currentEntry = null;
22 private Dictionary<string, IElement> propertiesInEvent = null;
23 private Variables variables;
24
29 {
30 this.lambda = Lambda;
31 }
32
37 {
38 get => this.variables;
39 internal set => this.variables = value;
40 }
41
47 public Task<bool> StartLedger(ILedgerProvider Provider)
48 {
49 return Task.FromResult(true);
50 }
51
56 public Task<bool> EndLedger()
57 {
58 return Task.FromResult(true);
59 }
60
66 public Task<bool> StartCollection(string CollectionName)
67 {
68 this.currentCollection = new Dictionary<string, IElement>()
69 {
70 { "Name", new StringValue(CollectionName) }
71 };
72
73 return Task.FromResult(true);
74 }
75
80 public Task<bool> EndCollection()
81 {
82 return Task.FromResult(true);
83 }
84
90 public Task<bool> StartBlock(string BlockID)
91 {
92 this.currentBlock = new Dictionary<string, IElement>()
93 {
94 { "Id", new StringValue(BlockID) }
95 };
96
97 return Task.FromResult(true);
98 }
99
106 public Task<bool> BlockMetaData(string Key, object Value)
107 {
108 this.currentBlock[Key] = Expression.Encapsulate(Value);
109 return Task.FromResult(true);
110 }
111
116 public Task<bool> EndBlock()
117 {
118 return Task.FromResult(true);
119 }
120
129 public Task<bool> StartEntry(string ObjectId, string TypeName, EntryType EntryType, DateTimeOffset EntryTimestamp)
130 {
131 this.propertiesInEvent = new Dictionary<string, IElement>();
132 this.currentEntry = new Dictionary<string, IElement>()
133 {
134 { "ObjectId", new ObjectValue(ObjectId) },
135 { "TypeName", new ObjectValue(TypeName) },
136 { "EntryType", new ObjectValue(EntryType) },
137 { "Timestamp", new ObjectValue(EntryTimestamp) },
138 { "this", new ObjectValue(this.propertiesInEvent) }
139 };
140
141 return Task.FromResult(true);
142 }
143
150 public Task<bool> ReportProperty(string PropertyName, object PropertyValue)
151 {
152 this.propertiesInEvent[PropertyName] = Expression.Encapsulate(PropertyValue);
153 return Task.FromResult(true);
154 }
155
160 public async Task<bool> EndEntry()
161 {
162 Dictionary<string, IElement> Data = new Dictionary<string, IElement>();
163
164 foreach (KeyValuePair<string, IElement> P in this.currentCollection)
165 Data[P.Key] = P.Value;
166
167 foreach (KeyValuePair<string, IElement> P in this.currentBlock)
168 Data[P.Key] = P.Value;
169
170 foreach (KeyValuePair<string, IElement> P in this.currentEntry)
171 Data[P.Key] = P.Value;
172
173 foreach (KeyValuePair<string, IElement> P in this.propertiesInEvent)
174 Data[P.Key] = P.Value;
175
176 try
177 {
178 this.arguments[0] = new ObjectValue(Data);
179
180 if (this.lambda.IsAsynchronous)
181 await this.lambda.EvaluateAsync(this.arguments, this.variables);
182 else
183 this.lambda.Evaluate(this.arguments, this.variables);
184
185 return true;
186 }
187 catch (Exception)
188 {
189 return false;
190 }
191 }
192
198 public Task<bool> CollectionCleared(DateTimeOffset EntryTimestamp)
199 {
200 return Task.FromResult(true);
201 }
202
208 public Task<bool> ReportError(string Message)
209 {
210 return Task.FromResult(true);
211 }
212
218 public Task<bool> ReportException(Exception Exception)
219 {
220 return Task.FromResult(true);
221 }
222 }
223}
Class managing a script expression.
Definition: Expression.cs:41
static IElement Encapsulate(object Value)
Encapsulates an object.
Definition: Expression.cs:5241
Export is serialized into calls to a lambda expression having one argument.
Task< bool > ReportProperty(string PropertyName, object PropertyValue)
Is called when a property is reported.
async Task< bool > EndEntry()
Is called when an entry is finished.
Task< bool > StartBlock(string BlockID)
Is called when a block in a collection is started.
Task< bool > CollectionCleared(DateTimeOffset EntryTimestamp)
Is called when the collection has been cleared.
Task< bool > StartEntry(string ObjectId, string TypeName, EntryType EntryType, DateTimeOffset EntryTimestamp)
Is called when an entry is started.
Task< bool > EndLedger()
Is called when export of ledger is finished.
Task< bool > EndCollection()
Is called when a collection is finished.
Task< bool > ReportException(Exception Exception)
Is called when an exception has occurred.
Task< bool > StartCollection(string CollectionName)
Is called when a collection is started.
Task< bool > ReportError(string Message)
Is called when an error is reported.
ExportToLambda(ILambdaExpression Lambda)
Export is serialized into calls to a lambda expression having one argument.
Task< bool > EndBlock()
Is called when a block in a collection is finished.
Task< bool > StartLedger(ILedgerProvider Provider)
Is called when export of ledger is started.
Task< bool > BlockMetaData(string Key, object Value)
Reports block meta-data.
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
Base interface for lambda expressions.
EntryType
Ledger entry type.
Definition: ILedgerEntry.cs:9