Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GraphVizUtilities.cs
2using System.Text;
3using System.Threading.Tasks;
7using Waher.Script;
8
10{
14 public static class GraphVizUtilities
15 {
16 #region Converting a GraphViz diagram to an image
17
24 public static Task<byte[]> DotToImage(string GraphText, ResultType ResultType)
25 {
26 return GraphVizToImage("dot", GraphText, ResultType);
27 }
28
35 public static Task<byte[]> NeatoToImage(string GraphText, ResultType ResultType)
36 {
37 return GraphVizToImage("neato", GraphText, ResultType);
38 }
39
46 public static Task<byte[]> FdpToImage(string GraphText, ResultType ResultType)
47 {
48 return GraphVizToImage("fdp", GraphText, ResultType);
49 }
50
57 public static Task<byte[]> SfdpToImage(string GraphText, ResultType ResultType)
58 {
59 return GraphVizToImage("sfdp", GraphText, ResultType);
60 }
61
68 public static Task<byte[]> TwoPiToImage(string GraphText, ResultType ResultType)
69 {
70 return GraphVizToImage("twopi", GraphText, ResultType);
71 }
72
79 public static Task<byte[]> CircoToImage(string GraphText, ResultType ResultType)
80 {
81 return GraphVizToImage("circo", GraphText, ResultType);
82 }
83
91 public static Task<byte[]> GraphVizToImage(string Language, string GraphText,
93 {
94 return GraphVizToImage(Language, GraphText, ResultType, new Variables());
95 }
96
105 public static async Task<byte[]> GraphVizToImage(string Language, string GraphText,
107 {
108 GraphInfo Graph = await GraphViz.GetFileName(Language, GraphText, ResultType, true, Variables);
109 return await Runtime.IO.Files.ReadAllBytesAsync(Graph.FileName);
110 }
111
112 #endregion
113
114 #region Converting semantic graphs to a GraphViz diagram
115
122 public static string GenerateGraph(ISemanticModel Graph, string Title)
123 {
124 return GenerateGraph(null, Graph, Title);
125 }
126
134 public static string GenerateGraph(SparqlResultSet ResultSet, ISemanticModel Graph, string Title)
135 {
136 if (ResultSet is null)
137 ResultSet = new SparqlResultSet(false); // Object only used for shortening URIs and Blank Nodes.
138
139 StringBuilder sb = new StringBuilder();
140 Dictionary<string, string> NodeNames = new Dictionary<string, string>();
141
142 sb.AppendLine("digraph G {");
143 sb.AppendLine("\trankdir=LR");
144 sb.AppendLine("\tbgcolor=\"transparent\"");
145 sb.AppendLine("\tnode [style=filled,fillcolor=white]");
146
147 if (!string.IsNullOrEmpty(Title))
148 {
149 sb.Append("\tlabel=\"");
150 sb.Append(JSON.Encode(Title));
151 sb.AppendLine("\"");
152 sb.AppendLine("\tlabelloc=\"t\"");
153 sb.AppendLine("\tfontsize=20");
154 }
155
156 int LiteralIndex = 0;
157
158 foreach (ISemanticTriple Triple in Graph)
159 {
160 AddShortName(ResultSet, Triple.Subject, NodeNames, sb, ref LiteralIndex);
161 AddShortName(ResultSet, Triple.Object, NodeNames, sb, ref LiteralIndex);
162 }
163
164 LiteralIndex = 0;
165
166 foreach (ISemanticTriple Triple in Graph)
167 {
168 sb.Append("\t");
169 sb.Append(GetNodeName(ResultSet, Triple.Subject, NodeNames, false, ref LiteralIndex));
170 sb.Append(" -> ");
171 sb.Append(GetNodeName(ResultSet, Triple.Object, NodeNames, false, ref LiteralIndex));
172 sb.Append(" [label=\"");
173 sb.Append(JSON.Encode(GetNodeName(ResultSet, Triple.Predicate, null, true, ref LiteralIndex)));
174 sb.AppendLine("\"]");
175 }
176
177 sb.AppendLine("}");
178
179 return sb.ToString();
180 }
181
182 private static string GetNodeName(SparqlResultSet Result, ISemanticElement Element,
183 Dictionary<string, string> NodeNames, bool IgnoreLiteral, ref int LiteralIndex)
184 {
185 string Name;
186
187 if (Element is UriNode UriNode)
188 Name = Result.GetShortUri(UriNode);
189 else if (Element is BlankNode BlankNode)
190 Name = Result.GetShortBlankNodeLabel(BlankNode);
191 else if (Element is SemanticLiteral)
192 {
193 if (IgnoreLiteral)
194 return "\"" + Element.ToString().Replace("\"", "\\\"") + "\"";
195 else
196 return "L" + (++LiteralIndex).ToString();
197 }
198 else
199 Name = Element.ToString();
200
201 if (NodeNames is null)
202 return Name;
203 else
204 return NodeNames[Name];
205 }
206
207 private static void AddShortName(SparqlResultSet Result, ISemanticElement Element,
208 Dictionary<string, string> NodeNames, StringBuilder sb, ref int LiteralIndex)
209 {
210 string Name;
211 string NodeName = null;
212 string AdditionalStyle = null;
213
214 if (Element is UriNode UriNode)
215 {
216 Name = Result.GetShortUri(UriNode);
217 AdditionalStyle = ",fillcolor=azure";
218 }
219 else if (Element is BlankNode BlankNode)
220 {
221 Name = Result.GetShortBlankNodeLabel(BlankNode);
222 AdditionalStyle = ",fillcolor=lightgray";
223 }
224 else if (Element is SemanticLiteral SemanticLiteral)
225 {
226 string Type = SemanticLiteral.StringType;
227 string ShortType = Result.GetShortUri(SemanticLiteral.StringType);
228
229 NodeName = "L" + (++LiteralIndex).ToString();
230 AdditionalStyle = ",fillcolor=honeydew,shape=box,style=\"rounded,filled\",margin=\"0.2,0\",height=0.35";
231
232 if (ShortType == Type)
233 Name = Element.ToString();
235 {
237 CustomLiteral.Language, ShortType);
238 }
239 else
240 {
242 null, ShortType);
243 }
244 }
245 else
246 Name = Element.ToString();
247
248 if (string.IsNullOrEmpty(NodeName))
249 {
250 NodeName = "N" + (NodeNames.Count + 1).ToString();
251 if (NodeNames.ContainsKey(Name))
252 return;
253
254 NodeNames[Name] = NodeName;
255 }
256
257 sb.Append("\t");
258 sb.Append(NodeName);
259 sb.Append(" [label=\"");
260 sb.Append(JSON.Encode(Name));
261 sb.Append('"');
262
263 if (!string.IsNullOrEmpty(AdditionalStyle))
264 sb.Append(AdditionalStyle);
265
266 sb.AppendLine("]");
267 }
268
269 #endregion
270 }
271}
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
Class managing GraphViz integration into Markdown documents.
Definition: GraphViz.cs:61
static Task< byte[]> DotToImage(string GraphText, ResultType ResultType)
Converts a GraphViz dot graph description to an image.
static Task< byte[]> CircoToImage(string GraphText, ResultType ResultType)
Converts a GraphViz circo graph description to an image.
static string GenerateGraph(SparqlResultSet ResultSet, ISemanticModel Graph, string Title)
Generates a GraphViz dot graph from a semantic graph.
static string GenerateGraph(ISemanticModel Graph, string Title)
Generates a GraphViz dot graph from a semantic graph.
static Task< byte[]> TwoPiToImage(string GraphText, ResultType ResultType)
Converts a GraphViz twopi graph description to an image.
static Task< byte[]> NeatoToImage(string GraphText, ResultType ResultType)
Converts a GraphViz neato graph description to an image.
static async Task< byte[]> GraphVizToImage(string Language, string GraphText, ResultType ResultType, Variables Variables)
Converts a GraphViz graph description to an image.
static Task< byte[]> FdpToImage(string GraphText, ResultType ResultType)
Converts a GraphViz fdp graph description to an image.
static Task< byte[]> GraphVizToImage(string Language, string GraphText, ResultType ResultType)
Converts a GraphViz graph description to an image.
static Task< byte[]> SfdpToImage(string GraphText, ResultType ResultType)
Converts a GraphViz sfdp graph description to an image.
Represents a blank node
Definition: BlankNode.cs:7
Abstract base class for semantic literal values.
abstract string StringType
Type name (or null if literal value is a string)
string StringValue
String representation of value.
Contains the results of a SPARQL query. https://www.w3.org/TR/2023/WD-sparql12-results-xml-20230516/ ...
string GetShortBlankNodeLabel(BlankNode Node)
Gets a short blank node label for the given blank node.
string GetShortUri(UriNode Node)
Gets a short URI label for the given uri node.
Collection of variables.
Definition: Variables.cs:25
Interface for semantic nodes.
Interface for semantic models.
Interface for semantic triples.
ISemanticElement Object
Object element
ISemanticElement Predicate
Predicate element
ISemanticElement Subject
Subject element
ResultType
Image types supported by GraphViz
Definition: GraphViz.cs:35
delegate string ToString(IElement Element)
Delegate for callback methods that convert an element value to a string.