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
125 {
126 object Value;
127
128 foreach (ScriptParameterNode Parameter in this.parameters)
129 {
130 lock (this.values)
131 {
132 if (this.values.TryGetVariable(Parameter.ParameterName, out Variable v))
133 Value = v.ValueObject;
134 else
135 Value = null;
136 }
137
138 await Parameter.PopulateForm(Parameters, Language, Value);
139 }
140 }
141
150 public async Task<SetEditableFormResult> SetParameters(DataForm Parameters, Language Language,
151 bool OnlySetChanged, IRequestOrigin Origin)
152 {
154
155 foreach (ScriptParameterNode Parameter in this.parameters)
156 await Parameter.SetParameter(Parameters, Language, OnlySetChanged, this.values, Result);
157
158 return Result;
159 }
160
161 }
162}
Static class managing editable parameters in objects. Editable parameters are defined by using the at...
Definition: Parameters.cs:26
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.
async Task PopulateForm(DataForm Parameters, Language Language, IRequestOrigin Origin)
Populates a data form with parameters for the object.
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.
async Task< SetEditableFormResult > SetParameters(DataForm Parameters, Language Language, bool OnlySetChanged, IRequestOrigin Origin)
Sets the parameters of the object, based on contents in the data form.
Task< string > GetConfirmationStringAsync(Language Language)
Gets a confirmation string, if any, of the command. If no confirmation is necessary,...
string CommandID
ID of command.
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 displayable name of the command.
Task StartQueryExecutionAsync(Query Query, Language Language)
Starts the execution of a query.
ICommand Copy()
Creates a copy of the command 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
Interface for requestors that can act as an origin for distributed requests.
CommandType
Command type.
Definition: ICommand.cs:11