Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ToVector.cs
8
10{
15 {
26 {
27 }
28
36 {
37 return this.ConvertToVector(Operand);
38 }
39
40 private IElement ConvertToVector(IElement Operand)
41 {
42 if (this.nullCheck && Operand.AssociatedObjectValue is null)
43 return Operand;
44
46 return Operand;
47
48 if (Operand is IVector V)
49 return VectorDefinition.Encapsulate(V.VectorElements, false, this);
50
51 if (Operand is ISet S)
52 return VectorDefinition.Encapsulate(S.ChildElements, false, this);
53
55 return ToVector.ToVector();
56
57 if (Operand?.AssociatedObjectValue is IToVector ToVector2)
58 return ToVector2.ToVector();
59
60 return VectorDefinition.Encapsulate(new IElement[] { Operand }, false, this);
61 }
62
69 public override PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary<string, IElement> AlreadyFound)
70 {
71 if (!(CheckAgainst is IVector Vector))
72 {
73 Vector = this.ConvertToVector(CheckAgainst) as IVector;
74 if (Vector is null)
75 return PatternMatchResult.Match;
76 }
77
78 if (this.op is VariableReference Ref)
79 return this.op.PatternMatch(Vector, AlreadyFound);
80
81 Dictionary<string, PatternRec> VariableNames = null;
82
83 this.ForAllChildNodes((ScriptNode Node, out ScriptNode NewNode, object State) =>
84 {
85 NewNode = null;
86
87 if (Node is VariableReference Ref2)
88 {
89 if (VariableNames is null)
90 VariableNames = new Dictionary<string, PatternRec>();
91
92 if (!VariableNames.ContainsKey(Ref2.VariableName))
93 {
94 if (AlreadyFound.TryGetValue(Ref2.VariableName, out IElement E))
95 {
96 VariableNames[Ref2.VariableName] = new PatternRec()
97 {
98 New = null,
99 Prev = E
100 };
101 }
102 else
103 {
104 VariableNames[Ref2.VariableName] = new PatternRec()
105 {
106 New = new ChunkedList<IElement>(),
107 Prev = null
108 };
109 }
110 }
111 }
112
113 return true;
114 }, null, SearchMethod.TreeOrder);
115
116 foreach (IElement Element in Vector.VectorElements)
117 {
118 switch (this.op.PatternMatch(Element, AlreadyFound))
119 {
120 case PatternMatchResult.Match:
121 if (!(VariableNames is null))
122 {
123 foreach (KeyValuePair<string, PatternRec> P in VariableNames)
124 {
125 if (!(P.Value.New is null))
126 {
127 if (AlreadyFound.TryGetValue(P.Key, out IElement E))
128 {
129 P.Value.New.Add(E);
130 AlreadyFound.Remove(P.Key);
131 }
132 else
133 P.Value.New.Add(ObjectValue.Null);
134 }
135 }
136 }
137 break;
138
139 case PatternMatchResult.NoMatch:
140 return PatternMatchResult.NoMatch;
141
142 case PatternMatchResult.Unknown:
143 default:
144 return PatternMatchResult.Unknown;
145 }
146 }
147
148 if (!(VariableNames is null))
149 {
150 foreach (KeyValuePair<string, PatternRec> P in VariableNames)
151 {
152 if (!(P.Value.New is null))
153 AlreadyFound[P.Key] = VectorDefinition.Encapsulate(P.Value.New, false, this);
154 }
155 }
156
157 return PatternMatchResult.Match;
158 }
159
160 private class PatternRec
161 {
162 public ChunkedList<IElement> New;
163 public IElement Prev;
164 }
165
166 #region IIterativeEvaluation
167
171 public bool CanEvaluateIteratively => true;
172
178
179 #endregion
180 }
181}
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
Base class for all types of elements.
Definition: Element.cs:14
Class managing a script expression.
Definition: Expression.cs:41
Base class for all unary operators performing operand null checks.
bool NullCheck
If null check is to be used.
readonly bool nullCheck
If null should be returned if operand is null.
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
virtual PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary< string, IElement > AlreadyFound)
Performs a pattern match operation.
Definition: ScriptNode.cs:169
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
override bool ForAllChildNodes(ScriptNodeEventHandler Callback, object State, SearchMethod Order)
Calls the callback method for all child nodes.
Represents a variable reference.
static readonly ObjectValue Null
Null value.
Definition: ObjectValue.cs:88
override IElement Evaluate(IElement Operand, Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: ToVector.cs:35
IIterativeEvaluator CreateEvaluator()
Creates an iterative evaluator for the node.
override PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary< string, IElement > AlreadyFound)
Performs a pattern match operation.
Definition: ToVector.cs:69
ToVector(ScriptNode Operand, bool NullCheck, int Start, int Length, Expression Expression)
To-Vector operator.
Definition: ToVector.cs:24
static IElement Encapsulate(Array Elements, bool CanEncapsulateAsMatrix, ScriptNode Node)
Encapsulates the elements of a vector.
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:21
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:34
Basic interface for vectors.
Definition: IVector.cs:9
Basic interface for all types of module elements.
Basic interface for all types of sets.
Definition: ISet.cs:10
A function that can be iteratively evaluated, meaning it can be iteratively computed one element at a...
An interative evaluator of a function supporting the IIterativeEvaluation interface.
Interface for objects that can be converted into matrices.
Definition: IToVector.cs:9
PatternMatchResult
Status result of a pattern matching operation.
Definition: ScriptNode.cs:17
SearchMethod
Method to traverse the expression structure
Definition: ScriptNode.cs:38