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.Threading.Tasks;
2using Waher.Content;
10using Waher.Script;
12
14{
19 {
24 : base()
25 {
26 }
27
31 [Page(2, "Script", 100)]
32 [Header(29, "Default value:")]
33 [ToolTip(30, "Default value presented to user.")]
34 public double? DefaultValue { get; set; }
35
39 [Page(2, "Script", 100)]
40 [Header(44, "Minimum Value:")]
41 [ToolTip(45, "The smallest value allowed.")]
42 public double? Min { get; set; }
43
47 [Page(2, "Script", 100)]
48 [Header(46, "Maximum Value:")]
49 [ToolTip(47, "The largest value allowed.")]
50 public double? Max { get; set; }
51
57 public override Task<string> GetTypeNameAsync(Language Language)
58 {
59 return Language.GetStringAsync(typeof(ScriptNode), 59, "Double-precision floating-point parameter");
60 }
61
68 public override async Task PopulateForm(DataForm Parameters, Language Language, object Value)
69 {
70 ValidationMethod Validation;
72
73 if (this.Min.HasValue || this.Max.HasValue)
74 {
75 Validation = new RangeValidation(
76 this.Min.HasValue ? CommonTypes.Encode(this.Min.Value) : null,
77 this.Max.HasValue ? CommonTypes.Encode(this.Max.Value) : null);
78 }
79 else
80 Validation = new BasicValidation();
81
82 if (this.RestrictToOptions)
83 {
84 Field = new ListSingleField(Parameters, this.ParameterName, this.Label, this.Required,
85 new string[] { this.DefaultValue.HasValue ? CommonTypes.Encode(this.DefaultValue.Value) : string.Empty },
86 await this.GetOptions(), this.Description, DoubleDataType.Instance, Validation, string.Empty, false, false, false);
87 }
88 else
89 {
91 new string[] { this.DefaultValue.HasValue ? CommonTypes.Encode(this.DefaultValue.Value) : string.Empty },
92 await this.GetOptions(), this.Description, DoubleDataType.Instance, Validation, string.Empty, false, false, false);
93 }
94
95 Parameters.Add(Field);
96
97 Page Page = Parameters.GetPage(this.Page);
98 Page.Add(Field);
99 }
100
110 public override async Task SetParameter(DataForm Parameters, Language Language, bool OnlySetChanged, Variables Values,
112 {
114 if (Field is null)
115 {
116 if (this.Required)
117 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 42, "Required parameter."));
118
119 Values[this.ParameterName] = null;
120 }
121 else
122 {
123 string s = Field.ValueString;
124
125 if (string.IsNullOrEmpty(s))
126 {
127 if (this.Required)
128 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 42, "Required parameter."));
129
130 Values[this.ParameterName] = null;
131 }
132 else if (CommonTypes.TryParse(s, out double Parsed))
133 Values[this.ParameterName] = Parsed;
134 else
135 Result.AddError(this.ParameterName, await Language.GetStringAsync(typeof(ScriptNode), 49, "Invalid value."));
136 }
137 }
138
139 }
140}
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
Static class managing editable parameters in objects. Editable parameters are defined by using the at...
Definition: Parameters.cs:26
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:17
string ValueString
Value as a single string. If field contains multiple values, they will be concatenated into a single ...
Definition: Field.cs:102
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.
bool RestrictToOptions
If only values defined in options are valid values.
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.