Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ScriptQuery.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using Waher.Content;
8using Waher.Script;
14
15namespace Waher.Things.Script
16{
21 {
22 private readonly Variables values;
23 private readonly ScriptParameterNode[] parameters;
24 private readonly VirtualNode node;
25 private readonly ScriptQueryNode queryNode;
26
34 {
35 this.node = Node;
36 this.queryNode = QueryNode;
37 this.parameters = Parameters;
38
39 this.values = new Variables()
40 {
41 ["this"] = this.node
42 };
43 }
44
48 public string CommandID => this.queryNode.CommandId;
49
53 public CommandType Type => CommandType.Query;
54
58 public string SortCategory => this.queryNode.SortCategory;
59
63 public string SortKey => this.queryNode.SortKey;
64
69 public Task<string> GetNameAsync(Language Language) => Task.FromResult(this.queryNode.CommandName);
70
75 public Task<string> GetConfirmationStringAsync(Language Language) => Task.FromResult(this.queryNode.Confirmation);
76
81 public Task<string> GetFailureStringAsync(Language Language) => Task.FromResult(this.queryNode.Failure);
82
87 public Task<string> GetSuccessStringAsync(Language Language) => Task.FromResult(this.queryNode.Success);
88
94 public Task<bool> CanExecuteAsync(RequestOrigin Caller) => Task.FromResult(true); // TODO: Configure access rights.
95
99 public Task ExecuteCommandAsync()
100 {
101 throw new Exception("Script query is not a command.");
102 }
103
110 {
111 try
112 {
114
115 this.values["Query"] = Query;
116 this.values["Language"] = Query;
117
118 if (!Query.IsStarted)
119 await Query.Start();
120
121 await Query.SetStatus(await Namespace.GetStringAsync(95, "Executing Query..."));
122
123 object Result = await this.queryNode.ParsedScript.EvaluateAsync(this.values);
124
125 if (!Query.HasTitle)
126 await Query.SetTitle(await this.GetNameAsync(Language));
127
128 if (!Query.HasReported && !(Result is null))
129 {
130 await Query.SetStatus(await Namespace.GetStringAsync(96, "Processing Result..."));
131 await Query.BeginSection(await Namespace.GetStringAsync(94, "Query Result"));
132
133 if (Result is ObjectMatrix ResultSet)
134 {
135 int NrColumns = ResultSet.Columns;
136 int Column;
137 Column[] Columns = new Column[NrColumns];
138 string[] ColumnNames = ResultSet.ColumnNames;
139 int NrColumnNames = ColumnNames?.Length ?? 0;
140
141 for (Column = 0; Column < NrColumns; Column++)
142 {
143 IVector ColumnVector = ResultSet.GetColumn(Column);
144 ColumnAlignment Alignment = ColumnAlignment.Left;
145 string Header = Column < NrColumnNames ? ColumnNames[Column] : Column.ToString();
146 byte? NrDec = null;
147
148 if (ColumnVector is DoubleVector dv)
149 {
150 Alignment = ColumnAlignment.Right;
151 NrDec = 0;
152
153 foreach (double d in dv.Values)
154 {
155 byte NrDec2 = CommonTypes.GetNrDecimals(d);
156 if (NrDec2 > NrDec)
157 NrDec = NrDec2;
158 }
159 }
160 else if (!(ColumnVector is ObjectVector))
161 Alignment = ColumnAlignment.Center;
162
163 Columns[Column] = new Column("C" + Column.ToString(), Header, null, null, null, null, Alignment, NrDec);
164 }
165
166 await Query.NewTable("ResultSet", await Namespace.GetStringAsync(97, "Result Set"), Columns);
167
168 int NrRows = ResultSet.Rows;
169 List<Record> Records = new List<Record>();
170 List<object> RecordElements = new List<object>();
171
172 foreach (IElement RowElement in ResultSet.VectorElements)
173 {
174 if (!(RowElement is IVector RowVector))
175 continue;
176
177 RecordElements.Clear();
178 foreach (IElement Element in RowVector.VectorElements)
179 RecordElements.Add(Element.AssociatedObjectValue);
180
181 Records.Add(new Record(RecordElements.ToArray()));
182 }
183
184 await Query.NewRecords("ResultSet", Records.ToArray());
185 await Query.TableDone("ResultSet");
186 }
187 else
188 await Query.NewObject(Result);
189
190 await Query.EndSection();
191 }
192
193 await Query.SetStatus(string.Empty);
194 }
195 catch (Exception ex)
196 {
197 await Query.LogMessage(ex);
198 }
199 finally
200 {
201 if (!Query.IsDone && !Query.IsAborted)
202 await Query.Done();
203 }
204 }
205
210 public ICommand Copy()
211 {
212 return new ScriptQuery(this.node, this.queryNode, this.parameters);
213 }
214
221 {
222 object Value;
223
224 foreach (ScriptParameterNode Parameter in this.parameters)
225 {
226 lock (this.values)
227 {
228 if (this.values.TryGetVariable(Parameter.ParameterName, out Variable v))
229 Value = v.ValueObject;
230 else
231 Value = null;
232 }
233
234 await Parameter.PopulateForm(Parameters, Language, Value);
235 }
236 }
237
245 public async Task<SetEditableFormResult> SetParameters(DataForm Parameters, Language Language, bool OnlySetChanged)
246 {
248
249 foreach (ScriptParameterNode Parameter in this.parameters)
250 await Parameter.SetParameter(Parameters, Language, OnlySetChanged, this.values, Result);
251
252 return Result;
253 }
254
255 }
256}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static byte GetNrDecimals(double x)
Calculates the number of decimals of a floating-point number.
Definition: CommonTypes.cs:903
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
async Task< Namespace > GetNamespaceAsync(string Name)
Gets the namespace object, given its name, if available.
Definition: Language.cs:99
Contains information about a namespace in a language.
Definition: Namespace.cs:17
Task< LanguageString > GetStringAsync(int Id)
Gets the string object, given its ID, if available.
Definition: Namespace.cs:65
Base class for all types of elements.
Definition: Element.cs:13
abstract object AssociatedObjectValue
Associated object value.
Definition: Element.cs:46
Contains information about a variable.
Definition: Variable.cs:10
Collection of variables.
Definition: Variables.cs:25
Defines a column in a table.
Definition: Column.cs:30
Class handling the reception of data from a query.
Definition: Query.cs:12
Task TableDone(string TableId)
Reports a table as being complete.
Definition: Query.cs:318
Task EndSection()
Ends a section. Each call to BeginSection(string) must be followed by a call to EndSection().
Definition: Query.cs:495
bool IsDone
If the query is done.
Definition: Query.cs:86
Task NewObject(object Object)
Reports a new object.
Definition: Query.cs:345
bool HasReported
If anything has been reported.
Definition: Query.cs:154
bool IsStarted
If the query has been started.
Definition: Query.cs:76
bool HasTitle
If a title has been set
Definition: Query.cs:149
Task Start()
Starts query execution.
Definition: Query.cs:204
Task NewRecords(string TableId, params Record[] Records)
Reports a new set of records in a table.
Definition: Query.cs:291
bool IsAborted
If the query is aborted.
Definition: Query.cs:81
Task BeginSection(string Header)
Begins a new section. Sections can be nested. Each call to BeginSection(string) must be followed by a...
Definition: Query.cs:474
Task NewTable(string TableId, string TableName, params Column[] Columns)
Defines a new table in the query output.
Definition: Query.cs:263
Task LogMessage(Exception Exception)
Logs an Exception as a query message.
Definition: Query.cs:372
async Task Done()
Query execution completed.
Definition: Query.cs:232
Task SetStatus(string Status)
Sets the current status of the query execution.
Definition: Query.cs:448
Task SetTitle(string Title)
Sets the title of the report.
Definition: Query.cs:421
Defines a record in a table.
Definition: Record.cs:9
Tokens available in request.
Definition: RequestOrigin.cs:9
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.
Represents a query on a script node.
Definition: ScriptQuery.cs:21
Task< string > GetNameAsync(Language Language)
Gets the name of data source.
async Task< SetEditableFormResult > SetParameters(DataForm Parameters, Language Language, bool OnlySetChanged)
Sets the parameters of the object, based on contents in the data form.
Definition: ScriptQuery.cs:245
string CommandID
ID of command.
Definition: ScriptQuery.cs:48
string SortCategory
Sort Category, if available.
Definition: ScriptQuery.cs:58
async Task PopulateForm(DataForm Parameters, Language Language)
Populates a data form with parameters for the object.
Definition: ScriptQuery.cs:220
Task< string > GetConfirmationStringAsync(Language Language)
Gets a confirmation string, if any, of the command. If no confirmation is necessary,...
async Task StartQueryExecutionAsync(Query Query, Language Language)
Starts the execution of a query.
Definition: ScriptQuery.cs:109
Task< string > GetSuccessStringAsync(Language Language)
Gets a success string, if any, of the command. If no specific success string is available,...
ScriptQuery(VirtualNode Node, ScriptQueryNode QueryNode, ScriptParameterNode[] Parameters)
Represents a query on a script node.
Definition: ScriptQuery.cs:33
Task< string > GetFailureStringAsync(Language Language)
Gets a failure string, if any, of the command. If no specific failure string is available,...
CommandType Type
Type of command.
Definition: ScriptQuery.cs:53
ICommand Copy()
Creates a copy of the command object.
Definition: ScriptQuery.cs:210
Task< bool > CanExecuteAsync(RequestOrigin Caller)
If the command can be executed by the caller.
Task ExecuteCommandAsync()
Executes the command.
Definition: ScriptQuery.cs:99
string SortKey
Sort Key, if available.
Definition: ScriptQuery.cs:63
Represents a query that can be executed on a script node or script reference node.
Virtual node, that can be used as a placeholder for services.
Definition: VirtualNode.cs:28
Basic interface for all types of elements.
Definition: IElement.cs:20
Basic interface for vectors.
Definition: IVector.cs:9
Interface for commands.
Definition: ICommand.cs:32
ColumnAlignment
Column alignment.
Definition: Column.cs:9
CommandType
Command type.
Definition: ICommand.cs:11