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