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;
2using System.Collections.Generic;
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
46 public Task<bool> StartLedger()
47 {
48 return Task.FromResult(true);
49 }
50
55 public Task<bool> EndLedger()
56 {
57 return Task.FromResult(true);
58 }
59
65 public Task<bool> StartCollection(string CollectionName)
66 {
67 this.currentCollection = new Dictionary<string, IElement>()
68 {
69 { "Name", new StringValue(CollectionName) }
70 };
71
72 return Task.FromResult(true);
73 }
74
79 public Task<bool> EndCollection()
80 {
81 return Task.FromResult(true);
82 }
83
89 public Task<bool> StartBlock(string BlockID)
90 {
91 this.currentBlock = new Dictionary<string, IElement>()
92 {
93 { "Id", new StringValue(BlockID) }
94 };
95
96 return Task.FromResult(true);
97 }
98
105 public Task<bool> BlockMetaData(string Key, object Value)
106 {
107 this.currentBlock[Key] = Expression.Encapsulate(Value);
108 return Task.FromResult(true);
109 }
110
115 public Task<bool> EndBlock()
116 {
117 return Task.FromResult(true);
118 }
119
128 public Task<bool> StartEntry(string ObjectId, string TypeName, EntryType EntryType, DateTimeOffset EntryTimestamp)
129 {
130 this.propertiesInEvent = new Dictionary<string, IElement>();
131 this.currentEntry = new Dictionary<string, IElement>()
132 {
133 { "ObjectId", new ObjectValue(ObjectId) },
134 { "TypeName", new ObjectValue(TypeName) },
135 { "EntryType", new ObjectValue(EntryType) },
136 { "Timestamp", new ObjectValue(EntryTimestamp) },
137 { "this", new ObjectValue(this.propertiesInEvent) }
138 };
139
140 return Task.FromResult(true);
141 }
142
149 public Task<bool> ReportProperty(string PropertyName, object PropertyValue)
150 {
151 this.propertiesInEvent[PropertyName] = Expression.Encapsulate(PropertyValue);
152 return Task.FromResult(true);
153 }
154
159 public async Task<bool> EndEntry()
160 {
161 Dictionary<string, IElement> Data = new Dictionary<string, IElement>();
162
163 foreach (KeyValuePair<string, IElement> P in this.currentCollection)
164 Data[P.Key] = P.Value;
165
166 foreach (KeyValuePair<string, IElement> P in this.currentBlock)
167 Data[P.Key] = P.Value;
168
169 foreach (KeyValuePair<string, IElement> P in this.currentEntry)
170 Data[P.Key] = P.Value;
171
172 foreach (KeyValuePair<string, IElement> P in this.propertiesInEvent)
173 Data[P.Key] = P.Value;
174
175 try
176 {
177 this.arguments[0] = new ObjectValue(Data);
178
179 if (this.lambda.IsAsynchronous)
180 await this.lambda.EvaluateAsync(this.arguments, this.variables);
181 else
182 this.lambda.Evaluate(this.arguments, this.variables);
183
184 return true;
185 }
186 catch (Exception)
187 {
188 return false;
189 }
190 }
191
197 public Task<bool> CollectionCleared(DateTimeOffset EntryTimestamp)
198 {
199 return Task.FromResult(true);
200 }
201
207 public Task<bool> ReportError(string Message)
208 {
209 return Task.FromResult(true);
210 }
211
217 public Task<bool> ReportException(Exception Exception)
218 {
219 return Task.FromResult(true);
220 }
221 }
222}
Class managing a script expression.
Definition: Expression.cs:39
static IElement Encapsulate(object Value)
Encapsulates an object.
Definition: Expression.cs:4955
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 > StartLedger()
Is called when export of ledger is started.
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 > BlockMetaData(string Key, object Value)
Reports block meta-data.
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
Base interface for lambda expressions.
EntryType
Ledger entry type.
Definition: ILedgerEntry.cs:9