Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
BinaryElementWiseOperator.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
6
7namespace Waher.Script.Model
8{
13 {
23 : base(Left, Right, Start, Length, Expression)
24 {
25 }
26
34 public override IElement Evaluate(IElement Left, IElement Right, Variables Variables)
35 {
36 ISet LS = Left.AssociatedSet;
37 ISet RS = Right.AssociatedSet;
38
39 if (!LS.Equals(RS))
40 {
41 bool b;
42
43 try
44 {
45 b = LS is IRightModule RM && RM.ScalarRing.Contains(Right);
46 }
47 catch (Exception)
48 {
49 b = false;
50 }
51
52 if (b)
53 {
54 LinkedList<IElement> Result = new LinkedList<IElement>();
55
56 foreach (IElement LeftChild in Left.ChildElements)
57 Result.AddLast(this.Evaluate(LeftChild, Right, Variables));
58
59 return Left.Encapsulate(Result, this);
60 }
61
62 try
63 {
64 b = RS is ILeftModule LM && LM.ScalarRing.Contains(Left);
65 }
66 catch (Exception)
67 {
68 b = false;
69 }
70
71 if (b)
72 {
73 LinkedList<IElement> Result = new LinkedList<IElement>();
74
75 foreach (IElement RightChild in Right.ChildElements)
76 Result.AddLast(this.Evaluate(Left, RightChild, Variables));
77
78 return Right.Encapsulate(Result, this);
79 }
80 }
81
82 return base.Evaluate(Left, Right, Variables);
83 }
84
92 public override async Task<IElement> EvaluateAsync(IElement Left, IElement Right, Variables Variables)
93 {
94 if (!this.IsAsynchronous)
95 return this.Evaluate(Left, Right, Variables);
96
97 ISet LS = Left.AssociatedSet;
98 ISet RS = Right.AssociatedSet;
99
100 if (!LS.Equals(RS))
101 {
102 bool b;
103
104 try
105 {
106 b = LS is IRightModule RM && RM.ScalarRing.Contains(Right);
107 }
108 catch (Exception)
109 {
110 b = false;
111 }
112
113 if (b)
114 {
115 LinkedList<IElement> Result = new LinkedList<IElement>();
116
117 foreach (IElement LeftChild in Left.ChildElements)
118 Result.AddLast(await this.EvaluateAsync(LeftChild, Right, Variables));
119
120 return Left.Encapsulate(Result, this);
121 }
122
123 try
124 {
125 b = RS is ILeftModule LM && LM.ScalarRing.Contains(Left);
126 }
127 catch (Exception)
128 {
129 b = false;
130 }
131
132 if (b)
133 {
134 LinkedList<IElement> Result = new LinkedList<IElement>();
135
136 foreach (IElement RightChild in Right.ChildElements)
137 Result.AddLast(await this.EvaluateAsync(Left, RightChild, Variables));
138
139 return Right.Encapsulate(Result, this);
140 }
141 }
142
143 return await base.EvaluateAsync(Left, Right, Variables);
144 }
145 }
146}
Class managing a script expression.
Definition: Expression.cs:39
Base class for binary element-wise operators.
override async Task< IElement > EvaluateAsync(IElement Left, IElement Right, Variables Variables)
Evaluates the operator.
BinaryElementWiseOperator(ScriptNode Left, ScriptNode Right, int Start, int Length, Expression Expression)
Base class for binary element-wise operators.
override IElement Evaluate(IElement Left, IElement Right, Variables Variables)
Evaluates the operator.
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Base class for binary scalar operators.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Collection of variables.
Definition: Variables.cs:25
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
IElement Encapsulate(ICollection< IElement > Elements, ScriptNode Node)
Encapsulates a set of elements into a similar structure as that provided by the current element.
Basic interface for all types of left modules.
Definition: ILeftModule.cs:10
Basic interface for all types of right modules.
Definition: IRightModule.cs:10
Basic interface for all types of sets.
Definition: ISet.cs:10
bool Contains(IElement Element)
Checks if the set contains an element.