1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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.Drawing;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Windows.Forms;
|
---|
26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
27 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
|
---|
30 | public partial class InteractiveSymbolicExpressionTreeChart : SymbolicExpressionTreeChart {
|
---|
31 | private ISymbolicExpressionTreeNode tempNode;
|
---|
32 | private VisualSymbolicExpressionTreeNode lastSelected; // previously selected node
|
---|
33 | private VisualSymbolicExpressionTreeNode currSelected; // currently selected node
|
---|
34 | private enum EditOp { NoOp, CopyNode, CopySubtree, CutNode, CutSubtree, DeleteNode, DeleteSubtree }
|
---|
35 | private enum TreeState { Valid, Invalid }
|
---|
36 | private EditOp lastOp = EditOp.NoOp;
|
---|
37 | private TreeState treeState = TreeState.Valid; // tree edit operations must leave the tree in a valid state
|
---|
38 |
|
---|
39 | public InteractiveSymbolicExpressionTreeChart() {
|
---|
40 | InitializeComponent();
|
---|
41 | // add extra actions in the context menu strips
|
---|
42 | this.contextMenuStrip.Opened += this.contextMenuStrip_Opened;
|
---|
43 | this.contextMenuStrip.Items.AddRange(new ToolStripItem[] {
|
---|
44 | insertNodeToolStripMenuItem,
|
---|
45 | changeValueToolStripMenuItem,
|
---|
46 | copyToolStripMenuItem,
|
---|
47 | cutToolStripMenuItem,
|
---|
48 | deleteToolStripMenuItem,
|
---|
49 | pasteToolStripMenuItem });
|
---|
50 | lastSelected = null;
|
---|
51 | currSelected = null;
|
---|
52 | tempNode = null;
|
---|
53 | }
|
---|
54 |
|
---|
55 | public bool TreeValid {
|
---|
56 | get { return TreeState.Valid == treeState; }
|
---|
57 | }
|
---|
58 |
|
---|
59 | // expose an additional event for signaling to the parent view when the tree structure was modified
|
---|
60 | // the emitting of the signal is conditional on the tree being valid, otherwise only a Repaint is issued
|
---|
61 | public event EventHandler SymbolicExpressionTreeChanged;
|
---|
62 | private void OnSymbolicExpressionTreeChanged(object sender, EventArgs e) {
|
---|
63 | if (IsValid(Tree)) {
|
---|
64 | treeStatusValue.Text = "Valid";
|
---|
65 | treeStatusValue.ForeColor = Color.Green;
|
---|
66 | treeState = TreeState.Valid;
|
---|
67 | var changed = SymbolicExpressionTreeChanged;
|
---|
68 | if (changed != null) {
|
---|
69 | changed(sender, e);
|
---|
70 | }
|
---|
71 | } else {
|
---|
72 | treeStatusValue.Text = "Invalid";
|
---|
73 | treeStatusValue.ForeColor = Color.Red;
|
---|
74 | treeState = TreeState.Invalid;
|
---|
75 | Repaint();
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | private void contextMenuStrip_Opened(object sender, EventArgs e) {
|
---|
80 | var menu = sender as ContextMenuStrip;
|
---|
81 | if (menu == null) return;
|
---|
82 | var point = menu.SourceControl.PointToClient(Cursor.Position);
|
---|
83 | var visualNode = FindVisualSymbolicExpressionTreeNodeAt(point.X, point.Y);
|
---|
84 | if (visualNode != null) {
|
---|
85 | lastSelected = currSelected;
|
---|
86 | if (lastSelected != null) lastSelected.LineColor = Color.Black;
|
---|
87 | currSelected = visualNode;
|
---|
88 | currSelected.LineColor = Color.LightGreen;
|
---|
89 | EnableTreeEditingMenuItems();
|
---|
90 | OnSymbolicExpressionTreeNodeClicked(currSelected, new MouseEventArgs(MouseButtons.Right, 1, point.X, point.Y, 0));
|
---|
91 | } else {
|
---|
92 | DisableTreeEditingMenuItems();
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | private void DisableTreeEditingMenuItems() {
|
---|
97 | insertNodeToolStripMenuItem.Visible = false;
|
---|
98 | changeValueToolStripMenuItem.Visible = false;
|
---|
99 | copyToolStripMenuItem.Visible = false;
|
---|
100 | cutToolStripMenuItem.Visible = false;
|
---|
101 | deleteToolStripMenuItem.Visible = false;
|
---|
102 | pasteToolStripMenuItem.Visible = false;
|
---|
103 | }
|
---|
104 |
|
---|
105 | private void EnableTreeEditingMenuItems() {
|
---|
106 |
|
---|
107 | var node = currSelected.SymbolicExpressionTreeNode;
|
---|
108 | changeValueToolStripMenuItem.Visible = (node is SymbolicExpressionTreeTerminalNode);
|
---|
109 | insertNodeToolStripMenuItem.Visible = !changeValueToolStripMenuItem.Visible;
|
---|
110 | copyToolStripMenuItem.Visible = true;
|
---|
111 | cutToolStripMenuItem.Visible = true;
|
---|
112 | deleteToolStripMenuItem.Visible = true;
|
---|
113 | pasteToolStripMenuItem.Visible = (lastOp == EditOp.CopyNode || lastOp == EditOp.CopySubtree || lastOp == EditOp.CutNode || lastOp == EditOp.CutSubtree) && insertNodeToolStripMenuItem.Visible;
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | protected override void SymbolicExpressionTreeChart_MouseClick(object sender, MouseEventArgs e) {
|
---|
118 | VisualSymbolicExpressionTreeNode visualTreeNode = FindVisualSymbolicExpressionTreeNodeAt(e.X, e.Y);
|
---|
119 | if (visualTreeNode != null) {
|
---|
120 | lastSelected = currSelected;
|
---|
121 | if (lastSelected != null) lastSelected.LineColor = Color.Black;
|
---|
122 | currSelected = visualTreeNode;
|
---|
123 | currSelected.LineColor = Color.LightGreen;
|
---|
124 | }
|
---|
125 | Repaint();
|
---|
126 | base.SymbolicExpressionTreeChart_MouseClick(sender, e);
|
---|
127 | }
|
---|
128 |
|
---|
129 | private void insertNodeToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
130 | if (currSelected == null || currSelected.SymbolicExpressionTreeNode is SymbolicExpressionTreeTerminalNode) return;
|
---|
131 | var node = currSelected.SymbolicExpressionTreeNode;
|
---|
132 |
|
---|
133 | var dialog = new InsertNodeDialog();
|
---|
134 | dialog.SetAllowedSymbols(node.Grammar.AllowedSymbols.Where(s => s.Enabled && s.InitialFrequency > 0.0 && !(s is ProgramRootSymbol || s is StartSymbol || s is Defun)));
|
---|
135 | dialog.DialogValidated += OnInsertNodeDialogValidated;
|
---|
136 | dialog.ShowDialog(this);
|
---|
137 | }
|
---|
138 |
|
---|
139 | private void changeValueToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
140 | if (currSelected == null) return;
|
---|
141 | var node = currSelected.SymbolicExpressionTreeNode;
|
---|
142 | var dialog = new ValueChangeDialog();
|
---|
143 | dialog.SetContent(node);
|
---|
144 | dialog.DialogValidated += OnChangeValueDialogValidated;
|
---|
145 | dialog.ShowDialog(this);
|
---|
146 | }
|
---|
147 |
|
---|
148 | public event EventHandler SymbolicExpressionTreeNodeChanged;
|
---|
149 | public event EventHandler SymbolicExpressionTreeNodeInserted;
|
---|
150 |
|
---|
151 | private void OnChangeValueDialogValidated(object sender, EventArgs e) {
|
---|
152 | SymbolicExpressionTreeNodeChanged(sender, e);
|
---|
153 | }
|
---|
154 |
|
---|
155 | private void OnInsertNodeDialogValidated(object sender, EventArgs e) {
|
---|
156 | var dialog = (InsertNodeDialog)sender;
|
---|
157 | var symbol = dialog.SelectedSymbol();
|
---|
158 | var node = symbol.CreateTreeNode();
|
---|
159 | var parent = currSelected.SymbolicExpressionTreeNode;
|
---|
160 | if (node is ConstantTreeNode) {
|
---|
161 | var constant = node as ConstantTreeNode;
|
---|
162 | constant.Value = double.Parse(dialog.constantValueTextBox.Text);
|
---|
163 | } else if (node is VariableTreeNode) {
|
---|
164 | var variable = node as VariableTreeNode;
|
---|
165 | variable.Weight = double.Parse(dialog.variableWeightTextBox.Text);
|
---|
166 | variable.VariableName = dialog.variableNamesCombo.Text;
|
---|
167 | } else {
|
---|
168 | if (node.Symbol.MinimumArity <= parent.SubtreeCount && node.Symbol.MaximumArity >= parent.SubtreeCount) {
|
---|
169 | for (int i = parent.SubtreeCount - 1; i >= 0; --i) {
|
---|
170 | var child = parent.GetSubtree(i);
|
---|
171 | parent.RemoveSubtree(i);
|
---|
172 | node.AddSubtree(child);
|
---|
173 | }
|
---|
174 | }
|
---|
175 | // we assume that there is no case in which the parents subtrees get transferred to node, but node doesn't get added as a subtree of parent
|
---|
176 | }
|
---|
177 | if (parent.Symbol.MaximumArity > parent.SubtreeCount) {
|
---|
178 | parent.AddSubtree(node);
|
---|
179 | Tree = Tree;
|
---|
180 | }
|
---|
181 | OnSymbolicExpressionTreeChanged(sender, e);
|
---|
182 | }
|
---|
183 |
|
---|
184 | private void cutNodeToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
185 | lastOp = EditOp.CutNode;
|
---|
186 | tempNode = currSelected.SymbolicExpressionTreeNode;
|
---|
187 | var visualNode = visualTreeNodes[tempNode];
|
---|
188 | visualNode.LineColor = Color.LightGray;
|
---|
189 | visualNode.TextColor = Color.LightGray;
|
---|
190 | Repaint();
|
---|
191 | }
|
---|
192 |
|
---|
193 | private void cutSubtreeToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
194 | lastOp = EditOp.CutSubtree;
|
---|
195 | tempNode = currSelected.SymbolicExpressionTreeNode; // should never be null
|
---|
196 | foreach (var node in tempNode.IterateNodesPostfix()) {
|
---|
197 | var visualNode = visualTreeNodes[node];
|
---|
198 | visualNode.LineColor = Color.LightGray;
|
---|
199 | visualNode.TextColor = Color.LightGray;
|
---|
200 | if (node.SubtreeCount > 0) {
|
---|
201 | foreach (var subtree in node.Subtrees) {
|
---|
202 | var visualLine = visualLines[new Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>(node, subtree)];
|
---|
203 | visualLine.LineColor = Color.LightGray;
|
---|
204 | }
|
---|
205 | }
|
---|
206 | }
|
---|
207 | Repaint();
|
---|
208 | }
|
---|
209 |
|
---|
210 | private void copyNodeToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
211 | lastOp = EditOp.CopyNode;
|
---|
212 | tempNode = currSelected.SymbolicExpressionTreeNode;
|
---|
213 | var visualNode = visualTreeNodes[tempNode];
|
---|
214 | visualNode.LineColor = Color.LightGray;
|
---|
215 | visualNode.TextColor = Color.LightGray;
|
---|
216 | Repaint();
|
---|
217 | }
|
---|
218 |
|
---|
219 | private void copySubtreeToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
220 | lastOp = EditOp.CopySubtree;
|
---|
221 | tempNode = currSelected.SymbolicExpressionTreeNode;
|
---|
222 | foreach (var node in tempNode.IterateNodesPostfix()) {
|
---|
223 | var visualNode = visualTreeNodes[node];
|
---|
224 | visualNode.LineColor = Color.LightGray;
|
---|
225 | visualNode.TextColor = Color.LightGray;
|
---|
226 | if (node.SubtreeCount > 0) {
|
---|
227 | foreach (var subtree in node.Subtrees) {
|
---|
228 | var visualLine = visualLines[new Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>(node, subtree)];
|
---|
229 | visualLine.LineColor = Color.LightGray;
|
---|
230 | }
|
---|
231 | }
|
---|
232 | }
|
---|
233 | Repaint();
|
---|
234 | }
|
---|
235 |
|
---|
236 | private void deleteNodeToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
237 | lastOp = EditOp.DeleteNode;
|
---|
238 | var node = currSelected.SymbolicExpressionTreeNode;
|
---|
239 | var parent = node.Parent;
|
---|
240 | if (parent.Symbol.MaximumArity >= node.SubtreeCount + parent.SubtreeCount - 1) { // -1 because tempNode will be removed
|
---|
241 | parent.RemoveSubtree(parent.IndexOfSubtree(node));
|
---|
242 | for (int i = node.SubtreeCount - 1; i >= 0; --i) {
|
---|
243 | var child = node.GetSubtree(i);
|
---|
244 | node.RemoveSubtree(i);
|
---|
245 | parent.AddSubtree(child);
|
---|
246 | }
|
---|
247 | } else {
|
---|
248 | // if the node cannot be deleted without breaking the tree, do nothing
|
---|
249 | // TODO: decide what to do
|
---|
250 | }
|
---|
251 | OnSymbolicExpressionTreeChanged(sender, e);
|
---|
252 | }
|
---|
253 |
|
---|
254 | private void deleteSubtreeToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
255 | lastOp = EditOp.DeleteSubtree;
|
---|
256 | var node = currSelected.SymbolicExpressionTreeNode;
|
---|
257 | var parent = node.Parent;
|
---|
258 | parent.RemoveSubtree(parent.IndexOfSubtree(node));
|
---|
259 | OnSymbolicExpressionTreeChanged(sender, e);
|
---|
260 | }
|
---|
261 |
|
---|
262 | private void pasteToolStripMenuItem_Clicked(object sender, EventArgs e) {
|
---|
263 | if (!(lastOp == EditOp.CopyNode || lastOp == EditOp.CopySubtree || lastOp == EditOp.CutNode || lastOp == EditOp.CutSubtree))
|
---|
264 | return;
|
---|
265 | // check if the copied/cut node (stored in the tempNode) can be inserted as a child of the current selected node
|
---|
266 | var node = currSelected.SymbolicExpressionTreeNode;
|
---|
267 | if (node is ConstantTreeNode || node is VariableTreeNode) return; // nothing to do
|
---|
268 | // check if the currently selected node can accept the copied node as a child
|
---|
269 | // no need to check the grammar, an arity check will do just fine here
|
---|
270 | if (node.Symbol.MaximumArity > node.SubtreeCount) {
|
---|
271 | switch (lastOp) {
|
---|
272 | case (EditOp.CutNode): {
|
---|
273 | // cut node
|
---|
274 | var parent = tempNode.Parent;
|
---|
275 | // arity checks to see if parent can accept node's children (we assume the grammar is already ok with that)
|
---|
276 | if (parent.Symbol.MaximumArity >= tempNode.SubtreeCount + parent.SubtreeCount - 1) {
|
---|
277 | // -1 because tempNode will be removed
|
---|
278 | parent.RemoveSubtree(parent.IndexOfSubtree(tempNode));
|
---|
279 | for (int i = tempNode.SubtreeCount - 1; i >= 0; --i) {
|
---|
280 | var child = tempNode.GetSubtree(i);
|
---|
281 | tempNode.RemoveSubtree(i);
|
---|
282 | parent.AddSubtree(child);
|
---|
283 | }
|
---|
284 | lastOp = EditOp.CopyNode;
|
---|
285 | }
|
---|
286 | break;
|
---|
287 | }
|
---|
288 | case (EditOp.CutSubtree): {
|
---|
289 | // cut subtree
|
---|
290 | tempNode.Parent.RemoveSubtree(tempNode.Parent.IndexOfSubtree(tempNode));
|
---|
291 | lastOp = EditOp.CopySubtree; // do this so the next paste will actually perform a copy
|
---|
292 | break;
|
---|
293 | }
|
---|
294 | case (EditOp.CopyNode): {
|
---|
295 | // copy node
|
---|
296 | var clone = tempNode.Clone() as ISymbolicExpressionTreeNode; // should never be null
|
---|
297 | clone.Parent = tempNode.Parent;
|
---|
298 | tempNode = clone;
|
---|
299 | if (tempNode != null && tempNode.SubtreeCount > 0) {
|
---|
300 | for (int i = tempNode.SubtreeCount - 1; i >= 0; --i)
|
---|
301 | tempNode.RemoveSubtree(i);
|
---|
302 | }
|
---|
303 | break;
|
---|
304 | }
|
---|
305 | case (EditOp.CopySubtree): {
|
---|
306 | // copy subtree
|
---|
307 | var clone = tempNode.Clone() as ISymbolicExpressionTreeNode;
|
---|
308 | clone.Parent = tempNode.Parent;
|
---|
309 | tempNode = clone;
|
---|
310 | break;
|
---|
311 | }
|
---|
312 | }
|
---|
313 | node.AddSubtree(tempNode);
|
---|
314 | Tree = Tree; // hack in order to trigger the reinitialization of the dictionaries after new nodes appeared in the graph
|
---|
315 | OnSymbolicExpressionTreeChanged(sender, e);
|
---|
316 | } else {
|
---|
317 | // throw exception or show a warning to the user about invalid arity
|
---|
318 | }
|
---|
319 | }
|
---|
320 |
|
---|
321 | private bool IsValid(ISymbolicExpressionTree tree) {
|
---|
322 | if (tree.IterateNodesPostfix().Any(node => node.SubtreeCount < node.Symbol.MinimumArity || node.SubtreeCount > node.Symbol.MaximumArity)) {
|
---|
323 | treeState = TreeState.Invalid;
|
---|
324 | return false;
|
---|
325 | }
|
---|
326 | treeState = TreeState.Valid;
|
---|
327 | return true;
|
---|
328 | }
|
---|
329 | }
|
---|
330 | }
|
---|