Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
IfHash.cs
1using System;
2using System.Threading.Tasks;
3using System.Xml;
10
12{
17 public class IfHash : ActionNode
18 {
19 private Hash hash;
20 private Realm realm;
21 private Then then;
22 private Else @else;
23
28 public IfHash()
29 : base()
30 {
31 }
32
36 public Hash Hash => this.hash;
37
41 public Realm Realm => this.realm;
42
46 public Then Then => this.then;
47
51 public Else Else => this.@else;
52
56 [DefaultValueNull]
57 public string ObjectVariable { get; set; }
58
62 public override string LocalName => nameof(IfHash);
63
68 public override IStateMachineNode Create()
69 {
70 return new IfHash();
71 }
72
77 public override async Task Parse(XmlElement Xml)
78 {
79 this.ObjectVariable = XML.Attribute(Xml, "objectVariable");
80
81 await base.Parse(Xml);
82
84 new Type[]
85 {
86 typeof(Hash),
87 typeof(Realm)
88 },
89 new bool[]
90 {
91 true,
92 false
93 });
94 }
95
99 protected override void OnChildNodesUpdated()
100 {
101 base.OnChildNodesUpdated();
102
103 this.hash = this.GetValueElement<Hash>();
104 this.realm = this.GetValueElement<Realm>();
105 this.then = this.GetChildElement<Then>(true);
106 this.@else = this.GetChildElement<Else>(false);
107 }
108
113 public override async Task Execute(EvaluationArguments Arguments)
114 {
115 object HashValue = await this.hash.Evaluate(Arguments);
116 object RealmValue = this.realm is null ? null : await this.realm.Evaluate(Arguments);
117 string Realm;
118 bool Exists;
119
120 if (!(HashValue is byte[] Hash))
121 throw new StateMachineException(Arguments.Machine, "Hash digest must be an array of bytes.");
122
123 if (RealmValue is null)
124 Realm = null;
125 else if (RealmValue is string s)
126 Realm = s;
127 else if (RealmValue is CaseInsensitiveString cis)
128 Realm = cis.Value;
129 else
130 throw new StateMachineException(Arguments.Machine, "Realm must be a string value.");
131
132 Realm ??= string.Empty;
133
134 if (string.IsNullOrEmpty(this.ObjectVariable))
135 Exists = await PersistedHashes.VerifyHash(Realm, Hash);
136 else
137 {
139 if (Obj is null)
140 Exists = await PersistedHashes.VerifyHash(Realm, Hash);
141 else
142 Exists = true;
143
144 if (Exists)
145 Arguments.Variables[this.ObjectVariable] = Obj;
146 else
147 Arguments.Variables[this.ObjectVariable] = null;
148 }
149
150 if (Exists)
151 await this.then.Execute(Arguments);
152 else if (!(this.@else is null))
153 await this.@else.Execute(Arguments);
154 }
155 }
156}
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
Represents a case-insensitive string.
Static class managing persistent counters.
static Task< bool > VerifyHash(byte[] Hash)
Verifies if a hash value has been persisted in the database, using the default (empty) realm.
static Task< object > TryGetAssociatedObject(byte[] Hash)
Tries to get the associated object of a hash value that has been persisted in the database,...
override async Task Execute(EvaluationArguments Arguments)
Evaluates the action node
Abstract base class for State-Machine action nodes.
Definition: ActionNode.cs:9
Actions executed if condition or conditions not matching.
Definition: Else.cs:7
Conditional execution, using an IFHASH-THEN[-ELSE] statement. Checks if a hash exists in the hash sto...
Definition: IfHash.cs:18
override async Task Parse(XmlElement Xml)
Parses the State-machine node.
Definition: IfHash.cs:77
override async Task Execute(EvaluationArguments Arguments)
Evaluates the action node
Definition: IfHash.cs:113
override IStateMachineNode Create()
Creates a new node of the corresponding type.
Definition: IfHash.cs:68
override void OnChildNodesUpdated()
Method called whenever ChildNodes is updated.
Definition: IfHash.cs:99
IfHash()
Conditional execution, using an IFHASH-THEN[-ELSE] statement. Checks if a hash exists in the hash sto...
Definition: IfHash.cs:28
Contains information required for evaluating script in a state-machine.
StateMachine Machine
Reference to state-machine definition.
void ConvertValueAttributesToElements(XmlElement Xml, Type[] ValueTypes, bool[] Required)
Converts value attributes to parsed elements. The XML definition has to be parsed before,...
Value()
Abstract base class for nodes with a value.
Definition: Value.cs:20
Task< object > Evaluate(EvaluationArguments Arguments)
Evaluates the value node.
Definition: Value.cs:147