src/Pure/General/graph_display.ML
author Christian Sternagel
Thu, 13 Dec 2012 13:11:38 +0100
changeset 50516 ed6b40d15d1c
parent 50450 358b6020f8b6
child 50715 8cfd585b9162
permissions -rw-r--r--
renamed "emb" to "list_hembeq"; make "list_hembeq" reflexive independent of the base order; renamed "sub" to "sublisteq"; dropped "transp_on" (state transitivity explicitly instead); no need to hide "sub" after renaming; replaced some ASCII symbols by proper Isabelle symbols; NEWS
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
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
     9
  type node =
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    10
   {name: string, ID: string, dir: string, unfold: bool,
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    11
    path: string, parents: string list, content: Pretty.T list}
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    12
  type graph = node list
49565
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    13
  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
    14
  val browserN: string
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    15
  val graphviewN: string
49566
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49565
diff changeset
    16
  val graphview_reportN: string
49561
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    17
  val display_graph: graph -> unit
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    18
end;
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    19
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    20
structure Graph_Display: GRAPH_DISPLAY =
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    21
struct
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    22
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    23
(* external graph representation *)
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    24
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    25
type node =
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    26
 {name: string, ID: string, dir: string, unfold: bool,
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    27
  path: string, parents: string list, content: Pretty.T list};
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    28
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    29
type graph = node list;
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    30
49565
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    31
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    32
(* print modes *)
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    33
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    34
val browserN = "browser";
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    35
val graphviewN = "graphview";
49566
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49565
diff changeset
    36
val graphview_reportN = "graphview_report";
49565
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    37
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    38
fun write_graph_browser path (graph: graph) =
49561
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    39
  File.write path (cat_lines (map (fn {name, ID, dir, unfold, path, parents, ...} =>
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    40
    "\"" ^ name ^ "\" \"" ^ ID ^ "\" \"" ^ dir ^ (if unfold then "\" + \"" else "\" \"") ^
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    41
    path ^ "\" > " ^ space_implode " " (map quote parents) ^ " ;") graph));
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    42
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    43
49565
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    44
val encode_content = YXML.parse_body o Pretty.symbolic_string_of o Pretty.chunks;
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    45
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    46
fun encode_graphview (graph: graph) =
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    47
  Graph.empty
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    48
  |> fold (fn {ID, name, content, ...} => Graph.new_node (ID, (name, content))) graph
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    49
  |> fold (fn {ID = a, parents = bs, ...} => fold (fn b => Graph.add_edge (b, a)) bs) graph
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    50
  |> let open XML.Encode in Graph.encode string (pair string encode_content) end;
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    51
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    52
fun write_graph_graphview path graph =
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    53
  File.write path (YXML.string_of_body (encode_graphview graph));
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    54
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    55
49561
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    56
(* display graph *)
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    57
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    58
fun display_graph graph =
49566
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49565
diff changeset
    59
  if print_mode_active graphview_reportN then
50450
358b6020f8b6 generalized notion of active area, where sendback is just one application;
wenzelm
parents: 50201
diff changeset
    60
    let
358b6020f8b6 generalized notion of active area, where sendback is just one application;
wenzelm
parents: 50201
diff changeset
    61
      val markup = Active.make_markup Markup.graphviewN {implicit = false, properties = []};
358b6020f8b6 generalized notion of active area, where sendback is just one application;
wenzelm
parents: 50201
diff changeset
    62
      val ((bg1, bg2), en) = YXML.output_markup_elem markup;
358b6020f8b6 generalized notion of active area, where sendback is just one application;
wenzelm
parents: 50201
diff changeset
    63
      val graph_yxml = YXML.string_of_body (encode_graphview graph);
358b6020f8b6 generalized notion of active area, where sendback is just one application;
wenzelm
parents: 50201
diff changeset
    64
    in writeln ("See " ^ bg1 ^ graph_yxml ^ bg2 ^ "graphview" ^ en) end
49566
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49565
diff changeset
    65
  else
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49565
diff changeset
    66
    let
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49565
diff changeset
    67
      val browser =
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49565
diff changeset
    68
        (case find_first (fn m => m = browserN orelse m = graphviewN) (print_mode_value ()) of
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49565
diff changeset
    69
          SOME m => m = browserN
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49565
diff changeset
    70
        | NONE => true);
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49565
diff changeset
    71
      val (write, tool) =
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49565
diff changeset
    72
        if browser then (write_graph_browser, "browser")
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49565
diff changeset
    73
        else (write_graph_graphview, "graphview");
49565
ea4308b7ef0f ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents: 49561
diff changeset
    74
49566
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49565
diff changeset
    75
      val _ = writeln "Displaying graph ...";
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49565
diff changeset
    76
      val path = Isabelle_System.create_tmp_path "graph" "";
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49565
diff changeset
    77
      val _ = write path graph;
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49565
diff changeset
    78
      val _ = Isabelle_System.isabelle_tool tool ("-c " ^ File.shell_path path ^ " &");
66cbf8bb4693 basic integration of graphview into document model;
wenzelm
parents: 49565
diff changeset
    79
    in () end;
49561
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    80
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    81
end;
26fc70e983c2 separate module Graph_Display;
wenzelm
parents:
diff changeset
    82