Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ConditionalProcessor.cs
1using System;
2using System.Threading.Tasks;
6
8{
12 public class ConditionalProcessor : IProcessor<object>
13 {
14 private readonly IProcessor<object> processor;
15 private readonly bool isAsynchronous;
16 private readonly ScriptNode conditions;
17 private readonly Variables variables;
18 private ObjectProperties properties = null;
19
27 {
28 this.processor = Processor;
29 this.variables = Variables;
30 this.conditions = Conditions;
31 this.isAsynchronous = Processor.IsAsynchronous || Conditions.IsAsynchronous;
32 }
33
37 public bool IsAsynchronous => this.isAsynchronous;
38
44 public bool Process(object Object)
45 {
46 try
47 {
48 if (this.properties is null)
49 this.properties = new ObjectProperties(Object, this.variables);
50 else
51 this.properties.Object = Object;
52
53 IElement E = this.conditions.Evaluate(this.properties);
54 if (!(E.AssociatedObjectValue is bool B) || !B)
55 return true;
56
57 return this.processor.Process(Object);
58 }
59 catch (Exception)
60 {
61 return true;
62 }
63 }
64
70 public async Task<bool> ProcessAsync(object Object)
71 {
72 try
73 {
74 if (this.properties is null)
75 this.properties = new ObjectProperties(Object, this.variables);
76 else
77 this.properties.Object = Object;
78
79 IElement E = await this.conditions.EvaluateAsync(this.properties);
80 if (!(E.AssociatedObjectValue is bool B) || !B)
81 return true;
82
83 return await this.processor.ProcessAsync(Object);
84 }
85 catch (Exception)
86 {
87 return true;
88 }
89 }
90
95 public bool Flush() => this.processor.Flush();
96
101 public Task<bool> FlushAsync() => this.processor.FlushAsync();
102 }
103}
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
virtual bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: ScriptNode.cs:142
Processor that only processes elements matching a set of conditions.
ConditionalProcessor(IProcessor< object > Processor, Variables Variables, ScriptNode Conditions)
Processor that only processes elements matching a set of conditions.
bool IsAsynchronous
If the processor operates asynchronously.
bool Process(object Object)
Processes an object synchronously.
Task< bool > FlushAsync()
Called at the end of processing, to allow for flushing of buffers, etc.
bool Flush()
Called at the end of processing, to allow for flushing of buffers, etc.
async Task< bool > ProcessAsync(object Object)
Processes an object asynchronously.
Collection of variables.
Definition: Variables.cs:25
Interface for processors of objects.
Definition: IProcessor.cs:9
Basic interface for all types of elements.
Definition: IElement.cs:21
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:34