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;
3using System.Threading.Tasks;
10
12{
16 [CollectionName("GraphStore")]
17 [TypeName(TypeNameSerialization.None)]
18 [Index("GraphUri")]
19 [Index("Created")]
20 public class GraphReference
21 {
25 public const int DatabaseMinFileCount = 10;
26
31 {
32 }
33
37 [ObjectId]
38 public string ObjectId { get; set; }
39
43 public CaseInsensitiveString GraphUri { get; set; }
44
49
53 public string Folder { get; set; }
54
58 public DateTime Created { get; set; }
59
63 public DateTime Updated { get; set; }
64
68 public int NrFiles { get; set; }
69
73 public string[] Creators { get; set; }
74
78 [DefaultValue(false)]
79 public bool InDatabase { get; set; }
80
84 [DefaultValue(0L)]
85 public long DatabaseKey { get; set; }
86
91 public async Task<IGraphSource> GetGraphSource()
92 {
93 if (this.InDatabase)
94 return new GraphStoreDbSource(this);
95
96 GraphStoreFileSource FileSource = new GraphStoreFileSource(this);
97
98 if (this.NrFiles <= DatabaseMinFileCount)
99 return FileSource;
100
101 ISemanticCube Model = await FileSource.LoadGraph(new Uri(this.GraphUri), true);
102
103 this.DatabaseKey = await RuntimeCounters.IncrementCounter("GraphStore.LastGraphKey");
104 this.InDatabase = true;
105
106 await Database.Update(this);
107 await this.AddTriplesToDatabase(new IEnumerable<ISemanticTriple>[] { Model });
108
109 return new GraphStoreDbSource(this);
110 }
111
116 public async Task ModelsAdded(ChunkedList<ISemanticModel> Models)
117 {
118 if (!this.InDatabase && this.NrFiles > DatabaseMinFileCount)
119 {
120 GraphStoreFileSource FileSource = new GraphStoreFileSource(this);
121 ISemanticCube Model = await FileSource.LoadGraph(new Uri(this.GraphUri), true);
122
123 this.DatabaseKey = await RuntimeCounters.IncrementCounter("GraphStore.LastGraphKey");
124 this.InDatabase = true;
125
126 await Database.Update(this);
127
128 Models.Clear();
129 if (!(Model is null))
130 Models.Add(Model);
131 }
132
133 await this.AddTriplesToDatabase(Models);
134 }
135
140 public async Task AddTriplesToDatabase(IEnumerable<IEnumerable<ISemanticTriple>> Models)
141 {
143 int c = 0;
144
145 foreach (IEnumerable<ISemanticTriple> Model in Models)
146 {
147 foreach (ISemanticTriple T in Model)
148 {
149 if (T.Subject is SemanticElement S &&
150 T.Predicate is SemanticElement P &&
151 T.Object is SemanticElement O)
152 {
153 ToSave.Add(new DatabaseTriple()
154 {
155 GraphKey = this.DatabaseKey,
156 S = S,
157 P = P,
158 O = O
159 });
160
161 c++;
162
163 if (c >= 1000)
164 {
165 await Database.Insert(ToSave.ToArray());
166 ToSave.Clear();
167 c = 0;
168 }
169 }
170 }
171 }
172
173 if (c > 0)
174 await Database.Insert(ToSave.ToArray());
175 }
176 }
177}
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:21
static async Task Update(object Object)
Updates an object in the database.
Definition: Database.cs:1211
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
Definition: Database.cs:97
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
void Clear()
Clears the collection.
Definition: ChunkedList.cs:306
void Add(T Item)
Adds an item to the collection.
Definition: ChunkedList.cs:272
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 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.
async Task ModelsAdded(ChunkedList< ISemanticModel > Models)
Method called when new semantic models have been added to the graph.
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.