Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GraphReference.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
9
11{
15 [CollectionName("GraphStore")]
16 [TypeName(TypeNameSerialization.None)]
17 [Index("GraphUri")]
18 [Index("Created")]
19 public class GraphReference
20 {
24 public const int DatabaseMinFileCount = 10;
25
30 {
31 }
32
36 [ObjectId]
37 public string ObjectId { get; set; }
38
42 public CaseInsensitiveString GraphUri { get; set; }
43
48
52 public string Folder { get; set; }
53
57 public DateTime Created { get; set; }
58
62 public DateTime Updated { get; set; }
63
67 public int NrFiles { get; set; }
68
72 public string[] Creators { get; set; }
73
77 [DefaultValue(false)]
78 public bool InDatabase { get; set; }
79
83 [DefaultValue(0L)]
84 public long DatabaseKey { get; set; }
85
90 public async Task<IGraphSource> GetGraphSource()
91 {
92 if (this.InDatabase)
93 return new GraphStoreDbSource(this);
94
95 GraphStoreFileSource FileSource = new GraphStoreFileSource(this);
96
97 if (this.NrFiles <= DatabaseMinFileCount)
98 return FileSource;
99
100 ISemanticCube Model = await FileSource.LoadGraph(new Uri(this.GraphUri), true);
101
102 this.DatabaseKey = await RuntimeCounters.IncrementCounter("GraphStore.LastGraphKey");
103 this.InDatabase = true;
104
105 await Database.Update(this);
106 await this.AddTriplesToDatabase(new IEnumerable<ISemanticTriple>[] { Model });
107
108 return new GraphStoreDbSource(this);
109 }
110
115 public async Task ModelsAdded(LinkedList<ISemanticModel> Models)
116 {
117 if (!this.InDatabase && this.NrFiles > DatabaseMinFileCount)
118 {
119 GraphStoreFileSource FileSource = new GraphStoreFileSource(this);
120 ISemanticCube Model = await FileSource.LoadGraph(new Uri(this.GraphUri), true);
121
122 this.DatabaseKey = await RuntimeCounters.IncrementCounter("GraphStore.LastGraphKey");
123 this.InDatabase = true;
124
125 await Database.Update(this);
126
127 Models.Clear();
128 if (!(Model is null))
129 Models.AddLast(Model);
130 }
131
132 await this.AddTriplesToDatabase(Models);
133 }
134
139 public async Task AddTriplesToDatabase(IEnumerable<IEnumerable<ISemanticTriple>> Models)
140 {
141 List<DatabaseTriple> ToSave = new List<DatabaseTriple>();
142 int c = 0;
143
144 foreach (IEnumerable<ISemanticTriple> Model in Models)
145 {
146 foreach (ISemanticTriple T in Model)
147 {
148 if (T.Subject is SemanticElement S &&
149 T.Predicate is SemanticElement P &&
150 T.Object is SemanticElement O)
151 {
152 ToSave.Add(new DatabaseTriple()
153 {
154 GraphKey = this.DatabaseKey,
155 S = S,
156 P = P,
157 O = O
158 });
159
160 c++;
161
162 if (c >= 1000)
163 {
164 await Database.Insert(ToSave.ToArray());
165 ToSave.Clear();
166 c = 0;
167 }
168 }
169 }
170 }
171
172 if (c > 0)
173 await Database.Insert(ToSave.ToArray());
174 }
175 }
176}
Abstract base class for semantic elements.
Represents a case-insensitive string.
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:19
static async Task Update(object Object)
Updates an object in the database.
Definition: Database.cs:626
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
Definition: Database.cs:95
Static class managing persistent counters.
static Task< long > IncrementCounter(CaseInsensitiveString Key)
Increments a counter.
Contains a reference to a graph in the graph store.
Contains a reference to a graph in the graph store.
async Task< IGraphSource > GetGraphSource()
Gets a Graph Source object corresponding to the graph referenced by the object.
GraphReference()
Contains a reference to a graph in the graph store.
async Task ModelsAdded(LinkedList< ISemanticModel > Models)
Method called when new semantic models have been added to the graph.
async Task AddTriplesToDatabase(IEnumerable< IEnumerable< ISemanticTriple > > Models)
Adds semantic triples to the database.
int NrFiles
Number of files used to define graph.
DateTime Created
When graph was created in graph store.
const int DatabaseMinFileCount
Number of files posted to a graph, before graph is converted to a database graph.
DateTime Updated
When graph was updated in graph store.
bool InDatabase
If triples are persisted in database.
string Folder
Folder where graph(s) are stored.
CaseInsensitiveString GraphDigest
Graph Digest, used to create file names.
Graph source in the local graph store, based on triples persisted in the object database.
Graph source in the local graph store, based on files.
Task< ISemanticCube > LoadGraph(Uri Source, ScriptNode Node, bool NullIfNotFound, RequestOrigin Caller)
Loads the graph
Interface for semantic cubes.
Interface for semantic triples.
ISemanticElement Object
Object element
ISemanticElement Predicate
Predicate element
ISemanticElement Subject
Subject element
TypeNameSerialization
How the type name should be serialized.