author | paulson <lp15@cam.ac.uk> |
Sun, 03 Aug 2025 20:34:24 +0100 | |
changeset 82913 | 7c870287f04f |
parent 82142 | 508a673c87ac |
permissions | -rw-r--r-- |
59202 | 1 |
/* Title: Tools/Graphview/mutator.scala |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
2 |
Author: Markus Kaiser, TU Muenchen |
59240 | 3 |
Author: Makarius |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
4 |
|
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
5 |
Filters and add-operations on graphs. |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
6 |
*/ |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
7 |
|
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
8 |
package isabelle.graphview |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
9 |
|
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
10 |
|
49558 | 11 |
import isabelle._ |
12 |
||
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
13 |
import java.awt.Color |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
14 |
|
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
15 |
|
75393 | 16 |
object Mutator { |
59221 | 17 |
sealed case class Info(enabled: Boolean, color: Color, mutator: Mutator) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
18 |
|
59459 | 19 |
def make(graphview: Graphview, m: Mutator): Info = |
73344 | 20 |
Info(true, graphview.Colors.next(), m) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
21 |
|
59218 | 22 |
class Graph_Filter( |
23 |
val name: String, |
|
24 |
val description: String, |
|
75393 | 25 |
pred: Graph_Display.Graph => Graph_Display.Graph |
26 |
) extends Filter { |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
27 |
def filter(graph: Graph_Display.Graph): Graph_Display.Graph = pred(graph) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
28 |
} |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
29 |
|
59218 | 30 |
class Graph_Mutator( |
31 |
val name: String, |
|
32 |
val description: String, |
|
75393 | 33 |
pred: (Graph_Display.Graph, Graph_Display.Graph) => Graph_Display.Graph |
34 |
) extends Mutator { |
|
59259
399506ee38a5
clarified static full_graph vs. dynamic visible_graph;
wenzelm
parents:
59246
diff
changeset
|
35 |
def mutate(full_graph: Graph_Display.Graph, graph: Graph_Display.Graph): Graph_Display.Graph = |
399506ee38a5
clarified static full_graph vs. dynamic visible_graph;
wenzelm
parents:
59246
diff
changeset
|
36 |
pred(full_graph, graph) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
37 |
} |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
38 |
|
59218 | 39 |
class Node_Filter( |
40 |
name: String, |
|
41 |
description: String, |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
42 |
pred: (Graph_Display.Graph, Graph_Display.Node) => Boolean) |
59218 | 43 |
extends Graph_Filter(name, description, g => g.restrict(pred(g, _))) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
44 |
|
59218 | 45 |
class Edge_Filter( |
46 |
name: String, |
|
47 |
description: String, |
|
59246 | 48 |
pred: (Graph_Display.Graph, Graph_Display.Edge) => Boolean) |
59218 | 49 |
extends Graph_Filter( |
50 |
name, |
|
51 |
description, |
|
73359 | 52 |
g => g.dest.foldLeft(g) { |
59218 | 53 |
case (graph, ((from, _), tos)) => |
73359 | 54 |
tos.foldLeft(graph) { |
55 |
case (gr, to) => if (pred(gr, (from, to))) gr else gr.del_edge(from, to) |
|
56 |
} |
|
59218 | 57 |
}) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
58 |
|
59218 | 59 |
class Node_Family_Filter( |
60 |
name: String, |
|
61 |
description: String, |
|
62 |
reverse: Boolean, |
|
63 |
parents: Boolean, |
|
64 |
children: Boolean, |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
65 |
pred: (Graph_Display.Graph, Graph_Display.Node) => Boolean) |
49565
ea4308b7ef0f
ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents:
49563
diff
changeset
|
66 |
extends Node_Filter( |
59218 | 67 |
name, |
68 |
description, |
|
69 |
(g, k) => |
|
70 |
reverse != |
|
71 |
(pred(g, k) || |
|
72 |
(parents && g.all_preds(List(k)).exists(pred(g, _))) || |
|
73 |
(children && g.all_succs(List(k)).exists(pred(g, _))))) |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
74 |
|
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
75 |
case class Identity() |
49565
ea4308b7ef0f
ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents:
49563
diff
changeset
|
76 |
extends Graph_Filter( |
59218 | 77 |
"Identity", |
78 |
"Does not change the graph.", |
|
79 |
g => g) |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
80 |
|
59218 | 81 |
case class Node_Expression( |
82 |
regex: String, |
|
83 |
reverse: Boolean, |
|
84 |
parents: Boolean, |
|
85 |
children: Boolean) |
|
86 |
extends Node_Family_Filter( |
|
87 |
"Filter by Name", |
|
88 |
"Only shows or hides all nodes with any family member's name matching a regex.", |
|
89 |
reverse, |
|
90 |
parents, |
|
91 |
children, |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
92 |
(g, node) => (regex.r findFirstIn node.toString).isDefined) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
93 |
|
59218 | 94 |
case class Node_List( |
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
95 |
list: List[Graph_Display.Node], |
59218 | 96 |
reverse: Boolean, |
97 |
parents: Boolean, |
|
98 |
children: Boolean) |
|
49565
ea4308b7ef0f
ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents:
49563
diff
changeset
|
99 |
extends Node_Family_Filter( |
59218 | 100 |
"Filter by Name List", |
101 |
"Only shows or hides all nodes with any family member's name matching any string in a comma separated list.", |
|
102 |
reverse, |
|
103 |
parents, |
|
104 |
children, |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
105 |
(g, node) => list.contains(node)) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
106 |
|
59246 | 107 |
case class Edge_Endpoints(edge: Graph_Display.Edge) |
49565
ea4308b7ef0f
ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents:
49563
diff
changeset
|
108 |
extends Edge_Filter( |
59218 | 109 |
"Hide edge", |
59246 | 110 |
"Hides specified edge.", |
111 |
(g, e) => e != edge) |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
112 |
|
75393 | 113 |
private def add_node_group( |
114 |
from: Graph_Display.Graph, |
|
115 |
to: Graph_Display.Graph, |
|
116 |
keys: List[Graph_Display.Node] |
|
117 |
) = { |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
118 |
// Add Nodes |
59218 | 119 |
val with_nodes = |
73359 | 120 |
keys.foldLeft(to) { case (graph, key) => graph.default_node(key, from.get_node(key)) } |
59218 | 121 |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
122 |
// Add Edges |
73359 | 123 |
keys.foldLeft(with_nodes) { |
124 |
case (gv, key) => |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
125 |
def add_edges(g: Graph_Display.Graph, keys: from.Keys, succs: Boolean) = |
73359 | 126 |
keys.foldLeft(g) { |
127 |
case (graph, end) => |
|
56372
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
wenzelm
parents:
50475
diff
changeset
|
128 |
if (!graph.keys_iterator.contains(end)) graph |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
129 |
else { |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
130 |
if (succs) graph.add_edge(key, end) |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
131 |
else graph.add_edge(end, key) |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
132 |
} |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
133 |
} |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
134 |
|
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
135 |
add_edges( |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
136 |
add_edges(gv, from.imm_preds(key), false), |
59218 | 137 |
from.imm_succs(key), true) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
138 |
} |
59218 | 139 |
} |
140 |
||
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
141 |
case class Add_Node_Expression(regex: String) |
49565
ea4308b7ef0f
ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents:
49563
diff
changeset
|
142 |
extends Graph_Mutator( |
59218 | 143 |
"Add by name", |
144 |
"Adds every node whose name matches the regex. " + |
|
145 |
"Adds all relevant edges.", |
|
59259
399506ee38a5
clarified static full_graph vs. dynamic visible_graph;
wenzelm
parents:
59246
diff
changeset
|
146 |
(full_graph, graph) => |
399506ee38a5
clarified static full_graph vs. dynamic visible_graph;
wenzelm
parents:
59246
diff
changeset
|
147 |
add_node_group(full_graph, graph, |
399506ee38a5
clarified static full_graph vs. dynamic visible_graph;
wenzelm
parents:
59246
diff
changeset
|
148 |
full_graph.keys.filter(node => (regex.r findFirstIn node.toString).isDefined).toList)) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
149 |
|
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
150 |
case class Add_Transitive_Closure(parents: Boolean, children: Boolean) |
49565
ea4308b7ef0f
ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents:
49563
diff
changeset
|
151 |
extends Graph_Mutator( |
59218 | 152 |
"Add transitive closure", |
153 |
"Adds all family members of all current nodes.", |
|
75394 | 154 |
{ (full_graph, graph) => |
59218 | 155 |
val withparents = |
59259
399506ee38a5
clarified static full_graph vs. dynamic visible_graph;
wenzelm
parents:
59246
diff
changeset
|
156 |
if (parents) add_node_group(full_graph, graph, full_graph.all_preds(graph.keys)) |
59228
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59221
diff
changeset
|
157 |
else graph |
56b34fc7a015
more dynamic visualizer -- re-use Isabelle/jEdit options;
wenzelm
parents:
59221
diff
changeset
|
158 |
if (children) |
59259
399506ee38a5
clarified static full_graph vs. dynamic visible_graph;
wenzelm
parents:
59246
diff
changeset
|
159 |
add_node_group(full_graph, withparents, full_graph.all_succs(graph.keys)) |
59218 | 160 |
else withparents |
161 |
}) |
|
59221 | 162 |
} |
163 |
||
75393 | 164 |
trait Mutator { |
59221 | 165 |
val name: String |
166 |
val description: String |
|
59259
399506ee38a5
clarified static full_graph vs. dynamic visible_graph;
wenzelm
parents:
59246
diff
changeset
|
167 |
def mutate(full_graph: Graph_Display.Graph, graph: Graph_Display.Graph): Graph_Display.Graph |
59221 | 168 |
|
169 |
override def toString: String = name |
|
170 |
} |
|
171 |
||
75393 | 172 |
trait Filter extends Mutator { |
71601 | 173 |
def mutate(full_graph: Graph_Display.Graph, graph: Graph_Display.Graph): Graph_Display.Graph = filter(graph) |
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
174 |
def filter(graph: Graph_Display.Graph): Graph_Display.Graph |
59221 | 175 |
} |