Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
InMemorySemanticCube.cs
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 using (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 }
72
78 public Task<ISemanticPlane> GetTriplesBySubject(ISemanticElement Subject)
79 {
80 this.CheckSubjectsOrdered();
81
82 if (!this.subjects.TryGetValue(Subject, out InMemorySemanticPlane Plane))
83 Plane = null;
84
85 return Task.FromResult<ISemanticPlane>(Plane);
86 }
87
93 public Task<ISemanticPlane> GetTriplesByPredicate(ISemanticElement Predicate)
94 {
95 this.CheckPredicatesOrdered();
96
97 if (!this.predicates.TryGetValue(Predicate, out InMemorySemanticPlane Plane))
98 Plane = null;
99
100 return Task.FromResult<ISemanticPlane>(Plane);
101 }
102
108 public Task<ISemanticPlane> GetTriplesByObject(ISemanticElement Object)
109 {
110 this.CheckObjectsOrdered();
111
112 if (!this.objects.TryGetValue(Object, out InMemorySemanticPlane Plane))
113 Plane = null;
114
115 return Task.FromResult<ISemanticPlane>(Plane);
116 }
117
124 public Task<ISemanticLine> GetTriplesBySubjectAndPredicate(ISemanticElement Subject, ISemanticElement Predicate)
125 {
126 this.CheckSubjectsOrdered();
127
128 if (!this.subjects.TryGetValue(Subject, out InMemorySemanticPlane Plane))
129 return Task.FromResult<ISemanticLine>(null);
130
131 return Plane.GetTriplesByX(Predicate);
132 }
133
140 public Task<ISemanticLine> GetTriplesBySubjectAndObject(ISemanticElement Subject, ISemanticElement Object)
141 {
142 this.CheckSubjectsOrdered();
143
144 if (!this.subjects.TryGetValue(Subject, out InMemorySemanticPlane Plane))
145 return Task.FromResult<ISemanticLine>(null);
146
147 return Plane.GetTriplesByY(Object);
148 }
149
156 public Task<ISemanticLine> GetTriplesByPredicateAndSubject(ISemanticElement Predicate, ISemanticElement Subject)
157 {
158 this.CheckPredicatesOrdered();
159
160 if (!this.predicates.TryGetValue(Predicate, out InMemorySemanticPlane Plane))
161 return Task.FromResult<ISemanticLine>(null);
162
163 return Plane.GetTriplesByX(Subject);
164 }
165
172 public Task<ISemanticLine> GetTriplesByPredicateAndObject(ISemanticElement Predicate, ISemanticElement Object)
173 {
174 this.CheckPredicatesOrdered();
175
176 if (!this.predicates.TryGetValue(Predicate, out InMemorySemanticPlane Plane))
177 return Task.FromResult<ISemanticLine>(null);
178
179 return Plane.GetTriplesByY(Object);
180 }
181
188 public Task<ISemanticLine> GetTriplesByObjectAndSubject(ISemanticElement Object, ISemanticElement Subject)
189 {
190 this.CheckObjectsOrdered();
191
192 if (!this.objects.TryGetValue(Object, out InMemorySemanticPlane Plane))
193 return Task.FromResult<ISemanticLine>(null);
194
195 return Plane.GetTriplesByX(Subject);
196 }
197
204 public Task<ISemanticLine> GetTriplesByObjectAndPredicate(ISemanticElement Object, ISemanticElement Predicate)
205 {
206 this.CheckObjectsOrdered();
207
208 if (!this.objects.TryGetValue(Object, out InMemorySemanticPlane Plane))
209 return Task.FromResult<ISemanticLine>(null);
210
211 return Plane.GetTriplesByY(Predicate);
212 }
213
221 public Task<IEnumerable<ISemanticTriple>> GetTriplesBySubjectAndPredicateAndObject(ISemanticElement Subject, ISemanticElement Predicate, ISemanticElement Object)
222 {
223 this.CheckSubjectsOrdered();
224
225 if (!this.subjects.TryGetValue(Subject, out InMemorySemanticPlane Plane))
226 return Task.FromResult<IEnumerable<ISemanticTriple>>(null);
227
228 return Plane.GetTriplesByXAndY(Predicate, Object);
229 }
230
235 public Task<IEnumerator<ISemanticElement>> GetSubjectEnumerator()
236 {
237 this.CheckSubjectsOrdered();
238
239 return Task.FromResult<IEnumerator<ISemanticElement>>(this.subjects.Keys.GetEnumerator());
240 }
241
246 public Task<IEnumerator<ISemanticElement>> GetPredicateEnumerator()
247 {
248 this.CheckPredicatesOrdered();
249
250 return Task.FromResult<IEnumerator<ISemanticElement>>(this.predicates.Keys.GetEnumerator());
251 }
252
257 public Task<IEnumerator<ISemanticElement>> GetObjectEnumerator()
258 {
259 this.CheckObjectsOrdered();
260
261 return Task.FromResult<IEnumerator<ISemanticElement>>(this.objects.Keys.GetEnumerator());
262 }
263
264 private void CheckSubjectsOrdered()
265 {
266 if (this.subjects is null)
267 {
268 SortedDictionary<ISemanticElement, InMemorySemanticPlane> Ordered =
269 new SortedDictionary<ISemanticElement, InMemorySemanticPlane>();
270 ISemanticElement LastPoint = null;
271 InMemorySemanticPlane Last = null;
272
273 foreach (ISemanticTriple T in this.triples)
274 {
275 if ((LastPoint is null || !LastPoint.Equals(T.Subject)) &&
276 !Ordered.TryGetValue(T.Subject, out Last))
277 {
278 Last = new InMemorySemanticPlane(T.Subject);
279 Ordered[T.Subject] = Last;
280 }
281
282 Last.Add(T.Predicate, T.Object, T);
283 }
284
285 this.subjects = Ordered;
286 }
287 }
288
289 private void CheckPredicatesOrdered()
290 {
291 if (this.predicates is null)
292 {
293 SortedDictionary<ISemanticElement, InMemorySemanticPlane> Ordered =
294 new SortedDictionary<ISemanticElement, InMemorySemanticPlane>();
295 ISemanticElement LastPoint = null;
296 InMemorySemanticPlane Last = null;
297
298 foreach (ISemanticTriple T in this.triples)
299 {
300 if ((LastPoint is null || !LastPoint.Equals(T.Predicate)) &&
301 !Ordered.TryGetValue(T.Predicate, out Last))
302 {
303 Last = new InMemorySemanticPlane(T.Predicate);
304 Ordered[T.Predicate] = Last;
305 }
306
307 Last.Add(T.Subject, T.Object, T);
308 }
309
310 this.predicates = Ordered;
311 }
312 }
313
314 private void CheckObjectsOrdered()
315 {
316 if (this.objects is null)
317 {
318 SortedDictionary<ISemanticElement, InMemorySemanticPlane> Ordered =
319 new SortedDictionary<ISemanticElement, InMemorySemanticPlane>();
320 ISemanticElement LastPoint = null;
321 InMemorySemanticPlane Last = null;
322
323 foreach (ISemanticTriple T in this.triples)
324 {
325 if ((LastPoint is null || !LastPoint.Equals(T.Object)) &&
326 !Ordered.TryGetValue(T.Object, out Last))
327 {
328 Last = new InMemorySemanticPlane(T.Object);
329 Ordered[T.Object] = Last;
330 }
331
332 Last.Add(T.Subject, T.Predicate, T);
333 }
334
335 this.objects = Ordered;
336 }
337 }
338
345 public async Task<IEnumerable<ISemanticTriple>> GetTriples(ISemanticElement Value, int AxisIndex)
346 {
347 switch (AxisIndex)
348 {
349 case 0: return await this.GetTriplesBySubject(Value);
350 case 1: return await this.GetTriplesByPredicate(Value);
351 case 2: return await this.GetTriplesByObject(Value);
352 default: return null;
353 }
354 }
355
364 public async Task<IEnumerable<ISemanticTriple>> GetTriples(ISemanticElement Value1, int Axis1Index,
365 ISemanticElement Value2, int Axis2Index)
366 {
367 if (Axis1Index == Axis2Index)
368 {
369 if (Value1.Equals(Value2))
370 return await this.GetTriples(Value1, Axis1Index);
371 }
372 else
373 {
374 switch (Axis1Index)
375 {
376 case 0:
377 switch (Axis2Index)
378 {
379 case 1: return await this.GetTriplesBySubjectAndPredicate(Value1, Value2);
380 case 2: return await this.GetTriplesBySubjectAndObject(Value1, Value2);
381 }
382 break;
383
384 case 1:
385 switch (Axis2Index)
386 {
387 case 0: return await this.GetTriplesByPredicateAndSubject(Value1, Value2);
388 case 2: return await this.GetTriplesByPredicateAndObject(Value1, Value2);
389 }
390 break;
391
392 case 2:
393 switch (Axis2Index)
394 {
395 case 0: return await this.GetTriplesByObjectAndSubject(Value1, Value2);
396 case 1: return await this.GetTriplesByObjectAndPredicate(Value1, Value2);
397 }
398 break;
399 }
400 }
401
402 return null;
403 }
404
405 }
406}
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.
ChunkedList< 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.