[英]How to visualize/draw automata in ocaml?
我正在做自動機的組合。 所以在最后,我想繪制組合自動機。 所以在ocaml中有沒有任何庫? 或者是否有為任何圖形可視化工具編寫的ocaml包裝器? 我已經谷歌搜索了,但沒有得到太多的ocaml。 關於ocamlgraph的任何評論? 我將在組合自動機中獲得超過100個州。
使用ocamlgraph - 它是一個圖形庫,可以為你生成一個dot / graphviz文件,但也可以做很多其他可能有趣的處理你的自動機的東西。 該庫可以執行固定點,生成樹,圖搜索,查找強連接組件等。
下面是一些帶有標記邊緣的有向圖的完整示例+用於執行深度優先搜索+模塊的模塊,用於創建它的點表示:
(* representation of a node -- must be hashable *)
module Node = struct
type t = int
let compare = Pervasives.compare
let hash = Hashtbl.hash
let equal = (=)
end
(* representation of an edge -- must be comparable *)
module Edge = struct
type t = string
let compare = Pervasives.compare
let equal = (=)
let default = ""
end
(* a functional/persistent graph *)
module G = Graph.Persistent.Digraph.ConcreteBidirectionalLabeled(Node)(Edge)
(* more modules available, e.g. graph traversal with depth-first-search *)
module D = Graph.Traverse.Dfs(G)
(* module for creating dot-files *)
module Dot = Graph.Graphviz.Dot(struct
include G (* use the graph module from above *)
let edge_attributes (a, e, b) = [`Label e; `Color 4711]
let default_edge_attributes _ = []
let get_subgraph _ = None
let vertex_attributes _ = [`Shape `Box]
let vertex_name v = string_of_int v
let default_vertex_attributes _ = []
let graph_attributes _ = []
end)
你可以寫你的程序; 例如:
(* work with the graph ... *)
let _ =
let g = G.empty in
let g = G.add_edge_e ...
...
let file = open_out_bin "mygraph.dot" in
let () = Dot.output_graph file g in
...
if D.has_cycle g then ... else ...
我只是將自動機寫為文本到文件(格式適合graphviz),然后對該文件運行graphviz。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.