src/Pure/General/graph_display.ML
author wenzelm
Wed, 31 Dec 2014 20:42:45 +0100
changeset 59210 8658b4290aed
parent 59208 2486d625878b
child 59211 7b74e8408711
permissions -rw-r--r--
clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names); tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49561
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/General/graph_display.ML
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
     3
49565
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
     4
Generic graph display, with browser and graphview backends.
49561
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
     5
*)
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
     6
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
     7
signature GRAPH_DISPLAY =
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
     8
sig
59208
2486d625878b for graph display, prefer graph data structure over list with dependencies;
wenzelm
parents: 59207
diff changeset
     9
  type node
2486d625878b for graph display, prefer graph data structure over list with dependencies;
wenzelm
parents: 59207
diff changeset
    10
  val content_node: string -> Pretty.T list -> node
2486d625878b for graph display, prefer graph data structure over list with dependencies;
wenzelm
parents: 59207
diff changeset
    11
  val session_node: {name: string, unfold: bool, directory: string, path: string} -> node
59210
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    12
  type entry = (string * node) * string list
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    13
  val eq_entry: entry * entry -> bool
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    14
  type graph = entry list
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    15
  val sort_graph: graph -> graph
49565
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    16
  val write_graph_browser: Path.T -> graph -> unit
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    17
  val browserN: string
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    18
  val graphviewN: string
50715
8cfd585b9162 prefer old graph browser in Isabelle/jEdit, which still produces better layout;
wenzelm
parents: 50450
diff changeset
    19
  val active_graphN: string
49561
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    20
  val display_graph: graph -> unit
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    21
end;
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    22
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    23
structure Graph_Display: GRAPH_DISPLAY =
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    24
struct
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    25
59210
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    26
(* type node *)
59208
2486d625878b for graph display, prefer graph data structure over list with dependencies;
wenzelm
parents: 59207
diff changeset
    27
59210
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    28
datatype node =
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    29
  Node of {name: string, content: Pretty.T list, unfold: bool, directory: string, path: string};
49561
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    30
59208
2486d625878b for graph display, prefer graph data structure over list with dependencies;
wenzelm
parents: 59207
diff changeset
    31
