[8409] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9539] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8409] | 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 {
|
---|
[9006] | 30 | internal delegate void
|
---|
[9043] | 31 | ModifyTree(ISymbolicExpressionTree tree, ISymbolicExpressionTreeNode node, ISymbolicExpressionTreeNode oldChild, ISymbolicExpressionTreeNode newChild, bool removeSubtree = true);
|
---|
[9006] | 32 |
|
---|
| 33 | internal sealed partial class InteractiveSymbolicExpressionTreeChart : SymbolicExpressionTreeChart {
|
---|
[8980] | 34 | private ISymbolicExpressionTreeNode tempNode; // node in clipboard (to be cut/copy/pasted etc)
|
---|
[8409] | 35 | private VisualSymbolicExpressionTreeNode currSelected; // currently selected node
|
---|
[9043] | 36 | private enum EditOp { NoOp, CopySubtree, CutSubtree }
|
---|
[8409] | 37 | private EditOp lastOp = EditOp.NoOp;
|
---|
| 38 |
|
---|
[9006] | 39 | // delegate to notify the parent container (the view) about the tree edit operations that it needs to perform
|
---|
| 40 | public ModifyTree ModifyTree { get; set; }
|
---|
[8980] | 41 |
|
---|
[8409] | 42 | public InteractiveSymbolicExpressionTreeChart() {
|
---|
| 43 | InitializeComponent();
|
---|
| 44 | currSelected = null;
|
---|
| 45 | tempNode = null;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | private void contextMenuStrip_Opened(object sender, EventArgs e) {
|
---|
[8980] | 49 | var menuStrip = (ContextMenuStrip)sender;
|
---|
| 50 | var point = menuStrip.SourceControl.PointToClient(Cursor.Position);
|
---|
| 51 | var ea = new MouseEventArgs(MouseButtons.Left, 1, point.X, point.Y, 0);
|
---|
[9006] | 52 | var visualNode = FindVisualSymbolicExpressionTreeNodeAt(ea.X, ea.Y);
|
---|
[9043] | 53 | if (visualNode != null) {
|
---|
| 54 | OnSymbolicExpressionTreeNodeClicked(visualNode, ea);
|
---|
| 55 | } else {
|
---|
| 56 | currSelected = null;
|
---|
| 57 | }
|
---|
[8980] | 58 |
|
---|
[8935] | 59 | if (currSelected == null) {
|
---|
| 60 | insertNodeToolStripMenuItem.Visible = false;
|
---|
[8991] | 61 | changeNodeToolStripMenuItem.Visible = false;
|
---|
[8935] | 62 | copyToolStripMenuItem.Visible = false;
|
---|
| 63 | cutToolStripMenuItem.Visible = false;
|
---|
[8991] | 64 | removeToolStripMenuItem.Visible = false;
|
---|
[8935] | 65 | pasteToolStripMenuItem.Visible = false;
|
---|
[8409] | 66 | } else {
|
---|
[8935] | 67 | var node = currSelected.SymbolicExpressionTreeNode;
|
---|
[8991] | 68 | insertNodeToolStripMenuItem.Visible = true;
|
---|
| 69 | changeNodeToolStripMenuItem.Visible = true;
|
---|
| 70 | changeNodeToolStripMenuItem.Enabled = (node is SymbolicExpressionTreeTerminalNode);
|
---|
| 71 | insertNodeToolStripMenuItem.Enabled = !changeNodeToolStripMenuItem.Enabled;
|
---|
[8935] | 72 | copyToolStripMenuItem.Visible = true;
|
---|
| 73 | cutToolStripMenuItem.Visible = true;
|
---|
[8991] | 74 | removeToolStripMenuItem.Visible = true;
|
---|
[9056] | 75 |
|
---|
[8991] | 76 | pasteToolStripMenuItem.Visible = true;
|
---|
[9056] | 77 | pasteToolStripMenuItem.Enabled = tempNode != null && insertNodeToolStripMenuItem.Enabled
|
---|
| 78 | && !(lastOp == EditOp.CutSubtree
|
---|
| 79 | && tempNode.IterateNodesBreadth().Contains(currSelected.SymbolicExpressionTreeNode));
|
---|
[8409] | 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[8935] | 83 | protected override void OnSymbolicExpressionTreeNodeClicked(object sender, MouseEventArgs e) {
|
---|
[9043] | 84 | currSelected = (VisualSymbolicExpressionTreeNode)sender; ;
|
---|
| 85 | if (currSelected != null) {
|
---|
| 86 | currSelected.LineColor = Color.FromArgb(130, currSelected.LineColor);
|
---|
| 87 | RepaintNode(currSelected);
|
---|
| 88 | }
|
---|
[9055] | 89 | base.OnSymbolicExpressionTreeNodeClicked(sender, e);
|
---|
[8409] | 90 | }
|
---|
| 91 |
|
---|
[9043] | 92 | protected override void SymbolicExpressionTreeChart_MouseClick(object sender, MouseEventArgs e) {
|
---|
[9055] | 93 | if (currSelected != null) {
|
---|
[9043] | 94 | currSelected.LineColor = Color.FromArgb(255, currSelected.LineColor);
|
---|
| 95 | RepaintNode(currSelected);
|
---|
| 96 | }
|
---|
[9055] | 97 | base.SymbolicExpressionTreeChart_MouseClick(sender, e);
|
---|
[9043] | 98 | }
|
---|
| 99 |
|
---|
[8980] | 100 | protected override void OnSymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) {
|
---|
[9006] | 101 | currSelected = null;
|
---|
| 102 | base.OnSymbolicExpressionTreeNodeDoubleClicked(sender, e);
|
---|
[8980] | 103 | }
|
---|
| 104 |
|
---|
[8409] | 105 | private void insertNodeToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
| 106 | if (currSelected == null || currSelected.SymbolicExpressionTreeNode is SymbolicExpressionTreeTerminalNode) return;
|
---|
[8980] | 107 | var parent = currSelected.SymbolicExpressionTreeNode;
|
---|
[8409] | 108 |
|
---|
[8935] | 109 | using (var dialog = new InsertNodeDialog()) {
|
---|
[8980] | 110 | dialog.SetAllowedSymbols(parent.Grammar.AllowedSymbols.Where(s => s.Enabled && s.InitialFrequency > 0.0 && !(s is ProgramRootSymbol || s is StartSymbol || s is Defun)));
|
---|
[8935] | 111 | dialog.ShowDialog(this);
|
---|
[9006] | 112 | if (dialog.DialogResult != DialogResult.OK) return;
|
---|
[8980] | 113 |
|
---|
[9006] | 114 | var symbol = dialog.SelectedSymbol();
|
---|
| 115 | var node = symbol.CreateTreeNode();
|
---|
| 116 | if (node is ConstantTreeNode) {
|
---|
| 117 | var constant = node as ConstantTreeNode;
|
---|
| 118 | constant.Value = double.Parse(dialog.constantValueTextBox.Text);
|
---|
| 119 | } else if (node is VariableTreeNode) {
|
---|
| 120 | var variable = node as VariableTreeNode;
|
---|
| 121 | variable.Weight = double.Parse(dialog.variableWeightTextBox.Text);
|
---|
| 122 | variable.VariableName = dialog.variableNamesCombo.Text;
|
---|
| 123 | } else if (node.Symbol.MinimumArity <= parent.SubtreeCount && node.Symbol.MaximumArity >= parent.SubtreeCount) {
|
---|
| 124 | for (int i = parent.SubtreeCount - 1; i >= 0; --i) {
|
---|
| 125 | var child = parent.GetSubtree(i);
|
---|
| 126 | parent.RemoveSubtree(i);
|
---|
| 127 | node.AddSubtree(child);
|
---|
[8980] | 128 | }
|
---|
| 129 | }
|
---|
[9006] | 130 | // the if condition will always be true for the final else clause above
|
---|
| 131 | if (parent.Symbol.MaximumArity > parent.SubtreeCount) {
|
---|
| 132 | ModifyTree(Tree, parent, null, node);
|
---|
| 133 | }
|
---|
[8935] | 134 | }
|
---|
[9006] | 135 | currSelected = null;
|
---|
[8409] | 136 | }
|
---|
[8991] | 137 | private void changeNodeToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
[8409] | 138 | if (currSelected == null) return;
|
---|
[8980] | 139 |
|
---|
[9006] | 140 | var node = (ISymbolicExpressionTreeNode)currSelected.SymbolicExpressionTreeNode.Clone();
|
---|
[8980] | 141 | var originalNode = currSelected.SymbolicExpressionTreeNode;
|
---|
[9006] | 142 |
|
---|
[8980] | 143 | ISymbolicExpressionTreeNode newNode = null;
|
---|
| 144 | var result = DialogResult.Cancel;
|
---|
[8409] | 145 | if (node is ConstantTreeNode) {
|
---|
[8980] | 146 | using (var dialog = new ConstantNodeEditDialog(node)) {
|
---|
| 147 | dialog.ShowDialog(this);
|
---|
| 148 | newNode = dialog.NewNode;
|
---|
| 149 | result = dialog.DialogResult;
|
---|
| 150 | }
|
---|
[8409] | 151 | } else if (node is VariableTreeNode) {
|
---|
[8980] | 152 | using (var dialog = new VariableNodeEditDialog(node)) {
|
---|
| 153 | dialog.ShowDialog(this);
|
---|
| 154 | newNode = dialog.NewNode;
|
---|
| 155 | result = dialog.DialogResult;
|
---|
[8409] | 156 | }
|
---|
| 157 | }
|
---|
[8980] | 158 | if (result != DialogResult.OK) return;
|
---|
[9006] | 159 | ModifyTree(Tree, originalNode.Parent, originalNode, newNode); // this will replace the original node with the new node
|
---|
| 160 | currSelected = null;
|
---|
[8409] | 161 | }
|
---|
[8991] | 162 | private void cutToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
[8409] | 163 | lastOp = EditOp.CutSubtree;
|
---|
[9043] | 164 | copySubtree();
|
---|
[8409] | 165 | }
|
---|
[8991] | 166 | private void copyToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
[8409] | 167 | lastOp = EditOp.CopySubtree;
|
---|
[9043] | 168 | copySubtree();
|
---|
| 169 | }
|
---|
| 170 | private void copySubtree() {
|
---|
[9006] | 171 | if (tempNode != null) {
|
---|
[9055] | 172 | foreach (var subtree in tempNode.IterateNodesPostfix()) {
|
---|
| 173 | var visualNode = GetVisualSymbolicExpressionTreeNode(subtree);
|
---|
| 174 | visualNode.LineColor = Color.FromArgb(255, visualNode.LineColor); // reset the alpha value to 255
|
---|
| 175 | visualNode.TextColor = Color.FromArgb(255, visualNode.TextColor);
|
---|
[9006] | 176 | if (subtree.Parent != null) {
|
---|
[9055] | 177 | var visualLine = GetVisualSymbolicExpressionTreeNodeConnection(subtree.Parent, subtree);
|
---|
| 178 | visualLine.LineColor = Color.FromArgb(255, visualLine.LineColor);
|
---|
[9006] | 179 | }
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
[8409] | 182 | tempNode = currSelected.SymbolicExpressionTreeNode;
|
---|
| 183 | foreach (var node in tempNode.IterateNodesPostfix()) {
|
---|
[8946] | 184 | var visualNode = GetVisualSymbolicExpressionTreeNode(node);
|
---|
[9043] | 185 | visualNode.LineColor = Color.FromArgb(100, visualNode.LineColor);
|
---|
| 186 | visualNode.TextColor = Color.FromArgb(100, visualNode.TextColor);
|
---|
[8935] | 187 | foreach (var subtree in node.Subtrees) {
|
---|
[8946] | 188 | var visualLine = GetVisualSymbolicExpressionTreeNodeConnection(node, subtree);
|
---|
[9043] | 189 | visualLine.LineColor = Color.FromArgb(100, visualLine.LineColor);
|
---|
[8409] | 190 | }
|
---|
| 191 | }
|
---|
[9006] | 192 | currSelected = null;
|
---|
[8409] | 193 | Repaint();
|
---|
| 194 | }
|
---|
[8991] | 195 | private void removeNodeToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
[8409] | 196 | var node = currSelected.SymbolicExpressionTreeNode;
|
---|
[9055] | 197 | if (node == tempNode) tempNode = null;
|
---|
[9006] | 198 | ModifyTree(Tree, node.Parent, node, null, removeSubtree: false);
|
---|
[8935] | 199 | currSelected = null; // because the currently selected node was just deleted
|
---|
[8409] | 200 | }
|
---|
[8991] | 201 | private void removeSubtreeToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
[8409] | 202 | var node = currSelected.SymbolicExpressionTreeNode;
|
---|
[9055] | 203 | if (node.IterateNodesPostfix().Contains(tempNode)) tempNode = null;
|
---|
[9006] | 204 | ModifyTree(Tree, node.Parent, node, null, removeSubtree: true);
|
---|
[8935] | 205 | currSelected = null; // because the currently selected node was just deleted
|
---|
[9006] | 206 | contextMenuStrip.Close(); // avoid display of submenus since the action has already been performed
|
---|
[8409] | 207 | }
|
---|
| 208 | private void pasteToolStripMenuItem_Clicked(object sender, EventArgs e) {
|
---|
[9006] | 209 | if (!(lastOp == EditOp.CopySubtree || lastOp == EditOp.CutSubtree)) return;
|
---|
[8409] | 210 | // check if the copied/cut node (stored in the tempNode) can be inserted as a child of the current selected node
|
---|
| 211 | var node = currSelected.SymbolicExpressionTreeNode;
|
---|
[8980] | 212 | if (node is ConstantTreeNode || node is VariableTreeNode) return;
|
---|
[8409] | 213 | // check if the currently selected node can accept the copied node as a child
|
---|
| 214 | // no need to check the grammar, an arity check will do just fine here
|
---|
[9006] | 215 | if (node.Symbol.MaximumArity <= node.SubtreeCount) return;
|
---|
| 216 | switch (lastOp) {
|
---|
| 217 | case EditOp.CutSubtree: {
|
---|
[9043] | 218 | if (tempNode.IterateNodesBreadth().Contains(node))
|
---|
[9057] | 219 | throw new ArgumentException();// cannot cut/paste a node into itself
|
---|
| 220 | ModifyTree(Tree, tempNode.Parent, tempNode, null); //remove node from its original parent
|
---|
| 221 | ModifyTree(Tree, node, null, tempNode); //insert it as a child to the new parent
|
---|
[9006] | 222 | break;
|
---|
| 223 | }
|
---|
| 224 | case EditOp.CopySubtree: {
|
---|
| 225 | var clone = (SymbolicExpressionTreeNode)tempNode.Clone();
|
---|
[9057] | 226 | ModifyTree(Tree, node, null, clone);
|
---|
[9006] | 227 | break;
|
---|
| 228 | }
|
---|
[8409] | 229 | }
|
---|
[9006] | 230 | currSelected = null; // because the tree will have changed
|
---|
[9043] | 231 | tempNode = null; // clear the clipboard after one paste
|
---|
[8409] | 232 | }
|
---|
| 233 | }
|
---|
| 234 | }
|
---|