Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
RecordObjects.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
12
14{
19 {
20 private readonly RecordOperation operation;
21 private SourceDefinition source;
22 private ElementList objects;
23
34 ElementList Objects, int Start, int Length, Expression Expression)
35 : base(Start, Length, Expression)
36 {
37 this.source = Source;
38 this.source?.SetParent(this);
39
40 this.objects = Objects;
41 this.objects?.SetParent(this);
42
43 this.operation = Operation;
44 }
45
50 public override bool IsAsynchronous => true;
51
58 {
59 return this.EvaluateAsync(Variables).Result;
60 }
61
68 public override async Task<IElement> EvaluateAsync(Variables Variables)
69 {
70 IDataSource Source = await this.source.GetSource(Variables);
71 IEnumerable<IElement> Objects;
72 IElement E;
73 long Count = 0;
74 object Item;
75
76 foreach (ScriptNode Object in this.objects.Elements)
77 {
78 E = await Object.EvaluateAsync(Variables);
79 if (E is IVector V)
80 Objects = V.ChildElements;
81 else if (E is ISet S)
82 Objects = S.ChildElements;
83 else
84 Objects = new IElement[] { E };
85
86 foreach (IElement E2 in Objects)
87 {
88 Item = E2.AssociatedObjectValue;
89
90 if (Item is Dictionary<string, IElement> ObjExNihilo)
91 {
92 GenericObject Obj2 = new GenericObject(Source.CollectionName, Source.TypeName, Guid.Empty);
93
94 foreach (KeyValuePair<string, IElement> P in ObjExNihilo)
95 Obj2[P.Key] = P.Value.AssociatedObjectValue;
96
97 Item = Obj2;
98 }
99 else if (Item is Dictionary<string, object> ObjExNihilo2)
100 {
101 GenericObject Obj2 = new GenericObject(Source.CollectionName, Source.TypeName, Guid.Empty);
102
103 foreach (KeyValuePair<string, object> P in ObjExNihilo2)
104 Obj2[P.Key] = P.Value;
105
106 Item = Obj2;
107 }
108
109 switch (this.operation)
110 {
111 case RecordOperation.New:
112 default:
113 await Ledger.NewEntry(Item);
114 break;
115
116 case RecordOperation.Update:
117 await Ledger.UpdatedEntry(Item);
118 break;
119
120 case RecordOperation.Delete:
121 await Ledger.DeletedEntry(Item);
122 break;
123 }
124 Count++;
125 }
126 }
127
128 return new DoubleNumber(Count);
129 }
130
138 public override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
139 {
140 if (Order == SearchMethod.DepthFirst)
141 {
142 if (!(this.source?.ForAllChildNodes(Callback, State, Order) ?? true))
143 return false;
144
145 if (!(this.objects?.ForAllChildNodes(Callback, State, Order) ?? true))
146 return false;
147 }
148
149 ScriptNode NewNode;
150 bool b;
151
152 if (!(this.source is null))
153 {
154 b = !Callback(this.source, out NewNode, State);
155 if (!(NewNode is null) && NewNode is SourceDefinition Source2)
156 {
157 this.source = Source2;
158 this.source.SetParent(this);
159 }
160
161 if (b || (Order == SearchMethod.TreeOrder && !this.source.ForAllChildNodes(Callback, State, Order)))
162 return false;
163 }
164
165 if (!(this.objects is null))
166 {
167 b = !Callback(this.objects, out NewNode, State);
168 if (!(NewNode is null) && NewNode is ElementList NewObjects)
169 {
170 this.objects = NewObjects;
171 this.objects.SetParent(this);
172 }
173
174 if (b || (Order == SearchMethod.TreeOrder && !this.objects.ForAllChildNodes(Callback, State, Order)))
175 return false;
176 }
177
178 if (Order == SearchMethod.BreadthFirst)
179 {
180 if (!(this.source?.ForAllChildNodes(Callback, State, Order) ?? true))
181 return false;
182
183 if (!(this.objects?.ForAllChildNodes(Callback, State, Order) ?? true))
184 return false;
185 }
186
187 return true;
188 }
189
191 public override bool Equals(object obj)
192 {
193 return obj is RecordObjects O &&
194 this.operation == O.operation &&
195 AreEqual(this.source, O.source) &&
196 AreEqual(this.objects, O.objects) &&
197 base.Equals(obj);
198 }
199
201 public override int GetHashCode()
202 {
203 int Result = base.GetHashCode();
204 Result ^= Result << 5 ^ this.operation.GetHashCode();
205 Result ^= Result << 5 ^ GetHashCode(this.source);
206 Result ^= Result << 5 ^ GetHashCode(this.objects);
207 return Result;
208 }
209
210 }
211}
Static interface for ledger persistence. In order to work, a ledger provider has to be assigned to it...
Definition: Ledger.cs:14
static async Task DeletedEntry(object Object)
Deletes an entry in the ledger.
Definition: Ledger.cs:180
static async Task NewEntry(object Object)
Adds an entry to the ledger.
Definition: Ledger.cs:118
static async Task UpdatedEntry(object Object)
Updates an entry in the ledger.
Definition: Ledger.cs:149
Generic object. Contains a sequence of properties.
Class managing a script expression.
Definition: Expression.cs:39
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
static bool AreEqual(ScriptNode S1, ScriptNode S2)
Compares if two script nodes are equal.
Definition: ScriptNode.cs:275
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
void SetParent(ScriptNode Parent)
Sets the parent node. Can only be used when expression is being parsed.
Definition: ScriptNode.cs:132
virtual Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection. This method should be ...
Definition: ScriptNode.cs:158
Represents a list of elements.
Definition: ElementList.cs:15
ScriptNode[] Elements
Elements.
Definition: ElementList.cs:59
Executes an RECORD ... OBJECT[S] ... statement against the ledger.
RecordObjects(SourceDefinition Source, RecordOperation Operation, ElementList Objects, int Start, int Length, Expression Expression)
Executes an RECORD ... OBJECT[S] ... statement against the ledger.
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node asynchronously, using the variables provided in the Variables collection.
override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
Abstract base class for source definitions
abstract Task< IDataSource > GetSource(Variables Variables)
Gets the actual data source, from its definition.
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33
Basic interface for vectors.
Definition: IVector.cs:9
Basic interface for all types of sets.
Definition: ISet.cs:10
Interface for script nodes with asynchronous evaluation
Interface for data sources that can be used in SQL statements.
Definition: IDataSource.cs:13
delegate bool ScriptNodeEventHandler(ScriptNode Node, out ScriptNode NewNode, object State)
Delegate for ScriptNode callback methods.
SearchMethod
Method to traverse the expression structure
Definition: ScriptNode.cs:38
RecordOperation
Ledger operation to record.
Definition: RecordParser.cs:10