Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
InMemorySemanticPlane.cs
1using System;
4using System.Threading.Tasks;
6
8{
13 {
15 private readonly ISemanticElement reference;
16 private SortedDictionary<ISemanticElement, InMemorySemanticLine> yPerX = null;
17 private SortedDictionary<ISemanticElement, InMemorySemanticLine> xPerY = null;
18
24 {
25 this.reference = Reference;
26 }
27
31 public ISemanticElement Reference => this.reference;
32
40 {
41 this.elements.Add(new Tuple<ISemanticElement, ISemanticElement, ISemanticTriple>(X, Y, Triple));
42 this.xPerY = null;
43 this.yPerX = null;
44 }
45
52 public void Add(IEnumerable<ISemanticTriple> Triples, int XIndex, int YIndex)
53 {
54 if (!(Triples is null))
55 {
56 foreach (ISemanticTriple Triple in Triples)
57 this.Add(Triple[XIndex], Triple[YIndex], Triple);
58 }
59 }
60
65 public IEnumerator<ISemanticTriple> GetEnumerator()
66 {
67 return new TripleEnumerator(this.elements.GetEnumerator());
68 }
69
74 IEnumerator IEnumerable.GetEnumerator()
75 {
76 return new TripleEnumerator(this.elements.GetEnumerator());
77 }
78
79 private class TripleEnumerator : IEnumerator<ISemanticTriple>
80 {
81 private readonly IEnumerator<Tuple<ISemanticElement, ISemanticElement, ISemanticTriple>> e;
82
83 public TripleEnumerator(IEnumerator<Tuple<ISemanticElement, ISemanticElement, ISemanticTriple>> e)
84 {
85 this.e = e;
86 }
87
88 public ISemanticTriple Current => this.e.Current.Item3;
89 object IEnumerator.Current => this.e.Current.Item3;
90 public void Dispose() => this.e.Dispose();
91 public bool MoveNext() => this.e.MoveNext();
92 public void Reset() => this.e.Reset();
93 }
94
100 public Task<ISemanticLine> GetTriplesByX(ISemanticElement X)
101 {
102 this.CheckXOrdered();
103
104 if (!this.yPerX.TryGetValue(X, out InMemorySemanticLine Line))
105 Line = null;
106
107 return Task.FromResult<ISemanticLine>(Line);
108 }
109
115 public Task<ISemanticLine> GetTriplesByY(ISemanticElement Y)
116 {
117 this.CheckYOrdered();
118
119 if (!this.xPerY.TryGetValue(Y, out InMemorySemanticLine Line))
120 Line = null;
121
122 return Task.FromResult<ISemanticLine>(Line);
123 }
124
131 public Task<IEnumerable<ISemanticTriple>> GetTriplesByXAndY(ISemanticElement X, ISemanticElement Y)
132 {
133 this.CheckXOrdered();
134
135 if (!this.yPerX.TryGetValue(X, out InMemorySemanticLine Line))
136 return Task.FromResult<IEnumerable<ISemanticTriple>>(null);
137
138 return Line.GetTriples(Y);
139 }
140
145 public Task<IEnumerator<ISemanticElement>> GetXAxisEnumerator()
146 {
147 this.CheckXOrdered();
148
149 return Task.FromResult<IEnumerator<ISemanticElement>>(this.yPerX.Keys.GetEnumerator());
150 }
151
156 public Task<IEnumerator<ISemanticElement>> GetYAxisEnumerator()
157 {
158 this.CheckYOrdered();
159
160 return Task.FromResult<IEnumerator<ISemanticElement>>(this.xPerY.Keys.GetEnumerator());
161 }
162
163 private void CheckXOrdered()
164 {
165 if (this.yPerX is null)
166 {
167 SortedDictionary<ISemanticElement, InMemorySemanticLine> Ordered =
168 new SortedDictionary<ISemanticElement, InMemorySemanticLine>();
169 ISemanticElement LastPoint = null;
170 InMemorySemanticLine Last = null;
171
172 foreach (Tuple<ISemanticElement, ISemanticElement, ISemanticTriple> P in this.elements)
173 {
174 if ((LastPoint is null || !LastPoint.Equals(P.Item1)) &&
175 !Ordered.TryGetValue(P.Item1, out Last))
176 {
177 Last = new InMemorySemanticLine(P.Item1, P.Item2);
178 Ordered[P.Item1] = Last;
179 }
180
181 Last.Add(P.Item2, P.Item3);
182 }
183
184 this.yPerX = Ordered;
185 }
186 }
187
188 private void CheckYOrdered()
189 {
190 if (this.xPerY is null)
191 {
192 SortedDictionary<ISemanticElement, InMemorySemanticLine> Ordered =
193 new SortedDictionary<ISemanticElement, InMemorySemanticLine>();
194 ISemanticElement LastPoint = null;
195 InMemorySemanticLine Last = null;
196
197 foreach (Tuple<ISemanticElement, ISemanticElement, ISemanticTriple> P in this.elements)
198 {
199 if ((LastPoint is null || !LastPoint.Equals(P.Item2)) &&
200 !Ordered.TryGetValue(P.Item2, out Last))
201 {
202 Last = new InMemorySemanticLine(P.Item2, P.Item1);
203 Ordered[P.Item2] = Last;
204 }
205
206 Last.Add(P.Item1, P.Item3);
207 }
208
209 this.xPerY = Ordered;
210 }
211 }
212 }
213}
Task< IEnumerable< ISemanticTriple > > GetTriplesByXAndY(ISemanticElement X, ISemanticElement Y)
Gets available triples in the plane, having a given X and Y-coordinate.
InMemorySemanticPlane(ISemanticElement Reference)
In-memory semantic plane.
ISemanticElement Reference
Plane reference.
IEnumerator< ISemanticTriple > GetEnumerator()
Gets an enumerator over all elements in plance.
Task< ISemanticLine > GetTriplesByX(ISemanticElement X)
Gets available triples in the plane, having a given X-coordinate.
Task< IEnumerator< ISemanticElement > > GetXAxisEnumerator()
Gets an enumerator of all elements along the X-axis.
Task< IEnumerator< ISemanticElement > > GetYAxisEnumerator()
Gets an enumerator of all elements along the Y-axis.
Task< ISemanticLine > GetTriplesByY(ISemanticElement Y)
Gets available triples in the plane, having a given Y-coordinate.
void Add(ISemanticElement X, ISemanticElement Y, ISemanticTriple Triple)
Adds an element to the plane.
void Add(IEnumerable< ISemanticTriple > Triples, int XIndex, int YIndex)
Adds a set of triples to the plane.
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
Interface for semantic nodes.
Interface for semantic lines.
Interface for semantic planes.
Interface for semantic triples.