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;
3using System.Threading.Tasks;
11
13{
18 {
20 private readonly ChunkedList<KeyValuePair<string, ScriptNode>> additionalFields;
21 private readonly ScriptNode condition;
22 private readonly ObjectProperties properties = null;
23 private readonly Variables contextVariables;
24 private readonly Variables entryVariables;
25 private readonly Variable variableObjectId;
26 private readonly Variable variableTypeName;
27 private readonly Variable variableEntryType;
28 private readonly Variable variableTimestamp;
29 private readonly Variable variableBlockId;
30 private readonly Variable variableCollection;
31 private string currentObjectId;
32 private string currentTypeName;
33 private EntryType currentEntryType;
34 private DateTimeOffset currentEntryTimestamp;
35
44 ChunkedList<KeyValuePair<string, ScriptNode>> AdditionalFields)
45 : base(Output)
46 {
47 this.condition = Condition;
48 this.contextVariables = Variables;
49 this.additionalFields = AdditionalFields;
50
51 this.properties = new ObjectProperties(
52 new GenericObject(string.Empty, string.Empty, Guid.Empty),
53 this.contextVariables);
54
55 this.entryVariables = new Variables()
56 {
57 ContextVariables = this.properties,
58 ConsoleOut = Variables.ConsoleOut
59 };
60
61 this.variableObjectId = this.entryVariables.Add("ObjectId", this.currentObjectId);
62 this.variableTypeName = this.entryVariables.Add("TypeName", this.currentTypeName);
63 this.variableEntryType = this.entryVariables.Add("EntryType", this.currentEntryType);
64 this.variableTimestamp = this.entryVariables.Add("Timestamp", this.currentEntryTimestamp);
65 this.variableBlockId = this.entryVariables.Add("BlockId", this.StartedBlock);
66 this.variableCollection = this.entryVariables.Add("Collection", this.StartedCollection);
67 }
68
72 public Variables EntryVariables => this.entryVariables;
73
79 public override Task<bool> StartCollection(string CollectionName)
80 {
81 this.variableCollection.SetValue(CollectionName);
82
83 return base.StartCollection(CollectionName);
84 }
85
91 public override Task<bool> StartBlock(string BlockID)
92 {
93 this.variableBlockId.SetValue(BlockID);
94
95 return base.StartBlock(BlockID);
96 }
97
104 public override Task<bool> BlockMetaData(string Key, object Value)
105 {
106 this.entryVariables[Key] = Value;
107
108 return base.BlockMetaData(Key, Value);
109 }
110
119 public override Task<bool> StartEntry(string ObjectId, string TypeName, EntryType EntryType, DateTimeOffset EntryTimestamp)
120 {
121 this.currentObjectId = ObjectId;
122 this.currentTypeName = TypeName;
123 this.currentEntryType = EntryType;
124 this.currentEntryTimestamp = EntryTimestamp;
125
126 this.variableObjectId.SetValue(this.currentObjectId);
127 this.variableTypeName.SetValue(this.currentTypeName);
128 this.variableEntryType.SetValue(this.currentEntryType);
129 this.variableTimestamp.SetValue(this.currentEntryTimestamp);
130
131 this.currentProperties.Clear();
132
133 return Task.FromResult(true);
134 }
135
142 public override Task<bool> ReportProperty(string PropertyName, object PropertyValue)
143 {
144 this.currentProperties.Add(new KeyValuePair<string, object>(PropertyName, PropertyValue));
145 return Task.FromResult(true);
146 }
147
152 public override async Task<bool> EndEntry()
153 {
154 if (!Guid.TryParse(this.currentObjectId, out Guid ObjectId))
155 ObjectId = Guid.Empty;
156
157 this.properties.Object = new GenericObject(this.StartedCollection,
158 this.currentTypeName, ObjectId, this.currentProperties);
159
160 if (!(this.additionalFields is null))
161 {
162 foreach (KeyValuePair<string, ScriptNode> P in this.additionalFields)
163 {
164 IElement E;
165
166 try
167 {
168 if (P.Value.IsAsynchronous)
169 E = await P.Value.EvaluateAsync(this.entryVariables);
170 else
171 E = P.Value.Evaluate(this.entryVariables);
172 }
174 {
175 E = ex.ReturnValue;
176 //ScriptReturnValueException.Reuse(ex);
177 }
178 catch (ScriptBreakLoopException ex)
179 {
180 E = ex.LoopValue ?? ObjectValue.Null;
181 //ScriptBreakLoopException.Reuse(ex);
182 }
184 {
185 E = ex.LoopValue ?? ObjectValue.Null;
186 //ScriptContinueLoopException.Reuse(ex);
187 }
188 catch (Exception ex)
189 {
190 E = new ObjectValue(ex);
191 }
192
193 this.entryVariables[P.Key] = E;
194 this.currentProperties.Add(new KeyValuePair<string, object>(P.Key, E.AssociatedObjectValue));
195 }
196 }
197
198 if (!(this.condition is null))
199 {
200 try
201 {
202 IElement E;
203
204 if (this.condition.IsAsynchronous)
205 E = await this.condition.EvaluateAsync(this.entryVariables);
206 else
207 E = this.condition.Evaluate(this.entryVariables);
208
209 if (!(E.AssociatedObjectValue is bool B && B))
210 return true;
211 }
212 catch (Exception)
213 {
214 return true;
215 }
216 }
217
218 if (!await base.StartEntry(this.currentObjectId, this.currentTypeName, this.currentEntryType, this.currentEntryTimestamp))
219 return false;
220
221 foreach (KeyValuePair<string, object> P in this.currentProperties)
222 {
223 if (!await base.ReportProperty(P.Key, P.Value))
224 return false;
225 }
226
227 if (!await base.EndEntry())
228 return false;
229
230 return true;
231 }
232
241 public override bool IncludeEntry(string ObjectId, string TypeName, EntryType EntryType, DateTimeOffset EntryTimestamp)
242 {
243 return true;
244 }
245
250 public override bool IncludeNonEntryEvent()
251 {
252 return true;
253 }
254 }
255}
Generic object. Contains a sequence of properties.
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
static readonly ObjectValue Null
Null value.
Definition: ObjectValue.cs:88
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.
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.
ExportCondition(ILedgerExport Output, ScriptNode Condition, Variables Variables, ChunkedList< KeyValuePair< string, ScriptNode > > AdditionalFields)
Only exports entries matching a given condition (or conditions)
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:126
Basic interface for all types of elements.
Definition: IElement.cs:21
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:34
EntryType
Ledger entry type.
Definition: ILedgerEntry.cs:9