Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicExpressionTreeChart.cs @ 15094

Last change on this file since 15094 was 14802, checked in by mkommend, 8 years ago

#2710: Merged r14432, r14494, r14706 into stable.

File size: 10.4 KB
RevLine 
[8409]1#region License Information
2/* HeuristicLab
[14186]3 * Copyright (C) 2002-2016 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
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 {
[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)
[11120]35    private VisualTreeNode<ISymbolicExpressionTreeNode> 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 {
[11120]67        var node = currSelected.Content;
[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
[11150]78                                                          && !(lastOp == EditOp.CutSubtree && tempNode.IterateNodesBreadth().Contains(node))
79                                                          && node.SubtreeCount < node.Symbol.MaximumArity;
[8409]80      }
81    }
82
[8935]83    protected override void OnSymbolicExpressionTreeNodeClicked(object sender, MouseEventArgs e) {
[11120]84      currSelected = (VisualTreeNode<ISymbolicExpressionTreeNode>)sender; ;
[9055]85      base.OnSymbolicExpressionTreeNodeClicked(sender, e);
[8409]86    }
87
[8980]88    protected override void OnSymbolicExpressionTreeNodeDoubleClicked(object sender, MouseEventArgs e) {
[9006]89      currSelected = null;
90      base.OnSymbolicExpressionTreeNodeDoubleClicked(sender, e);
[8980]91    }
92
[11416]93
94    private static readonly ISymbolicExpressionGrammar grammar = new TypeCoherentExpressionGrammar();
[8409]95    private void insertNodeToolStripMenuItem_Click(object sender, EventArgs e) {
[11120]96      if (currSelected == null || currSelected.Content is SymbolicExpressionTreeTerminalNode) return;
97      var parent = currSelected.Content;
[8409]98
[8935]99      using (var dialog = new InsertNodeDialog()) {
[11416]100        dialog.SetAllowedSymbols(grammar.Symbols.Where(s => !(s is ProgramRootSymbol || s is StartSymbol || s is Defun || s is GroupSymbol))); // allow everything
[8935]101        dialog.ShowDialog(this);
[9006]102        if (dialog.DialogResult != DialogResult.OK) return;
[8980]103
[14802]104        var symbol = dialog.SelectedSymbol;
[9006]105        var node = symbol.CreateTreeNode();
106        if (node is ConstantTreeNode) {
107          var constant = node as ConstantTreeNode;
108          constant.Value = double.Parse(dialog.constantValueTextBox.Text);
109        } else if (node is VariableTreeNode) {
110          var variable = node as VariableTreeNode;
111          variable.Weight = double.Parse(dialog.variableWeightTextBox.Text);
[14802]112          variable.VariableName = dialog.SelectedVariableName;
[9006]113        } else if (node.Symbol.MinimumArity <= parent.SubtreeCount && node.Symbol.MaximumArity >= parent.SubtreeCount) {
114          for (int i = parent.SubtreeCount - 1; i >= 0; --i) {
115            var child = parent.GetSubtree(i);
116            parent.RemoveSubtree(i);
117            node.AddSubtree(child);
[8980]118          }
119        }
[9006]120        // the if condition will always be true for the final else clause above
121        if (parent.Symbol.MaximumArity > parent.SubtreeCount) {
122          ModifyTree(Tree, parent, null, node);
123        }
[8935]124      }
[9006]125      currSelected = null;
[8409]126    }
[8991]127    private void changeNodeToolStripMenuItem_Click(object sender, EventArgs e) {
[8409]128      if (currSelected == null) return;
[8980]129
[11120]130      var node = (ISymbolicExpressionTreeNode)currSelected.Content.Clone();
131      var originalNode = currSelected.Content;
[9006]132
[8980]133      ISymbolicExpressionTreeNode newNode = null;
134      var result = DialogResult.Cancel;
[8409]135      if (node is ConstantTreeNode) {
[8980]136        using (var dialog = new ConstantNodeEditDialog(node)) {
137          dialog.ShowDialog(this);
138          newNode = dialog.NewNode;
139          result = dialog.DialogResult;
140        }
[8409]141      } else if (node is VariableTreeNode) {
[8980]142        using (var dialog = new VariableNodeEditDialog(node)) {
143          dialog.ShowDialog(this);
144          newNode = dialog.NewNode;
145          result = dialog.DialogResult;
[8409]146        }
147      }
[8980]148      if (result != DialogResult.OK) return;
[9006]149      ModifyTree(Tree, originalNode.Parent, originalNode, newNode); // this will replace the original node with the new node
150      currSelected = null;
[8409]151    }
[8991]152    private void cutToolStripMenuItem_Click(object sender, EventArgs e) {
[8409]153      lastOp = EditOp.CutSubtree;
[9043]154      copySubtree();
[8409]155    }
[8991]156    private void copyToolStripMenuItem_Click(object sender, EventArgs e) {
[8409]157      lastOp = EditOp.CopySubtree;
[9043]158      copySubtree();
159    }
160    private void copySubtree() {
[9006]161      if (tempNode != null) {
[9055]162        foreach (var subtree in tempNode.IterateNodesPostfix()) {
163          var visualNode = GetVisualSymbolicExpressionTreeNode(subtree);
[11150]164          visualNode.LineColor = Color.Black;
165          visualNode.TextColor = Color.Black;
[9006]166          if (subtree.Parent != null) {
[9055]167            var visualLine = GetVisualSymbolicExpressionTreeNodeConnection(subtree.Parent, subtree);
[11150]168            visualLine.LineColor = Color.Black;
[9006]169          }
170        }
171      }
[11120]172      tempNode = currSelected.Content;
[8409]173      foreach (var node in tempNode.IterateNodesPostfix()) {
[8946]174        var visualNode = GetVisualSymbolicExpressionTreeNode(node);
[11150]175        visualNode.LineColor = Color.LightGray;
176        visualNode.TextColor = Color.LightGray;
[8935]177        foreach (var subtree in node.Subtrees) {
[8946]178          var visualLine = GetVisualSymbolicExpressionTreeNodeConnection(node, subtree);
[11150]179          visualLine.LineColor = Color.LightGray;
[8409]180        }
181      }
[9006]182      currSelected = null;
[11150]183      RepaintNodes(); // no need to redo the layout and repaint everything since this operation does not change the tree
[8409]184    }
[8991]185    private void removeNodeToolStripMenuItem_Click(object sender, EventArgs e) {
[11120]186      var node = currSelected.Content;
[9055]187      if (node == tempNode) tempNode = null;
[9006]188      ModifyTree(Tree, node.Parent, node, null, removeSubtree: false);
[8935]189      currSelected = null; // because the currently selected node was just deleted
[8409]190    }
[8991]191    private void removeSubtreeToolStripMenuItem_Click(object sender, EventArgs e) {
[11120]192      var node = currSelected.Content;
[9055]193      if (node.IterateNodesPostfix().Contains(tempNode)) tempNode = null;
[9006]194      ModifyTree(Tree, node.Parent, node, null, removeSubtree: true);
[8935]195      currSelected = null; // because the currently selected node was just deleted
[9006]196      contextMenuStrip.Close(); // avoid display of submenus since the action has already been performed
[8409]197    }
198    private void pasteToolStripMenuItem_Clicked(object sender, EventArgs e) {
[9006]199      if (!(lastOp == EditOp.CopySubtree || lastOp == EditOp.CutSubtree)) return;
[8409]200      // check if the copied/cut node (stored in the tempNode) can be inserted as a child of the current selected node
[11120]201      var node = currSelected.Content;
[8980]202      if (node is ConstantTreeNode || node is VariableTreeNode) return;
[8409]203      // check if the currently selected node can accept the copied node as a child
204      // no need to check the grammar, an arity check will do just fine here
[9006]205      if (node.Symbol.MaximumArity <= node.SubtreeCount) return;
206      switch (lastOp) {
207        case EditOp.CutSubtree: {
[9043]208            if (tempNode.IterateNodesBreadth().Contains(node))
[9057]209              throw new ArgumentException();// cannot cut/paste a node into itself
[11120]210            ModifyTree(Tree, tempNode.Parent, tempNode, null); //remove node from its original parent
[9057]211            ModifyTree(Tree, node, null, tempNode); //insert it as a child to the new parent
[9006]212            break;
213          }
214        case EditOp.CopySubtree: {
215            var clone = (SymbolicExpressionTreeNode)tempNode.Clone();
[9057]216            ModifyTree(Tree, node, null, clone);
[9006]217            break;
218          }
[8409]219      }
[9006]220      currSelected = null; // because the tree will have changed
[9043]221      tempNode = null; // clear the clipboard after one paste
[8409]222    }
223  }
224}
Note: See TracBrowser for help on using the repository browser.