如何在ocaml中可视化/绘制自动机? [英] How to visualize/draw automata in ocaml?
问题描述
我正在做自动机的合成.因此,最后,我想绘制合成的自动机.那么,在ocaml中是否有用于该库的库?还是为任何图形可视化工具编写了ocaml包装器?我已经用谷歌搜索了,但是对于ocaml却收效甚微.对ocamlgraph有何评论?我将在合成自动机中获得100多个状态.
I am doing composition of automata. So at the end of that, I want to draw the composed automata also. So are there any libraries for that in ocaml? or are there ocaml wrappers written for any graph visualization tool? I have googled for it but didn't get much for ocaml. Any comments on ocamlgraph? I will get more than 100 states in composed automata.
推荐答案
使用 ocamlgraph –一个图形库,可以为您生成一个点/graphviz文件,但也可以做很多其他有趣的事情来处理自动机. 该库可以执行固定点,生成树,图形搜索,查找强连接的组件等,等等.
Use ocamlgraph -- it is a graph library that can generate a dot/graphviz file for you but can also do a lot of other stuff that maybe interesting for handling your automata. The library can do fixpoints, spanning trees, graph search, find strongly connected components, etc., etc.
这是一些带有标记边的有向图的完整示例+用于进行深度优先搜索的模块+用于为其创建点表示的模块:
Here is a complete example of some directed graph with labeled edges + module for doing depth-first-search + module for creating dot-representations of it:
(* 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)
您可以编写程序;例如像这样的东西:
with that you can write your program; e.g. something like this:
(* 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 ...
这篇关于如何在ocaml中可视化/绘制自动机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!