Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GraphStoreDbSource.cs
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Threading.Tasks;
10using Waher.Things;
11
13{
18 {
19 private readonly GraphReference reference;
20 private ISemanticCube cube = null;
21
26 {
27 this.reference = Reference;
28 }
29
35 public Grade Supports(Uri _)
36 {
37 return Grade.NotAtAll; // Explicitly selected by processor.
38 }
39
48 public Task<ISemanticCube> LoadGraph(Uri Source, ScriptNode Node, bool NullIfNotFound,
49 RequestOrigin Caller)
50 {
51 // TODO: Check access privileges
52
53 return Task.FromResult<ISemanticCube>(this);
54 }
55
61 public async Task<ISemanticPlane> GetTriplesBySubject(ISemanticElement Subject)
62 {
63 if (this.cube is null)
64 {
65 InMemorySemanticPlane Result = new InMemorySemanticPlane(Subject);
66 IEnumerable<DatabaseTriple> Triples = await Database.Find<DatabaseTriple>(new FilterAnd(
67 new FilterFieldEqualTo("GraphKey", this.reference.DatabaseKey),
68 new FilterFieldEqualTo("S", Subject)));
69
70 Result.Add(Triples, 1, 2);
71
72 return Result;
73 }
74 else
75 return await this.cube.GetTriplesBySubject(Subject);
76 }
77
83 public async Task<ISemanticPlane> GetTriplesByPredicate(ISemanticElement Predicate)
84 {
85 if (this.cube is null)
86 {
87 InMemorySemanticPlane Result = new InMemorySemanticPlane(Predicate);
88 IEnumerable<DatabaseTriple> Triples = await Database.Find<DatabaseTriple>(new FilterAnd(
89 new FilterFieldEqualTo("GraphKey", this.reference.DatabaseKey),
90 new FilterFieldEqualTo("P", Predicate)));
91
92 Result.Add(Triples, 0, 2);
93
94 return Result;
95 }
96 else
97 return await this.cube.GetTriplesByPredicate(Predicate);
98 }
99
105 public async Task<ISemanticPlane> GetTriplesByObject(ISemanticElement Object)
106 {
107 if (this.cube is null)
108 {
109 InMemorySemanticPlane Result = new InMemorySemanticPlane(Object);
110 IEnumerable<DatabaseTriple> Triples = await Database.Find<DatabaseTriple>(new FilterAnd(
111 new FilterFieldEqualTo("GraphKey", this.reference.DatabaseKey),
112 new FilterFieldEqualTo("O", Object)));
113
114 Result.Add(Triples, 0, 1);
115
116 return Result;
117 }
118 else
119 return await this.cube.GetTriplesByObject(Object);
120 }
121
128 public async Task<ISemanticLine> GetTriplesBySubjectAndPredicate(ISemanticElement Subject, ISemanticElement Predicate)
129 {
130 if (this.cube is null)
131 {
132 InMemorySemanticLine Result = new InMemorySemanticLine(Subject, Predicate);
133 IEnumerable<DatabaseTriple> Triples = await Database.Find<DatabaseTriple>(new FilterAnd(
134 new FilterFieldEqualTo("GraphKey", this.reference.DatabaseKey),
135 new FilterFieldEqualTo("S", Subject),
136 new FilterFieldEqualTo("P", Predicate)));
137
138 Result.Add(Triples, 2);
139
140 return Result;
141 }
142 else
143 return await this.cube.GetTriplesBySubjectAndPredicate(Subject, Predicate);
144 }
145
152 public async Task<ISemanticLine> GetTriplesBySubjectAndObject(ISemanticElement Subject, ISemanticElement Object)
153 {
154 if (this.cube is null)
155 {
156 InMemorySemanticLine Result = new InMemorySemanticLine(Subject, Object);
157 IEnumerable<DatabaseTriple> Triples = await Database.Find<DatabaseTriple>(new FilterAnd(
158 new FilterFieldEqualTo("GraphKey", this.reference.DatabaseKey),
159 new FilterFieldEqualTo("S", Subject),
160 new FilterFieldEqualTo("O", Object)));
161
162 Result.Add(Triples, 1);
163
164 return Result;
165 }
166 else
167 return await this.cube.GetTriplesBySubjectAndObject(Subject, Object);
168 }
169
176 public async Task<ISemanticLine> GetTriplesByPredicateAndSubject(ISemanticElement Predicate, ISemanticElement Subject)
177 {
178 if (this.cube is null)
179 {
180 InMemorySemanticLine Result = new InMemorySemanticLine(Predicate, Subject);
181 IEnumerable<DatabaseTriple> Triples = await Database.Find<DatabaseTriple>(new FilterAnd(
182 new FilterFieldEqualTo("GraphKey", this.reference.DatabaseKey),
183 new FilterFieldEqualTo("P", Predicate),
184 new FilterFieldEqualTo("S", Subject)));
185
186 Result.Add(Triples, 2);
187
188 return Result;
189 }
190 else
191 return await this.cube.GetTriplesByPredicateAndSubject(Predicate, Subject);
192 }
193
200 public async Task<ISemanticLine> GetTriplesByPredicateAndObject(ISemanticElement Predicate, ISemanticElement Object)
201 {
202 if (this.cube is null)
203 {
204 InMemorySemanticLine Result = new InMemorySemanticLine(Predicate, Object);
205 IEnumerable<DatabaseTriple> Triples = await Database.Find<DatabaseTriple>(new FilterAnd(
206 new FilterFieldEqualTo("GraphKey", this.reference.DatabaseKey),
207 new FilterFieldEqualTo("P", Predicate),
208 new FilterFieldEqualTo("O", Object)));
209
210 Result.Add(Triples, 0);
211
212 return Result;
213 }
214 else
215 return await this.cube.GetTriplesByPredicateAndObject(Predicate, Object);
216 }
217
224 public async Task<ISemanticLine> GetTriplesByObjectAndSubject(ISemanticElement Object, ISemanticElement Subject)
225 {
226 if (this.cube is null)
227 {
228 InMemorySemanticLine Result = new InMemorySemanticLine(Object, Subject);
229 IEnumerable<DatabaseTriple> Triples = await Database.Find<DatabaseTriple>(new FilterAnd(
230 new FilterFieldEqualTo("GraphKey", this.reference.DatabaseKey),
231 new FilterFieldEqualTo("O", Object),
232 new FilterFieldEqualTo("S", Subject)));
233
234 Result.Add(Triples, 1);
235
236 return Result;
237 }
238 else
239 return await this.cube.GetTriplesByObjectAndSubject(Object, Subject);
240 }
241
248 public async Task<ISemanticLine> GetTriplesByObjectAndPredicate(ISemanticElement Object, ISemanticElement Predicate)
249 {
250 if (this.cube is null)
251 {
252 InMemorySemanticLine Result = new InMemorySemanticLine(Object, Predicate);
253 IEnumerable<DatabaseTriple> Triples = await Database.Find<DatabaseTriple>(new FilterAnd(
254 new FilterFieldEqualTo("GraphKey", this.reference.DatabaseKey),
255 new FilterFieldEqualTo("O", Object),
256 new FilterFieldEqualTo("P", Predicate)));
257
258 Result.Add(Triples, 2);
259
260 return Result;
261 }
262 else
263 return await this.cube.GetTriplesByObjectAndPredicate(Object, Predicate);
264 }
265
273 public async Task<IEnumerable<ISemanticTriple>> GetTriplesBySubjectAndPredicateAndObject(ISemanticElement Subject, ISemanticElement Predicate, ISemanticElement Object)
274 {
275 if (this.cube is null)
276 {
277 IEnumerable<DatabaseTriple> Triples = await Database.Find<DatabaseTriple>(new FilterAnd(
278 new FilterFieldEqualTo("GraphKey", this.reference.DatabaseKey),
279 new FilterFieldEqualTo("S", Subject),
280 new FilterFieldEqualTo("P", Predicate),
281 new FilterFieldEqualTo("O", Object)));
282
283 return Triples;
284 }
285 else
286 return await this.cube.GetTriplesBySubjectAndPredicateAndObject(Subject, Predicate, Object);
287 }
288
289 private async Task<ISemanticCube> GetCube()
290 {
291 if (this.cube is null)
292 {
293 IEnumerable<DatabaseTriple> Triples = await Database.Find<DatabaseTriple>(
294 new FilterFieldEqualTo("GraphKey", this.reference.DatabaseKey));
296
297 foreach (DatabaseTriple T in Triples)
298 Cube.Add(T);
299
300 this.cube = Cube;
301 }
302
303 return this.cube;
304 }
305
310 public async Task<IEnumerator<ISemanticElement>> GetSubjectEnumerator()
311 {
312 ISemanticCube Cube = await this.GetCube();
313 return await Cube.GetSubjectEnumerator();
314 }
315
320 public async Task<IEnumerator<ISemanticElement>> GetPredicateEnumerator()
321 {
322 ISemanticCube Cube = await this.GetCube();
323 return await Cube.GetPredicateEnumerator();
324 }
325
330 public async Task<IEnumerator<ISemanticElement>> GetObjectEnumerator()
331 {
332 ISemanticCube Cube = await this.GetCube();
333 return await Cube.GetObjectEnumerator();
334 }
335
342 public async Task<IEnumerable<ISemanticTriple>> GetTriples(ISemanticElement Value, int AxisIndex)
343 {
344 switch (AxisIndex)
345 {
346 case 0: return await this.GetTriplesBySubject(Value);
347 case 1: return await this.GetTriplesByPredicate(Value);
348 case 2: return await this.GetTriplesByObject(Value);
349 default: return null;
350 }
351 }
352
361 public async Task<IEnumerable<ISemanticTriple>> GetTriples(ISemanticElement Value1, int Axis1Index,
362 ISemanticElement Value2, int Axis2Index)
363 {
364 if (Axis1Index == Axis2Index)
365 {
366 if (Value1.Equals(Value2))
367 return await this.GetTriples(Value1, Axis1Index);
368 }
369 else
370 {
371 switch (Axis1Index)
372 {
373 case 0:
374 switch (Axis2Index)
375 {
376 case 1: return await this.GetTriplesBySubjectAndPredicate(Value1, Value2);
377 case 2: return await this.GetTriplesBySubjectAndObject(Value1, Value2);
378 }
379 break;
380
381 case 1:
382 switch (Axis2Index)
383 {
384 case 0: return await this.GetTriplesByPredicateAndSubject(Value1, Value2);
385 case 2: return await this.GetTriplesByPredicateAndObject(Value1, Value2);
386 }
387 break;
388
389 case 2:
390 switch (Axis2Index)
391 {
392 case 0: return await this.GetTriplesByObjectAndSubject(Value1, Value2);
393 case 1: return await this.GetTriplesByObjectAndPredicate(Value1, Value2);
394 }
395 break;
396 }
397 }
398
399 return null;
400 }
401
406 public IEnumerator<ISemanticTriple> GetEnumerator()
407 {
408 return this.GetCube().Result.GetEnumerator();
409 }
410
415 IEnumerator IEnumerable.GetEnumerator()
416 {
417 return this.GetCube().Result.GetEnumerator();
418 }
419
420 }
421}
void Add(ISemanticElement Point, ISemanticTriple Triple)
Adds an element to the line.
void Add(ISemanticElement X, ISemanticElement Y, ISemanticTriple Triple)
Adds an element to the plane.
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:19
static Task< IEnumerable< object > > Find(string Collection, params string[] SortOrder)
Finds objects in a given collection.
Definition: Database.cs:247
This filter selects objects that conform to all child-filters provided.
Definition: FilterAnd.cs:10
This filter selects objects that have a named field equal to a given value.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
Contains a reference to a graph in the graph store.
Contains a reference to a graph in the graph store.
Graph source in the local graph store, based on triples persisted in the object database.
async Task< ISemanticLine > GetTriplesByObjectAndPredicate(ISemanticElement Object, ISemanticElement Predicate)
Gets available triples in the cube, having a given object and predicate.
async Task< ISemanticLine > GetTriplesBySubjectAndObject(ISemanticElement Subject, ISemanticElement Object)
Gets available triples in the cube, having a given subject and object.
async Task< IEnumerator< ISemanticElement > > GetSubjectEnumerator()
Gets an enumerator of all subjects.
async Task< ISemanticLine > GetTriplesByPredicateAndObject(ISemanticElement Predicate, ISemanticElement Object)
Gets available triples in the cube, having a given predicate and object.
async Task< IEnumerable< ISemanticTriple > > GetTriplesBySubjectAndPredicateAndObject(ISemanticElement Subject, ISemanticElement Predicate, ISemanticElement Object)
Gets available triples in the cube, having a given subject, predicate and object.
async Task< ISemanticPlane > GetTriplesByObject(ISemanticElement Object)
Gets available triples in the cube, having a given object.
async Task< IEnumerable< ISemanticTriple > > GetTriples(ISemanticElement Value, int AxisIndex)
Gets available triples in the cube, having a given value, along a given axis.
IEnumerator< ISemanticTriple > GetEnumerator()
Gets an enumerator of available triples.
async Task< IEnumerator< ISemanticElement > > GetObjectEnumerator()
Gets an enumerator of all objects.
async Task< ISemanticLine > GetTriplesByPredicateAndSubject(ISemanticElement Predicate, ISemanticElement Subject)
Gets available triples in the cube, having a given predicate and subject.
async Task< IEnumerator< ISemanticElement > > GetPredicateEnumerator()
Gets an enumerator of all predicates.
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.
Task< ISemanticCube > LoadGraph(Uri Source, ScriptNode Node, bool NullIfNotFound, RequestOrigin Caller)
Loads the graph
GraphStoreDbSource(GraphReference Reference)
Graph source in the local graph store, based on triples persisted in the object database.
async Task< ISemanticPlane > GetTriplesByPredicate(ISemanticElement Predicate)
Gets available triples in the cube, having a given predicate.
async Task< ISemanticLine > GetTriplesByObjectAndSubject(ISemanticElement Object, ISemanticElement Subject)
Gets available triples in the cube, having a given object and subject.
async Task< ISemanticPlane > GetTriplesBySubject(ISemanticElement Subject)
Gets available triples in the cube, having a given subject.
Grade Supports(Uri _)
How well a source with a given URI can be loaded by the class.
Tokens available in request.
Definition: RequestOrigin.cs:9
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.
Grade
Grade enumeration
Definition: Grade.cs:7