Free cookie consent management tool by TermsFeed Policy Generator

source: branches/irace/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicExpressionTreeChart.cs @ 12081

Last change on this file since 12081 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 10.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Drawing;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
30  internal delegate void
31  ModifyTree(ISymbolicExpressionTree tree, ISymbolicExpressionTreeNode node, ISymbolicExpressionTreeNode oldChild, ISymbolicExpressionTreeNode newChild, bool removeSubtree = true);
32
33  internal sealed partial class InteractiveSymbolicExpressionTreeChart : SymbolicExpressionTreeChart {
34    private ISymbolicExpressionTreeNode tempNode; // node in clipboard (to be cut/copy/pasted etc)
35    private VisualTreeNode<ISymbolicExpressionTreeNode> currSelected; // currently selected node
36    private enum EditOp { NoOp, CopySubtree, CutSubtree }
37    private EditOp lastOp = EditOp.NoOp;
38
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; }
41
42    public InteractiveSymbolicExpressionTreeChart() {
43      InitializeComponent();
44      currSelected = null;
45      tempNode = null;
46    }
47
48    private void contextMenuStrip_Opened(object sender, EventArgs e) {
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);
52      var visualNode = FindVisualSymbolicExpressionTreeNodeAt(ea.X, ea.Y);
53      if (visualNode != null) {
54        OnSymbolicExpressionTreeNodeClicked(visualNode, ea);
55      } else {
56        currSelected = null;
57      }
58
59      if (currSelected == null) {
60        insertNodeToolStripMenuItem.Visible = false;
61        changeNodeToolStripMenuItem.Visible = false;
62        copyToolStripMenuItem.Visible = false;
63        cutToolStripMenuItem.Visible = false;
64        removeToolStripMenuItem.Visible = false;
65        pasteToolStripMenuItem.Visible = false;
66      } else {
67        var node = currSelected.Content;
68        insertNodeToolStripMenuItem.Visible = true;
69        changeNodeToolStripMenuItem.Visible = true;
70        changeNodeToolStripMenuItem.Enabled = (node is SymbolicExpressionTreeTerminalNode);
71        insertNodeToolStripMenuItem.Enabled = !changeNodeToolStripMenuItem.Enabled;
72        copyToolStripMenuItem.Visible = true;
73        cutToolStripMenuItem.Visible = true;
74        removeToolStripMenuItem.Visible = true;
75
76        pasteToolStripMenuItem.Visible = true;
77        pasteToolStripMenuItem.Enabled = tempNode != null && insertNodeToolStripMenuItem.Enabled
78                                                          && !(lastOp == EditOp.CutSubtree && tempNode.IterateNodesBreadth().Contains(node))
79                                                          && node.SubtreeCount < node.Symbol.MaximumArity;
80      }
81    }
82
83    protected override void OnSymbolicExpressionTreeNodeClicked(object sender, MouseEventArgs e) {
84      currSelected = (VisualTreeNode<ISymbolicExpressionTreeNode>)sender; ;
85      base.OnSymbolicExpressionTreeNodeClicked(sender, e);
86    }
87
88    protected override void OnSymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) {
89      currSelected = null;
90      base.OnSymbolicExpressionTreeNodeDoubleClicked(sender, e);
91    }
92
93    private void insertNodeToolStripMenuItem_Click(object sender, EventArgs e) {
94      if (currSelected == null || currSelected.Content is SymbolicExpressionTreeTerminalNode) return;
95      var parent = currSelected.Content;
96
97      using (var dialog = new InsertNodeDialog()) {
98        dialog.SetAllowedSymbols(parent.Grammar.Symbols.Where(s => !(s is ProgramRootSymbol || s is StartSymbol || s is Defun || s is GroupSymbol))); // allow everything
99        dialog.ShowDialog(this);
100        if (dialog.DialogResult != DialogResult.OK) return;
101
102        var symbol = dialog.SelectedSymbol();
103        var node = symbol.CreateTreeNode();
104        if (node is ConstantTreeNode) {
105          var constant = node as ConstantTreeNode;
106          constant.Value = double.Parse(dialog.constantValueTextBox.Text);
107        } else if (node is VariableTreeNode) {
108          var variable = node as VariableTreeNode;
109          variable.Weight = double.Parse(dialog.variableWeightTextBox.Text);
110          variable.VariableName = dialog.variableNamesCombo.Text;
111        } else if (node.Symbol.MinimumArity <= parent.SubtreeCount && node.Symbol.MaximumArity >= parent.SubtreeCount) {
112          for (int i = parent.SubtreeCount - 1; i >= 0; --i) {
113            var child = parent.GetSubtree(i);
114            parent.RemoveSubtree(i);
115            node.AddSubtree(child);
116          }
117        }
118        // the if condition will always be true for the final else clause above
119        if (parent.Symbol.MaximumArity > parent.SubtreeCount) {
120          ModifyTree(Tree, parent, null, node);
121        }
122      }
123      currSelected = null;
124    }
125    private void changeNodeToolStripMenuItem_Click(object sender, EventArgs e) {
126      if (currSelected == null) return;
127
128      var node = (ISymbolicExpressionTreeNode)currSelected.Content.Clone();
129      var originalNode = currSelected.Content;
130
131      ISymbolicExpressionTreeNode newNode = null;
132      var result = DialogResult.Cancel;
133      if (node is ConstantTreeNode) {
134        using (var dialog = new ConstantNodeEditDialog(node)) {
135          dialog.ShowDialog(this);
136          newNode = dialog.NewNode;
137          result = dialog.DialogResult;
138        }
139      } else if (node is VariableTreeNode) {
140        using (var dialog = new VariableNodeEditDialog(node)) {
141          dialog.ShowDialog(this);
142          newNode = dialog.NewNode;
143          result = dialog.DialogResult;
144        }
145      }
146      if (result != DialogResult.OK) return;
147      ModifyTree(Tree, originalNode.Parent, originalNode, newNode); // this will replace the original node with the new node
148      currSelected = null;
149    }
150    private void cutToolStripMenuItem_Click(object sender, EventArgs e) {
151      lastOp = EditOp.CutSubtree;
152      copySubtree();
153    }
154    private void copyToolStripMenuItem_Click(object sender, EventArgs e) {
155      lastOp = EditOp.CopySubtree;
156      copySubtree();
157    }
158    private void copySubtree() {
159      if (tempNode != null) {
160        foreach (var subtree in tempNode.IterateNodesPostfix()) {
161          var visualNode = GetVisualSymbolicExpressionTreeNode(subtree);
162          visualNode.LineColor = Color.Black;
163          visualNode.TextColor = Color.Black;
164          if (subtree.Parent != null) {
165            var visualLine = GetVisualSymbolicExpressionTreeNodeConnection(subtree.Parent, subtree);
166            visualLine.LineColor = Color.Black;
167          }
168        }
169      }
170      tempNode = currSelected.Content;
171      foreach (var node in tempNode.IterateNodesPostfix()) {
172        var visualNode = GetVisualSymbolicExpressionTreeNode(node);
173        visualNode.LineColor = Color.LightGray;
174        visualNode.TextColor = Color.LightGray;
175        foreach (var subtree in node.Subtrees) {
176          var visualLine = GetVisualSymbolicExpressionTreeNodeConnection(node, subtree);
177          visualLine.LineColor = Color.LightGray;
178        }
179      }
180      currSelected = null;
181      RepaintNodes(); // no need to redo the layout and repaint everything since this operation does not change the tree
182    }
183    private void removeNodeToolStripMenuItem_Click(object sender, EventArgs e) {
184      var node = currSelected.Content;
185      if (node == tempNode) tempNode = null;
186      ModifyTree(Tree, node.Parent, node, null, removeSubtree: false);
187      currSelected = null; // because the currently selected node was just deleted
188    }
189    private void removeSubtreeToolStripMenuItem_Click(object sender, EventArgs e) {
190      var node = currSelected.Content;
191      if (node.IterateNodesPostfix().Contains(tempNode)) tempNode = null;
192      ModifyTree(Tree, node.Parent, node, null, removeSubtree: true);
193      currSelected = null; // because the currently selected node was just deleted
194      contextMenuStrip.Close(); // avoid display of submenus since the action has already been performed
195    }
196    private void pasteToolStripMenuItem_Clicked(object sender, EventArgs e) {
197      if (!(lastOp == EditOp.CopySubtree || lastOp == EditOp.CutSubtree)) return;
198      // check if the copied/cut node (stored in the tempNode) can be inserted as a child of the current selected node
199      var node = currSelected.Content;
200      if (node is ConstantTreeNode || node is VariableTreeNode) return;
201      // check if the currently selected node can accept the copied node as a child
202      // no need to check the grammar, an arity check will do just fine here
203      if (node.Symbol.MaximumArity <= node.SubtreeCount) return;
204      switch (lastOp) {
205        case EditOp.CutSubtree: {
206            if (tempNode.IterateNodesBreadth().Contains(node))
207              throw new ArgumentException();// cannot cut/paste a node into itself
208            ModifyTree(Tree, tempNode.Parent, tempNode, null); //remove node from its original parent
209            ModifyTree(Tree, node, null, tempNode); //insert it as a child to the new parent
210            break;
211          }
212        case EditOp.CopySubtree: {
213            var clone = (SymbolicExpressionTreeNode)tempNode.Clone();
214            ModifyTree(Tree, node, null, clone);
215            break;
216          }
217      }
218      currSelected = null; // because the tree will have changed
219      tempNode = null; // clear the clipboard after one paste
220    }
221  }
222}
Note: See TracBrowser for help on using the repository browser.