| author | wenzelm | 
| Sat, 03 Jan 2015 22:04:31 +0100 | |
| changeset 59255 | db265648139c | 
| parent 59244 | 19b5fc4b2b38 | 
| child 59285 | d0d0953e063f | 
| permissions | -rw-r--r-- | 
| 49561 | 1 | (* Title: Pure/General/graph_display.ML | 
| 2 | Author: Makarius | |
| 3 | ||
| 59244 
19b5fc4b2b38
more uniform support for graph display in ML/Scala;
 wenzelm parents: 
59211diff
changeset | 4 | Support for graph display. | 
| 49561 | 5 | *) | 
| 6 | ||
| 7 | signature GRAPH_DISPLAY = | |
| 8 | sig | |
| 59208 
2486d625878b
for graph display, prefer graph data structure over list with dependencies;
 wenzelm parents: 
59207diff
changeset | 9 | type node | 
| 
2486d625878b
for graph display, prefer graph data structure over list with dependencies;
 wenzelm parents: 
59207diff
changeset | 10 | val content_node: string -> Pretty.T list -> node | 
| 
2486d625878b
for graph display, prefer graph data structure over list with dependencies;
 wenzelm parents: 
59207diff
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: 
59208diff
changeset | 12 | type entry = (string * node) * string list | 
| 
8658b4290aed
clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
 wenzelm parents: 
59208diff
changeset | 13 | val eq_entry: entry * entry -> bool | 
| 59244 
19b5fc4b2b38
more uniform support for graph display in ML/Scala;
 wenzelm parents: 
59211diff
changeset | 14 | val sort_graph: entry list -> entry list | 
| 
19b5fc4b2b38
more uniform support for graph display in ML/Scala;
 wenzelm parents: 
59211diff
changeset | 15 | val write_graph_browser: Path.T -> entry list -> unit | 
| 49565 
ea4308b7ef0f
ML support for generic graph display, with browser and graphview backends (via print modes);
 wenzelm parents: 
49561diff
changeset | 16 | val browserN: string | 
| 
ea4308b7ef0f
ML support for generic graph display, with browser and graphview backends (via print modes);
 wenzelm parents: 
49561diff
changeset | 17 | val graphviewN: string | 
| 59244 
19b5fc4b2b38
more uniform support for graph display in ML/Scala;
 wenzelm parents: 
59211diff
changeset | 18 | val display_graph: entry list -> unit | 
| 49561 | 19 | end; | 
| 20 | ||
| 21 | structure Graph_Display: GRAPH_DISPLAY = | |
| 22 | struct | |
| 23 | ||
| 59244 
19b5fc4b2b38
more uniform support for graph display in ML/Scala;
 wenzelm parents: 
59211diff
changeset | 24 | (* graph entries *) | 
| 59208 
2486d625878b
for graph display, prefer graph data structure over list with dependencies;
 wenzelm parents: 
59207diff
changeset | 25 | |
| 59210 
8658b4290aed
clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
 wenzelm parents: 
59208diff
changeset | 26 | datatype node = | 
| 
8658b4290aed
clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
 wenzelm parents: 
59208diff
changeset | 27 |   Node of {name: string, content: Pretty.T list, unfold: bool, directory: string, path: string};
 | 
| 49561 | 28 | |
| 59208 
2486d625878b
for graph display, prefer graph data structure over list with dependencies;
 wenzelm parents: 
59207diff
changeset | 29 | fun content_node name content = | 
| 59210 
8658b4290aed
clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
 wenzelm parents: 
59208diff
changeset | 30 |   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: 
59208diff
changeset | 31 | |
| 
8658b4290aed
clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
 wenzelm parents: 
59208diff
changeset | 32 | fun session_node {name, unfold, directory, path} =
 | 
| 
8658b4290aed
clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
 wenzelm parents: 
59208diff
changeset | 33 |   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: 
59208diff
changeset | 34 | |
| 
8658b4290aed
clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
 wenzelm parents: 
59208diff
changeset | 35 | type entry = (string * node) * string list; | 
| 
8658b4290aed
clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
 wenzelm parents: 
59208diff
changeset | 36 | 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: 
59208diff
changeset | 37 | |
| 
8658b4290aed
clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
 wenzelm parents: 
59208diff
changeset | 38 | |
| 59244 
19b5fc4b2b38
more uniform support for graph display in ML/Scala;
 wenzelm parents: 
59211diff
changeset | 39 | (* graph structure *) | 
| 
19b5fc4b2b38
more uniform support for graph display in ML/Scala;
 wenzelm parents: 
59211diff
changeset | 40 | |
| 
19b5fc4b2b38
more uniform support for graph display in ML/Scala;
 wenzelm parents: 
