Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ScriptConsolidator.cs
1using System;
3using System.Threading.Tasks;
6using Waher.Events;
9using Waher.Script;
14
16{
21 {
22 private readonly SortedDictionary<string, IElement> content = new SortedDictionary<string, IElement>();
23 private readonly Variables variables = HttpServer.CreateSessionVariables();
24 private readonly string threadId;
25 private readonly int expectedSources;
26 private object tag = null;
27
34 {
35 this.threadId = ThreadId;
36 this.expectedSources = ExpectedSources;
37 }
38
42 public string ThreadId
43 {
44 get => this.threadId;
45 }
46
50 public int ExpectedSources
51 {
52 get => this.expectedSources;
53 }
54
58 public object Tag
59 {
60 get => this.tag;
61 set => this.tag = value;
62 }
63
67 public Task<string[]> GetSources()
68 {
69 lock (this.content)
70 {
71 string[] Result = new string[this.content.Count];
72 this.content.Keys.CopyTo(Result, 0);
73 return Task.FromResult<string[]>(Result);
74 }
75 }
79 public Task<int> GetNrReportedSources()
80 {
81 lock (this.content)
82 {
83 return Task.FromResult<int>(this.content.Count);
84 }
85 }
86
93 public async Task<bool> Add(string Source, MarkdownDocument Markdown)
94 {
95 return await this.Add(Source, await Markdown.GenerateMarkdown(false));
96 }
97
105 public async Task<bool> Add(string Source, MarkdownDocument Markdown, string Id)
106 {
107 return await this.Add(Source, await Markdown.GenerateMarkdown(false), Id);
108 }
109
116 public Task<bool> Add(string Source, string Text)
117 {
118 return this.Add(Source, Text, string.Empty);
119 }
120
128 public Task<bool> Add(string Source, string Text, string Id)
129 {
130 return this.Add(Source, Text, Id, false);
131 }
132
140 public async Task<bool> Update(string Source, MarkdownDocument Markdown, string Id)
141 {
142 return await this.Update(Source, await Markdown.GenerateMarkdown(false), Id);
143 }
144
152 public Task<bool> Update(string Source, string Text, string Id)
153 {
154 return this.Add(Source, Text, Id, true);
155 }
156
157 private async Task<bool> Add(string Source, string Text, string _, bool Update)
158 {
159 IElement Content;
160 bool Result;
161
162 try
163 {
164 Expression Exp = new Expression(Text);
165 if (!(Exp.Root is null))
166 Content = await Exp.Root.EvaluateAsync(this.variables);
167 else
168 Content = ObjectValue.Null;
169 }
171 {
172 Content = ex.ReturnValue;
173 //ScriptReturnValueException.Reuse(ex);
174 }
175 catch (ScriptBreakLoopException ex)
176 {
177 Content = ex.LoopValue ?? ObjectValue.Null;
178 //ScriptBreakLoopException.Reuse(ex);
179 }
181 {
182 Content = ex.LoopValue ?? ObjectValue.Null;
183 //ScriptContinueLoopException.Reuse(ex);
184 }
185 catch (Exception ex)
186 {
187 Content = new ObjectValue(ex);
188 }
189
190 LinkedList<IDisposableAsync> ToDispose = null;
191
192 lock (this.content)
193 {
194 if (this.content.ContainsKey(Source))
195 {
196 if (Update)
197 this.content[Source] = Content;
198 else if (Content is IDisposableAsync DisposableAsync)
199 {
200 ToDispose ??= new LinkedList<IDisposableAsync>();
201 ToDispose.AddLast(DisposableAsync);
202 }
203 else if (Content is IDisposable Disposable)
204 Disposable.Dispose();
205
206 Result = false;
207 }
208 else
209 {
210 this.content[Source] = Content;
211 Result = true;
212 }
213 }
214
215 if (!(ToDispose is null))
216 {
217 foreach (IDisposableAsync DisposableAsync in ToDispose)
218 await DisposableAsync.DisposeAsync();
219 }
220
221 EventHandlerAsync<SourceEventArgs> h = Update ? this.Updated : this.Added;
222 await h.Raise(this, new SourceEventArgs(Source));
223
224 return Result;
225 }
226
230 [Obsolete("Use DisposeAsync() instead.")]
231 public void Dispose()
232 {
233 try
234 {
235 this.DisposeAsync().Wait();
236 }
237 catch (Exception ex)
238 {
239 Log.Exception(ex);
240 }
241 }
242
246 public Task DisposeAsync()
247 {
248 return this.Disposed.Raise(this, EventArgs.Empty, false);
249 }
250
254 public event EventHandlerAsync<SourceEventArgs> Added = null;
255
259 public event EventHandlerAsync<SourceEventArgs> Updated = null;
260
264 public event EventHandlerAsync Disposed = null;
265
271 {
272 LinkedList<IElement> Elements = new LinkedList<IElement>();
273
274 lock (this.content)
275 {
276 foreach (KeyValuePair<string, IElement> P in this.content)
277 {
278 if (P.Value is IVector Vector)
279 {
280 foreach (IElement E in Vector.ChildElements)
281 Elements.AddLast(this.Append(E, P.Key));
282 }
283 else
284 Elements.AddLast(this.Append(P.Value, P.Key));
285 }
286 }
287
288 return VectorDefinition.Encapsulate(Elements, false, null);
289 }
290
291 private IElement Append(IElement Element, string Source)
292 {
293 object Obj = Element.AssociatedObjectValue;
294
295 if (Obj is GenericObject GenObj)
296 {
297 GenObj["Source"] = Source;
298 return Element;
299 }
300 else if (Obj is IDictionary<string, IElement> ObjExNihilo)
301 {
302 ObjExNihilo["Source"] = new StringValue(Source);
303 return Element;
304 }
305 else
306 return Element;
307 }
308
309 }
310}
Contains a markdown document. This markdown document class supports original markdown,...
Task< string > GenerateMarkdown()
Generates Markdown from the markdown text.
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1657
Implements an HTTP server.
Definition: HttpServer.cs:41
static SessionVariables CreateSessionVariables()
Creates a new collection of variables, that contains access to the global set of variables.
Definition: HttpServer.cs:2130
Generic object. Contains a sequence of properties.
Base class for all types of elements.
Definition: Element.cs:14
abstract object AssociatedObjectValue
Associated object value.
Definition: Element.cs:43
Class managing a script expression.
Definition: Expression.cs:41
ScriptNode Root
Root script node.
Definition: Expression.cs:4496
static readonly ObjectValue Null
Null value.
Definition: ObjectValue.cs:88
static IElement Encapsulate(Array Elements, bool CanEncapsulateAsMatrix, ScriptNode Node)
Encapsulates the elements of a vector.
Collection of variables.
Definition: Variables.cs:25
Consolidates responses from occupants in a MUC room.
Task< string[]> GetSources()
Consolidated sources.
EventHandlerAsync Disposed
Event raised when consolidator has been disposed.
async Task< bool > Add(string Source, MarkdownDocument Markdown, string Id)
Adds incoming markdown information.
int ExpectedSources
Expected number of sources that will respond.
EventHandlerAsync< SourceEventArgs > Added
Event raised when content from a source has been added.
async Task< bool > Add(string Source, MarkdownDocument Markdown)
Adds incoming markdown information.
Task< bool > Add(string Source, string Text)
Adds incoming markdown information.
EventHandlerAsync< SourceEventArgs > Updated
Event raised when content from a source has been updated.
ScriptConsolidator(string ThreadId, int ExpectedSources)
Consolidates responses from occupants in a MUC room.
Task< int > GetNrReportedSources()
Number of sources that have reported content.
IVector GetResult()
Gets the result from all sources.
object Tag
External tag object that can be tagged to the object by its owner.
Task< bool > Update(string Source, string Text, string Id)
Updates incoming markdown information.
Task< bool > Add(string Source, string Text, string Id)
Adds incoming markdown information.
async Task< bool > Update(string Source, MarkdownDocument Markdown, string Id)
Updates incoming markdown information.
Interface for asynchronously disposable objects.
Task DisposeAsync()
Disposes of the object, asynchronously.
Basic interface for all types of elements.
Definition: IElement.cs:21
ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
Definition: IElement.cs:50
Basic interface for vectors.
Definition: IVector.cs:9
Definition: ImplTypes.g.cs:58
delegate Task EventHandlerAsync(object Sender, EventArgs e)
Asynchronous version of EventArgs.