Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SemanticDataSet.cs
1using System.Collections;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4
6{
11 {
12 private readonly LinkedList<ISemanticCube> sources = new LinkedList<ISemanticCube>();
13 private ISemanticCube first = null;
14 private int count = 0;
15
20 public SemanticDataSet(params ISemanticCube[] Sources)
21 : this((IEnumerable<ISemanticCube>)Sources)
22 {
23 }
24
29 public SemanticDataSet(IEnumerable<ISemanticCube> Sources)
30 {
31 foreach (ISemanticCube Source in Sources)
32 {
33 if (!(Source is null))
34 {
35 if (this.first is null)
36 this.first = Source;
37
38 this.sources.AddLast(Source);
39 this.count++;
40 }
41 }
42 }
43
48 public void Add(ISemanticCube Source)
49 {
50 if (!(Source is null))
51 {
52 if (this.first is null)
53 this.first = Source;
54
55 this.sources.AddLast(Source);
56 this.count++;
57 }
58 }
59
64 public IEnumerator<ISemanticTriple> GetEnumerator()
65 {
66 return this.CreateJoinedEnumerator();
67 }
68
73 IEnumerator IEnumerable.GetEnumerator()
74 {
75 return this.CreateJoinedEnumerator();
76 }
77
78 private IEnumerator<ISemanticTriple> CreateJoinedEnumerator()
79 {
80 LinkedList<IEnumerator<ISemanticTriple>> Enumerators = new LinkedList<IEnumerator<ISemanticTriple>>();
81
82 foreach (ISemanticCube Source in this.sources)
83 Enumerators.AddLast(Source.GetEnumerator());
84
85 return new JoinedTripleEnumerator(Enumerators, true);
86 }
87
93 public async Task<ISemanticPlane> GetTriplesBySubject(ISemanticElement Subject)
94 {
95 if (this.first is null)
96 return null;
97
98 if (this.count <= 1)
99 return await this.first.GetTriplesBySubject(Subject);
100 else
101 {
102 InMemorySemanticPlane Result = new InMemorySemanticPlane(Subject);
103
104 foreach (ISemanticCube Source in this.sources)
105 Result.Add(await Source.GetTriplesBySubject(Subject), 1, 2);
106
107 return Result;
108 }
109 }
110
116 public async Task<ISemanticPlane> GetTriplesByPredicate(ISemanticElement Predicate)
117 {
118 if (this.first is null)
119 return null;
120
121 if (this.count <= 1)
122 return await this.first.GetTriplesByPredicate(Predicate);
123 else
124 {
125 InMemorySemanticPlane Result = new InMemorySemanticPlane(Predicate);
126
127 foreach (ISemanticCube Source in this.sources)
128 Result.Add(await Source.GetTriplesByPredicate(Predicate), 0, 2);
129
130 return Result;
131 }
132 }
133
139 public async Task<ISemanticPlane> GetTriplesByObject(ISemanticElement Object)
140 {
141 if (this.first is null)
142 return null;
143
144 if (this.count <= 1)
145 return await this.first.GetTriplesByObject(Object);
146 else
147 {
148 InMemorySemanticPlane Result = new InMemorySemanticPlane(Object);
149
150 foreach (ISemanticCube Source in this.sources)
151 Result.Add(await Source.GetTriplesByObject(Object), 0, 1);
152
153 return Result;
154 }
155 }
156
163 public async Task<ISemanticLine> GetTriplesBySubjectAndPredicate(ISemanticElement Subject, ISemanticElement Predicate)
164 {
165 if (this.first is null)
166 return null;
167
168 if (this.count <= 1)
169 return await this.first.GetTriplesBySubjectAndPredicate(Subject, Predicate);
170 else
171 {
172 InMemorySemanticLine Result = new InMemorySemanticLine(Subject, Predicate);
173
174 foreach (ISemanticCube Source in this.sources)
175 Result.Add(await Source.GetTriplesBySubjectAndPredicate(Subject, Predicate), 2);
176
177 return Result;
178 }
179 }
180
187 public async Task<ISemanticLine> GetTriplesBySubjectAndObject(ISemanticElement Subject, ISemanticElement Object)
188 {
189 if (this.first is null)
190 return null;
191
192 if (this.count <= 1)
193 return await this.first.GetTriplesBySubjectAndObject(Subject, Object);
194 else
195 {
196 InMemorySemanticLine Result = new InMemorySemanticLine(Subject, Object);
197
198 foreach (ISemanticCube Source in this.sources)
199 Result.Add(await Source.GetTriplesBySubjectAndObject(Subject, Object), 1);
200
201 return Result;
202 }
203 }
204
211 public async Task<ISemanticLine> GetTriplesByPredicateAndSubject(ISemanticElement Predicate, ISemanticElement Subject)
212 {
213 if (this.first is null)
214 return null;
215
216 if (this.count <= 1)
217 return await this.first.GetTriplesByPredicateAndSubject(Predicate, Subject);
218 else
219 {
220 InMemorySemanticLine Result = new InMemorySemanticLine(Predicate, Subject);
221
222 foreach (ISemanticCube Source in this.sources)
223 Result.Add(await Source.GetTriplesByPredicateAndSubject(Predicate, Subject), 2);
224
225 return Result;
226 }
227 }
228
235 public async Task<ISemanticLine> GetTriplesByPredicateAndObject(ISemanticElement Predicate, ISemanticElement Object)
236 {
237 if (this.first is null)
238 return null;
239
240 if (this.count <= 1)
241 return await this.first.GetTriplesByPredicateAndObject(Predicate, Object);
242 else
243 {
244 InMemorySemanticLine Result = new InMemorySemanticLine(Predicate, Object);
245
246 foreach (ISemanticCube Source in this.sources)
247 Result.Add(await Source.GetTriplesByPredicateAndObject(Predicate, Object), 0);
248
249 return Result;
250 }
251 }
252
259 public async Task<ISemanticLine> GetTriplesByObjectAndSubject(ISemanticElement Object, ISemanticElement Subject)
260 {
261 if (this.first is null)
262 return null;
263
264 if (this.count <= 1)
265 return await this.first.GetTriplesByObjectAndSubject(Object, Subject);
266 else
267 {
268 InMemorySemanticLine Result = new InMemorySemanticLine(Object, Subject);
269
270 foreach (ISemanticCube Source in this.sources)
271 Result.Add(await Source.GetTriplesByObjectAndSubject(Object, Subject), 1);
272
273 return Result;
274 }
275 }
276
283 public async Task<ISemanticLine> GetTriplesByObjectAndPredicate(ISemanticElement Object, ISemanticElement Predicate)
284 {
285 if (this.first is null)
286 return null;
287
288 if (this.count <= 1)
289 return await this.first.GetTriplesByObjectAndPredicate(Object, Predicate);
290 else
291 {
292 InMemorySemanticLine Result = new InMemorySemanticLine(Object, Predicate);
293
294 foreach (ISemanticCube Source in this.sources)
295 Result.Add(await Source.GetTriplesByObjectAndPredicate(Object, Predicate), 0);
296
297 return Result;
298 }
299 }
300
308 public async Task<IEnumerable<ISemanticTriple>> GetTriplesBySubjectAndPredicateAndObject(ISemanticElement Subject, ISemanticElement Predicate, ISemanticElement Object)
309 {
310 if (this.first is null)
311 return null;
312
313 if (this.count <= 1)
314 return await this.first.GetTriplesBySubjectAndPredicateAndObject(Subject, Predicate, Object);
315 else
316 {
317 LinkedList<ISemanticTriple> Result = new LinkedList<ISemanticTriple>();
318
319 foreach (ISemanticCube Source in this.sources)
320 {
321 IEnumerable<ISemanticTriple> Part = await this.first.GetTriplesBySubjectAndPredicateAndObject(Subject, Predicate, Object);
322 if (Part is null)
323 continue;
324
325 foreach (ISemanticTriple Triple in Part)
326 Result.AddLast(Triple);
327 }
328
329 return Result;
330 }
331 }
332
337 public async Task<IEnumerator<ISemanticElement>> GetSubjectEnumerator()
338 {
339 LinkedList<IEnumerator<ISemanticElement>> Enumerators = new LinkedList<IEnumerator<ISemanticElement>>();
340
341 foreach (ISemanticCube Source in this.sources)
342 Enumerators.AddLast(await Source.GetSubjectEnumerator());
343
344 return new JoinedElementEnumerator(Enumerators, true);
345 }
346
351 public async Task<IEnumerator<ISemanticElement>> GetPredicateEnumerator()
352 {
353 LinkedList<IEnumerator<ISemanticElement>> Enumerators = new LinkedList<IEnumerator<ISemanticElement>>();
354
355 foreach (ISemanticCube Source in this.sources)
356 Enumerators.AddLast(await Source.GetPredicateEnumerator());
357
358 return new JoinedElementEnumerator(Enumerators, true);
359 }
360
365 public async Task<IEnumerator<ISemanticElement>> GetObjectEnumerator()
366 {
367 LinkedList<IEnumerator<ISemanticElement>> Enumerators = new LinkedList<IEnumerator<ISemanticElement>>();
368
369 foreach (ISemanticCube Source in this.sources)
370 Enumerators.AddLast(await Source.GetObjectEnumerator());
371
372 return new JoinedElementEnumerator(Enumerators, true);
373 }
374
381 public async Task<IEnumerable<ISemanticTriple>> GetTriples(ISemanticElement Value, int AxisIndex)
382 {
383 switch (AxisIndex)
384 {
385 case 0: return await this.GetTriplesBySubject(Value);
386 case 1: return await this.GetTriplesByPredicate(Value);
387 case 2: return await this.GetTriplesByObject(Value);
388 default: return null;
389 }
390 }
391
400 public async Task<IEnumerable<ISemanticTriple>> GetTriples(ISemanticElement Value1, int Axis1Index,
401 ISemanticElement Value2, int Axis2Index)
402 {
403 if (Axis1Index == Axis2Index)
404 {
405 if (Value1.Equals(Value2))
406 return await this.GetTriples(Value1, Axis1Index);
407 }
408 else
409 {
410 switch (Axis1Index)
411 {
412 case 0:
413 switch (Axis2Index)
414 {
415 case 1: return await this.GetTriplesBySubjectAndPredicate(Value1, Value2);
416 case 2: return await this.GetTriplesBySubjectAndObject(Value1, Value2);
417 }
418 break;
419
420 case 1:
421 switch (Axis2Index)
422 {
423 case 0: return await this.GetTriplesByPredicateAndSubject(Value1, Value2);
424 case 2: return await this.GetTriplesByPredicateAndObject(Value1, Value2);
425 }
426 break;
427
428 case 2:
429 switch (Axis2Index)
430 {
431 case 0: return await this.GetTriplesByObjectAndSubject(Value1, Value2);
432 case 1: return await this.GetTriplesByObjectAndPredicate(Value1, Value2);
433 }
434 break;
435 }
436 }
437
438 return null;
439 }
440 }
441}
Enumerator of elements from a collection of semantic data sources. The enumerator can remove duplicat...
async Task< ISemanticLine > GetTriplesBySubjectAndPredicate(ISemanticElement Subject, ISemanticElement Predicate)
Gets available triples in the cube, having a given subject and predicate.
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.
async Task< IEnumerable< ISemanticTriple > > GetTriples(ISemanticElement Value, int AxisIndex)
Gets available triples in the cube, having a given value, along a given axis.
async Task< ISemanticLine > GetTriplesByPredicateAndObject(ISemanticElement Predicate, ISemanticElement Object)
Gets available triples in the cube, having a given predicate and object.
async Task< ISemanticLine > GetTriplesBySubjectAndObject(ISemanticElement Subject, ISemanticElement Object)
Gets available triples in the cube, having a given subject and object.
async Task< ISemanticLine > GetTriplesByPredicateAndSubject(ISemanticElement Predicate, ISemanticElement Subject)
Gets available triples in the cube, having a given predicate and subject.
void Add(ISemanticCube Source)
Adds a source to the data set.
async Task< ISemanticLine > GetTriplesByObjectAndPredicate(ISemanticElement Object, ISemanticElement Predicate)
Gets available triples in the cube, having a given object and predicate.
async Task< IEnumerator< ISemanticElement > > GetPredicateEnumerator()
Gets an enumerator of all predicates.
async Task< ISemanticPlane > GetTriplesBySubject(ISemanticElement Subject)
Gets available triples in the cube, having a given subject.
async Task< ISemanticLine > GetTriplesByObjectAndSubject(ISemanticElement Object, ISemanticElement Subject)
Gets available triples in the cube, having a given object and subject.
async Task< ISemanticPlane > GetTriplesByPredicate(ISemanticElement Predicate)
Gets available triples in the cube, having a given predicate.
async Task< IEnumerator< ISemanticElement > > GetSubjectEnumerator()
Gets an enumerator of all subjects.
async Task< IEnumerable< ISemanticTriple > > GetTriplesBySubjectAndPredicateAndObject(ISemanticElement Subject, ISemanticElement Predicate, ISemanticElement Object)
Gets available triples in the cube, having a given subject, predicate and object.
IEnumerator< ISemanticTriple > GetEnumerator()
Gets an enumerator of available data sources.
SemanticDataSet(params ISemanticCube[] Sources)
In-memory semantic cube.
async Task< IEnumerator< ISemanticElement > > GetObjectEnumerator()
Gets an enumerator of all objects.
SemanticDataSet(IEnumerable< ISemanticCube > Sources)
In-memory semantic cube.
async Task< ISemanticPlane > GetTriplesByObject(ISemanticElement Object)
Gets available triples in the cube, having a given object.
Interface for semantic cubes.
Task< IEnumerator< ISemanticElement > > GetObjectEnumerator()
Gets an enumerator of all objects.
Task< ISemanticLine > GetTriplesByPredicateAndSubject(ISemanticElement Predicate, ISemanticElement Subject)
Gets available triples in the cube, having a given predicate and subject.
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 > GetTriplesBySubjectAndObject(ISemanticElement Subject, ISemanticElement Object)
Gets available triples in the cube, having a given subject and object.
Task< ISemanticLine > GetTriplesByObjectAndSubject(ISemanticElement Object, ISemanticElement Subject)
Gets available triples in the cube, having a given object and subject.
Task< ISemanticPlane > GetTriplesBySubject(ISemanticElement Subject)
Gets available triples in the cube, having a given subject.
Task< ISemanticLine > GetTriplesBySubjectAndPredicate(ISemanticElement Subject, ISemanticElement Predicate)
Gets available triples in the cube, having a given subject and predicate.
Task< IEnumerator< ISemanticElement > > GetPredicateEnumerator()
Gets an enumerator of all predicates.
Task< IEnumerator< ISemanticElement > > GetSubjectEnumerator()
Gets an enumerator of all subjects.
Task< ISemanticPlane > GetTriplesByPredicate(ISemanticElement Predicate)
Gets available triples in the cube, having a given predicate.
Task< ISemanticPlane > GetTriplesByObject(ISemanticElement Object)
Gets available triples in the cube, having a given object.
Task< ISemanticLine > GetTriplesByObjectAndPredicate(ISemanticElement Object, ISemanticElement Predicate)
Gets available triples in the cube, having a given object and predicate.
Task< ISemanticLine > GetTriplesByPredicateAndObject(ISemanticElement Predicate, ISemanticElement Object)
Gets available triples in the cube, having a given predicate and object.
Interface for semantic nodes.
Interface for semantic triples.