Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ScriptDoubleParameterNode.cs
1using System.Text.RegularExpressions;
2using System.Threading.Tasks;
3using Waher.Content;
11using Waher.Script;
13
15{
20 {
25 : base()
26 {
27 }
28
32 [Page(2, "Script", 100)]
33 [Header(29, "Default value:")]
34 [ToolTip(30, "Default value presented to user.")]
35 public double? DefaultValue { get; set; }
36
40 [Page(2, "Script", 100)]
41 [Header(44, "Minimum Value:")]
42 [ToolTip(45, "The smallest value allowed.")]
43 public double? Min { get; set; }
44
48 [Page(2, "Script", 100)]
49 [Header(46, "Maximum Value:")]
50 [ToolTip(47, "The largest value allowed.")]
51 public double? Max { get; set; }
52
58 public override Task<string> GetTypeNameAsync(Language Language)
59 {
60 return Language.GetStringAsync(typeof(ScriptNode), 59, "Double-precision floating-point parameter");
61 }
62
69 public override async Task PopulateForm(DataForm Parameters, Language Language, object Value)
70 {
71 ValidationMethod Validation;
73
74 if (this.Min.HasValue || this.Max.HasValue)
75 {
76 Validation = new RangeValidation(
77 this.Min.HasValue ? CommonTypes.Encode(this.Min.Value) : null,
78 this.Max.HasValue ? CommonTypes.Encode(this.Max.Value) : null);
79 }
80 else
81 Validation = new BasicValidation();
82
83 if (this.RestrictToOptions)
84 {
85 Field = new ListSingleField(Parameters, this.ParameterName, this.Label, this.Required,
86 new string[] { this.DefaultValue.HasValue ? CommonTypes.Encode(this.DefaultValue.Value) : string.Empty },
87 await this.GetOptions(), this.Description, DoubleDataType.Instance, Validation, string.Empty, false, false, false);
88 }
89 else
90 {
92 new string[] { this.DefaultValue.HasValue ? CommonTypes.Encode(this.DefaultValue.Value) : string.Empty },
93 await this.GetOptions(), this.Description, DoubleDataType.Instance, Validation, string.Empty, false, false, false);
94 }
95
96 Parameters.Add(Field);
97
98 Page Page = Parameters.GetPage(this.Page);
99 Page.Add(Field);
100 }
101
111 public override async Task SetParameter(DataForm Parameters, Language Language, bool OnlySetChanged, Variables Values,
113 {
115 if (Field is null)
116 {
117 if (this.Required)
118 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 42, "Required parameter."));
119
120 Values[this.ParameterName] = null;
121 }
122 else
123 {
124 string s = Field.ValueString;
125
126 if (string.IsNullOrEmpty(s))
127 {
128 if (this.Required)
129 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 42, "Required parameter."));
130
131 Values[this.ParameterName] = null;
132 }
133 else if (CommonTypes.TryParse(s, out double Parsed))
134 Values[this.ParameterName] = Parsed;
135 else
136 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 49, "Invalid value."));
137 }
138 }
139
140 }
141}
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
Static class managing editable parameters in objects. Editable parameters are defined by using the at...
Definition: Parameters.cs:25
void AddError(string Key, string Value)
Adds an error to the list of errors.
Implements support for data forms. Data Forms are defined in the following XEPs:
Definition: DataForm.cs:42
static readonly DoubleDataType Instance
Public instance of data type.
Base class for form fields
Definition: Field.cs:16
string ValueString
Value as a single string. If field contains multiple values, they will be concatenated into a single ...
Definition: Field.cs:101
Class managing a page in a data form layout.
Definition: Page.cs:11
void Add(LayoutElement Element)
Adds a layout element.
Definition: Section.cs:135
Contains information about a language.
Definition: Language.cs:17
Task< string > GetStringAsync(Type Type, int Id, string Default)
Gets the string value of a string ID. If no such string exists, a string is created with the default ...
Definition: Language.cs:209
Collection of variables.
Definition: Variables.cs:25
Represents a double-precision floating-point-valued script parameter.
override async Task SetParameter(DataForm Parameters, Language Language, bool OnlySetChanged, Variables Values, SetEditableFormResult Result)
Sets the parameters of the object, based on contents in the data form.
override async Task PopulateForm(DataForm Parameters, Language Language, object Value)
Populates a data form with parameters for the object.
ScriptDoubleParameterNode()
Represents a double-precision floating-point-valued script parameter.
override Task< string > GetTypeNameAsync(Language Language)
Gets the type name of the node.
Represents a parameter with possible options on a command.
async Task< KeyValuePair< string, string >[]> GetOptions()
Gets available options, if any are defined.
Node defined by script.
Definition: ScriptNode.cs:19
bool Required
If parameter is required.
string Description
Parameter description.