Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SemanticGraphs.cs
1using System.Collections.Generic;
2using System.Threading.Tasks;
4
6{
10 public static class SemanticGraphs
11 {
17 public static Task<InMemorySemanticCube> CreateInMemoryCube(ISemanticModel Model)
18 {
19 return InMemorySemanticCube.Create(Model);
20 }
21
26 public static void ClearTags(ISemanticModel Model)
27 {
28 foreach (ISemanticTriple Triple in Model)
29 {
30 Triple.Subject.Tag = null;
31 Triple.Predicate.Tag = null;
32 Triple.Object.Tag = null;
33 }
34 }
35
41 public static async Task<SemanticGraph> GetGraph(ISemanticModel Model)
42 {
43 SemanticGraph Result = new SemanticGraph();
44 IEnumerator<ISemanticTriple> e = Model.GetEnumerator();
45
46 if (e is IAsyncEnumerator eAsync)
47 {
48 while (await eAsync.MoveNextAsync())
49 Result.Add(e.Current);
50 }
51 else
52 {
53 while (e.MoveNext())
54 Result.Add(e.Current);
55 }
56
57 return Result;
58 }
59
65 public static async Task<SemanticGraph[]> GetConnectedGraphs(ISemanticModel Model)
66 {
68 return await GetConnectedGraphs(Model, Cube);
69 }
70
77 public static async Task<SemanticGraph[]> GetConnectedGraphs(ISemanticModel Model, ISemanticCube Cube)
78 {
79 Dictionary<ISemanticElement, int> Nodes = new Dictionary<ISemanticElement, int>();
80 List<SemanticGraph> Graphs = new List<SemanticGraph>();
81 LinkedList<ISemanticTriple> ToCheck = new LinkedList<ISemanticTriple>();
83 int NrTraces = 0;
84 int TraceNr;
85
86 ClearTags(Model);
87
88 foreach (ISemanticTriple Triple in Model)
89 {
90 if (Triple.Subject.Tag is null)
91 {
92 if (Nodes.TryGetValue(Triple.Subject, out TraceNr))
93 Triple.Subject.Tag = TraceNr;
94 else
95 {
96 TraceNr = NrTraces++;
97 Triple.Subject.Tag = TraceNr;
98 Nodes[Triple.Subject] = TraceNr;
99 Connections[TraceNr, TraceNr] = true;
100 }
101
102 ToCheck.AddLast(Triple);
103 }
104 else if (Triple.Object.Tag is null)
105 {
106 TraceNr = (int)Triple.Subject.Tag;
107
108 if (Triple.Object.IsLiteral)
109 {
110 Triple.Object.Tag = TraceNr;
111 continue;
112 }
113
114 if (Nodes.TryGetValue(Triple.Object, out int i))
115 {
116 if (i == TraceNr)
117 continue;
118
119 Connections[i, TraceNr] = true;
120 continue;
121 }
122 else
123 {
124 Triple.Object.Tag = TraceNr;
125 Nodes[Triple.Object] = TraceNr;
126
127 ISemanticPlane Plane = await Cube.GetTriplesByObject(Triple.Object);
128
129 foreach (ISemanticTriple T2 in Plane)
130 ToCheck.AddLast(T2);
131 }
132 }
133 else
134 {
135 if (!Triple.Subject.Tag.Equals(Triple.Object.Tag))
136 {
137 TraceNr = (int)Triple.Subject.Tag;
138 int TraceNr2 = (int)Triple.Object.Tag;
139
140 Connections[TraceNr, TraceNr2] = true;
141 }
142
143 continue;
144 }
145
146 while (!(ToCheck.First is null))
147 {
148 ISemanticTriple T = ToCheck.First.Value;
149 ToCheck.RemoveFirst();
150
151 if (T.Subject.Tag is null)
152 {
153 T.Subject.Tag = TraceNr;
154
155 if (Nodes.TryGetValue(T.Subject, out int i))
156 {
157 if (i != TraceNr)
158 Connections[i, TraceNr] = true;
159 }
160 else
161 Nodes[T.Subject] = TraceNr;
162 }
163 else
164 {
165 int i = (int)T.Subject.Tag;
166 if (i != TraceNr)
167 Connections[i, TraceNr] = true;
168 }
169
170 if (T.Object.Tag is null)
171 {
172 if (T.Object.IsLiteral)
173 T.Object.Tag = TraceNr;
174 else
175 {
176 if (Nodes.TryGetValue(T.Object, out int i))
177 {
178 if (i != TraceNr)
179 Connections[i, TraceNr] = true;
180 }
181 else
182 {
183 T.Object.Tag = TraceNr;
184 Nodes[T.Object] = TraceNr;
185
186 ISemanticPlane Plane = await Cube.GetTriplesByObject(T.Object);
187
188 foreach (ISemanticTriple T2 in Plane)
189 ToCheck.AddLast(T2);
190 }
191 }
192 }
193 else
194 {
195 int i = (int)T.Object.Tag;
196 if (i != TraceNr)
197 Connections[i, TraceNr] = true;
198 }
199 }
200 }
201
202 Dictionary<int, SemanticGraph> GraphByTrace = new Dictionary<int, SemanticGraph>();
203
204 while (NrTraces-- > 0)
205 {
206 if (!Connections[NrTraces, NrTraces])
207 continue;
208
209 SortedDictionary<int, bool> Connected = new SortedDictionary<int, bool>();
210 GetTraces(Connections, NrTraces, Connected);
211
212 SemanticGraph Graph = new SemanticGraph();
213 Graphs.Add(Graph);
214
215 foreach (int i in Connected.Keys)
216 GraphByTrace[i] = Graph;
217 }
218
219 int LastTraceNr = -1;
220 SemanticGraph LastGraph = null;
221
222 foreach (ISemanticTriple Triple in Model)
223 {
224 TraceNr = (int)Triple.Subject.Tag;
225
226 if (TraceNr != LastTraceNr)
227 {
228 LastTraceNr = TraceNr;
229 LastGraph = GraphByTrace[TraceNr];
230 }
231
232 LastGraph.Add(Triple);
233 }
234
235 return Graphs.ToArray();
236 }
237
238 private static void GetTraces(SymmetricMatrix<bool> M, int TraceNr, SortedDictionary<int, bool> Elements)
239 {
240 int i;
241
242 for (i = M.Size - 1; i >= 0; i--)
243 {
244 if (M[i, TraceNr])
245 {
246 M[i, TraceNr] = false;
247 Elements[i] = true;
248 GetTraces(M, i, Elements);
249 }
250 }
251 }
252 }
253}
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 ...
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.