Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
NumericalParameter.cs
1using System;
2using System.Text;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
9using Waher.Script;
11
13{
18 {
19 private decimal? value;
20 private decimal? min = null;
21 private decimal? max = null;
22 private bool minIncluded = true;
23 private bool maxIncluded = true;
24
28 public decimal? Value
29 {
30 get => this.@value;
31 set
32 {
33 this.@value = value;
34 this.ProtectedValue = null;
35 }
36 }
37
41 [DefaultValueNull]
42 public decimal? Min
43 {
44 get => this.min;
45 set => this.min = value;
46 }
47
51 [DefaultValueNull]
52 public decimal? Max
53 {
54 get => this.max;
55 set => this.max = value;
56 }
57
61 [DefaultValue(true)]
62 public bool MinIncluded
63 {
64 get => this.minIncluded;
65 set => this.minIncluded = value;
66 }
67
71 [DefaultValue(true)]
72 public bool MaxIncluded
73 {
74 get => this.maxIncluded;
75 set => this.maxIncluded = value;
76 }
77
81 public override object ObjectValue => this.@value;
82
86 public override string StringValue
87 {
88 get => this.Value.HasValue ? CommonTypes.Encode(this.Value.Value) : string.Empty;
89 set
90 {
91 if (CommonTypes.TryParse(value, out decimal d))
92 this.Value = d;
93 else
94 this.Value = null;
95 }
96 }
97
101 public override void ClearValue()
102 {
103 this.@value = default;
104 }
105
113 public override void ToMarkdown(MarkdownOutput Markdown, Contract Contract, int Level, int Indentation)
114 {
115 Markdown.Append(MarkdownDocument.Encode(this.@value?.ToString() ?? string.Empty));
116 }
117
122 public override void Serialize(StringBuilder Xml)
123 {
124 Xml.Append("<numericalParameter");
125
126 if (!string.IsNullOrEmpty(this.Expression))
127 {
128 Xml.Append(" exp=\"");
129 Xml.Append(XML.Encode(this.Expression.Normalize(NormalizationForm.FormC)));
130 Xml.Append('"');
131 }
132
133 if (!string.IsNullOrEmpty(this.Guide))
134 {
135 Xml.Append(" guide=\"");
136 Xml.Append(XML.Encode(this.Guide.Normalize(NormalizationForm.FormC)));
137 Xml.Append('"');
138 }
139
140 if (this.max.HasValue)
141 {
142 Xml.Append(" max=\"");
143 Xml.Append(CommonTypes.Encode(this.max.Value));
144 Xml.Append("\" maxIncluded=\"");
145 Xml.Append(XML.Encode(CommonTypes.Encode(this.maxIncluded)));
146 Xml.Append('"');
147 }
148
149 if (this.min.HasValue)
150 {
151 Xml.Append(" min=\"");
152 Xml.Append(CommonTypes.Encode(this.min.Value));
153 Xml.Append("\" minIncluded=\"");
154 Xml.Append(XML.Encode(CommonTypes.Encode(this.minIncluded)));
155 Xml.Append('"');
156 }
157
158 Xml.Append(" name=\"");
159 Xml.Append(XML.Encode(this.Name.Value.Normalize(NormalizationForm.FormC)));
160 Xml.Append('"');
161
163 {
164 Xml.Append(" protected=\"");
165 Xml.Append(Convert.ToBase64String(this.ProtectedValue));
166 Xml.Append('"');
167 }
168
169 if (this.Protection != ProtectionLevel.Normal)
170 {
171 Xml.Append(" protection=\"");
172 Xml.Append(this.Protection.ToString());
173 Xml.Append('"');
174 }
175
176 if (this.@value.HasValue && this.CanSerializeValue)
177 {
178 Xml.Append(" value=\"");
179 Xml.Append(CommonTypes.Encode(this.@value.Value));
180 Xml.Append('"');
181 }
182
183 if (this.Descriptions is null || this.Descriptions.Length == 0)
184 Xml.Append("/>");
185 else
186 {
187 Xml.Append('>');
188
189 foreach (HumanReadableText Description in this.Descriptions)
190 Description.Serialize(Xml, "description", null);
191
192 Xml.Append("</numericalParameter>");
193 }
194 }
195
202 public override Task<string> IsParameterValid(Variables Variables, LegalComponent Legal)
203 {
204 decimal Diff;
205
206 if (!this.@value.HasValue)
207 return Task.FromResult("Value missing.");
208
209 if (this.min.HasValue)
210 {
211 Diff = this.@value.Value - this.min.Value;
212
213 if (Diff < 0 || (Diff == 0 && !this.minIncluded))
214 return Task.FromResult("Value below minimum permitted.");
215 }
216
217 if (this.max.HasValue)
218 {
219 Diff = this.@value.Value - this.max.Value;
220
221 if (Diff > 0 || (Diff == 0 && !this.maxIncluded))
222 return Task.FromResult("Value above maximum permitted.");
223 }
224
225 return base.IsParameterValid(Variables, Legal);
226 }
227
232 public override void Populate(Variables Variables)
233 {
234 Variables[this.Name] = this.@value;
235 }
236
242 public override bool UpdateRequiresReview(Parameter UpdatedParameter)
243 {
244 return !(UpdatedParameter is NumericalParameter UpdatedNumericalParameter) ||
245 this.min != UpdatedNumericalParameter.min ||
246 this.max != UpdatedNumericalParameter.max ||
247 this.minIncluded != UpdatedNumericalParameter.minIncluded ||
248 this.maxIncluded != UpdatedNumericalParameter.maxIncluded ||
249 base.UpdateRequiresReview(UpdatedParameter);
250 }
251
257 public override Parameter TryCreateNew(object NewValue)
258 {
259 if (!(NewValue is decimal D))
260 {
261 if (NewValue is double Dbl)
262 D = (decimal)Dbl;
263 else if (NewValue is float F)
264 D = (decimal)F;
265 else if (NewValue is sbyte i8)
266 D = i8;
267 else if (NewValue is short i16)
268 D = i16;
269 else if (NewValue is int i32)
270 D = i32;
271 else if (NewValue is long i64)
272 D = i64;
273 else if (NewValue is byte ui8)
274 D = ui8;
275 else if (NewValue is ushort ui16)
276 D = ui16;
277 else if (NewValue is uint ui32)
278 D = ui32;
279 else if (NewValue is ulong ui64)
280 D = ui64;
281 else if (NewValue is string s && CommonTypes.TryParse(s, out decimal D2))
282 D = D2;
283 else
284 return null;
285 }
286
287 if (this.min.HasValue && (D < this.min.Value || D == this.min.Value && !this.minIncluded))
288 return null;
289
290 if (this.max.HasValue && (D > this.max.Value || D == this.max.Value && !this.maxIncluded))
291 return null;
292
293 return new NumericalParameter()
294 {
296 Expression = this.Expression,
297 Guide = this.Guide,
298 Name = this.Name,
299 Value = D,
300 Max = this.Max,
302 Min = this.Min,
304 Protection = this.Protection
305 };
306 }
307
313 public override Task<bool> Import(XmlElement Xml)
314 {
315 this.Value = Xml.HasAttribute("value") ? XML.Attribute(Xml, "value", 0.0m) : (decimal?)null;
316 this.Min = Xml.HasAttribute("min") ? XML.Attribute(Xml, "min", 0.0m) : (decimal?)null;
317 this.MinIncluded = XML.Attribute(Xml, "minIncluded", true);
318 this.Max = Xml.HasAttribute("max") ? XML.Attribute(Xml, "max", 0.0m) : (decimal?)null;
319 this.MaxIncluded = XML.Attribute(Xml, "maxIncluded", true);
320
321 return base.Import(Xml);
322 }
323
324 }
325}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static string Encode(bool x)
Encodes a Boolean for use in XML and other formats.
Definition: CommonTypes.cs:594
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
Definition: CommonTypes.cs:46
Contains a markdown document. This markdown document class supports original markdown,...
static string Encode(string s)
Encodes all special characters in a string so that it can be included in a markdown document without ...
Helps with common XML-related tasks.
Definition: XML.cs:19
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:914
static string Encode(string s)
Encodes a string for use in XML.
Definition: XML.cs:27
Class managing a script expression.
Definition: Expression.cs:39
Collection of variables.
Definition: Variables.cs:25