Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SemanticGraph.cs
2using System.Text;
3using System.Threading.Tasks;
7
9{
14 {
15 private readonly Dictionary<ISemanticElement, bool> nodes = new Dictionary<ISemanticElement, bool>();
16 private ISemanticElement lastSubject = null;
17 private ISemanticElement[] nodesStatic = null;
18
23 : base()
24 {
25 }
26
31 public override void Add(ISemanticTriple Triple)
32 {
33 base.Add(Triple);
34
35 if (this.lastSubject is null || !this.lastSubject.Equals(Triple.Subject))
36 {
37 this.nodes[Triple.Subject] = true;
38 this.lastSubject = Triple.Subject;
39 this.nodesStatic = null;
40 }
41
42 if (!Triple.Object.IsLiteral &&
43 Triple.Predicate is UriNode Predicate &&
44 Predicate.Uri != Rdf.type)
45 {
46 this.nodes[Triple.Object] = true;
47 this.nodesStatic = null;
48 }
49 }
50
55 {
56 get
57 {
58 if (this.nodesStatic is null)
59 {
60 ISemanticElement[] Result = new ISemanticElement[this.nodes.Count];
61 this.nodes.Keys.CopyTo(Result, 0);
62 this.nodesStatic = Result;
63 }
64
65 return this.nodesStatic;
66 }
67 }
68
73 public async Task<string> ExportPlantUml()
74 {
75 StringBuilder Output = new StringBuilder();
76 await this.ExportPlantUml(Output);
77 return Output.ToString();
78 }
79
84 public async Task ExportPlantUml(StringBuilder Output)
85 {
86 Output.AppendLine("@startuml");
87
88 Dictionary<ISemanticElement, string> NodeIds = new Dictionary<ISemanticElement, string>();
89 Dictionary<string, ChunkedList<LinkInfo>> LinksByNodeId = new Dictionary<string, ChunkedList<LinkInfo>>();
90 int i = 0;
91
92 foreach (ISemanticElement Node in this.Nodes)
93 {
94 if (!NodeIds.TryGetValue(Node, out string NodeId))
95 {
96 NodeId = "n" + (++i).ToString();
97 NodeIds[Node] = NodeId;
98 }
99 }
100
101 foreach (ISemanticElement Node in this.Nodes)
102 {
103 string NodeId = NodeIds[Node];
104 ISemanticPlane Plane = await this.GetTriplesBySubject(Node);
106 ChunkedList<LinkInfo> Links = null;
107 string StereoType = null;
108 bool IsRdfType;
109
110 if (!(Plane is null))
111 {
112 IEnumerator<ISemanticElement> Predicates = await Plane.GetXAxisEnumerator();
113
114 while (Predicates.MoveNext())
115 {
116 ISemanticLine Line = await Plane.GetTriplesByX(Predicates.Current);
117 string PropertyName;
118
119 if (Predicates.Current is UriNode UriNode)
120 {
121 PropertyName = UriNode.ShortName;
122 IsRdfType = UriNode.Uri == Rdf.type;
123 }
124 else
125 {
126 PropertyName = Predicates.Current.ToString();
127 IsRdfType = false;
128 }
129
130 if (!(Line is null))
131 {
132 IEnumerator<ISemanticElement> Values = await Line.GetValueEnumerator();
133
134 while (Values.MoveNext())
135 {
136 if (IsRdfType && StereoType is null)
137 {
138 if (Values.Current is UriNode UriNode2)
139 StereoType = UriNode2.ShortName;
140 else
141 StereoType = Values.Current.ToString();
142 }
143 else if (Values.Current is ISemanticLiteral Literal)
144 {
145 if (Properties is null)
147
148 Properties.Add(new KeyValuePair<string, object>(PropertyName, Literal.StringValue));
149 }
150 else if (NodeIds.TryGetValue(Values.Current, out string ObjectId))
151 {
152 if (Links is null)
153 Links = new ChunkedList<LinkInfo>();
154
155 Links.Add(new LinkInfo()
156 {
157 From = NodeId,
158 To = ObjectId,
159 Type = Node is BlankNode ? "*--" : "-->",
160 Label = PropertyName
161 });
162 }
163 else
164 {
165 if (Properties is null)
167
168 string ValueString;
169
170 if (Values.Current is UriNode UriNode2)
171 ValueString = UriNode2.ShortName;
172 else
173 ValueString = Values.Current.ToString();
174
175 Properties.Add(new KeyValuePair<string, object>(PropertyName, ValueString));
176 }
177 }
178 }
179 }
180 }
181
182 if (Properties is null)
183 {
184 Output.Append("object \"");
185
186 if (Node is UriNode UriNode)
187 Output.Append(JSON.Encode(UriNode.ShortName));
188 else
189 Output.Append(JSON.Encode(Node.ToString()));
190
191 Output.Append("\" as ");
192 Output.Append(NodeId);
193
194 if (!string.IsNullOrEmpty(StereoType))
195 {
196 Output.Append("<<");
197 Output.Append(StereoType);
198 Output.Append(">>");
199 }
200
201 Output.AppendLine();
202 }
203 else
204 {
205 Output.Append("map \"");
206
207 if (Node is UriNode UriNode)
208 Output.Append(JSON.Encode(UriNode.ShortName));
209 else
210 Output.Append(JSON.Encode(Node.ToString()));
211
212 Output.Append("\" as ");
213 Output.Append(NodeId);
214
215 if (!string.IsNullOrEmpty(StereoType))
216 {
217 Output.Append("<<");
218 Output.Append(StereoType);
219 Output.Append(">>");
220 }
221
222 Output.AppendLine(" {");
223
224 foreach (KeyValuePair<string, object> P in Properties)
225 {
226 Output.Append('\t');
227 Output.Append(P.Key);
228 Output.Append(" => ");
229 Output.AppendLine(P.Value?.ToString());
230 }
231
232 Output.AppendLine("}");
233 }
234
235 if (!(Links is null))
236 LinksByNodeId[NodeId] = Links;
237 }
238
239 foreach (ChunkedList<LinkInfo> Links in LinksByNodeId.Values)
240 {
241 foreach (LinkInfo Link in Links)
242 {
243 Output.Append(Link.From);
244 Output.Append(' ');
245 Output.Append(Link.Type);
246 Output.Append(' ');
247 Output.Append(Link.To);
248 Output.Append(" : ");
249 Output.AppendLine(Link.Label);
250 }
251 }
252
253 Output.AppendLine("@enduml");
254 }
255
256 private class LinkInfo
257 {
258 public string From;
259 public string To;
260 public string Type;
261 public string Label;
262 }
263
264 }
265}
Helps with common JSON-related tasks.
Definition: JSON.cs:16
static string Encode(string s)
Encodes a string for inclusion in JSON.
Definition: JSON.cs:537
Task< ISemanticPlane > GetTriplesBySubject(ISemanticElement Subject)
Gets available triples in the cube, having a given subject.
Represents a blank node
Definition: BlankNode.cs:7
string ShortName
Short name, if available.
Definition: UriNode.cs:54
static readonly Uri type
URI representing rdf:type
Definition: Rdf.cs:51
Contains triples that form a graph.
async Task< string > ExportPlantUml()
Exports graph to PlantUML.
async Task ExportPlantUml(StringBuilder Output)
Exports graph to PlantUML.
ISemanticElement[] Nodes
Nodes in graph.
override void Add(ISemanticTriple Triple)
Adds a triple to the model.
SemanticGraph()
Contains triples that form a graph.
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
void Add(T Item)
Adds an item to the collection.
Definition: ChunkedList.cs:272
Interface for semantic nodes.
bool IsLiteral
If element is a literal.
Interface for semantic lines.
Task< IEnumerator< ISemanticElement > > GetValueEnumerator()
Gets an enumerator of all values along the line.
Interface for semantic literals.
Interface for semantic planes.
Task< IEnumerator< ISemanticElement > > GetXAxisEnumerator()
Gets an enumerator of all elements along the X-axis.
Task< ISemanticLine > GetTriplesByX(ISemanticElement X)
Gets available triples in the plane, having a given X-coordinate.
Interface for semantic triples.
ISemanticElement Object
Object element
ISemanticElement Predicate
Predicate element
ISemanticElement Subject
Subject element
delegate string ToString(IElement Element)
Delegate for callback methods that convert an element value to a string.