Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ContractEventNode.cs
1using System;
2using System.Text;
3using System.Threading.Tasks;
4using System.Xml;
12
14{
18 public abstract class ContractEventNode : EventNode
19 {
20 private ScriptableStringAttribute contractIdVariable;
21 private ScriptableStringAttribute contractXmlVariable;
22 private ScriptableStringAttribute parametersVariable;
23 private ScriptableStringAttribute rolesVariable;
24 private ScriptableStringAttribute machineReadableVariable;
25 private ScriptableStringAttribute localName;
26 private ScriptableStringAttribute @namespace;
28 private ExpressionAttribute condition;
29
34 : base()
35 {
36 }
37
41 [DefaultValueNull]
43 {
44 get => this.contractIdVariable?.Definition;
45 set => this.contractIdVariable = new ScriptableStringAttribute(value, this);
46 }
47
51 [DefaultValueNull]
53 {
54 get => this.contractXmlVariable?.Definition;
55 set => this.contractXmlVariable = new ScriptableStringAttribute(value, this);
56 }
57
61 [DefaultValueNull]
63 {
64 get => this.parametersVariable?.Definition;
65 set => this.parametersVariable = new ScriptableStringAttribute(value, this);
66 }
67
71 [DefaultValueNull]
73 {
74 get => this.rolesVariable?.Definition;
75 set => this.rolesVariable = new ScriptableStringAttribute(value, this);
76 }
77
81 [DefaultValueNull]
83 {
84 get => this.machineReadableVariable?.Definition;
85 set => this.machineReadableVariable = new ScriptableStringAttribute(value, this);
86 }
87
91 [DefaultValueNull]
92 public string Condition
93 {
94 get => this.condition?.Expression;
95 set => this.condition = new ExpressionAttribute(value, false, this);
96 }
97
101 [DefaultValueNull]
103 {
104 get => this.localName?.Definition;
105 set => this.localName = new ScriptableStringAttribute(value, this);
106 }
107
111 [DefaultValueNull]
113 {
114 get => this.@namespace?.Definition;
115 set => this.@namespace = new ScriptableStringAttribute(value, this);
116 }
117
121 [DefaultValueNull]
122 public string NameTypeDefinition
123 {
124 get => this.nameType?.Definition;
125 set => this.nameType = new ScriptableEnumAttribute<SecondaryNameType>(value, this);
126 }
127
132 public override Task Parse(XmlElement Xml)
133 {
134 this.contractIdVariable = new ScriptableStringAttribute(XML.Attribute(Xml, "contractIdVariable"), this);
135 this.contractXmlVariable = new ScriptableStringAttribute(XML.Attribute(Xml, "contractXmlVariable"), this);
136 this.parametersVariable = new ScriptableStringAttribute(XML.Attribute(Xml, "parametersVariable"), this);
137 this.rolesVariable = new ScriptableStringAttribute(XML.Attribute(Xml, "rolesVariable"), this);
138 this.machineReadableVariable = new ScriptableStringAttribute(XML.Attribute(Xml, "machineReadableVariable"), this);
139 this.condition = new ExpressionAttribute(XML.Attribute(Xml, "condition"), true, this);
140 this.localName = new ScriptableStringAttribute(XML.Attribute(Xml, "localName"), this);
141 this.@namespace = new ScriptableStringAttribute(XML.Attribute(Xml, "namespace"), this);
142 this.nameType = new ScriptableEnumAttribute<SecondaryNameType>(XML.Attribute(Xml, "nameType"), this);
143
144 return base.Parse(Xml);
145 }
146
156 public override Task<EventHandlerReference> Register(int EventIndex, EvaluationArguments Arguments, OnEvent Event)
157 {
158 return this.Register(EventIndex, Arguments, null, null);
159 }
160
171 protected async Task<EventHandlerReference> Register(int EventIndex, EvaluationArguments Arguments,
172 string LegalIdVariable, string RoleVariable)
173 {
174 string ContractIdVariable = await this.contractIdVariable.Evaluate(Arguments.Variables);
175 string ContractXmlVariable = await this.contractXmlVariable.Evaluate(Arguments.Variables);
176 string ParametersVariable = await this.parametersVariable.Evaluate(Arguments.Variables);
177 string RolesVariable = await this.rolesVariable.Evaluate(Arguments.Variables);
178 string MachineReadableVariable = await this.machineReadableVariable.Evaluate(Arguments.Variables);
179 string LocalName = await this.localName.Evaluate(Arguments.Variables);
180 string Namespace = await this.@namespace.Evaluate(Arguments.Variables);
181 SecondaryNameType NameType = this.nameType.IsEmpty ? SecondaryNameType.None : await this.nameType.Evaluate(Arguments.Variables);
182
183 ContractEventHandler Handler = new ContractEventHandler(Arguments, EventIndex,
184 this, ContractIdVariable, ContractXmlVariable, ParametersVariable,
185 RolesVariable, MachineReadableVariable, LegalIdVariable, RoleVariable,
186 LocalName, Namespace, NameType);
187
188 await Database.Insert(Handler);
189 await Handler.AddToCache();
190
191 return new EventHandlerReference(Handler);
192 }
193
199 public async Task<bool> EvaluateCondition(EvaluationArguments Arguments)
200 {
201 if (this.condition is null)
202 return true;
203
204 try
205 {
206 object Obj = await this.condition.Evaluate(Arguments.Variables);
207
208 if (Obj is bool b)
209 return b;
210 else
211 return false;
212 }
213 catch (Exception)
214 {
215 return false;
216 }
217 }
218
222 public override string Label
223 {
224 get
225 {
226 StringBuilder sb = new StringBuilder();
227
228 sb.Append(this.GetType().Name);
229 sb.Append('(');
230 sb.Append(this.localName.Definition);
231 sb.Append(')');
232
233 return sb.ToString();
234 }
235 }
236
242 public override async Task<bool> Applies(EvaluationArguments Arguments)
243 {
244 if (!(this.condition?.HasExpreession ?? false))
245 return true;
246
247 return await this.EvaluateCondition(Arguments);
248 }
249 }
250}
Helps with common XML-related tasks.
Definition: XML.cs:21
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:1062
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:21
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
Definition: Database.cs:97
async Task AddToCache()
Removes the event handler from the cache.
async Task< object > Evaluate(Variables Variables)
Evaluates the attribute
async Task< T > Evaluate(Variables Variables)
Evaluates the attribute
Contains information required for evaluating script in a state-machine.
async Task< bool > EvaluateCondition(EvaluationArguments Arguments)
Evaluates the condition.
override Task< EventHandlerReference > Register(int EventIndex, EvaluationArguments Arguments, OnEvent Event)
Registers the event
string ContractXmlVariableDefinition
Contract XML Variable definition.
override Task Parse(XmlElement Xml)
Parses the State-machine node.
string MachineReadableVariableDefinition
Machine-readable Variable definition.
async Task< EventHandlerReference > Register(int EventIndex, EvaluationArguments Arguments, string LegalIdVariable, string RoleVariable)
Registers the event
ContractEventNode()
Abstract base class for contract event nodes.
override async Task< bool > Applies(EvaluationArguments Arguments)
If the event node applies to the current context.
Abstract base class for State-Machine event nodes.
Definition: EventNode.cs:11
Action executed when entering a state.
Definition: OnEvent.cs:19