Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
OnContractTemplateApproved.cs
1using System;
3using System.Threading.Tasks;
4using System.Xml;
14
16{
21 {
22 private ScriptableBooleanAttribute checkExisting;
23
28 : base()
29 {
30 }
31
35 [DefaultValueNull]
37 {
38 get => this.checkExisting?.Definition;
39 set => this.checkExisting = new ScriptableBooleanAttribute(value, this);
40 }
41
45 public override string LocalName => nameof(OnContractTemplateApproved);
46
51 public override IStateMachineNode Create()
52 {
53 return new OnContractTemplateApproved();
54 }
55
60 public override Task Parse(XmlElement Xml)
61 {
62 this.checkExisting = new ScriptableBooleanAttribute(XML.Attribute(Xml, "checkExisting"), this);
63
64 return base.Parse(Xml);
65 }
66
76 public override async Task<EventHandlerReference> Register(int EventIndex, EvaluationArguments Arguments,
78 {
79 EventHandlerReference Ref = await base.Register(EventIndex, Arguments, Event);
80 if (Ref is null)
81 return null;
82
84 !this.checkExisting.IsEmpty &&
85 await this.checkExisting.Evaluate(Arguments.Variables))
86 {
87 IEnumerable<Contract> ContractTemplates;
88 Filter FirstFilter = new FilterFieldEqualTo("CanActAsTemplate", true);
90 {
91 FirstFilter
92 };
93
95 {
96 if (!string.IsNullOrEmpty(ContractEventHandler.Namespace))
97 Filters.Add(new FilterFieldEqualTo("ForMachinesNamespace", ContractEventHandler.Namespace));
98
99 if (!string.IsNullOrEmpty(ContractEventHandler.LocalName))
100 Filters.Add(new FilterFieldEqualTo("ForMachinesLocalName", ContractEventHandler.LocalName));
101
102 if (Filters.Count == 1)
103 ContractTemplates = await Database.Find<Contract>(FirstFilter, "-Created");
104 else
105 ContractTemplates = await Database.Find<Contract>(new FilterAnd(Filters.ToArray()), "-Created");
106 }
107 else
108 {
109 Filters.Add(new FilterFieldEqualTo("SecondaryType", ContractEventHandler.NameType));
110
111 if (!string.IsNullOrEmpty(ContractEventHandler.Namespace))
112 Filters.Add(new FilterFieldEqualTo("SecondaryNamespace", ContractEventHandler.Namespace));
113
114 if (!string.IsNullOrEmpty(ContractEventHandler.LocalName))
115 Filters.Add(new FilterFieldEqualTo("SecondaryLocalName", ContractEventHandler.LocalName));
116
117 ContractTemplates = await Database.Find<Contract>(new FilterAnd(Filters.ToArray()), "-Created");
118 }
119
120 if (!(ContractTemplates is null))
121 {
122 foreach (Contract Contract in ContractTemplates)
123 {
124 KeyValuePair<string, object>[] ToSet = await StateMachineProcessor.GetVariablesToSet(ContractEventHandler, Contract, null, null);
125
126 foreach (KeyValuePair<string, object> P in ToSet)
127 Arguments.Variables[P.Key] = P.Value;
128
129 try
130 {
131 if (await this.EvaluateCondition(Arguments))
132 return new ExistingTemplateFound(Ref, ToSet);
133 }
134 catch (Exception)
135 {
136 continue;
137 }
138 }
139 }
140
141 return Ref;
142 }
143
144 return Ref;
145 }
146
147 private class ExistingTemplateFound : EventHandlerReference
148 {
149 private readonly KeyValuePair<string, object>[] toSet;
150 private readonly EventHandlerReference reference;
151
152 public ExistingTemplateFound(EventHandlerReference Ref,
153 KeyValuePair<string, object>[] ToSet)
154 : base(Ref.EventHandler, true)
155 {
156 this.reference = Ref;
157 this.toSet = ToSet;
158 }
159
160 public override async Task Prepare(EvaluationArguments Arguments)
161 {
162 if (!(this.reference is null))
163 await this.reference.Prepare(Arguments);
164
165 foreach (KeyValuePair<string, object> P in this.toSet)
166 Arguments.Variables[P.Key] = P.Value;
167 }
168 }
169
170 }
171}
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 Task< IEnumerable< object > > Find(string Collection, params string[] SortOrder)
Finds objects in a given collection.
Definition: Database.cs:238
This filter selects objects that conform to all child-filters provided.
Definition: FilterAnd.cs:10
This filter selects objects that have a named field equal to a given value.
Base class for all filter classes.
Definition: Filter.cs:15
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
int Count
Number of elements in collection.
Definition: ChunkedList.cs:68
void Add(T Item)
Adds an item to the collection.
Definition: ChunkedList.cs:272
T[] ToArray()
Returns an array containing all elements of the collection.
string Namespace
Namespace of machine-readable part of contract.
SecondaryNameType NameType
On which element the name check should be performed.
string LocalName
Local-name of machine-readable part of contract.
Abstract base class for persisted state-machine event handlers.
Definition: EventHandler.cs:18
virtual Task Prepare(EvaluationArguments Arguments)
Prepares the collection of event arguments for event handler execution.
Contains information required for evaluating script in a state-machine.
async Task< bool > EvaluateCondition(EvaluationArguments Arguments)
Evaluates the condition.
override Task Parse(XmlElement Xml)
Parses the State-machine node.
override IStateMachineNode Create()
Creates a new node of the corresponding type.
override async Task< EventHandlerReference > Register(int EventIndex, EvaluationArguments Arguments, OnEvent Event)
Registers the event
OnContractTemplateApproved()
Event raised when a contract template has been approved.
Action executed when entering a state.
Definition: OnEvent.cs:19