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
Line 
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;
23using System.Collections.Generic;
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;
31using HeuristicLab.Problems.DataAnalysis.Symbolic;
32using HeuristicLab.Visualization;
33
34namespace HeuristicLab.EvolutionaryTracking.Views {
35  [View("SymbolicExpressionTreeGenealogyGraph")]
36  [Content(typeof(SymbolicExpressionTreeGenealogyGraph), IsDefaultView = true)]
37  public sealed partial class GenealogyGraphView : ItemView {
38    private VisualSymbolicExpressionTreeNode selectedVisualSymbExprTreeNode;
39    private SymbolicExpressionTreeNodeSimilarityComparer comparer;
40    private Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> clonedNodes;
41
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();
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;
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
58      comparer = new SymbolicExpressionTreeNodeSimilarityComparer {
59        MatchConstantValues = true,
60        MatchVariableWeights = true,
61        MatchVariableNames = true
62      };
63    }
64
65    protected override void DeregisterContentEvents() {
66      // TODO: Deregister your event handlers here
67      genealogyGraphChart.GenealogyGraphNodeClicked -= graphChart_GenealogyGraphNodeClicked;
68      symbolicExpressionTreeChart.SymbolicExpressionTreeNodeClicked -= treeChart_SymbolicExpressionTreeNodeClicked;
69      symbolicExpressionTreeChart.SymbolicExpressionTreeNodeDoubleClicked -= treeChart_SymbolicExpressionTreeNodeDoubleClicked;
70      Content.GraphUpdated -= genealogyGraphUpdated;
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;
79      symbolicExpressionTreeChart.SymbolicExpressionTreeNodeDoubleClicked += treeChart_SymbolicExpressionTreeNodeDoubleClicked;
80      Content.GraphUpdated += genealogyGraphUpdated;
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;
91        //        var best = Content.Nodes.OrderByDescending(x => x.Quality).First();
92        //        symbolicExpressionTreeChart.Tree = best.SymbolicExpressionTree;
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) {
109      // the type hierarchy goes like this: VisualGenealogyGraphNode --> GenealogyGraphNode --> SymbolicExpressionTree
110      var visualGenealogyGraphNode = (VisualGenealogyGraphNode)sender;
111      var genealogyGraphNode = visualGenealogyGraphNode.Data;
112      symbolicExpressionTreeChart.Tree = genealogyGraphNode.SymbolicExpressionTree;
113      if (genealogyGraphNode.InEdges == null) return;
114      var arc = genealogyGraphNode.InEdges.Last(x => x.Source != x.Target);
115      if (arc == null || arc.Data == null) return;
116      var fragment = (IFragment)arc.Data;
117      if (fragment.Root == null) return;
118      foreach (var node in fragment.Root.IterateNodesBreadth()) {
119        var visualSymbExprTreeNode = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node);
120        visualSymbExprTreeNode.LineColor = Color.OrangeRed;
121      }
122      symbolicExpressionTreeChart.Repaint();
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
140    private void genealogyGraphUpdated(object sender, EventArgs args) {
141      genealogyGraphChart.Graph = Content;
142    }
143
144    private void treeChart_SymbolicExpressionTreeNodeClicked(object sender, MouseEventArgs e) {
145      genealogyGraphChart.Chart.UpdateEnabled = false;
146      switch (e.Button) {
147        case MouseButtons.Left: {
148            selectedVisualSymbExprTreeNode = (VisualSymbolicExpressionTreeNode)sender;
149            if (selectedVisualSymbExprTreeNode == null) return;
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;
159            }
160            foreach (var node in selectedSubtree.IterateNodesPrefix()) {
161              symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node).FillColor = Color.LightBlue;
162            }
163            break;
164          }
165        case MouseButtons.Middle: {
166            var visualNode = (VisualSymbolicExpressionTreeNode)sender;
167            if (selectedVisualSymbExprTreeNode == null || selectedVisualSymbExprTreeNode == visualNode) return;
168            var selectedNode = visualNode.SymbolicExpressionTreeNode;
169            if (clonedNodes.ContainsKey(selectedNode)) {
170              var selectedClone = clonedNodes[selectedNode];
171              var parent = selectedClone.Parent;
172              if (parent == null) break;
173              int index = parent.IndexOfSubtree(selectedClone);
174              if (index == -1) break;
175              parent.RemoveSubtree(parent.IndexOfSubtree(selectedClone));
176              foreach (var node in selectedNode.IterateNodesPrefix()) {
177                symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node).FillColor = Color.Transparent;
178              }
179            }
180            break;
181          }
182      }
183      if (selectedVisualSymbExprTreeNode == null || clonedNodes == null) return;
184      // update visual graph nodes that contain matching trees
185      MatchNodesAndRepaint();
186      // refresh the tree chart
187      symbolicExpressionTreeChart.Repaint();
188    }
189
190    void MatchNodesAndRepaint() {
191      genealogyGraphChart.Chart.UpdateEnabled = false;
192      genealogyGraphChart.ClearAllNodes(); // clear node colors
193      var fragment = new Fragment(clonedNodes[selectedVisualSymbExprTreeNode.SymbolicExpressionTreeNode]);
194      var fragmentLength = fragment.Length;
195      // highlight nodes
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)
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
211      genealogyGraphChart.Chart.UpdateEnabled = true;
212      genealogyGraphChart.Chart.EnforceUpdate();
213    }
214
215    private void treeChart_SymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) {
216    }
217    #endregion
218
219    private void matchConstantsCheckBox_CheckedChanged(object sender, EventArgs e) {
220      comparer.MatchConstantValues = matchConstantsCheckBox.Checked;
221      if (selectedVisualSymbExprTreeNode != null)
222        MatchNodesAndRepaint();
223    }
224
225    private void matchVariableWeightsCheckBox_CheckedChanged(object sender, EventArgs e) {
226      comparer.MatchVariableWeights = matchVariableWeightsCheckBox.Checked;
227      if (selectedVisualSymbExprTreeNode != null)
228        MatchNodesAndRepaint();
229    }
230
231    private void matchVariableNamesCheckBox_CheckedChanged(object sender, EventArgs e) {
232      comparer.MatchVariableNames = matchVariableNamesCheckBox.Checked;
233      if (selectedVisualSymbExprTreeNode != null)
234        MatchNodesAndRepaint();
235    }
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    }
249  }
250}
Note: See TracBrowser for help on using the repository browser.