59211diff
changeset | 41 | structure Graph = | 
| 59210 
8658b4290aed
clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
 wenzelm parents: 
59208diff
changeset | 42 | 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: 
59207diff
changeset | 43 | |
| 59244 
19b5fc4b2b38
more uniform support for graph display in ML/Scala;
 wenzelm parents: 
59211diff
changeset | 44 | fun build_graph entries = | 
| 59210 
8658b4290aed
clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
 wenzelm parents: 
59208diff
changeset | 45 | let | 
| 
8658b4290aed
clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
 wenzelm parents: 
59208diff
changeset | 46 | val ident_names = | 
| 
8658b4290aed
clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
 wenzelm parents: 
59208diff
changeset | 47 |       fold (fn ((ident, Node {name, ...}), _) => Symtab.update_new (ident, (name, ident)))
 | 
| 59244 
19b5fc4b2b38
more uniform support for graph display in ML/Scala;
 wenzelm parents: 
59211diff
changeset | 48 | entries Symtab.empty; | 
| 59210 
8658b4290aed
clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
 wenzelm parents: 
59208diff
changeset | 49 | 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: 
59208diff
changeset | 50 | in | 
| 59244 
19b5fc4b2b38
more uniform support for graph display in ML/Scala;
 wenzelm parents: 
59211diff
changeset | 51 | Graph.empty | 
| 
19b5fc4b2b38
more uniform support for graph display in ML/Scala;
 wenzelm parents: 
59211diff
changeset | 52 | |> fold (fn ((ident, node), _) => Graph.new_node (the_key ident, node)) entries | 
| 
19b5fc4b2b38
more uniform support for graph display in ML/Scala;
 wenzelm parents: 
59211diff
changeset | 53 | |> fold (fn ((ident, _), parents) => | 
| 
19b5fc4b2b38
more uniform support for graph display in ML/Scala;
 wenzelm parents: 
59211diff
changeset | 54 | fold (fn parent => Graph.add_edge (the_key parent, the_key ident)) parents) entries | 
| 59210 
8658b4290aed
clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
 wenzelm parents: 
59208diff
changeset | 55 | end; | 
| 49561 | 56 | |
| 59244 
19b5fc4b2b38
more uniform support for graph display in ML/Scala;
 wenzelm parents: 
59211diff
changeset | 57 | val sort_graph = build_graph #> (fn graph => | 
| 
19b5fc4b2b38
more uniform support for graph display in ML/Scala;
 wenzelm parents: 
