Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionaryTracking/HeuristicLab.EvolutionaryTracking.Views/3.4/GenealogyGraphView.cs @ 9573

Last change on this file since 9573 was 9420, checked in by bburlacu, 12 years ago

#1772: Improved the LineageExplorerView, added generation labels in the GenealogyGraphChart, added new visual component VisualGenealogyGraphTextLabel.

File size: 11.6 KB
RevLine 
[7779]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
[7792]23using System.Collections.Generic;
[7779]24using System.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Core.Views;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
30using HeuristicLab.MainForm;
[8213]31using HeuristicLab.Problems.DataAnalysis.Symbolic;
[7779]32using HeuristicLab.Visualization;
33
34namespace HeuristicLab.EvolutionaryTracking.Views {
35  [View("SymbolicExpressionTreeGenealogyGraph")]
36  [Content(typeof(SymbolicExpressionTreeGenealogyGraph), IsDefaultView = true)]
37  public sealed partial class GenealogyGraphView : ItemView {
[8556]38    private VisualSymbolicExpressionTreeNode selectedVisualSymbExprTreeNode;
[9084]39    private SymbolicExpressionTreeNodeSimilarityComparer comparer;
40    private Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> clonedNodes;
41
[7779]42    public new SymbolicExpressionTreeGenealogyGraph Content {
43      get { return (SymbolicExpressionTreeGenealogyGraph)base.Content; }
44      set { base.Content = value; }
45    }
46
47    public GenealogyGraphView()
48      : base() {
49      InitializeComponent();
[8556]50      // set button icons here because if set in the designer file, they get overwritten
51      this.moveModeButton.Image = Common.Resources.VSImageLibrary.Pointer;
52      this.zoomModeButton.Image = Common.Resources.VSImageLibrary.Zoom;
53      this.selectModeButton.Image = Common.Resources.VSImageLibrary.Object;
[9420]54      this.highlightAllButton.Image = Common.Resources.VSImageLibrary.Gradient;
55      this.simpleLineagesCheckBox.Image = Common.Resources.VSImageLibrary.ArrowDown;
56      this.lockGenealogyCheckBox.Image = Common.Resources.VSImageLibrary.ProtectForm;
57
[9084]58      comparer = new SymbolicExpressionTreeNodeSimilarityComparer {
59        MatchConstantValues = true,
60        MatchVariableWeights = true,
61        MatchVariableNames = true
62      };
[7779]63    }
64
65    protected override void DeregisterContentEvents() {
66      // TODO: Deregister your event handlers here
67      genealogyGraphChart.GenealogyGraphNodeClicked -= graphChart_GenealogyGraphNodeClicked;
68      symbolicExpressionTreeChart.SymbolicExpressionTreeNodeClicked -= treeChart_SymbolicExpressionTreeNodeClicked;
[9420]69      symbolicExpressionTreeChart.SymbolicExpressionTreeNodeDoubleClicked -= treeChart_SymbolicExpressionTreeNodeDoubleClicked;
70      Content.GraphUpdated -= genealogyGraphUpdated;
[7779]71      base.DeregisterContentEvents();
72    }
73
74    protected override void RegisterContentEvents() {
75      base.RegisterContentEvents();
76      // TODO: Register your event handlers here
77      genealogyGraphChart.GenealogyGraphNodeClicked += graphChart_GenealogyGraphNodeClicked;
78      symbolicExpressionTreeChart.SymbolicExpressionTreeNodeClicked += treeChart_SymbolicExpressionTreeNodeClicked;
[8556]79      symbolicExpressionTreeChart.SymbolicExpressionTreeNodeDoubleClicked += treeChart_SymbolicExpressionTreeNodeDoubleClicked;
[9420]80      Content.GraphUpdated += genealogyGraphUpdated;
[7779]81    }
82
83    #region Event Handlers (Content)
84    // TODO: Put event handlers of the content here
85    protected override void OnContentChanged() {
86      base.OnContentChanged();
87      if (Content == null)
88        genealogyGraphChart.Graph = null;
89      else {
90        genealogyGraphChart.Graph = Content;
[9420]91        //        var best = Content.Nodes.OrderByDescending(x => x.Quality).First();
92        //        symbolicExpressionTreeChart.Tree = best.SymbolicExpressionTree;
[7779]93      }
94    }
95
96    private void Content_GraphChanged(object sender, EventArgs e) {
97    }
98
99    protected override void SetEnabledStateOfControls() {
100      base.SetEnabledStateOfControls();
101      genealogyGraphChart.Enabled = Content != null;
102    }
103    #endregion
104
105    #region Event Handlers (child controls)
106
107    // TODO: Put event handlers of child controls here.
108    private void graphChart_GenealogyGraphNodeClicked(object sender, MouseEventArgs e) {
[9084]109      // the type hierarchy goes like this: VisualGenealogyGraphNode --> GenealogyGraphNode --> SymbolicExpressionTree
[7779]110      var visualGenealogyGraphNode = (VisualGenealogyGraphNode)sender;
[9084]111      var genealogyGraphNode = visualGenealogyGraphNode.Data;
[8556]112      symbolicExpressionTreeChart.Tree = genealogyGraphNode.SymbolicExpressionTree;
[9084]113      if (genealogyGraphNode.InEdges == null) return;
[9420]114      var arc = genealogyGraphNode.InEdges.Last(x => x.Source != x.Target);
[9250]115      if (arc == null || arc.Data == null) return;
[9084]116      var fragment = (IFragment)arc.Data;
[9239]117      if (fragment.Root == null) return;
[9084]118      foreach (var node in fragment.Root.IterateNodesBreadth()) {
119        var visualSymbExprTreeNode = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node);
120        visualSymbExprTreeNode.LineColor = Color.OrangeRed;
[7779]121      }
[9420]122      symbolicExpressionTreeChart.Repaint();
[7779]123    }
124
125    private void moveModeButton_CheckedChanged(object sender, EventArgs e) {
126      var btn = (RadioButton)sender;
127      if (btn.Checked) { genealogyGraphChart.Chart.Mode = ChartMode.Move; }
128    }
129
130    private void zoomModeButton_CheckedChanged(object sender, EventArgs e) {
131      var btn = (RadioButton)sender;
132      if (btn.Checked) { genealogyGraphChart.Chart.Mode = ChartMode.Zoom; }
133    }
134
135    private void selectModeButton_CheckedChanged(object sender, EventArgs e) {
136      var btn = (RadioButton)sender;
137      if (btn.Checked) { genealogyGraphChart.Chart.Mode = ChartMode.Select; }
138    }
139
[9420]140    private void genealogyGraphUpdated(object sender, EventArgs args) {
141      genealogyGraphChart.Graph = Content;
142    }
143
[7779]144    private void treeChart_SymbolicExpressionTreeNodeClicked(object sender, MouseEventArgs e) {
[8556]145      genealogyGraphChart.Chart.UpdateEnabled = false;
146      switch (e.Button) {
147        case MouseButtons.Left: {
148            selectedVisualSymbExprTreeNode = (VisualSymbolicExpressionTreeNode)sender;
149            if (selectedVisualSymbExprTreeNode == null) return;
[9084]150            ISymbolicExpressionTreeNode selectedSubtree = selectedVisualSymbExprTreeNode.SymbolicExpressionTreeNode;
151            var clonedSubtree = (ISymbolicExpressionTreeNode)selectedSubtree.Clone();
152            // map original nodes to cloned nodes
153            clonedNodes = selectedSubtree.IterateNodesPrefix()
154                                         .Zip(clonedSubtree.IterateNodesPrefix(), (original, clone) => new { original, clone })
155                                         .ToDictionary(p => p.original, p => p.clone);
156
157            foreach (var node in symbolicExpressionTreeChart.Tree.IterateNodesPrefix()) {
158              symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node).FillColor = Color.Transparent;
[8556]159            }
[9084]160            foreach (var node in selectedSubtree.IterateNodesPrefix()) {
[8556]161              symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node).FillColor = Color.LightBlue;
[9084]162            }
163            break;
[8556]164          }
165        case MouseButtons.Middle: {
166            var visualNode = (VisualSymbolicExpressionTreeNode)sender;
[9084]167            if (selectedVisualSymbExprTreeNode == null || selectedVisualSymbExprTreeNode == visualNode) return;
[8556]168            var selectedNode = visualNode.SymbolicExpressionTreeNode;
[9084]169            if (clonedNodes.ContainsKey(selectedNode)) {
170              var selectedClone = clonedNodes[selectedNode];
171              var parent = selectedClone.Parent;
[9420]172              if (parent == null) break;
173              int index = parent.IndexOfSubtree(selectedClone);
174              if (index == -1) break;
[9084]175              parent.RemoveSubtree(parent.IndexOfSubtree(selectedClone));
176              foreach (var node in selectedNode.IterateNodesPrefix()) {
[8556]177                symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node).FillColor = Color.Transparent;
[9084]178              }
[8556]179            }
[9084]180            break;
[8556]181          }
182      }
[9084]183      if (selectedVisualSymbExprTreeNode == null || clonedNodes == null) return;
[8556]184      // update visual graph nodes that contain matching trees
[9084]185      MatchNodesAndRepaint();
186      // refresh the tree chart
187      symbolicExpressionTreeChart.Repaint();
188    }
189
190    void MatchNodesAndRepaint() {
191      genealogyGraphChart.Chart.UpdateEnabled = false;
[7779]192      genealogyGraphChart.ClearAllNodes(); // clear node colors
[9084]193      var fragment = new Fragment(clonedNodes[selectedVisualSymbExprTreeNode.SymbolicExpressionTreeNode]);
194      var fragmentLength = fragment.Length;
[9420]195      // highlight nodes
[9084]196      genealogyGraphChart.HighlightNodes(from node in genealogyGraphChart.Graph.Nodes
197                                         let tree = node.SymbolicExpressionTree
198                                         where tree.Length >= fragmentLength
199                                         where tree.Root.ContainsFragment(fragment, comparer)
[9420]200                                         select tree);
201      // highlight edges that contain a matching fragment
202      var fragmentSimilarityComparer = new SymbolicExpressionTreeFragmentSimilarityComparer {
203        SimilarityComparer = comparer
204      };
205      var matchingEdges = genealogyGraphChart.Graph.Nodes.Where(n => n.InEdges != null)
206                           .SelectMany(n => n.InEdges).Where(edge => edge.Data != null && ((IFragment)edge.Data).Root != null)
207                           .Where(edge => fragmentSimilarityComparer.Equals(fragment, (IFragment)edge.Data));
208      genealogyGraphChart.HighlightArcs(matchingEdges, Color.DodgerBlue);
209
210
[8213]211      genealogyGraphChart.Chart.UpdateEnabled = true;
212      genealogyGraphChart.Chart.EnforceUpdate();
[7779]213    }
214
[8556]215    private void treeChart_SymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) {
216    }
[9084]217    #endregion
[9420]218
[9084]219    private void matchConstantsCheckBox_CheckedChanged(object sender, EventArgs e) {
220      comparer.MatchConstantValues = matchConstantsCheckBox.Checked;
221      if (selectedVisualSymbExprTreeNode != null)
222        MatchNodesAndRepaint();
[7779]223    }
[8213]224
[9084]225    private void matchVariableWeightsCheckBox_CheckedChanged(object sender, EventArgs e) {
226      comparer.MatchVariableWeights = matchVariableWeightsCheckBox.Checked;
227      if (selectedVisualSymbExprTreeNode != null)
228        MatchNodesAndRepaint();
[8213]229    }
230
[9084]231    private void matchVariableNamesCheckBox_CheckedChanged(object sender, EventArgs e) {
232      comparer.MatchVariableNames = matchVariableNamesCheckBox.Checked;
233      if (selectedVisualSymbExprTreeNode != null)
234        MatchNodesAndRepaint();
[8213]235    }
[9420]236
237    private void simpleLineagesCheckBox_CheckedChanged(object sender, EventArgs e) {
238      genealogyGraphChart.SimpleLineages = simpleLineagesCheckBox.Checked;
239    }
240
241    private void highlightAllButton_Click(object sender, EventArgs e) {
242      symbolicExpressionTreeChart.Repaint();
243      genealogyGraphChart.HighlightAll();
244    }
245
246    private void lockGenealogyCheckBox_CheckedChanged(object sender, EventArgs e) {
247      genealogyGraphChart.LockGenealogy = lockGenealogyCheckBox.Checked;
248    }
[7779]249  }
250}
Note: See TracBrowser for help on using the repository browser.