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