59211diff
changeset | 58 | Graph.topological_order graph |> map (fn key => | 
| 
19b5fc4b2b38
more uniform support for graph display in ML/Scala;
 wenzelm parents: 
59211diff
changeset | 59 | let val (_, (node, (preds, _))) = Graph.get_entry graph key | 
| 
19b5fc4b2b38
more uniform support for graph display in ML/Scala;
 wenzelm parents: 
59211diff
changeset | 60 | in ((#2 key, node), map #2 (Graph.Keys.dest preds)) end)); | 
| 
19b5fc4b2b38
more uniform support for graph display in ML/Scala;
 wenzelm parents: 
59211diff
changeset | 61 | |
| 49565 
ea4308b7ef0f
ML support for generic graph display, with browser and graphview backends (via print modes);
 wenzelm parents: 
49561diff
changeset | 62 | |
| 
ea4308b7ef0f
ML support for generic graph display, with browser and graphview backends (via print modes);
 wenzelm parents: 
49561diff
changeset | 63 | (* print modes *) | 
| 
ea4308b7ef0f
ML support for generic graph display, with browser and graphview backends (via print modes);
 wenzelm parents: 
49561diff
changeset | 64 | |
| 
ea4308b7ef0f
ML support for generic graph display, with browser and graphview backends (via print modes);
 wenzelm parents: 
49561diff
changeset | 65 | val browserN = "browser"; | 
| 
ea4308b7ef0f
ML support for generic graph display, with browser and graphview backends (via print modes);
 wenzelm parents: 
49561diff
changeset | 66 | val graphviewN = "graphview"; | 
| 50715 
8cfd585b9162
prefer old graph browser in Isabelle/jEdit, which still produces better layout;
 wenzelm parents: 
50450diff
changeset | 67 | |
| 
8cfd585b9162
prefer old graph browser in Isabelle/jEdit, which still produces better layout;
 wenzelm parents: 
50450diff
changeset | 68 | fun is_browser () = | 
| 
8cfd585b9162
prefer old graph browser in Isabelle/jEdit, which still produces better layout;
 wenzelm parents: 
50450diff
changeset | 69 | (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: 
50450diff
changeset | 70 | SOME m => m = browserN | 
| 
8cfd585b9162
prefer old graph browser in Isabelle/jEdit, which still produces better layout;
 wenzelm parents: 
50450diff
changeset | 71 | | NONE => true); | 
| 
8cfd585b9162
prefer old graph browser in Isabelle/jEdit, which still produces better layout;
 wenzelm parents: 
50450diff
changeset | 72 | |
| 49565 
ea4308b7ef0f
ML support for generic graph display, with browser and graphview backends (via print modes);
 wenzelm parents: 
49561diff
changeset | 73 | |
| 50715 
8cfd585b9162
prefer old graph browser in Isabelle/jEdit, which still produces better layout;
 wenzelm parents: 
50450diff
changeset | 74 | (* encode graph *) | 
| 
8cfd585b9162
prefer old graph browser in Isabelle/jEdit, which still produces better layout;
 wenzelm parents: 
50450diff
changeset | 75 | |
| 59210 
8658b4290aed
clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
 wenzelm parents: 
59208diff
changeset | 76 | val encode_browser = | 
| 59244 
19b5fc4b2b38
more uniform support for graph display in ML/Scala;
 wenzelm parents: 
59211diff
changeset | 77 |   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: 
59207diff
changeset | 78 | "\"" ^ name ^ "\" \"" ^ key ^ "\" \"" ^ directory ^ (if unfold then "\" + \"" else "\" \"") ^ | 
| 59210 
8658b4290aed
clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
 wenzelm parents: 
59208diff
changeset | 79 | path ^ "\" > " ^ space_implode " " (map quote parents) ^ " ;") | 
| 
8658b4290aed
clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
 wenzelm parents: 
59208diff
changeset | 80 | #> cat_lines; | 
| 50715 
8cfd585b9162
prefer old graph browser in Isabelle/jEdit, which still produces better layout;
 wenzelm parents: 
50450diff
changeset | 81 | |
| 59244 
19b5fc4b2b38
more uniform support for graph display in ML/Scala;
 wenzelm parents: 
59211diff
changeset | 82 | fun write_graph_browser path entries = | 
| 
19b5fc4b2b38
more uniform support for graph display in ML/Scala;
 wenzelm parents: 
59211diff
changeset | 83 | File.write path (encode_browser entries); | 
| 49561 | 84 | |
| 85 | ||
| 59210 
8658b4290aed
clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
 wenzelm parents: 
59208diff
changeset | 86 | fun encode_node (Node {name, content, ...}) =
 | 
| 
8658b4290aed
clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
 wenzelm parents: 
59208diff
changeset | 87 | (name, content) |> | 
| 
8658b4290aed
clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
 wenzelm parents: 
59208diff
changeset | 88 | let open XML.Encode | 
| 
8658b4290aed
clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
 wenzelm parents: 
59208diff
changeset | 89 | 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: 
49561diff
changeset | 90 | |
| 59210 
8658b4290aed
clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
 wenzelm parents: 
59208diff
changeset | 91 | val encode_graph = | 
| 
8658b4290aed
clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
 wenzelm parents: 
59208diff
changeset | 92 | 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: 
49561diff
changeset | 93 | |
| 
ea4308b7ef0f
ML support for generic graph display, with browser and graphview backends (via print modes);
 wenzelm parents: 
49561diff
changeset | 94 | |
| 49561 | 95 | (* display graph *) | 
| 96 | ||
| 59210 
8658b4290aed
clarified Graph_Display.graph etc.: sort_graph determines order from structure (and names);
 wenzelm parents: 
59208diff
changeset | 97 | val display_graph = | 
| 59244 
19b5fc4b2b38
more uniform support for graph display in ML/Scala;
 wenzelm parents: 
59211diff
changeset | 98 | sort_graph #> (fn entries => | 
| 59211 | 99 | let | 
| 100 | val (markup, body) = | |
| 59244 
19b5fc4b2b38
more uniform support for graph display in ML/Scala;
 wenzelm parents: 
59211diff
changeset | 101 | if is_browser () then (Markup.browserN, encode_browser entries) | 
| 
19b5fc4b2b38
more uniform support for graph display in ML/Scala;
 wenzelm parents: 
59211diff
changeset | 102 | else (Markup.graphviewN, YXML.string_of_body (encode_graph entries)); | 
| 59211 | 103 | val ((bg1, bg2), en) = | 
| 104 |         YXML.output_markup_elem (Active.make_markup markup {implicit = false, properties = []});
 | |
| 105 |     in writeln ("See " ^ bg1 ^ body ^ bg2 ^ "graph" ^ en) end);
 | |
| 49561 | 106 | |
| 107 | end; |