graphviz.bmx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. SuperStrict
  2. Rem
  3. bbdoc: Text / Graphviz
  4. End Rem
  5. Module Text.Graphviz
  6. ModuleInfo "Version: 1.00"
  7. ModuleInfo "License: Eclipse Public License 1.0"
  8. ModuleInfo "Copyright: AT&T Research"
  9. ModuleInfo "Copyright: Wrapper - 2024 Bruce A Henderson"
  10. ModuleInfo "History: 1.00 Initial Release"
  11. ModuleInfo "History: Graphviz 12.2.1"
  12. Import "common.bmx"
  13. '
  14. ' Renamed tree_map() to gv_tree_map() to avoid conflict with BlitzMax's tree_map() function.
  15. '
  16. Rem
  17. bbdoc: A Graphviz context.
  18. about:
  19. End Rem
  20. Type TGVGraphviz
  21. Field gvcPtr:Byte Ptr
  22. Method New()
  23. gvcPtr = bmx_gvc_new()
  24. End Method
  25. Rem
  26. bbdoc: Lays out the graph.
  27. End Rem
  28. Method Layout:Int(graph:TAGraph, layout:String)
  29. Local res:Int = -1
  30. If gvcPtr Then
  31. Local t:Byte Ptr = layout.ToUtf8String()
  32. res = gvLayout(gvcPtr, graph.graphPtr, t)
  33. MemFree(t)
  34. End If
  35. Return res
  36. End Method
  37. Rem
  38. bbdoc: Converts the graph to SVG.
  39. End Rem
  40. Method ToSvg:String(graph:TAGraph)
  41. Return RenderToString(graph, "svg")
  42. End Method
  43. Rem
  44. bbdoc: Produces output in the DOT language.
  45. about: It reproduces the input, along with layout information for the graph. In particular, a bb attribute is attached to the graph,
  46. specifying the bounding box of the drawing. If the graph has a label, its position is specified by the lp attribute.
  47. Each node gets pos, width and the record rectangles are given in the rects attribute. If the node is a polygon and the vertices
  48. attribute is defined, this attribute contains the vertices of the node.
  49. Every edge is assigned a pos attribute, and if the edge has a label, the label position is given in lp.
  50. End Rem
  51. Method ToDot:String(graph:TAGraph)
  52. Return RenderToString(graph, "dot")
  53. End Method
  54. Rem
  55. bbdoc: Produces output in the DOT language.
  56. about: It extends the @dot format by providing much more detailed information about how graph components are drawn.
  57. It relies on additional attributes for nodes, edges and graphs.
  58. End Rem
  59. Method ToXDot:String(graph:TAGraph)
  60. Return RenderToString(graph, "xdot")
  61. End Method
  62. Rem
  63. bbdoc: Produces a pretty printed version of the input, with no layout performed.
  64. End Rem
  65. Method Prettify:String(graph:TAGraph)
  66. Return RenderToString(graph, "canon")
  67. End Method
  68. Rem
  69. bbdoc: Produces output in JSON format that contains the same information produced by #ToDot
  70. about: Assumes the graph has been processed by one of the layout algorithms.
  71. End Rem
  72. Method ToJson0:String(graph:TAGraph)
  73. Return RenderToString(graph, "json0")
  74. End Method
  75. Rem
  76. bbdoc: Produces output in JSON format that contains the same information produced by #ToXDot
  77. about: Assumes the graph has been processed by one of the layout algorithms.
  78. End Rem
  79. Method ToJson:String(graph:TAGraph)
  80. Return RenderToString(graph, "json")
  81. End Method
  82. Rem
  83. bbdoc: Produces JSON output similar to #ToJson0, except it only uses the content of the graph on input.
  84. about: Does not assume that the graph has been processed by any layout algorithm, and the only xdot information appearing in the
  85. output was in the original input file.
  86. End Rem
  87. Method ToDotJson:String(graph:TAGraph)
  88. Return RenderToString(graph, "dot_json")
  89. End Method
  90. Rem
  91. bbdoc: Produces JSON output similar to #ToJson, except it only uses the content of the graph on input.
  92. about: Does not assume that the graph has been processed by any layout algorithm, and the only xdot information appearing in the
  93. output was in the original input file.
  94. End Rem
  95. Method ToXDotJson:String(graph:TAGraph)
  96. Return RenderToString(graph, "xdot_json")
  97. End Method
  98. Rem
  99. bbdoc: Produces output using a simple, line-based language.
  100. End Rem
  101. Method ToPlain:String(graph:TAGraph)
  102. Return RenderToString(graph, "plain")
  103. End Method
  104. Rem
  105. bbdoc: Produces output using a simple, line-based language.
  106. about: On edges, it also provides port names on head and tail nodes when applicable.
  107. see https://graphviz.org/docs/outputs/plain/
  108. End Rem
  109. Method ToPlainExt:String(graph:TAGraph)
  110. Return RenderToString(graph, "plain-ext")
  111. End Method
  112. Rem
  113. bbdoc: Produces output in the form of a text string.
  114. about: The output is in the format specified by @format.
  115. Note that this method assumes the specified format is a text-based format.
  116. End Rem
  117. Method RenderToString:String(graph:TAGraph, format:String)
  118. Local res:Int
  119. If gvcPtr Then
  120. Local s:Byte Ptr
  121. Local length:UInt
  122. Local t:Byte Ptr = format.ToUtf8String()
  123. res = gvRenderData(gvcPtr, graph.graphPtr, t, Varptr s, length)
  124. MemFree(t)
  125. If res <> 0 Then
  126. Return Null
  127. End If
  128. Local render:String = String.FromUTF8Bytes(s, length)
  129. gvFreeRenderData(s)
  130. Return render
  131. End If
  132. Return Null
  133. End Method
  134. Rem
  135. bbdoc: Frees the context.
  136. End Rem
  137. Method Free()
  138. If gvcPtr
  139. gvFreeContext(gvcPtr)
  140. gvcPtr = Null
  141. End If
  142. End Method
  143. Method Delete()
  144. Free()
  145. End Method
  146. End Type
  147. Rem
  148. bbdoc: A Graphviz graph.
  149. about: A graph is the fundamental data structure representing a set of nodes
  150. and the connections (edges) between them. It may be directed or undirected, and
  151. can contain attributes that influence layout, style, and labeling. In addition to
  152. nodes and edges, graphs can also include subgraphs and clusters to group related
  153. elements, helping to produce clear and visually informative diagrams when rendered
  154. by Graphviz.
  155. End Rem
  156. Type TAGraph
  157. Field graphPtr:Byte Ptr
  158. Rem
  159. bbdoc: Creates a new graph from a string.
  160. about: @text is the string representation of the graph.
  161. End Rem
  162. Function FromString:TAGraph(text:String)
  163. Local g:TAGraph = New TAGraph
  164. Local t:Byte Ptr = text.ToUtf8String()
  165. g.graphPtr = agmemread(t)
  166. MemFree(t)
  167. If Not g.graphPtr Then
  168. Return Null
  169. End If
  170. Return g
  171. End Function
  172. Rem
  173. bbdoc: Returns #True if the graph is directed.
  174. End Rem
  175. Method IsDirected:Int()
  176. If graphPtr
  177. Return agisdirected(graphPtr)
  178. End If
  179. Return False
  180. End Method
  181. Rem
  182. bbdoc: Returns #True if the graph is undirected.
  183. End Rem
  184. Method IsUndirected:Int()
  185. If graphPtr
  186. Return agisundirected(graphPtr)
  187. End If
  188. Return False
  189. End Method
  190. Rem
  191. bbdoc: Returns #True if the graph is a strict graph.
  192. End Rem
  193. Method IsStrict:Int()
  194. If graphPtr
  195. Return agisstrict(graphPtr)
  196. End If
  197. Return False
  198. End Method
  199. Rem
  200. bbdoc: Returns #True if the graph is strict with no loops.
  201. End Rem
  202. Method IsSimple:Int()
  203. If graphPtr
  204. Return agissimple(graphPtr)
  205. End If
  206. Return False
  207. End Method
  208. Rem
  209. bbdoc: Used to improve the aspect ratio of graphs having many leaves or disconnected nodes.
  210. about: The usual layout for such a graph is generally very wide or tall.
  211. Unflatten inserts invisible edges or adjusts the minlen on edges to improve layout compaction.
  212. End Rem
  213. Method Unflatten(maxMinlen:Int = 0, doFans:Int = False, chainLimit:Int = 0)
  214. If graphPtr
  215. bmx_graphviz_unflatten(graphPtr, doFans, maxMinlen, chainLimit)
  216. End If
  217. End Method
  218. End Type