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;
2using System.Collections;
3using System.Collections.Generic;
4using System.Threading.Tasks;
5
7{
12 {
13 private readonly LinkedList<Tuple<ISemanticElement, ISemanticElement, ISemanticTriple>> elements = new LinkedList<Tuple<ISemanticElement, ISemanticElement, ISemanticTriple>>();
14 private readonly ISemanticElement reference;
15 private SortedDictionary<ISemanticElement, InMemorySemanticLine> yPerX = null;
16 private SortedDictionary<ISemanticElement, InMemorySemanticLine> xPerY = null;
17
23 {
24 this.reference = Reference;
25 }
26
30 public ISemanticElement Reference => this.reference;
31
39 {
40 this.elements.AddLast(new Tuple<ISemanticElement, ISemanticElement, ISemanticTriple>(X, Y, Triple));
41 this.xPerY = null;
42 this.yPerX = null;
43 }
44
51 public void Add(IEnumerable<ISemanticTriple> Triples, int XIndex, int YIndex)
52 {
53 if (!(Triples is null))
54 {
55 foreach (ISemanticTriple Triple in Triples)
56 this.Add(Triple[XIndex], Triple[YIndex], Triple);
57 }
58 }
59
64 public IEnumerator<ISemanticTriple> GetEnumerator()
65 {
66 return new TripleEnumerator(this.elements.GetEnumerator());
67 }
68
73 IEnumerator IEnumerable.GetEnumerator()
74 {
75 return new TripleEnumerator(this.elements.GetEnumerator());
76 }
77
78 private class TripleEnumerator : IEnumerator<ISemanticTriple>
79 {
80 private readonly IEnumerator<Tuple<ISemanticElement, ISemanticElement, ISemanticTriple>> e;
81
82 public TripleEnumerator(IEnumerator<Tuple<ISemanticElement, ISemanticElement, ISemanticTriple>> e)
83 {
84 this.e = e;
85 }
86
87 public ISemanticTriple Current => this.e.Current.Item3;
88 object IEnumerator.Current => this.e.Current.Item3;
89 public void Dispose() => this.e.Dispose();
90 public bool MoveNext() => this.e.MoveNext();
91 public void Reset() => this.e.Reset();
92 }
93
99 public Task<ISemanticLine> GetTriplesByX(ISemanticElement X)
100 {
101 this.CheckXOrdered();
102
103 if (!this.yPerX.TryGetValue(X, out InMemorySemanticLine Line))
104 Line = null;
105
106 return Task.FromResult<ISemanticLine>(Line);
107 }
108
114 public Task<ISemanticLine> GetTriplesByY(ISemanticElement Y)
115 {
116 this.CheckYOrdered();
117
118 if (!this.xPerY.TryGetValue(Y, out InMemorySemanticLine Line))
119 Line = null;
120
121 return Task.FromResult<ISemanticLine>(Line);
122 }
123
130 public Task<IEnumerable<ISemanticTriple>> GetTriplesByXAndY(ISemanticElement X, ISemanticElement Y)
131 {
132 this.CheckXOrdered();
133
134 if (!this.yPerX.TryGetValue(X, out InMemorySemanticLine Line))
135 return Task.FromResult<IEnumerable<ISemanticTriple>>(null);
136
137 return Line.GetTriples(Y);
138 }
139
144 public Task<IEnumerator<ISemanticElement>> GetXAxisEnumerator()
145 {
146 this.CheckXOrdered();
147
148 return Task.FromResult<IEnumerator<ISemanticElement>>(this.yPerX.Keys.GetEnumerator());
149 }
150
155 public Task<IEnumerator<ISemanticElement>> GetYAxisEnumerator()
156 {
157 this.CheckYOrdered();
158
159 return Task.FromResult<IEnumerator<ISemanticElement>>(this.xPerY.Keys.GetEnumerator());
160 }
161
162 private void CheckXOrdered()
163 {
164 if (this.yPerX is null)
165 {
166 SortedDictionary<ISemanticElement, InMemorySemanticLine> Ordered =
167 new SortedDictionary<ISemanticElement, InMemorySemanticLine>();
168 ISemanticElement LastPoint = null;
169 InMemorySemanticLine Last = null;
170
171 foreach (Tuple<ISemanticElement, ISemanticElement, ISemanticTriple> P in this.elements)
172 {
173 if ((LastPoint is null || !LastPoint.Equals(P.Item1)) &&
174 !Ordered.TryGetValue(P.Item1, out Last))
175 {
176 Last = new InMemorySemanticLine(P.Item1, P.Item2);
177 Ordered[P.Item1] = Last;
178 }
179
180 Last.Add(P.Item2, P.Item3);
181 }
182
183 this.yPerX = Ordered;
184 }
185 }
186
187 private void CheckYOrdered()
188 {
189 if (this.xPerY is null)
190 {
191 SortedDictionary<ISemanticElement, InMemorySemanticLine> Ordered =
192 new SortedDictionary<ISemanticElement, InMemorySemanticLine>();
193 ISemanticElement LastPoint = null;
194 InMemorySemanticLine Last = null;
195
196 foreach (Tuple<ISemanticElement, ISemanticElement, ISemanticTriple> P in this.elements)
197 {
198 if ((LastPoint is null || !LastPoint.Equals(P.Item2)) &&
199 !Ordered.TryGetValue(P.Item2, out Last))
200 {
201 Last = new InMemorySemanticLine(P.Item2, P.Item1);
202 Ordered[P.Item2] = Last;
203 }
204
205 Last.Add(P.Item1, P.Item3);
206 }
207
208 this.xPerY = Ordered;
209 }
210 }
211 }
212}
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.
Interface for semantic nodes.
Interface for semantic lines.
Interface for semantic planes.
Interface for semantic triples.