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;
2using System.Collections.Generic;
3using System.Threading.Tasks;
6using Waher.Events;
10using Waher.Script;
15
17{
22 {
23 private readonly SortedDictionary<string, IElement> content = new SortedDictionary<string, IElement>();
24 private readonly Variables variables = HttpServer.CreateVariables();
25 private readonly string threadId;
26 private readonly int expectedSources;
27 private object tag = null;
28
35 {
36 this.threadId = ThreadId;
37 this.expectedSources = ExpectedSources;
38 }
39
43 public string ThreadId
44 {
45 get => this.threadId;
46 }
47
51 public int ExpectedSources
52 {
53 get => this.expectedSources;
54 }
55
59 public object Tag
60 {
61 get => this.tag;
62 set => this.tag = value;
63 }
64
68 public Task<string[]> GetSources()
69 {
70 lock (this.content)
71 {
72 string[] Result = new string[this.content.Count];
73 this.content.Keys.CopyTo(Result, 0);
74 return Task.FromResult<string[]>(Result);
75 }
76 }
80 public Task<int> GetNrReportedSources()
81 {
82 lock (this.content)
83 {
84 return Task.FromResult<int>(this.content.Count);
85 }
86 }
87
94 public async Task<bool> Add(string Source, MarkdownDocument Markdown)
95 {
96 return await this.Add(Source, await Markdown.GenerateMarkdown(false));
97 }
98
106 public async Task<bool> Add(string Source, MarkdownDocument Markdown, string Id)
107 {
108 return await this.Add(Source, await Markdown.GenerateMarkdown(false), Id);
109 }
110
117 public Task<bool> Add(string Source, string Text)
118 {
119 return this.Add(Source, Text, string.Empty);
120 }
121
129 public Task<bool> Add(string Source, string Text, string Id)
130 {
131 return this.Add(Source, Text, Id, false);
132 }
133
141 public async Task<bool> Update(string Source, MarkdownDocument Markdown, string Id)
142 {
143 return await this.Update(Source, await Markdown.GenerateMarkdown(false), Id);
144 }
145
153 public Task<bool> Update(string Source, string Text, string Id)
154 {
155 return this.Add(Source, Text, Id, true);
156 }
157
158 private async Task<bool> Add(string Source, string Text, string _, bool Update)
159 {
160 IElement Content;
161 bool Result;
162
163 try
164 {
165 Expression Exp = new Expression(Text);
166 if (!(Exp.Root is null))
167 Content = await Exp.Root.EvaluateAsync(this.variables);
168 else
169 Content = ObjectValue.Null;
170 }
172 {
173 Content = ex.ReturnValue;
174 }
175 catch (Exception ex)
176 {
177 Content = new ObjectValue(ex);
178 }
179
180 lock (this.content)
181 {
182 if (this.content.ContainsKey(Source))
183 {
184 if (Update)
185 this.content[Source] = Content;
186 else if (Content is IDisposable Disposable)
187 Disposable.Dispose();
188
189 Result = false;
190 }
191 else
192 {
193 this.content[Source] = Content;
194 Result = true;
195 }
196 }
197
198 EventHandlerAsync<SourceEventArgs> h = Update ? this.Updated : this.Added;
199 await h.Raise(this, new SourceEventArgs(Source));
200
201 return Result;
202 }
203
207 [Obsolete("Use DisposeAsync() instead.")]
208 public void Dispose()
209 {
210 try
211 {
212 this.DisposeAsync().Wait();
213 }
214 catch (Exception ex)
215 {
216 Log.Exception(ex);
217 }
218 }
219
223 public Task DisposeAsync()
224 {
225 return this.Disposed.Raise(this, EventArgs.Empty, false);
226 }
227
231 public event EventHandlerAsync<SourceEventArgs> Added = null;
232
236 public event EventHandlerAsync<SourceEventArgs> Updated = null;
237
241 public event EventHandlerAsync Disposed = null;
242
248 {
249 LinkedList<IElement> Elements = new LinkedList<IElement>();
250
251 lock (this.content)
252 {
253 foreach (KeyValuePair<string, IElement> P in this.content)
254 {
255 if (P.Value is IVector Vector)
256 {
257 foreach (IElement E in Vector.ChildElements)
258 Elements.AddLast(this.Append(E, P.Key));
259 }
260 else
261 Elements.AddLast(this.Append(P.Value, P.Key));
262 }
263 }
264
265 return VectorDefinition.Encapsulate(Elements, false, null);
266 }
267
268 private IElement Append(IElement Element, string Source)
269 {
270 object Obj = Element.AssociatedObjectValue;
271
272 if (Obj is GenericObject GenObj)
273 {
274 GenObj["Source"] = Source;
275 return Element;
276 }
277 else if (Obj is IDictionary<string, IElement> ObjExNihilo)
278 {
279 ObjExNihilo["Source"] = new StringValue(Source);
280 return Element;
281 }
282 else
283 return Element;
284 }
285
286 }
287}
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:13
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:1647
Implements an HTTP server.
Definition: HttpServer.cs:36
static Variables CreateVariables()
Creates a new collection of variables, that contains access to the global set of variables.
Definition: HttpServer.cs:1604
Generic object. Contains a sequence of properties.
Base class for all types of elements.
Definition: Element.cs:13
abstract object AssociatedObjectValue
Associated object value.
Definition: Element.cs:46
Class managing a script expression.
Definition: Expression.cs:39
ScriptNode Root
Root script node.
Definition: Expression.cs:4299
static readonly ObjectValue Null
Null value.
Definition: ObjectValue.cs:86
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.
Basic interface for all types of elements.
Definition: IElement.cs:20
ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
Definition: IElement.cs:49
Basic interface for vectors.
Definition: IVector.cs:9
delegate Task EventHandlerAsync(object Sender, EventArgs e)
Asynchronous version of EventArgs.