fun content_node name content =
59210
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    32
  Node {name = name, content = content, unfold = true, directory = "", path = ""};
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    33
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    34
fun session_node {name, unfold, directory, path} =
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    35
  Node {name = name, content = [], unfold = unfold, directory = directory, path = path};
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    36
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    37
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    38
(* type graph *)
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    39
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    40
type entry = (string * node) * string list;
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    41
val eq_entry: entry * entry -> bool = op = o apply2 (#1 o #1);
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    42
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    43
type graph = entry list;
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    44
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    45
structure Aux_Graph =
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    46
  Graph(type key = string * string val ord = prod_ord string_ord string_ord);
59208
2486d625878b for graph display, prefer graph data structure over list with dependencies;
wenzelm
parents: 59207
diff changeset
    47
59210
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    48
fun sort_graph (graph: graph) =
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    49
  let
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    50
    val ident_names =
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    51
      fold (fn ((ident, Node {name, ...}), _) => Symtab.update_new (ident, (name, ident)))
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    52
        graph Symtab.empty;
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    53
    val the_key = the o Symtab.lookup ident_names;
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    54
    val G =
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    55
      Aux_Graph.empty
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    56
      |> fold (fn ((ident, node), _) => Aux_Graph.new_node (the_key ident, node)) graph
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    57
      |> fold (fn ((ident, _), parents) =>
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    58
          fold (fn parent => Aux_Graph.add_edge (the_key parent, the_key ident)) parents) graph
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    59
  in
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    60
    Aux_Graph.topological_order G |> map (fn key =>
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    61
      let val (_, (node, (preds, _))) = Aux_Graph.get_entry G key
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    62
      in ((#2 key, node), map #2 (Aux_Graph.Keys.dest preds)) end)
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    63
  end;
49561
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    64
49565
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    65
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    66
(* print modes *)
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    67
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    68
val browserN = "browser";
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    69
val graphviewN = "graphview";
50715
8cfd585b9162 prefer old graph browser in Isabelle/jEdit, which still produces better layout;
wenzelm
parents: 50450
diff changeset
    70
val active_graphN = "active_graph";
8cfd585b9162 prefer old graph browser in Isabelle/jEdit, which still produces better layout;
wenzelm
parents: 50450
diff changeset
    71
8cfd585b9162 prefer old graph browser in Isabelle/jEdit, which still produces better layout;
wenzelm
parents: 50450
diff changeset
    72
fun is_browser () =
8cfd585b9162 prefer old graph browser in Isabelle/jEdit, which still produces better layout;
wenzelm
parents: 50450
diff changeset
    73
  (case find_first (fn m => m = browserN orelse m = graphviewN) (print_mode_value ()) of
8cfd585b9162 prefer old graph browser in Isabelle/jEdit, which still produces better layout;
wenzelm
parents: 50450
diff changeset
    74
    SOME m => m = browserN
8cfd585b9162 prefer old graph browser in Isabelle/jEdit, which still produces better layout;
wenzelm
parents: 50450
diff changeset
    75
  | NONE => true);
8cfd585b9162 prefer old graph browser in Isabelle/jEdit, which still produces better layout;
wenzelm
parents: 50450
diff changeset
    76
49565
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    77
50715
8cfd585b9162 prefer old graph browser in Isabelle/jEdit, which still produces better layout;
wenzelm
parents: 50450
diff changeset
    78
(* encode graph *)
8cfd585b9162 prefer old graph browser in Isabelle/jEdit, which still produces better layout;
wenzelm
parents: 50450
diff changeset
    79
59210
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    80
val encode_browser =
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    81
  map (fn ((key, Node {name, unfold, content, directory, path}), parents) =>
59208
2486d625878b for graph display, prefer graph data structure over list with dependencies;
wenzelm
parents: 59207
diff changeset
    82
    "\"" ^ name ^ "\" \"" ^ key ^ "\" \"" ^ directory ^ (if unfold then "\" + \"" else "\" \"") ^
59210
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    83
    path ^ "\" > " ^ space_implode " " (map quote parents) ^ " ;")
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    84
  #> cat_lines;
50715
8cfd585b9162 prefer old graph browser in Isabelle/jEdit, which still produces better layout;
wenzelm
parents: 50450
diff changeset
    85
59210
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    86
fun write_graph_browser path graph =
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    87
  File.write path (encode_browser graph);
49561
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    88
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    89
59210
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    90
fun encode_node (Node {name, content, ...}) =
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    91
  (name, content) |>
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    92
    let open XML.Encode
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    93
    in pair string (YXML.parse_body o Pretty.symbolic_string_of o Pretty.chunks) end;
49565
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    94
59210
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    95
val encode_graph =
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
    96
  let open XML.Encode in list (pair (pair string encode_node) (list string)) end;
49565
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    97
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    98
49561
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    99
(* display graph *)
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
   100
59210
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
   101
val display_graph =
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
   102
  sort_graph #> (fn graph =>
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
   103
    if print_mode_active active_graphN then
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
   104
      let
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
   105
        val (markup, body) =
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
   106
          if is_browser () then (Markup.browserN, encode_browser graph)
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
   107
          else (Markup.graphviewN, YXML.string_of_body (encode_graph graph));
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
   108
        val ((bg1, bg2), en) =
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
   109
          YXML.output_markup_elem (Active.make_markup markup {implicit = false, properties = []});
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
   110
      in writeln ("See " ^ bg1 ^ body ^ bg2 ^ "graph" ^ en) end
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
   111
    else
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
   112
      let
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
   113
        val _ = writeln "Displaying graph ...";
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
   114
        val path = Isabelle_System.create_tmp_path "graph" "";
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
   115
        val _ = write_graph_browser path graph;
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
   116
        val _ = Isabelle_System.isabelle_tool "browser" ("-c " ^ File.shell_path path ^ " &");
8658b4290aed clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
wenzelm
parents: 59208
diff changeset
   117
      in () end);
49561
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
   118
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
   119
end;