author | wenzelm |
Sun, 21 Mar 2021 23:15:55 +0100 | |
changeset 73461 | 067c23324784 |
parent 73359 | d8a0e996614b |
child 75393 | 87ebf5a50283 |
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 |
import scala.collection.immutable.SortedSet |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
15 |
|
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
16 |
|
59221 | 17 |
object Mutator |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
18 |
{ |
59221 | 19 |
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
|
20 |
|
59459 | 21 |
def make(graphview: Graphview, m: Mutator): Info = |
73344 | 22 |
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
|
23 |
|
59218 | 24 |
class Graph_Filter( |
25 |
val name: String, |
|
26 |
val description: String, |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
27 |
pred: Graph_Display.Graph => Graph_Display.Graph) extends Filter |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
28 |
{ |
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
29 |
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
|
30 |
} |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
31 |
|
59218 | 32 |
class Graph_Mutator( |
33 |
val name: String, |
|
34 |
val description: String, |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
35 |
pred: (Graph_Display.Graph, Graph_Display.Graph) => Graph_Display.Graph) extends Mutator |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
36 |
{ |
59259
399506ee38a5
clarified static full_graph vs. dynamic visible_graph;
wenzelm
parents:
59246
diff
changeset
|
37 |
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
|
38 |
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
|
39 |
} |
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
40 |
|
59218 | 41 |
class Node_Filter( |
42 |
name: String, |
|
43 |
description: String, |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
44 |
pred: (Graph_Display.Graph, Graph_Display.Node) => Boolean) |
59218 | 45 |
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
|
46 |
|
59218 | 47 |
class Edge_Filter( |
48 |
name: String, |
|
49 |
description: String, |
|
59246 | 50 |
pred: (Graph_Display.Graph, Graph_Display.Edge) => Boolean) |
59218 | 51 |
extends Graph_Filter( |
52 |
name, |
|
53 |
description, |
|
73359 | 54 |
g => g.dest.foldLeft(g) { |
59218 | 55 |
case (graph, ((from, _), tos)) => |
73359 | 56 |
tos.foldLeft(graph) { |
57 |
case (gr, to) => if (pred(gr, (from, to))) gr else gr.del_edge(from, to) |
|
58 |
} |
|
59218 | 59 |
}) |
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
60 |
|
59218 | 61 |
class Node_Family_Filter( |
62 |
name: String, |
|
63 |
description: String, |
|
64 |
reverse: Boolean, |
|
65 |
parents: Boolean, |
|
66 |
children: Boolean, |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
67 |
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
|
68 |
extends Node_Filter( |
59218 | 69 |
name, |
70 |
description, |
|
71 |
(g, k) => |
|
72 |
reverse != |
|
73 |
(pred(g, k) || |
|
74 |
(parents && g.all_preds(List(k)).exists(pred(g, _))) || |
|
75 |
(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
|
76 |
|
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
77 |
case class Identity() |
49565
ea4308b7ef0f
ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents:
49563
diff
changeset
|
78 |
extends Graph_Filter( |
59218 | 79 |
"Identity", |
80 |
"Does not change the graph.", |
|
81 |
g => g) |
|
49557
61988f9df94d
added Graphview tool, based on Isabelle/Scala and Swing/Graphics2D;
Markus Kaiser <markus.kaiser@in.tum.de>
parents:
diff
changeset
|
82 |
|
59218 | 83 |
case class Node_Expression( |
84 |
regex: String, |
|
85 |
reverse: Boolean, |
|
86 |
parents: Boolean, |
|
87 |
children: Boolean) |
|
88 |
extends Node_Family_Filter( |
|
89 |
"Filter by Name", |
|
90 |
"Only shows or hides all nodes with any family member's name matching a regex.", |
|
91 |
reverse, |
|
92 |
parents, |
|
93 |
children, |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
94 |
(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
|
95 |
|
59218 | 96 |
case class Node_List( |
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
97 |
list: List[Graph_Display.Node], |
59218 | 98 |
reverse: Boolean, |
99 |
parents: Boolean, |
|
100 |
children: Boolean) |
|
49565
ea4308b7ef0f
ML support for generic graph display, with browser and graphview backends (via print modes);
wenzelm
parents:
49563
diff
changeset
|
101 |
extends Node_Family_Filter( |
59218 | 102 |
"Filter by Name List", |
103 |
"Only shows or hides all nodes with any family member's name matching any string in a comma separated list.", |
|
104 |
reverse, |
|
105 |
parents, |
|
106 |
children, |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
107 |
(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
|
108 |
|
59246 | 109 |
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
|
110 |
extends Edge_Filter( |
59218 | 111 |
"Hide edge", |
59246 | 112 |
"Hides specified edge.", |
113 |
(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
|
114 |
|
59245
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
115 |
private def add_node_group(from: Graph_Display.Graph, to: Graph_Display.Graph, |
be4180f3c236
more formal Graph_Display.Node (with ordering) and Graph_Display.Edge;
wenzelm
parents:
59240
diff
changeset
|
116 |
keys: List[Graph_Display.Node]) = |
59218 | 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.", |
|
59259
399506ee38a5
clarified static full_graph vs. dynamic visible_graph;
wenzelm
parents:
59246
diff
changeset
|
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 |
||
164 |
trait Mutator |
|
165 |
{ |
|
166 |
val name: String |
|
167 |
val description: String |
|
59259
399506ee38a5
clarified static full_graph vs. dynamic visible_graph;
wenzelm
parents:
59246
diff
changeset
|
168 |
def mutate(full_graph: Graph_Display.Graph, graph: Graph_Display.Graph): Graph_Display.Graph |
59221 | 169 |
|
170 |
override def toString: String = name |
|
171 |
} |
|
172 |
||
173 |
trait Filter extends Mutator |
|
174 |
{ |
|
71601 | 175 |
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
|
176 |
def filter(graph: Graph_Display.Graph): Graph_Display.Graph |
59221 | 177 |
} |