src/Tools/Graphview/mutator.scala
changeset 59245 be4180f3c236
parent 59240 e411afcfaa29
child 59246 32903b99c2ef
equal deleted inserted replaced
59244:19b5fc4b2b38 59245:be4180f3c236
    22     Info(true, visualizer.Colors.next, m)
    22     Info(true, visualizer.Colors.next, m)
    23 
    23 
    24   class Graph_Filter(
    24   class Graph_Filter(
    25     val name: String,
    25     val name: String,
    26     val description: String,
    26     val description: String,
    27     pred: Model.Graph => Model.Graph) extends Filter
    27     pred: Graph_Display.Graph => Graph_Display.Graph) extends Filter
    28   {
    28   {
    29     def filter(graph: Model.Graph) : Model.Graph = pred(graph)
    29     def filter(graph: Graph_Display.Graph): Graph_Display.Graph = pred(graph)
    30   }
    30   }
    31 
    31 
    32   class Graph_Mutator(
    32   class Graph_Mutator(
    33     val name: String,
    33     val name: String,
    34     val description: String,
    34     val description: String,
    35     pred: (Model.Graph, Model.Graph) => Model.Graph) extends Mutator
    35     pred: (Graph_Display.Graph, Graph_Display.Graph) => Graph_Display.Graph) extends Mutator
    36   {
    36   {
    37     def mutate(complete_graph: Model.Graph, graph: Model.Graph): Model.Graph =
    37     def mutate(complete_graph: Graph_Display.Graph, graph: Graph_Display.Graph): Graph_Display.Graph =
    38       pred(complete_graph, graph)
    38       pred(complete_graph, graph)
    39   }
    39   }
    40 
    40 
    41   class Node_Filter(
    41   class Node_Filter(
    42     name: String,
    42     name: String,
    43     description: String,
    43     description: String,
    44     pred: (Model.Graph, String) => Boolean)
    44     pred: (Graph_Display.Graph, Graph_Display.Node) => Boolean)
    45     extends Graph_Filter(name, description, g => g.restrict(pred(g, _)))
    45     extends Graph_Filter(name, description, g => g.restrict(pred(g, _)))
    46 
    46 
    47   class Edge_Filter(
    47   class Edge_Filter(
    48     name: String,
    48     name: String,
    49     description: String,
    49     description: String,
    50     pred: (Model.Graph, String, String) => Boolean)
    50     pred: (Graph_Display.Graph, Graph_Display.Node, Graph_Display.Node) => Boolean)
    51     extends Graph_Filter(
    51     extends Graph_Filter(
    52       name,
    52       name,
    53       description,
    53       description,
    54       g => (g /: g.dest) {
    54       g => (g /: g.dest) {
    55         case (graph, ((from, _), tos)) =>
    55         case (graph, ((from, _), tos)) =>
    61     name: String,
    61     name: String,
    62     description: String,
    62     description: String,
    63     reverse: Boolean,
    63     reverse: Boolean,
    64     parents: Boolean,
    64     parents: Boolean,
    65     children: Boolean,
    65     children: Boolean,
    66     pred: (Model.Graph, String) => Boolean)
    66     pred: (Graph_Display.Graph, Graph_Display.Node) => Boolean)
    67     extends Node_Filter(
    67     extends Node_Filter(
    68       name,
    68       name,
    69       description,
    69       description,
    70       (g, k) =>
    70       (g, k) =>
    71         reverse !=
    71         reverse !=
    88       "Filter by Name",
    88       "Filter by Name",
    89       "Only shows or hides all nodes with any family member's name matching a regex.",
    89       "Only shows or hides all nodes with any family member's name matching a regex.",
    90       reverse,
    90       reverse,
    91       parents,
    91       parents,
    92       children,
    92       children,
    93       (g, k) => (regex.r findFirstIn k).isDefined)
    93       (g, node) => (regex.r findFirstIn node.toString).isDefined)
    94 
    94 
    95   case class Node_List(
    95   case class Node_List(
    96     list: List[String],
    96     list: List[Graph_Display.Node],
    97     reverse: Boolean,
    97     reverse: Boolean,
    98     parents: Boolean,
    98     parents: Boolean,
    99     children: Boolean)
    99     children: Boolean)
   100     extends Node_Family_Filter(
   100     extends Node_Family_Filter(
   101       "Filter by Name List",
   101       "Filter by Name List",
   102       "Only shows or hides all nodes with any family member's name matching any string in a comma separated list.",
   102       "Only shows or hides all nodes with any family member's name matching any string in a comma separated list.",
   103       reverse,
   103       reverse,
   104       parents,
   104       parents,
   105       children,
   105       children,
   106       (g, k) => list.exists(_ == k))
   106       (g, node) => list.contains(node))
   107 
   107 
   108   case class Edge_Endpoints(source: String, dest: String)
   108   case class Edge_Endpoints(source: Graph_Display.Node, dest: Graph_Display.Node)
   109     extends Edge_Filter(
   109     extends Edge_Filter(
   110       "Hide edge",
   110       "Hide edge",
   111       "Hides the edge whose endpoints match strings.",
   111       "Hides the edge whose endpoints match strings.",
   112       (g, s, d) => !(s == source && d == dest))
   112       (g, s, d) => !(s == source && d == dest))
   113 
   113 
   114   private def add_node_group(from: Model.Graph, to: Model.Graph, keys: List[String]) =
   114   private def add_node_group(from: Graph_Display.Graph, to: Graph_Display.Graph,
       
   115     keys: List[Graph_Display.Node]) =
   115   {
   116   {
   116     // Add Nodes
   117     // Add Nodes
   117     val with_nodes =
   118     val with_nodes =
   118       (to /: keys)((graph, key) => graph.default_node(key, from.get_node(key)))
   119       (to /: keys)((graph, key) => graph.default_node(key, from.get_node(key)))
   119 
   120 
   120     // Add Edges
   121     // Add Edges
   121     (with_nodes /: keys) {
   122     (with_nodes /: keys) {
   122       (gv, key) => {
   123       (gv, key) => {
   123         def add_edges(g: Model.Graph, keys: SortedSet[String], succs: Boolean) =
   124         def add_edges(g: Graph_Display.Graph, keys: from.Keys, succs: Boolean) =
   124           (g /: keys) {
   125           (g /: keys) {
   125             (graph, end) => {
   126             (graph, end) => {
   126               if (!graph.keys_iterator.contains(end)) graph
   127               if (!graph.keys_iterator.contains(end)) graph
   127               else {
   128               else {
   128                 if (succs) graph.add_edge(key, end)
   129                 if (succs) graph.add_edge(key, end)
   143       "Add by name",
   144       "Add by name",
   144       "Adds every node whose name matches the regex. " +
   145       "Adds every node whose name matches the regex. " +
   145       "Adds all relevant edges.",
   146       "Adds all relevant edges.",
   146       (complete_graph, graph) =>
   147       (complete_graph, graph) =>
   147         add_node_group(complete_graph, graph,
   148         add_node_group(complete_graph, graph,
   148           complete_graph.keys.filter(k => (regex.r findFirstIn k).isDefined).toList))
   149           complete_graph.keys.filter(node => (regex.r findFirstIn node.toString).isDefined).toList))
   149 
   150 
   150   case class Add_Transitive_Closure(parents: Boolean, children: Boolean)
   151   case class Add_Transitive_Closure(parents: Boolean, children: Boolean)
   151     extends Graph_Mutator(
   152     extends Graph_Mutator(
   152       "Add transitive closure",
   153       "Add transitive closure",
   153       "Adds all family members of all current nodes.",
   154       "Adds all family members of all current nodes.",
   163 
   164 
   164 trait Mutator
   165 trait Mutator
   165 {
   166 {
   166   val name: String
   167   val name: String
   167   val description: String
   168   val description: String
   168   def mutate(complete_graph: Model.Graph, graph: Model.Graph): Model.Graph
   169   def mutate(complete_graph: Graph_Display.Graph, graph: Graph_Display.Graph): Graph_Display.Graph
   169 
   170 
   170   override def toString: String = name
   171   override def toString: String = name
   171 }
   172 }
   172 
   173 
   173 trait Filter extends Mutator
   174 trait Filter extends Mutator
   174 {
   175 {
   175   def mutate(complete_graph: Model.Graph, graph: Model.Graph) = filter(graph)
   176   def mutate(complete_graph: Graph_Display.Graph, graph: Graph_Display.Graph) = filter(graph)
   176   def filter(graph: Model.Graph) : Model.Graph
   177   def filter(graph: Graph_Display.Graph): Graph_Display.Graph
   177 }
   178 }