Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ExportCondition.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
10
12{
17 {
18 private readonly LinkedList<KeyValuePair<string, object>> currentProperties = new LinkedList<KeyValuePair<string, object>>();
19 private readonly List<KeyValuePair<string, ScriptNode>> additionalFields;
20 private readonly ScriptNode condition;
21 private readonly ObjectProperties properties = null;
22 private readonly Variables contextVariables;
23 private readonly Variables entryVariables;
24 private readonly Variable variableObjectId;
25 private readonly Variable variableTypeName;
26 private readonly Variable variableEntryType;
27 private readonly Variable variableTimestamp;
28 private readonly Variable variableBlockId;
29 private readonly Variable variableCollection;
30 private string currentObjectId;
31 private string currentTypeName;
32 private EntryType currentEntryType;
33 private DateTimeOffset currentEntryTimestamp;
34
43 List<KeyValuePair<string, ScriptNode>> AdditionalFields)
44 : base(Output)
45 {
46 this.condition = Condition;
47 this.contextVariables = Variables;
48 this.additionalFields = AdditionalFields;
49
50 this.properties = new ObjectProperties(
51 new GenericObject(string.Empty, string.Empty, Guid.Empty),
52 this.contextVariables);
53
54 this.entryVariables = new Variables()
55 {
56 ContextVariables = this.properties,
57 ConsoleOut = Variables.ConsoleOut
58 };
59
60 this.variableObjectId = this.entryVariables.Add("ObjectId", this.currentObjectId);
61 this.variableTypeName = this.entryVariables.Add("TypeName", this.currentTypeName);
62 this.variableEntryType = this.entryVariables.Add("EntryType", this.currentEntryType);
63 this.variableTimestamp = this.entryVariables.Add("Timestamp", this.currentEntryTimestamp);
64 this.variableBlockId = this.entryVariables.Add("BlockId", this.StartedBlock);
65 this.variableCollection = this.entryVariables.Add("Collection", this.StartedCollection);
66 }
67
71 public Variables EntryVariables => this.entryVariables;
72
78 public override Task<bool> StartCollection(string CollectionName)
79 {
80 this.variableCollection.SetValue(CollectionName);
81
82 return base.StartCollection(CollectionName);
83 }
84
90 public override Task<bool> StartBlock(string BlockID)
91 {
92 this.variableBlockId.SetValue(BlockID);
93
94 return base.StartBlock(BlockID);
95 }
96
103 public override Task<bool> BlockMetaData(string Key, object Value)
104 {
105 this.entryVariables[Key] = Value;
106
107 return base.BlockMetaData(Key, Value);
108 }
109
118 public override Task<bool> StartEntry(string ObjectId, string TypeName, EntryType EntryType, DateTimeOffset EntryTimestamp)
119 {
120 this.currentObjectId = ObjectId;
121 this.currentTypeName = TypeName;
122 this.currentEntryType = EntryType;
123 this.currentEntryTimestamp = EntryTimestamp;
124
125 this.variableObjectId.SetValue(this.currentObjectId);
126 this.variableTypeName.SetValue(this.currentTypeName);
127 this.variableEntryType.SetValue(this.currentEntryType);
128 this.variableTimestamp.SetValue(this.currentEntryTimestamp);
129
130 this.currentProperties.Clear();
131
132 return Task.FromResult(true);
133 }
134
141 public override Task<bool> ReportProperty(string PropertyName, object PropertyValue)
142 {
143 this.currentProperties.AddLast(new KeyValuePair<string, object>(PropertyName, PropertyValue));
144 return Task.FromResult(true);
145 }
146
151 public override async Task<bool> EndEntry()
152 {
153 if (!Guid.TryParse(this.currentObjectId, out Guid ObjectId))
154 ObjectId = Guid.Empty;
155
156 this.properties.Object = new GenericObject(this.StartedCollection,
157 this.currentTypeName, ObjectId, this.currentProperties);
158
159 if (!(this.additionalFields is null))
160 {
161 foreach (KeyValuePair<string, ScriptNode> P in this.additionalFields)
162 {
163 IElement E;
164
165 try
166 {
167 if (P.Value.IsAsynchronous)
168 E = await P.Value.EvaluateAsync(this.entryVariables);
169 else
170 E = P.Value.Evaluate(this.entryVariables);
171 }
173 {
174 E = ex.ReturnValue;
175 }
176 catch (Exception ex)
177 {
178 E = new ObjectValue(ex);
179 }
180
181 this.entryVariables[P.Key] = E;
182 this.currentProperties.AddLast(new KeyValuePair<string, object>(P.Key, E.AssociatedObjectValue));
183 }
184 }
185
186 if (!(this.condition is null))
187 {
188 try
189 {
190 IElement E;
191
192 if (this.condition.IsAsynchronous)
193 E = await this.condition.EvaluateAsync(this.entryVariables);
194 else
195 E = this.condition.Evaluate(this.entryVariables);
196
197 if (!(E.AssociatedObjectValue is bool B && B))
198 return true;
199 }
200 catch (Exception)
201 {
202 return true;
203 }
204 }
205
206 if (!await base.StartEntry(this.currentObjectId, this.currentTypeName, this.currentEntryType, this.currentEntryTimestamp))
207 return false;
208
209 foreach (KeyValuePair<string, object> P in this.currentProperties)
210 {
211 if (!await base.ReportProperty(P.Key, P.Value))
212 return false;
213 }
214
215 if (!await base.EndEntry())
216 return false;
217
218 return true;
219 }
220
229 public override bool IncludeEntry(string ObjectId, string TypeName, EntryType EntryType, DateTimeOffset EntryTimestamp)
230 {
231 return true;
232 }
233
238 public override bool IncludeNonEntryEvent()
239 {
240 return true;
241 }
242 }
243}
Generic object. Contains a sequence of properties.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
Abstract base class for ledger exports with custom entry filter rules.
string StartedBlock
Block that is currently being processed.
string StartedCollection
Collection that is currently being processed.
Only exports entries matching a given condition (or conditions)
override Task< bool > StartCollection(string CollectionName)
Is called when a collection is started.
override Task< bool > StartBlock(string BlockID)
Is called when a block in a collection is started.
ExportCondition(ILedgerExport Output, ScriptNode Condition, Variables Variables, List< KeyValuePair< string, ScriptNode > > AdditionalFields)
Only exports entries matching a given condition (or conditions)
override Task< bool > ReportProperty(string PropertyName, object PropertyValue)
Is called when a property is reported.
override async Task< bool > EndEntry()
Is called when an entry is finished.
override Task< bool > BlockMetaData(string Key, object Value)
Reports block meta-data.
override bool IncludeNonEntryEvent()
If a non-entry event should be included.
override Task< bool > StartEntry(string ObjectId, string TypeName, EntryType EntryType, DateTimeOffset EntryTimestamp)
Is called when an entry is started.
override bool IncludeEntry(string ObjectId, string TypeName, EntryType EntryType, DateTimeOffset EntryTimestamp)
If an entry should be included.
Contains information about a variable.
Definition: Variable.cs:10
Collection of variables.
Definition: Variables.cs:25
virtual Variable Add(string Name, object Value)
Adds a variable to the collection.
Definition: Variables.cs:122
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33
EntryType
Ledger entry type.
Definition: ILedgerEntry.cs:9