Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SemanticGraphs.cs
2using System.Threading.Tasks;
5
7{
11 public static class SemanticGraphs
12 {
18 public static Task<InMemorySemanticCube> CreateInMemoryCube(ISemanticModel Model)
19 {
20 return InMemorySemanticCube.Create(Model);
21 }
22
27 public static void ClearTags(ISemanticModel Model)
28 {
29 foreach (ISemanticTriple Triple in Model)
30 {
31 Triple.Subject.Tag = null;
32 Triple.Predicate.Tag = null;
33 Triple.Object.Tag = null;
34 }
35 }
36
42 public static async Task<SemanticGraph> GetGraph(ISemanticModel Model)
43 {
44 SemanticGraph Result = new SemanticGraph();
45 using (IEnumerator<ISemanticTriple> e = Model.GetEnumerator())
46 {
47 if (e is IAsyncEnumerator eAsync)
48 {
49 while (await eAsync.MoveNextAsync())
50 Result.Add(e.Current);
51 }
52 else
53 {
54 while (e.MoveNext())
55 Result.Add(e.Current);
56 }
57 }
58
59 return Result;
60 }
61
67 public static async Task<SemanticGraph[]> GetConnectedGraphs(ISemanticModel Model)
68 {
70 return await GetConnectedGraphs(Model, Cube);
71 }
72
79 public static async Task<SemanticGraph[]> GetConnectedGraphs(ISemanticModel Model, ISemanticCube Cube)
80 {
81 Dictionary<ISemanticElement, int> Nodes = new Dictionary<ISemanticElement, int>();
85 int NrTraces = 0;
86 int TraceNr;
87
88 ClearTags(Model);
89
90 foreach (ISemanticTriple Triple in Model)
91 {
92 if (Triple.Subject.Tag is null)
93 {
94 if (Nodes.TryGetValue(Triple.Subject, out TraceNr))
95 Triple.Subject.Tag = TraceNr;
96 else
97 {
98 TraceNr = NrTraces++;
99 Triple.Subject.Tag = TraceNr;
100 Nodes[Triple.Subject] = TraceNr;
101 Connections[TraceNr, TraceNr] = true;
102 }
103
104 ToCheck.Add(Triple);
105 }
106 else if (Triple.Object.Tag is null)
107 {
108 TraceNr = (int)Triple.Subject.Tag;
109
110 if (Triple.Object.IsLiteral)
111 {
112 Triple.Object.Tag = TraceNr;
113 continue;
114 }
115
116 if (Nodes.TryGetValue(Triple.Object, out int i))
117 {
118 if (i == TraceNr)
119 continue;
120
121 Connections[i, TraceNr] = true;
122 continue;
123 }
124 else
125 {
126 Triple.Object.Tag = TraceNr;
127 Nodes[Triple.Object] = TraceNr;
128
129 ISemanticPlane Plane = await Cube.GetTriplesByObject(Triple.Object);
130
131 foreach (ISemanticTriple T2 in Plane)
132 ToCheck.Add(T2);
133 }
134 }
135 else
136 {
137 if (!Triple.Subject.Tag.Equals(Triple.Object.Tag))
138 {
139 TraceNr = (int)Triple.Subject.Tag;
140 int TraceNr2 = (int)Triple.Object.Tag;
141
142 Connections[TraceNr, TraceNr2] = true;
143 }
144
145 continue;
146 }
147
148 while (ToCheck.HasFirstItem)
149 {
150 ISemanticTriple T = ToCheck.RemoveFirst();
151
152 if (T.Subject.Tag is null)
153 {
154 T.Subject.Tag = TraceNr;
155
156 if (Nodes.TryGetValue(T.Subject, out int i))
157 {
158 if (i != TraceNr)
159 Connections[i, TraceNr] = true;
160 }
161 else
162 Nodes[T.Subject] = TraceNr;
163 }
164 else
165 {
166 int i = (int)T.Subject.Tag;
167 if (i != TraceNr)
168 Connections[i, TraceNr] = true;
169 }
170
171 if (T.Object.Tag is null)
172 {
173 if (T.Object.IsLiteral)
174 T.Object.Tag = TraceNr;
175 else
176 {
177 if (Nodes.TryGetValue(T.Object, out int i))
178 {
179 if (i != TraceNr)
180 Connections[i, TraceNr] = true;
181 }
182 else
183 {
184 T.Object.Tag = TraceNr;
185 Nodes[T.Object] = TraceNr;
186
187 ISemanticPlane Plane = await Cube.GetTriplesByObject(T.Object);
188
189 foreach (ISemanticTriple T2 in Plane)
190 ToCheck.Add(T2);
191 }
192 }
193 }
194 else
195 {
196 int i = (int)T.Object.Tag;
197 if (i != TraceNr)
198 Connections[i, TraceNr] = true;
199 }
200 }
201 }
202
203 Dictionary<int, SemanticGraph> GraphByTrace = new Dictionary<int, SemanticGraph>();
204
205 while (NrTraces-- > 0)
206 {
207 if (!Connections[NrTraces, NrTraces])
208 continue;
209
210 SortedDictionary<int, bool> Connected = new SortedDictionary<int, bool>();
211 GetTraces(Connections, NrTraces, Connected);
212
213 SemanticGraph Graph = new SemanticGraph();
214 Graphs.Add(Graph);
215
216 foreach (int i in Connected.Keys)
217 GraphByTrace[i] = Graph;
218 }
219
220 int LastTraceNr = -1;
221 SemanticGraph LastGraph = null;
222
223 foreach (ISemanticTriple Triple in Model)
224 {
225 TraceNr = (int)Triple.Subject.Tag;
226
227 if (TraceNr != LastTraceNr)
228 {
229 LastTraceNr = TraceNr;
230 LastGraph = GraphByTrace[TraceNr];
231 }
232
233 LastGraph.Add(Triple);
234 }
235
236 return Graphs.ToArray();
237 }
238
239 private static void GetTraces(SymmetricMatrix<bool> M, int TraceNr, SortedDictionary<int, bool> Elements)
240 {
241 int i;
242
243 for (i = M.Size - 1; i >= 0; i--)
244 {
245 if (M[i, TraceNr])
246 {
247 M[i, TraceNr] = false;
248 Elements[i] = true;
249 GetTraces(M, i, Elements);
250 }
251 }
252 }
253 }
254}
static async Task< InMemorySemanticCube > Create(ISemanticModel Model)
Creates an in-memory semantic cube from a semantic model.
Contains triples that form a graph.
override void Add(ISemanticTriple Triple)
Adds a triple to the model.
Static class for extracting semantic graph information from semantic models.
static async Task< SemanticGraph > GetGraph(ISemanticModel Model)
Gets a graph from a semantic model.
static async Task< SemanticGraph[]> GetConnectedGraphs(ISemanticModel Model, ISemanticCube Cube)
Gets connected graphs available in a semantic model.
static async Task< SemanticGraph[]> GetConnectedGraphs(ISemanticModel Model)
Gets connected graphs available in a semantic model.
static Task< InMemorySemanticCube > CreateInMemoryCube(ISemanticModel Model)
Creates an in-memory semantic cube from a semantic model.
static void ClearTags(ISemanticModel Model)
Clears any tags set on elements in the model.
Implements a dynamic symmetric matrix.
int Size
Size of Matrix. The size represents both width and height, as a symmetric matrix have the same width ...
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
Interface for semantic cubes.
object Tag
Property used by processor, to tag information to an element.
bool IsLiteral
If element is a literal.
Interface for semantic models.
Interface for semantic planes.
Interface for semantic triples.
ISemanticElement Object
Object element
ISemanticElement Subject
Subject element
Interface for asynchronous enumerators.