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;
11using Waher.Script;
13
15{
20 {
21 private decimal? value;
22 private decimal? min = null;
23 private decimal? max = null;
24 private bool minIncluded = true;
25 private bool maxIncluded = true;
26
30 public decimal? Value
31 {
32 get => this.@value;
33 set
34 {
35 this.@value = value;
36 this.ProtectedValue = null;
37 }
38 }
39
43 [DefaultValueNull]
44 public decimal? Min
45 {
46 get => this.min;
47 set => this.min = value;
48 }
49
53 [DefaultValueNull]
54 public decimal? Max
55 {
56 get => this.max;
57 set => this.max = value;
58 }
59
63 [DefaultValue(true)]
64 public bool MinIncluded
65 {
66 get => this.minIncluded;
67 set => this.minIncluded = value;
68 }
69
73 [DefaultValue(true)]
74 public bool MaxIncluded
75 {
76 get => this.maxIncluded;
77 set => this.maxIncluded = value;
78 }
79
83 public override object ObjectValue => this.@value;
84
88 public override string StringValue
89 {
90 get => this.Value.HasValue ? CommonTypes.Encode(this.Value.Value) : string.Empty;
91 set
92 {
93 if (CommonTypes.TryParse(value, out decimal d))
94 this.Value = d;
95 else
96 this.Value = null;
97 }
98 }
99
103 public override void ClearValue()
104 {
105 this.@value = default;
106 }
107
113 public override Task ToMarkdown(MarkdownOutput Markdown, MarkdownOutputState State)
114 {
115 Markdown.Append(MarkdownDocument.Encode(this.@value?.ToString() ?? string.Empty));
116 return Task.CompletedTask;
117 }
118
123 public override void Serialize(StringBuilder Xml)
124 {
125 Xml.Append("<numericalParameter");
126
127 if (!string.IsNullOrEmpty(this.Expression))
128 {
129 Xml.Append(" exp=\"");
130 Xml.Append(XML.Encode(this.Expression.Normalize(NormalizationForm.FormC)));
131 Xml.Append('"');
132 }
133
134 if (!string.IsNullOrEmpty(this.Guide))
135 {
136 Xml.Append(" guide=\"");
137 Xml.Append(XML.Encode(this.Guide.Normalize(NormalizationForm.FormC)));
138 Xml.Append('"');
139 }
140
141 if (this.max.HasValue)
142 {
143 Xml.Append(" max=\"");
144 Xml.Append(CommonTypes.Encode(this.max.Value));
145 Xml.Append("\" maxIncluded=\"");
146 Xml.Append(XML.Encode(CommonTypes.Encode(this.maxIncluded)));
147 Xml.Append('"');
148 }
149
150 if (this.min.HasValue)
151 {
152 Xml.Append(" min=\"");
153 Xml.Append(CommonTypes.Encode(this.min.Value));
154 Xml.Append("\" minIncluded=\"");
155 Xml.Append(XML.Encode(CommonTypes.Encode(this.minIncluded)));
156 Xml.Append('"');
157 }
158
159 Xml.Append(" name=\"");
160 Xml.Append(XML.Encode(this.Name.Value.Normalize(NormalizationForm.FormC)));
161 Xml.Append('"');
162
164 {
165 Xml.Append(" protected=\"");
166 Xml.Append(Convert.ToBase64String(this.ProtectedValue));
167 Xml.Append('"');
168 }
169
170 if (this.Protection != ProtectionLevel.Normal)
171 {
172 Xml.Append(" protection=\"");
173 Xml.Append(this.Protection.ToString());
174 Xml.Append('"');
175 }
176
177 if (this.@value.HasValue && this.CanSerializeValue)
178 {
179 Xml.Append(" value=\"");
180 Xml.Append(CommonTypes.Encode(this.@value.Value));
181 Xml.Append('"');
182 }
183
184 if (this.Descriptions is null || this.Descriptions.Length == 0)
185 Xml.Append("/>");
186 else
187 {
188 Xml.Append('>');
189
190 foreach (HumanReadableText Description in this.Descriptions)
191 Description.Serialize(Xml, "description", null);
192
193 Xml.Append("</numericalParameter>");
194 }
195 }
196
204 public override Task<string> IsParameterValid(Variables Variables,
206 {
207 decimal Diff;
208
209 if (!this.@value.HasValue)
210 return Task.FromResult("Value missing.");
211
212 if (this.min.HasValue)
213 {
214 Diff = this.@value.Value - this.min.Value;
215
216 if (Diff < 0 || (Diff == 0 && !this.minIncluded))
217 return Task.FromResult("Value below minimum permitted.");
218 }
219
220 if (this.max.HasValue)
221 {
222 Diff = this.@value.Value - this.max.Value;
223
224 if (Diff > 0 || (Diff == 0 && !this.maxIncluded))
225 return Task.FromResult("Value above maximum permitted.");
226 }
227
228 return base.IsParameterValid(Variables, Legal, Profiler);
229 }
230
237 {
238 Variables[this.Name] = this.@value;
239 }
240
246 public override bool UpdateRequiresReview(Parameter UpdatedParameter)
247 {
248 return !(UpdatedParameter is NumericalParameter UpdatedNumericalParameter) ||
249 this.min != UpdatedNumericalParameter.min ||
250 this.max != UpdatedNumericalParameter.max ||
251 this.minIncluded != UpdatedNumericalParameter.minIncluded ||
252 this.maxIncluded != UpdatedNumericalParameter.maxIncluded ||
253 base.UpdateRequiresReview(UpdatedParameter);
254 }
255
261 public override Parameter TryCreateNew(object NewValue)
262 {
263 if (!(NewValue is decimal D))
264 {
265 if (NewValue is double Dbl)
266 D = (decimal)Dbl;
267 else if (NewValue is float F)
268 D = (decimal)F;
269 else if (NewValue is sbyte i8)
270 D = i8;
271 else if (NewValue is short i16)
272 D = i16;
273 else if (NewValue is int i32)
274 D = i32;
275 else if (NewValue is long i64)
276 D = i64;
277 else if (NewValue is byte ui8)
278 D = ui8;
279 else if (NewValue is ushort ui16)
280 D = ui16;
281 else if (NewValue is uint ui32)
282 D = ui32;
283 else if (NewValue is ulong ui64)
284 D = ui64;
285 else if (NewValue is string s && CommonTypes.TryParse(s, out decimal D2))
286 D = D2;
287 else
288 return null;
289 }
290
291 if (this.min.HasValue && (D < this.min.Value || D == this.min.Value && !this.minIncluded))
292 return null;
293
294 if (this.max.HasValue && (D > this.max.Value || D == this.max.Value && !this.maxIncluded))
295 return null;
296
297 return new NumericalParameter()
298 {
300 Expression = this.Expression,
301 Guide = this.Guide,
302 Name = this.Name,
303 Value = D,
304 Max = this.Max,
306 Min = this.Min,
308 Protection = this.Protection
309 };
310 }
311
317 public override Task<bool> Import(XmlElement Xml)
318 {
319 this.Value = Xml.HasAttribute("value") ? XML.Attribute(Xml, "value", 0.0m) : (decimal?)null;
320 this.Min = Xml.HasAttribute("min") ? XML.Attribute(Xml, "min", 0.0m) : (decimal?)null;
321 this.MinIncluded = XML.Attribute(Xml, "minIncluded", true);
322 this.Max = Xml.HasAttribute("max") ? XML.Attribute(Xml, "max", 0.0m) : (decimal?)null;
323 this.MaxIncluded = XML.Attribute(Xml, "maxIncluded", true);
324
325 return base.Import(Xml);
326 }
327
331 public override TextAlignment CellAlignment => TextAlignment.Right;
332
338 public override bool CopyValidationRules(Parameter From)
339 {
340 if (!(From is NumericalParameter Typed))
341 return false;
342
343 this.min = Typed.min;
344 this.max = Typed.max;
345 this.minIncluded = Typed.minIncluded;
346 this.maxIncluded = Typed.maxIncluded;
347
348 return base.CopyValidationRules(From);
349 }
350
354 public override void ClearValidationRules()
355 {
356 this.min = null;
357 this.max = null;
358
359 base.ClearValidationRules();
360 }
361 }
362}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:15
static string Encode(bool x)
Encodes a Boolean for use in XML and other formats.
Definition: CommonTypes.cs:596
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
Definition: CommonTypes.cs:48
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:21
static string Attribute(XmlElement E, string Name)
Gets the value of an XML attribute.
Definition: XML.cs:1062
static string Encode(string s)
Encodes a string for use in XML.
Definition: XML.cs:29
Class that keeps track of events and timing.
Definition: Profiler.cs:68
Class that keeps track of events and timing for one thread.
Class managing a script expression.
Definition: Expression.cs:41
Collection of variables.
Definition: Variables.cs:25
TextAlignment
Text alignment of contents.