Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
NumericParameter.cs
1using System;
2using System.Text;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
8
10{
14 public class NumericalParameter : RangeParameter<decimal>
15 {
19 public override string StringValue
20 {
21 get => this.Value.HasValue ? CommonTypes.Encode(this.Value.Value) : string.Empty;
22 set
23 {
24 if (CommonTypes.TryParse(value, out decimal d))
25 this.Value = d;
26 else
27 this.Value = null;
28 }
29 }
30
34 public override string ParameterType => "numericalParameter";
35
41 public override void Serialize(StringBuilder Xml, bool UsingTemplate)
42 {
43 Xml.Append("<numericalParameter");
44
45 if (!UsingTemplate)
46 {
47 if (!string.IsNullOrEmpty(this.Expression))
48 {
49 Xml.Append(" exp=\"");
50 Xml.Append(XML.Encode(this.Expression.Normalize(NormalizationForm.FormC)));
51 Xml.Append('"');
52 }
53
54 if (!string.IsNullOrEmpty(this.Guide))
55 {
56 Xml.Append(" guide=\"");
57 Xml.Append(XML.Encode(this.Guide.Normalize(NormalizationForm.FormC)));
58 Xml.Append('"');
59 }
60
61 if (this.Max.HasValue)
62 {
63 Xml.Append(" max=\"");
64 Xml.Append(CommonTypes.Encode(this.Max.Value));
65 Xml.Append("\" maxIncluded=\"");
66 Xml.Append(XML.Encode(CommonTypes.Encode(this.MaxIncluded)));
67 Xml.Append('"');
68 }
69
70 if (this.Min.HasValue)
71 {
72 Xml.Append(" min=\"");
73 Xml.Append(CommonTypes.Encode(this.Min.Value));
74 Xml.Append("\" minIncluded=\"");
75 Xml.Append(XML.Encode(CommonTypes.Encode(this.MinIncluded)));
76 Xml.Append('"');
77 }
78 }
79
80 Xml.Append(" name=\"");
81 Xml.Append(XML.Encode(this.Name));
82 Xml.Append('"');
83
84 if (this.CanSerializeProtectedValue)
85 {
86 Xml.Append(" protected=\"");
87 Xml.Append(Convert.ToBase64String(this.ProtectedValue));
88 Xml.Append('"');
89 }
90
91 if (!UsingTemplate && this.Protection != ProtectionLevel.Normal)
92 {
93 Xml.Append(" protection=\"");
94 Xml.Append(this.Protection.ToString());
95 Xml.Append('"');
96 }
97
98 if (this.Value.HasValue && this.CanSerializeValue)
99 {
100 Xml.Append(" value=\"");
101 Xml.Append(CommonTypes.Encode(this.Value.Value));
102 Xml.Append('"');
103 }
104
105 if (UsingTemplate || this.Descriptions is null || this.Descriptions.Length == 0)
106 Xml.Append("/>");
107 else
108 {
109 Xml.Append('>');
110
111 foreach (HumanReadableText Description in this.Descriptions)
112 Description.Serialize(Xml, "description", null);
113
114 Xml.Append("</numericalParameter>");
115 }
116 }
117
123 public override void SetValue(object Value)
124 {
125 this.Value = ToDecimal(Value);
126 }
127
134 public static decimal ToDecimal(object Value)
135 {
136 if (Value is double d)
137 return (decimal)d;
138 else if (Value is float f)
139 return (decimal)f;
140 else if (Value is decimal dec)
141 return dec;
142 else if (Value is int i)
143 return i;
144 else if (Value is long l)
145 return l;
146 else if (Value is short s)
147 return s;
148 else if (Value is sbyte sb)
149 return sb;
150 else if (Value is uint ui)
151 return ui;
152 else if (Value is ulong ul)
153 return ul;
154 else if (Value is ushort us)
155 return us;
156 else if (Value is byte ub)
157 return ub;
158 else if (Value is string str && CommonTypes.TryParse(str, out dec))
159 return dec;
160 else
161 throw new ArgumentException("Invalid parameter type.", nameof(Value));
162 }
163
169 public override void SetMinValue(object Value, bool? Inclusive)
170 {
171 this.Min = ToDecimal(Value);
172
173 if (Inclusive.HasValue)
174 this.MinIncluded = Inclusive.Value;
175 }
176
182 public override void SetMaxValue(object Value, bool? Inclusive)
183 {
184 this.Max = ToDecimal(Value);
185
186 if (Inclusive.HasValue)
187 this.MaxIncluded = Inclusive.Value;
188 }
189
195 public override Task<bool> Import(XmlElement Xml)
196 {
197 this.Value = Xml.HasAttribute("value") ? XML.Attribute(Xml, "value", 0.0m) : (decimal?)null;
198 this.Min = Xml.HasAttribute("min") ? XML.Attribute(Xml, "min", 0.0m) : (decimal?)null;
199 this.MinIncluded = XML.Attribute(Xml, "minIncluded", true);
200 this.Max = Xml.HasAttribute("max") ? XML.Attribute(Xml, "max", 0.0m) : (decimal?)null;
201 this.MaxIncluded = XML.Attribute(Xml, "maxIncluded", true);
202
203 return base.Import(Xml);
204 }
205
206 }
207}
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
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
override void Serialize(StringBuilder Xml)
Serializes the element in normalized form.
override void SetValue(object Value)
Sets the parameter value.
override Task< bool > Import(XmlElement Xml)
Imports parameter values from its XML definition.
override string ParameterType
Parameter type name, corresponding to the local name of the parameter element in XML.
override void SetMinValue(object Value, bool? Inclusive)
Sets the minimum value allowed by the parameter.
override string StringValue
String representation of value.
override void SetMaxValue(object Value, bool? Inclusive)
Sets the maximum value allowed by the parameter.
override void Serialize(StringBuilder Xml, bool UsingTemplate)
Serializes the parameter, in normalized form.
static decimal ToDecimal(object Value)
Converts an object value to a System.Decimal value.
ProtectionLevel
Parameter protection levels