-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathGraphVizUtilities.cs
More file actions
271 lines (236 loc) · 8.46 KB
/
GraphVizUtilities.cs
File metadata and controls
271 lines (236 loc) · 8.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Waher.Content.Semantic;
using Waher.Content.Semantic.Model;
using Waher.Content.Semantic.Model.Literals;
using Waher.Script;
namespace Waher.Content.Markdown.GraphViz
{
/// <summary>
/// GraphViz conversion utilities.
/// </summary>
public static class GraphVizUtilities
{
#region Converting a GraphViz diagram to an image
/// <summary>
/// Converts a GraphViz dot graph description to an image.
/// </summary>
/// <param name="GraphText">Graph description.</param>
/// <param name="ResultType">Type of image to return.</param>
/// <returns>Binary image.</returns>
public static Task<byte[]> DotToImage(string GraphText, ResultType ResultType)
{
return GraphVizToImage("dot", GraphText, ResultType);
}
/// <summary>
/// Converts a GraphViz neato graph description to an image.
/// </summary>
/// <param name="GraphText">Graph description.</param>
/// <param name="ResultType">Type of image to return.</param>
/// <returns>Binary image.</returns>
public static Task<byte[]> NeatoToImage(string GraphText, ResultType ResultType)
{
return GraphVizToImage("neato", GraphText, ResultType);
}
/// <summary>
/// Converts a GraphViz fdp graph description to an image.
/// </summary>
/// <param name="GraphText">Graph description.</param>
/// <param name="ResultType">Type of image to return.</param>
/// <returns>Binary image.</returns>
public static Task<byte[]> FdpToImage(string GraphText, ResultType ResultType)
{
return GraphVizToImage("fdp", GraphText, ResultType);
}
/// <summary>
/// Converts a GraphViz sfdp graph description to an image.
/// </summary>
/// <param name="GraphText">Graph description.</param>
/// <param name="ResultType">Type of image to return.</param>
/// <returns>Binary image.</returns>
public static Task<byte[]> SfdpToImage(string GraphText, ResultType ResultType)
{
return GraphVizToImage("sfdp", GraphText, ResultType);
}
/// <summary>
/// Converts a GraphViz twopi graph description to an image.
/// </summary>
/// <param name="GraphText">Graph description.</param>
/// <param name="ResultType">Type of image to return.</param>
/// <returns>Binary image.</returns>
public static Task<byte[]> TwoPiToImage(string GraphText, ResultType ResultType)
{
return GraphVizToImage("twopi", GraphText, ResultType);
}
/// <summary>
/// Converts a GraphViz circo graph description to an image.
/// </summary>
/// <param name="GraphText">Graph description.</param>
/// <param name="ResultType">Type of image to return.</param>
/// <returns>Binary image.</returns>
public static Task<byte[]> CircoToImage(string GraphText, ResultType ResultType)
{
return GraphVizToImage("circo", GraphText, ResultType);
}
/// <summary>
/// Converts a GraphViz graph description to an image.
/// </summary>
/// <param name="Language">Graph language.</param>
/// <param name="GraphText">Graph description.</param>
/// <param name="ResultType">Type of image to return.</param>
/// <returns>Binary image.</returns>
public static Task<byte[]> GraphVizToImage(string Language, string GraphText,
ResultType ResultType)
{
return GraphVizToImage(Language, GraphText, ResultType, new Variables());
}
/// <summary>
/// Converts a GraphViz graph description to an image.
/// </summary>
/// <param name="Language">Graph language.</param>
/// <param name="GraphText">Graph description.</param>
/// <param name="ResultType">Type of image to return.</param>
/// <param name="Variables">Variables for color definitions.</param>
/// <returns>Binary image.</returns>
public static async Task<byte[]> GraphVizToImage(string Language, string GraphText,
ResultType ResultType, Variables Variables)
{
GraphInfo Graph = await GraphViz.GetFileName(Language, GraphText, ResultType, true, Variables);
return await Runtime.IO.Files.ReadAllBytesAsync(Graph.FileName);
}
#endregion
#region Converting semantic graphs to a GraphViz diagram
/// <summary>
/// Generates a GraphViz dot graph from a semantic graph.
/// </summary>
/// <param name="Graph">Semantic graph.</param>
/// <param name="Title">Title of graph.</param>
/// <returns>GraphViz dot graph.</returns>
public static string GenerateGraph(ISemanticModel Graph, string Title)
{
return GenerateGraph(null, Graph, Title);
}
/// <summary>
/// Generates a GraphViz dot graph from a semantic graph.
/// </summary>
/// <param name="ResultSet">SPARQL Result Set for shortening URLs and Blank Nodes</param>
/// <param name="Graph">Semantic graph.</param>
/// <param name="Title">Title of graph.</param>
/// <returns>GraphViz dot graph.</returns>
public static string GenerateGraph(SparqlResultSet ResultSet, ISemanticModel Graph, string Title)
{
if (ResultSet is null)
ResultSet = new SparqlResultSet(false); // Object only used for shortening URIs and Blank Nodes.
StringBuilder sb = new StringBuilder();
Dictionary<string, string> NodeNames = new Dictionary<string, string>();
sb.AppendLine("digraph G {");
sb.AppendLine("\trankdir=LR");
sb.AppendLine("\tbgcolor=\"transparent\"");
sb.AppendLine("\tnode [style=filled,fillcolor=white]");
if (!string.IsNullOrEmpty(Title))
{
sb.Append("\tlabel=\"");
sb.Append(JSON.Encode(Title));
sb.AppendLine("\"");
sb.AppendLine("\tlabelloc=\"t\"");
sb.AppendLine("\tfontsize=20");
}
int LiteralIndex = 0;
foreach (ISemanticTriple Triple in Graph)
{
AddShortName(ResultSet, Triple.Subject, NodeNames, sb, ref LiteralIndex);
AddShortName(ResultSet, Triple.Object, NodeNames, sb, ref LiteralIndex);
}
LiteralIndex = 0;
foreach (ISemanticTriple Triple in Graph)
{
sb.Append("\t");
sb.Append(GetNodeName(ResultSet, Triple.Subject, NodeNames, false, ref LiteralIndex));
sb.Append(" -> ");
sb.Append(GetNodeName(ResultSet, Triple.Object, NodeNames, false, ref LiteralIndex));
sb.Append(" [label=\"");
sb.Append(JSON.Encode(GetNodeName(ResultSet, Triple.Predicate, null, true, ref LiteralIndex)));
sb.AppendLine("\"]");
}
sb.AppendLine("}");
return sb.ToString();
}
private static string GetNodeName(SparqlResultSet Result, ISemanticElement Element,
Dictionary<string, string> NodeNames, bool IgnoreLiteral, ref int LiteralIndex)
{
string Name;
if (Element is UriNode UriNode)
Name = Result.GetShortUri(UriNode);
else if (Element is BlankNode BlankNode)
Name = Result.GetShortBlankNodeLabel(BlankNode);
else if (Element is SemanticLiteral)
{
if (IgnoreLiteral)
return "\"" + Element.ToString().Replace("\"", "\\\"") + "\"";
else
return "L" + (++LiteralIndex).ToString();
}
else
Name = Element.ToString();
if (NodeNames is null)
return Name;
else
return NodeNames[Name];
}
private static void AddShortName(SparqlResultSet Result, ISemanticElement Element,
Dictionary<string, string> NodeNames, StringBuilder sb, ref int LiteralIndex)
{
string Name;
string NodeName = null;
string AdditionalStyle = null;
if (Element is UriNode UriNode)
{
Name = Result.GetShortUri(UriNode);
AdditionalStyle = ",fillcolor=azure";
}
else if (Element is BlankNode BlankNode)
{
Name = Result.GetShortBlankNodeLabel(BlankNode);
AdditionalStyle = ",fillcolor=lightgray";
}
else if (Element is SemanticLiteral SemanticLiteral)
{
string Type = SemanticLiteral.StringType;
string ShortType = Result.GetShortUri(SemanticLiteral.StringType);
NodeName = "L" + (++LiteralIndex).ToString();
AdditionalStyle = ",fillcolor=honeydew,shape=box,style=\"rounded,filled\",margin=\"0.2,0\",height=0.35";
if (ShortType == Type)
Name = Element.ToString();
else if (SemanticLiteral is CustomLiteral CustomLiteral)
{
Name = CustomLiteral.ToString(CustomLiteral.StringValue,
CustomLiteral.Language, ShortType);
}
else
{
Name = CustomLiteral.ToString(SemanticLiteral.StringValue,
null, ShortType);
}
}
else
Name = Element.ToString();
if (string.IsNullOrEmpty(NodeName))
{
NodeName = "N" + (NodeNames.Count + 1).ToString();
if (NodeNames.ContainsKey(Name))
return;
NodeNames[Name] = NodeName;
}
sb.Append("\t");
sb.Append(NodeName);
sb.Append(" [label=\"");
sb.Append(JSON.Encode(Name));
sb.Append('"');
if (!string.IsNullOrEmpty(AdditionalStyle))
sb.Append(AdditionalStyle);
sb.AppendLine("]");
}
#endregion
}
}