Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7799 was 7799, checked in by bburlacu, 11 years ago

#1772: Sanitized IGenealogyGraph interface, implemented new graph arc semantics (an arc signifies an interaction between the two nodes that it connects and has a data object containing specific information about the interaction).

File size: 8.9 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.Visualization;
32
33namespace HeuristicLab.EvolutionaryTracking.Views {
34  [View("SymbolicExpressionTreeGenealogyGraph")]
35  [Content(typeof(SymbolicExpressionTreeGenealogyGraph), IsDefaultView = true)]
36  public sealed partial class GenealogyGraphView : ItemView {
37    private VisualSymbolicExpressionTreeNode _selectedVisualSymbolicExpressionTreeNode;
38
39    public new SymbolicExpressionTreeGenealogyGraph Content {
40      get { return (SymbolicExpressionTreeGenealogyGraph)base.Content; }
41      set { base.Content = value; }
42    }
43
44    public GenealogyGraphView()
45      : base() {
46      InitializeComponent();
47      similarityModeSelector.SelectedIndex = 0; // set default similarity mode to "exact"
48    }
49
50    protected override void DeregisterContentEvents() {
51      // TODO: Deregister your event handlers here
52      genealogyGraphChart.GenealogyGraphNodeClicked -= graphChart_GenealogyGraphNodeClicked;
53      symbolicExpressionTreeChart.SymbolicExpressionTreeNodeClicked -= treeChart_SymbolicExpressionTreeNodeClicked;
54      base.DeregisterContentEvents();
55    }
56
57    protected override void RegisterContentEvents() {
58      base.RegisterContentEvents();
59      // TODO: Register your event handlers here
60      genealogyGraphChart.GenealogyGraphNodeClicked += graphChart_GenealogyGraphNodeClicked;
61      symbolicExpressionTreeChart.SymbolicExpressionTreeNodeClicked += treeChart_SymbolicExpressionTreeNodeClicked;
62    }
63
64    #region Event Handlers (Content)
65    // TODO: Put event handlers of the content here
66    protected override void OnContentChanged() {
67      base.OnContentChanged();
68      if (Content == null)
69        genealogyGraphChart.Graph = null;
70      else {
71        genealogyGraphChart.Graph = Content;
72        var best = Content.Values.OrderByDescending(x => x.Quality).First();
73        symbolicExpressionTreeChart.Tree = (ISymbolicExpressionTree)best.Data;
74      }
75    }
76
77    private void Content_GraphChanged(object sender, EventArgs e) {
78    }
79
80    protected override void SetEnabledStateOfControls() {
81      base.SetEnabledStateOfControls();
82      genealogyGraphChart.Enabled = Content != null;
83    }
84    #endregion
85
86    #region Event Handlers (child controls)
87
88    // TODO: Put event handlers of child controls here.
89    private void graphChart_GenealogyGraphNodeClicked(object sender, MouseEventArgs e) {
90      // the hierarchy goes like this: VisualGenealogyGraphNode --> GenealogyGraphNode --> symbolicExpressionTree
91      var visualGenealogyGraphNode = (VisualGenealogyGraphNode)sender;
92      var genealogyGraphNode = (GenealogyGraphNode)visualGenealogyGraphNode.Data;
93      symbolicExpressionTreeChart.Tree = (ISymbolicExpressionTree)genealogyGraphNode.Data;
94      if (_selectedVisualSymbolicExpressionTreeNode != null) {
95        var nodes = symbolicExpressionTreeChart.Tree.IterateNodesBreadth() as List<ISymbolicExpressionTreeNode>;
96        var fragments = _selectedVisualSymbolicExpressionTreeNode.SymbolicExpressionTreeNode.IterateNodesBreadth() as List<ISymbolicExpressionTreeNode>;
97        int index = SymbolicExpressionTreeMatching.FindMatch(nodes, fragments, similarityModeSelector.SelectedIndex);
98        if (index != -1) {
99          _selectedVisualSymbolicExpressionTreeNode = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(nodes[index + fragments.Count - 1]);
100          var subtree = _selectedVisualSymbolicExpressionTreeNode.SymbolicExpressionTreeNode;
101          foreach (var visualNode in subtree.IterateNodesBreadth().Select(node => symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node))) {
102            visualNode.FillColor = Color.LightBlue;
103          }
104          symbolicExpressionTreeChart.Repaint();
105        }
106      }
107      var tree = symbolicExpressionTreeChart.Tree;
108      var graphNode = Content.GetNode(tree);
109      if (graphNode.InEdges != null) {
110        var arc = graphNode.InEdges.Find(a => a.Data != null);
111        if (arc != null) {
112          var fragment = arc.Data as ISymbolicExpressionTreeNode;
113          foreach (var node in fragment.IterateNodesBreadth()) {
114            var visualNode = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node);
115            visualNode.FillColor = Color.LightGreen;
116          }
117        }
118      }
119      // what's left to be done here is to:
120      // - get the symbolic expression tree
121      // - get the corresponding fragment
122      // - for all symbolic expression tree nodes in the fragment, colorize the corresponding visual nodes
123      // - repaint the tree
124    }
125
126    private void moveModeButton_CheckedChanged(object sender, EventArgs e) {
127      var btn = (RadioButton)sender;
128      if (btn.Checked) { genealogyGraphChart.Chart.Mode = ChartMode.Move; }
129    }
130
131    private void zoomModeButton_CheckedChanged(object sender, EventArgs e) {
132      var btn = (RadioButton)sender;
133      if (btn.Checked) { genealogyGraphChart.Chart.Mode = ChartMode.Zoom; }
134    }
135
136    private void selectModeButton_CheckedChanged(object sender, EventArgs e) {
137      var btn = (RadioButton)sender;
138      if (btn.Checked) { genealogyGraphChart.Chart.Mode = ChartMode.Select; }
139    }
140
141    private void treeChart_SymbolicExpressionTreeNodeClicked(object sender, MouseEventArgs e) {
142      _selectedVisualSymbolicExpressionTreeNode = (VisualSymbolicExpressionTreeNode)sender;
143      // find out which individuals in the genealogy graph contain this specific subtree
144      var treeNode = _selectedVisualSymbolicExpressionTreeNode.SymbolicExpressionTreeNode;
145      Color[] colors = { Color.LightSkyBlue, Color.PaleGreen, Color.Tan };
146
147      genealogyGraphChart.ClearAllNodes(); // clear node colors
148      // color each graph node according to the degree to which it matches the selected tree fragment
149      foreach (var i in Enum.GetValues(typeof(SymbolicExpressionTreeMatching.SimilarityLevel)).Cast<int>().Reverse()) {
150        var owners = genealogyGraphChart.Graph.TraceFragment(treeNode, i).ToList();
151        if (owners.Any()) genealogyGraphChart.HighlightNodes(owners, colors[i]); // highlight matching individuals from the genealogy
152      }
153
154      // highlight subtree nodes in the tree chart
155      foreach (var visualNode in symbolicExpressionTreeChart.Tree.IterateNodesPostfix().Select(node => symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node)))
156        visualNode.FillColor = Color.Transparent;
157
158      foreach (var visualNode in treeNode.IterateNodesPostfix().Select(node => symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node)))
159        visualNode.FillColor = Color.LightBlue;
160
161      // refresh the tree chart
162      symbolicExpressionTreeChart.Repaint();
163    }
164
165    private void similarityModeSelector_SelectedIndexChanged(object sender, EventArgs e) {
166      if (_selectedVisualSymbolicExpressionTreeNode == null) return;
167      var treeNode = _selectedVisualSymbolicExpressionTreeNode.SymbolicExpressionTreeNode;
168      var owners = genealogyGraphChart.Graph.TraceFragment(treeNode, similarityModeSelector.SelectedIndex).ToList();
169      if (owners.Any()) {
170        genealogyGraphChart.ClearAllNodes(); // clear the fill color of all nodes
171        genealogyGraphChart.HighlightNodes(owners, Color.LightSkyBlue); // highlight matching individuals from the genealogy
172      }
173      // highlight subtree nodes in the tree chart
174      foreach (var visualNode in symbolicExpressionTreeChart.Tree.IterateNodesPostfix().Select(node => symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node)))
175        visualNode.FillColor = Color.Transparent;
176      foreach (var visualNode in treeNode.IterateNodesPostfix().Select(node => symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node)))
177        visualNode.FillColor = Color.LightBlue;
178    }
179    #endregion
180  }
181}
Note: See TracBrowser for help on using the repository browser.