Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ScriptCommand.cs
1using System;
2using System.Threading.Tasks;
6using Waher.Script;
9
10namespace Waher.Things.Script
11{
16 {
17 private readonly Variables values;
18 private readonly ScriptParameterNode[] parameters;
19 private readonly VirtualNode node;
20 private readonly ScriptCommandNode commandNode;
21
29 {
30 this.node = Node;
31 this.commandNode = CommandNode;
32 this.parameters = Parameters;
33
34 this.values = new Variables()
35 {
36 ["this"] = this.node
37 };
38 }
39
43 public string CommandID => this.commandNode.CommandId;
44
48 public CommandType Type => this.parameters.Length == 0 ? CommandType.Simple : CommandType.Parametrized;
49
53 public string SortCategory => this.commandNode.SortCategory;
54
58 public string SortKey => this.commandNode.SortKey;
59
64 public Task<string> GetNameAsync(Language Language) => Task.FromResult(this.commandNode.CommandName);
65
70 public Task<string> GetConfirmationStringAsync(Language Language) => Task.FromResult(this.commandNode.Confirmation);
71
76 public Task<string> GetFailureStringAsync(Language Language) => Task.FromResult(this.commandNode.Failure);
77
82 public Task<string> GetSuccessStringAsync(Language Language) => Task.FromResult(this.commandNode.Success);
83
89 public Task<bool> CanExecuteAsync(RequestOrigin Caller) => Task.FromResult(true); // TODO: Configure access rights.
90
94 public Task ExecuteCommandAsync()
95 {
96 return this.commandNode.ParsedScript.EvaluateAsync(this.values);
97 }
98
105 {
106 throw new Exception("Script command is not a query.");
107 }
108
113 public ICommand Copy()
114 {
115 return new ScriptCommand(this.node, this.commandNode, this.parameters);
116 }
117
124 {
125 object Value;
126
127 foreach (ScriptParameterNode Parameter in this.parameters)
128 {
129 lock (this.values)
130 {
131 if (this.values.TryGetVariable(Parameter.ParameterName, out Variable v))
132 Value = v.ValueObject;
133 else
134 Value = null;
135 }
136
137 await Parameter.PopulateForm(Parameters, Language, Value);
138 }
139 }
140
148 public async Task<SetEditableFormResult> SetParameters(DataForm Parameters, Language Language, bool OnlySetChanged)
149 {
151
152 foreach (ScriptParameterNode Parameter in this.parameters)
153 await Parameter.SetParameter(Parameters, Language, OnlySetChanged, this.values, Result);
154
155 return Result;
156 }
157
158 }
159}
Static class managing editable parameters in objects. Editable parameters are defined by using the at...
Definition: Parameters.cs:25
Implements support for data forms. Data Forms are defined in the following XEPs:
Definition: DataForm.cs:42
Contains information about a language.
Definition: Language.cs:17
Contains information about a variable.
Definition: Variable.cs:10
Collection of variables.
Definition: Variables.cs:25
Class handling the reception of data from a query.
Definition: Query.cs:12
Tokens available in request.
Definition: RequestOrigin.cs:9
Represents a command on a script node.
string SortCategory
Sort Category, if available.
string SortKey
Sort Key, if available.
Task< bool > CanExecuteAsync(RequestOrigin Caller)
If the command can be executed by the caller.
Task< string > GetSuccessStringAsync(Language Language)
Gets a success string, if any, of the command. If no specific success string is available,...
Task ExecuteCommandAsync()
Executes the command.
Task< string > GetConfirmationStringAsync(Language Language)
Gets a confirmation string, if any, of the command. If no confirmation is necessary,...
string CommandID
ID of command.
async Task< SetEditableFormResult > SetParameters(DataForm Parameters, Language Language, bool OnlySetChanged)
Sets the parameters of the object, based on contents in the data form.
CommandType Type
Type of command.
ScriptCommand(VirtualNode Node, ScriptCommandNode CommandNode, ScriptParameterNode[] Parameters)
Represents a command on a script node.
Task< string > GetNameAsync(Language Language)
Gets the name of data source.
Task StartQueryExecutionAsync(Query Query, Language Language)
Starts the execution of a query.
ICommand Copy()
Creates a copy of the command object.
async Task PopulateForm(DataForm Parameters, Language Language)
Populates a data form with parameters for the object.
Task< string > GetFailureStringAsync(Language Language)
Gets a failure string, if any, of the command. If no specific failure string is available,...
Represents a command that can be executed on a script node or script reference node.
Represents a parameter on a command.
abstract 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.
abstract Task PopulateForm(DataForm Parameters, Language Language, object Value)
Populates a data form with parameters for the object.
Virtual node, that can be used as a placeholder for services.
Definition: VirtualNode.cs:28
Interface for commands.
Definition: ICommand.cs:32
CommandType
Command type.
Definition: ICommand.cs:11