Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ToVector.cs
1using System.Collections.Generic;
6
8{
13 {
24 {
25 }
26
34 {
35 return this.ConvertToVector(Operand);
36 }
37
38 private IElement ConvertToVector(IElement Operand)
39 {
40 if (this.nullCheck && Operand.AssociatedObjectValue is null)
41 return Operand;
42
44 return Operand;
45
46 if (Operand is IVector V)
47 return VectorDefinition.Encapsulate(V.VectorElements, false, this);
48
49 if (Operand is ISet S)
50 return VectorDefinition.Encapsulate(S.ChildElements, false, this);
51
53 return ToVector.ToVector();
54
55 if (Operand?.AssociatedObjectValue is IToVector ToVector2)
56 return ToVector2.ToVector();
57
58 return VectorDefinition.Encapsulate(new IElement[] { Operand }, false, this);
59 }
60
67 public override PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary<string, IElement> AlreadyFound)
68 {
69 if (!(CheckAgainst is IVector Vector))
70 {
71 Vector = this.ConvertToVector(CheckAgainst) as IVector;
72 if (Vector is null)
73 return PatternMatchResult.Match;
74 }
75
76 if (this.op is VariableReference Ref)
77 return this.op.PatternMatch(Vector, AlreadyFound);
78
79 Dictionary<string, PatternRec> VariableNames = null;
80
81 this.ForAllChildNodes((ScriptNode Node, out ScriptNode NewNode, object State) =>
82 {
83 NewNode = null;
84
85 if (Node is VariableReference Ref2)
86 {
87 if (VariableNames is null)
88 VariableNames = new Dictionary<string, PatternRec>();
89
90 if (!VariableNames.ContainsKey(Ref2.VariableName))
91 {
92 if (AlreadyFound.TryGetValue(Ref2.VariableName, out IElement E))
93 {
94 VariableNames[Ref2.VariableName] = new PatternRec()
95 {
96 New = null,
97 Prev = E
98 };
99 }
100 else
101 {
102 VariableNames[Ref2.VariableName] = new PatternRec()
103 {
104 New = new List<IElement>(),
105 Prev = null
106 };
107 }
108 }
109 }
110
111 return true;
112 }, null, SearchMethod.TreeOrder);
113
114 foreach (IElement Element in Vector.VectorElements)
115 {
116 switch (this.op.PatternMatch(Element, AlreadyFound))
117 {
118 case PatternMatchResult.Match:
119 if (!(VariableNames is null))
120 {
121 foreach (KeyValuePair<string, PatternRec> P in VariableNames)
122 {
123 if (!(P.Value.New is null))
124 {
125 if (AlreadyFound.TryGetValue(P.Key, out IElement E))
126 {
127 P.Value.New.Add(E);
128 AlreadyFound.Remove(P.Key);
129 }
130 else
131 P.Value.New.Add(ObjectValue.Null);
132 }
133 }
134 }
135 break;
136
137 case PatternMatchResult.NoMatch:
138 return PatternMatchResult.NoMatch;
139
140 case PatternMatchResult.Unknown:
141 default:
142 return PatternMatchResult.Unknown;
143 }
144 }
145
146 if (!(VariableNames is null))
147 {
148 foreach (KeyValuePair<string, PatternRec> P in VariableNames)
149 {
150 if (!(P.Value.New is null))
151 AlreadyFound[P.Key] = VectorDefinition.Encapsulate(P.Value.New, false, this);
152 }
153 }
154
155 return PatternMatchResult.Match;
156 }
157
158 private class PatternRec
159 {
160 public List<IElement> New;
161 public IElement Prev;
162 }
163 }
164}
Base class for all types of elements.
Definition: Element.cs:13
Class managing a script expression.
Definition: Expression.cs:39
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:86
override IElement Evaluate(IElement Operand, Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
Definition: ToVector.cs:33
override PatternMatchResult PatternMatch(IElement CheckAgainst, Dictionary< string, IElement > AlreadyFound)
Performs a pattern match operation.
Definition: ToVector.cs:67
ToVector(ScriptNode Operand, bool NullCheck, int Start, int Length, Expression Expression)
To-Vector operator.
Definition: ToVector.cs:22
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:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33
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
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