【问题标题】:How to visualize/draw automata in ocaml?如何在 ocaml 中可视化/绘制自动机?
【发布时间】:2012-02-18 10:49:09
【问题描述】:

我正在做自动机的合成。所以最后,我还想画出组合的自动机。那么在 ocaml 中是否有任何库?或者是否有为任何图形可视化工具编写的 ocaml 包装器?我已经用谷歌搜索了它,但对 ocaml 没有太多了解。 ocamlgraph上有任何cmets吗?我将在组合自动机中获得 100 多个状态。

【问题讨论】:

    标签: ocaml visualization image-graphviz


    【解决方案1】:

    我只是将自动机作为文本写入文件(以适合 graphviz 的格式),然后针对该文件运行 graphviz。

    【讨论】:

    • 我也想过。但是有没有更好的选择?
    • OCaml 中的Graphics 模块和/或CamlPDF 包可以工作。虽然,我不会共同签署这个解决方案;你必须想出一种布局技术。我也会推荐graphviz。
    【解决方案2】:

    使用ocamlgraph——它是一个图形库,可以为你生成一个点/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 ...
    

    【讨论】:

    • 好的,谢谢。你能告诉我更多的细节吗?
    • 节点类型为int,则Dot模块中的函数vertex_namelet vertex_name v = string_of_int v
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    • 2014-02-11
    • 1970-01-01
    • 2020-09-17
    • 1970-01-01
    相关资源
    最近更新 更多