Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
InMemorySemanticCube.cs
1using System.Collections.Generic;
2using System.Threading.Tasks;
4
6{
11 {
12 private SortedDictionary<ISemanticElement, InMemorySemanticPlane> subjects = null;
13 private SortedDictionary<ISemanticElement, InMemorySemanticPlane> predicates = null;
14 private SortedDictionary<ISemanticElement, InMemorySemanticPlane> objects = null;
15
20 {
21 }
22
28 public static async Task<InMemorySemanticCube> Create(ISemanticModel Model)
29 {
30 if (Model is InMemorySemanticCube Result)
31 return Result;
32
33 Result = new InMemorySemanticCube();
34 await Result.Add(Model);
35
36 return Result;
37 }
38
43 public override void Add(ISemanticTriple Triple)
44 {
45 base.Add(Triple);
46
47 this.subjects = null;
48 this.predicates = null;
49 this.objects = null;
50 }
51
56 public virtual async Task Add(ISemanticModel Model)
57 {
58 IEnumerator<ISemanticTriple> e = Model.GetEnumerator();
59
60 if (e is IAsyncEnumerator eAsync)
61 {
62 while (await eAsync.MoveNextAsync())
63 this.Add(e.Current);
64 }
65 else
66 {
67 while (e.MoveNext())
68 this.Add(e.Current);
69 }
70 }
71
77 public Task<ISemanticPlane> GetTriplesBySubject(ISemanticElement Subject)
78 {
79 this.CheckSubjectsOrdered();
80
81 if (!this.subjects.TryGetValue(Subject, out InMemorySemanticPlane Plane))
82 Plane = null;
83
84 return Task.FromResult<ISemanticPlane>(Plane);
85 }
86
92 public Task<ISemanticPlane> GetTriplesByPredicate(ISemanticElement Predicate)
93 {
94 this.CheckPredicatesOrdered();
95
96 if (!this.predicates.TryGetValue(Predicate, out InMemorySemanticPlane Plane))
97 Plane = null;
98
99 return Task.FromResult<ISemanticPlane>(Plane);
100 }
101
107 public Task<ISemanticPlane> GetTriplesByObject(ISemanticElement Object)
108 {
109 this.CheckObjectsOrdered();
110
111 if (!this.objects.TryGetValue(Object, out InMemorySemanticPlane Plane))
112 Plane = null;
113
114 return Task.FromResult<ISemanticPlane>(Plane);
115 }
116
123 public Task<ISemanticLine> GetTriplesBySubjectAndPredicate(ISemanticElement Subject, ISemanticElement Predicate)
124 {
125 this.CheckSubjectsOrdered();
126
127 if (!this.subjects.TryGetValue(Subject, out InMemorySemanticPlane Plane))
128 return Task.FromResult<ISemanticLine>(null);
129
130 return Plane.GetTriplesByX(Predicate);
131 }
132
139 public Task<ISemanticLine> GetTriplesBySubjectAndObject(ISemanticElement Subject, ISemanticElement Object)
140 {
141 this.CheckSubjectsOrdered();
142
143 if (!this.subjects.TryGetValue(Subject, out InMemorySemanticPlane Plane))
144 return Task.FromResult<ISemanticLine>(null);
145
146 return Plane.GetTriplesByY(Object);
147 }
148
155 public Task<ISemanticLine> GetTriplesByPredicateAndSubject(ISemanticElement Predicate, ISemanticElement Subject)
156 {
157 this.CheckPredicatesOrdered();
158
159 if (!this.predicates.TryGetValue(Predicate, out InMemorySemanticPlane Plane))
160 return Task.FromResult<ISemanticLine>(null);
161
162 return Plane.GetTriplesByX(Subject);
163 }
164
171 public Task<ISemanticLine> GetTriplesByPredicateAndObject(ISemanticElement Predicate, ISemanticElement Object)
172 {
173 this.CheckPredicatesOrdered();
174
175 if (!this.predicates.TryGetValue(Predicate, out InMemorySemanticPlane Plane))
176 return Task.FromResult<ISemanticLine>(null);
177
178 return Plane.GetTriplesByY(Object);
179 }
180
187 public Task<ISemanticLine> GetTriplesByObjectAndSubject(ISemanticElement Object, ISemanticElement Subject)
188 {
189 this.CheckObjectsOrdered();
190
191 if (!this.objects.TryGetValue(Object, out InMemorySemanticPlane Plane))
192 return Task.FromResult<ISemanticLine>(null);
193
194 return Plane.GetTriplesByX(Subject);
195 }
196
203 public Task<ISemanticLine> GetTriplesByObjectAndPredicate(ISemanticElement Object, ISemanticElement Predicate)
204 {
205 this.CheckObjectsOrdered();
206
207 if (!this.objects.TryGetValue(Object, out InMemorySemanticPlane Plane))
208 return Task.FromResult<ISemanticLine>(null);
209
210 return Plane.GetTriplesByY(Predicate);
211 }
212
220 public Task<IEnumerable<ISemanticTriple>> GetTriplesBySubjectAndPredicateAndObject(ISemanticElement Subject, ISemanticElement Predicate, ISemanticElement Object)
221 {
222 this.CheckSubjectsOrdered();
223
224 if (!this.subjects.TryGetValue(Subject, out InMemorySemanticPlane Plane))
225 return Task.FromResult<IEnumerable<ISemanticTriple>>(null);
226
227 return Plane.GetTriplesByXAndY(Predicate, Object);
228 }
229
234 public Task<IEnumerator<ISemanticElement>> GetSubjectEnumerator()
235 {
236 this.CheckSubjectsOrdered();
237
238 return Task.FromResult<IEnumerator<ISemanticElement>>(this.subjects.Keys.GetEnumerator());
239 }
240
245 public Task<IEnumerator<ISemanticElement>> GetPredicateEnumerator()
246 {
247 this.CheckPredicatesOrdered();
248
249 return Task.FromResult<IEnumerator<ISemanticElement>>(this.predicates.Keys.GetEnumerator());
250 }
251
256 public Task<IEnumerator<ISemanticElement>> GetObjectEnumerator()
257 {
258 this.CheckObjectsOrdered();
259
260 return Task.FromResult<IEnumerator<ISemanticElement>>(this.objects.Keys.GetEnumerator());
261 }
262
263 private void CheckSubjectsOrdered()
264 {
265 if (this.subjects is null)
266 {
267 SortedDictionary<ISemanticElement, InMemorySemanticPlane> Ordered =
268 new SortedDictionary<ISemanticElement, InMemorySemanticPlane>();
269 ISemanticElement LastPoint = null;
270 InMemorySemanticPlane Last = null;
271
272 foreach (ISemanticTriple T in this.triples)
273 {
274 if ((LastPoint is null || !LastPoint.Equals(T.Subject)) &&
275 !Ordered.TryGetValue(T.Subject, out Last))
276 {
277 Last = new InMemorySemanticPlane(T.Subject);
278 Ordered[T.Subject] = Last;
279 }
280
281 Last.Add(T.Predicate, T.Object, T);
282 }
283
284 this.subjects = Ordered;
285 }
286 }
287
288 private void CheckPredicatesOrdered()
289 {
290 if (this.predicates is null)
291 {
292 SortedDictionary<ISemanticElement, InMemorySemanticPlane> Ordered =
293 new SortedDictionary<ISemanticElement, InMemorySemanticPlane>();
294 ISemanticElement LastPoint = null;
295 InMemorySemanticPlane Last = null;
296
297 foreach (ISemanticTriple T in this.triples)
298 {
299 if ((LastPoint is null || !LastPoint.Equals(T.Predicate)) &&
300 !Ordered.TryGetValue(T.Predicate, out Last))
301 {
302 Last = new InMemorySemanticPlane(T.Predicate);
303 Ordered[T.Predicate] = Last;
304 }
305
306 Last.Add(T.Subject, T.Object, T);
307 }
308
309 this.predicates = Ordered;
310 }
311 }
312
313 private void CheckObjectsOrdered()
314 {
315 if (this.objects is null)
316 {
317 SortedDictionary<ISemanticElement, InMemorySemanticPlane> Ordered =
318 new SortedDictionary<ISemanticElement, InMemorySemanticPlane>();
319 ISemanticElement LastPoint = null;
320 InMemorySemanticPlane Last = null;
321
322 foreach (ISemanticTriple T in this.triples)
323 {
324 if ((LastPoint is null || !LastPoint.Equals(T.Object)) &&
325 !Ordered.TryGetValue(T.Object, out Last))
326 {
327 Last = new InMemorySemanticPlane(T.Object);
328 Ordered[T.Object] = Last;
329 }
330
331 Last.Add(T.Subject, T.Predicate, T);
332 }
333
334 this.objects = Ordered;
335 }
336 }
337
344 public async Task<IEnumerable<ISemanticTriple>> GetTriples(ISemanticElement Value, int AxisIndex)
345 {
346 switch (AxisIndex)
347 {
348 case 0: return await this.GetTriplesBySubject(Value);
349 case 1: return await this.GetTriplesByPredicate(Value);
350 case 2: return await this.GetTriplesByObject(Value);
351 default: return null;
352 }
353 }
354
363 public async Task<IEnumerable<ISemanticTriple>> GetTriples(ISemanticElement Value1, int Axis1Index,
364 ISemanticElement Value2, int Axis2Index)
365 {
366 if (Axis1Index == Axis2Index)
367 {
368 if (Value1.Equals(Value2))
369 return await this.GetTriples(Value1, Axis1Index);
370 }
371 else
372 {
373 switch (Axis1Index)
374 {
375 case 0:
376 switch (Axis2Index)
377 {
378 case 1: return await this.GetTriplesBySubjectAndPredicate(Value1, Value2);
379 case 2: return await this.GetTriplesBySubjectAndObject(Value1, Value2);
380 }
381 break;
382
383 case 1:
384 switch (Axis2Index)
385 {
386 case 0: return await this.GetTriplesByPredicateAndSubject(Value1, Value2);
387 case 2: return await this.GetTriplesByPredicateAndObject(Value1, Value2);
388 }
389 break;
390
391 case 2:
392 switch (Axis2Index)
393 {
394 case 0: return await this.GetTriplesByObjectAndSubject(Value1, Value2);
395 case 1: return await this.GetTriplesByObjectAndPredicate(Value1, Value2);
396 }
397 break;
398 }
399 }
400
401 return null;
402 }
403
404 }
405}
async Task< IEnumerable< ISemanticTriple > > GetTriples(ISemanticElement Value, int AxisIndex)
Gets available triples in the cube, having a given value, along a given axis.
Task< IEnumerable< ISemanticTriple > > GetTriplesBySubjectAndPredicateAndObject(ISemanticElement Subject, ISemanticElement Predicate, ISemanticElement Object)
Gets available triples in the cube, having a given subject, predicate and object.
Task< ISemanticLine > GetTriplesBySubjectAndPredicate(ISemanticElement Subject, ISemanticElement Predicate)
Gets available triples in the cube, having a given subject and predicate.
Task< IEnumerator< ISemanticElement > > GetObjectEnumerator()
Gets an enumerator of all objects.
Task< ISemanticLine > GetTriplesByPredicateAndObject(ISemanticElement Predicate, ISemanticElement Object)
Gets available triples in the cube, having a given predicate and object.
Task< IEnumerator< ISemanticElement > > GetPredicateEnumerator()
Gets an enumerator of all predicates.
static async Task< InMemorySemanticCube > Create(ISemanticModel Model)
Creates an in-memory semantic cube from a semantic model.
override void Add(ISemanticTriple Triple)
Adds a triple to the cube.
Task< ISemanticPlane > GetTriplesByPredicate(ISemanticElement Predicate)
Gets available triples in the cube, having a given predicate.
virtual async Task Add(ISemanticModel Model)
Adds a model to the cube.
Task< ISemanticPlane > GetTriplesBySubject(ISemanticElement Subject)
Gets available triples in the cube, having a given subject.
Task< ISemanticLine > GetTriplesByObjectAndSubject(ISemanticElement Object, ISemanticElement Subject)
Gets available triples in the cube, having a given object and subject.
Task< IEnumerator< ISemanticElement > > GetSubjectEnumerator()
Gets an enumerator of all subjects.
Task< ISemanticLine > GetTriplesBySubjectAndObject(ISemanticElement Subject, ISemanticElement Object)
Gets available triples in the cube, having a given subject and object.
Task< ISemanticLine > GetTriplesByPredicateAndSubject(ISemanticElement Predicate, ISemanticElement Subject)
Gets available triples in the cube, having a given predicate and subject.
async Task< IEnumerable< ISemanticTriple > > GetTriples(ISemanticElement Value1, int Axis1Index, ISemanticElement Value2, int Axis2Index)
Gets available triples in the cube, having two given values, along two given axes.
Task< ISemanticLine > GetTriplesByObjectAndPredicate(ISemanticElement Object, ISemanticElement Predicate)
Gets available triples in the cube, having a given object and predicate.
Task< ISemanticPlane > GetTriplesByObject(ISemanticElement Object)
Gets available triples in the cube, having a given object.
readonly LinkedList< ISemanticTriple > triples
Triples in model.
Interface for semantic cubes.
Interface for semantic nodes.
Interface for semantic lines.
Interface for semantic models.
Interface for semantic planes.
Interface for semantic triples.
ISemanticElement Object
Object element
ISemanticElement Predicate
Predicate element
ISemanticElement Subject
Subject element
Interface for asynchronous enumerators.