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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Drawing;
|
---|
25 | using System.Linq;
|
---|
26 | using System.Windows.Forms;
|
---|
27 | using HeuristicLab.Core.Views;
|
---|
28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
29 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
|
---|
30 | using HeuristicLab.MainForm;
|
---|
31 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
32 | using HeuristicLab.Visualization;
|
---|
33 |
|
---|
34 | namespace 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 | comparer = new SymbolicExpressionTreeNodeSimilarityComparer {
|
---|
55 | MatchConstantValues = true,
|
---|
56 | MatchVariableWeights = true,
|
---|
57 | MatchVariableNames = true
|
---|
58 | };
|
---|
59 | }
|
---|
60 |
|
---|
61 | protected override void DeregisterContentEvents() {
|
---|
62 | // TODO: Deregister your event handlers here
|
---|
63 | genealogyGraphChart.GenealogyGraphNodeClicked -= graphChart_GenealogyGraphNodeClicked;
|
---|
64 | symbolicExpressionTreeChart.SymbolicExpressionTreeNodeClicked -= treeChart_SymbolicExpressionTreeNodeClicked;
|
---|
65 | base.DeregisterContentEvents();
|
---|
66 | }
|
---|
67 |
|
---|
68 | protected override void RegisterContentEvents() {
|
---|
69 | base.RegisterContentEvents();
|
---|
70 | // TODO: Register your event handlers here
|
---|
71 | genealogyGraphChart.GenealogyGraphNodeClicked += graphChart_GenealogyGraphNodeClicked;
|
---|
72 | symbolicExpressionTreeChart.SymbolicExpressionTreeNodeClicked += treeChart_SymbolicExpressionTreeNodeClicked;
|
---|
73 | symbolicExpressionTreeChart.SymbolicExpressionTreeNodeDoubleClicked += treeChart_SymbolicExpressionTreeNodeDoubleClicked;
|
---|
74 | }
|
---|
75 |
|
---|
76 | #region Event Handlers (Content)
|
---|
77 | // TODO: Put event handlers of the content here
|
---|
78 | protected override void OnContentChanged() {
|
---|
79 | base.OnContentChanged();
|
---|
80 | if (Content == null)
|
---|
81 | genealogyGraphChart.Graph = null;
|
---|
82 | else {
|
---|
83 | genealogyGraphChart.Graph = Content;
|
---|
84 | var best = Content.Nodes.OrderByDescending(x => x.Quality).First();
|
---|
85 | symbolicExpressionTreeChart.Tree = best.SymbolicExpressionTree;
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | private void Content_GraphChanged(object sender, EventArgs e) {
|
---|
90 | }
|
---|
91 |
|
---|
92 | protected override void SetEnabledStateOfControls() {
|
---|
93 | base.SetEnabledStateOfControls();
|
---|
94 | genealogyGraphChart.Enabled = Content != null;
|
---|
95 | }
|
---|
96 | #endregion
|
---|
97 |
|
---|
98 | #region Event Handlers (child controls)
|
---|
99 |
|
---|
100 | // TODO: Put event handlers of child controls here.
|
---|
101 | private void graphChart_GenealogyGraphNodeClicked(object sender, MouseEventArgs e) {
|
---|
102 | // the type hierarchy goes like this: VisualGenealogyGraphNode --> GenealogyGraphNode --> SymbolicExpressionTree
|
---|
103 | var visualGenealogyGraphNode = (VisualGenealogyGraphNode)sender;
|
---|
104 | var genealogyGraphNode = visualGenealogyGraphNode.Data;
|
---|
105 | symbolicExpressionTreeChart.Tree = genealogyGraphNode.SymbolicExpressionTree;
|
---|
106 | if (genealogyGraphNode.InEdges == null) return;
|
---|
107 | var arc = genealogyGraphNode.InEdges.First(x => x.Source != x.Target);
|
---|
108 | if (arc == null || arc.Data == null) return;
|
---|
109 | var fragment = (IFragment)arc.Data;
|
---|
110 | if (fragment.Root == null) return;
|
---|
111 | foreach (var node in fragment.Root.IterateNodesBreadth()) {
|
---|
112 | var visualSymbExprTreeNode = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node);
|
---|
113 | visualSymbExprTreeNode.LineColor = Color.OrangeRed;
|
---|
114 | }
|
---|
115 | symbolicExpressionTreeChart.Repaint(); // pain orange nodes
|
---|
116 | }
|
---|
117 |
|
---|
118 | private void moveModeButton_CheckedChanged(object sender, EventArgs e) {
|
---|
119 | var btn = (RadioButton)sender;
|
---|
120 | if (btn.Checked) { genealogyGraphChart.Chart.Mode = ChartMode.Move; }
|
---|
121 | }
|
---|
122 |
|
---|
123 | private void zoomModeButton_CheckedChanged(object sender, EventArgs e) {
|
---|
124 | var btn = (RadioButton)sender;
|
---|
125 | if (btn.Checked) { genealogyGraphChart.Chart.Mode = ChartMode.Zoom; }
|
---|
126 | }
|
---|
127 |
|
---|
128 | private void selectModeButton_CheckedChanged(object sender, EventArgs e) {
|
---|
129 | var btn = (RadioButton)sender;
|
---|
130 | if (btn.Checked) { genealogyGraphChart.Chart.Mode = ChartMode.Select; }
|
---|
131 | }
|
---|
132 |
|
---|
133 | private void treeChart_SymbolicExpressionTreeNodeClicked(object sender, MouseEventArgs e) {
|
---|
134 | genealogyGraphChart.Chart.UpdateEnabled = false;
|
---|
135 | switch (e.Button) {
|
---|
136 | case MouseButtons.Left: {
|
---|
137 | selectedVisualSymbExprTreeNode = (VisualSymbolicExpressionTreeNode)sender;
|
---|
138 | if (selectedVisualSymbExprTreeNode == null) return;
|
---|
139 | ISymbolicExpressionTreeNode selectedSubtree = selectedVisualSymbExprTreeNode.SymbolicExpressionTreeNode;
|
---|
140 | var clonedSubtree = (ISymbolicExpressionTreeNode)selectedSubtree.Clone();
|
---|
141 | // map original nodes to cloned nodes
|
---|
142 | clonedNodes = selectedSubtree.IterateNodesPrefix()
|
---|
143 | .Zip(clonedSubtree.IterateNodesPrefix(), (original, clone) => new { original, clone })
|
---|
144 | .ToDictionary(p => p.original, p => p.clone);
|
---|
145 |
|
---|
146 | foreach (var node in symbolicExpressionTreeChart.Tree.IterateNodesPrefix()) {
|
---|
147 | symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node).FillColor = Color.Transparent;
|
---|
148 | }
|
---|
149 | foreach (var node in selectedSubtree.IterateNodesPrefix()) {
|
---|
150 | symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node).FillColor = Color.LightBlue;
|
---|
151 | }
|
---|
152 | break;
|
---|
153 | }
|
---|
154 | case MouseButtons.Middle: {
|
---|
155 | var visualNode = (VisualSymbolicExpressionTreeNode)sender;
|
---|
156 | if (selectedVisualSymbExprTreeNode == null || selectedVisualSymbExprTreeNode == visualNode) return;
|
---|
157 | var selectedNode = visualNode.SymbolicExpressionTreeNode;
|
---|
158 | if (clonedNodes.ContainsKey(selectedNode)) {
|
---|
159 | var selectedClone = clonedNodes[selectedNode];
|
---|
160 | var parent = selectedClone.Parent;
|
---|
161 | parent.RemoveSubtree(parent.IndexOfSubtree(selectedClone));
|
---|
162 | foreach (var node in selectedNode.IterateNodesPrefix()) {
|
---|
163 | symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node).FillColor = Color.Transparent;
|
---|
164 | }
|
---|
165 | }
|
---|
166 | break;
|
---|
167 | }
|
---|
168 | }
|
---|
169 | if (selectedVisualSymbExprTreeNode == null || clonedNodes == null) return;
|
---|
170 | // update visual graph nodes that contain matching trees
|
---|
171 | MatchNodesAndRepaint();
|
---|
172 | // refresh the tree chart
|
---|
173 | symbolicExpressionTreeChart.Repaint();
|
---|
174 | }
|
---|
175 |
|
---|
176 | void MatchNodesAndRepaint() {
|
---|
177 | genealogyGraphChart.Chart.UpdateEnabled = false;
|
---|
178 | genealogyGraphChart.ClearAllNodes(); // clear node colors
|
---|
179 | var fragment = new Fragment(clonedNodes[selectedVisualSymbExprTreeNode.SymbolicExpressionTreeNode]);
|
---|
180 | var fragmentLength = fragment.Length;
|
---|
181 | genealogyGraphChart.HighlightNodes(from node in genealogyGraphChart.Graph.Nodes
|
---|
182 | let tree = node.SymbolicExpressionTree
|
---|
183 | where tree.Length >= fragmentLength
|
---|
184 | where tree.Root.ContainsFragment(fragment, comparer)
|
---|
185 | select tree, Color.DodgerBlue);
|
---|
186 | genealogyGraphChart.Chart.UpdateEnabled = true;
|
---|
187 | genealogyGraphChart.Chart.EnforceUpdate();
|
---|
188 | }
|
---|
189 |
|
---|
190 | private void treeChart_SymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) {
|
---|
191 | }
|
---|
192 | #endregion
|
---|
193 | private void matchConstantsCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
194 | comparer.MatchConstantValues = matchConstantsCheckBox.Checked;
|
---|
195 | if (selectedVisualSymbExprTreeNode != null)
|
---|
196 | MatchNodesAndRepaint();
|
---|
197 | }
|
---|
198 |
|
---|
199 | private void matchVariableWeightsCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
200 | comparer.MatchVariableWeights = matchVariableWeightsCheckBox.Checked;
|
---|
201 | if (selectedVisualSymbExprTreeNode != null)
|
---|
202 | MatchNodesAndRepaint();
|
---|
203 | }
|
---|
204 |
|
---|
205 | private void matchVariableNamesCheckBox_CheckedChanged(object sender, EventArgs e) {
|
---|
206 | comparer.MatchVariableNames = matchVariableNamesCheckBox.Checked;
|
---|
207 | if (selectedVisualSymbExprTreeNode != null)
|
---|
208 | MatchNodesAndRepaint();
|
---|
209 | }
|
---|
210 | }
|
---|
211 | }
|
---|