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 |
|
---|
150 | private void OnChangeValueDialogValidated(object sender, EventArgs e) {
|
---|
151 | SymbolicExpressionTreeNodeChanged(sender, e);
|
---|
152 | }
|
---|
153 |
|
---|
154 | private void OnInsertNodeDialogValidated(object sender, EventArgs e) {
|
---|
155 | var dialog = (InsertNodeDialog)sender;
|
---|
156 | var symbol = dialog.SelectedSymbol();
|
---|
157 | var node = symbol.CreateTreeNode();
|
---|
158 | var parent = currSelected.SymbolicExpressionTreeNode;
|
---|
159 | if (node is ConstantTreeNode) {
|
---|
160 | var constant = node as ConstantTreeNode;
|
---|
161 | constant.Value = double.Parse(dialog.constantValueTextBox.Text);
|
---|
162 | } else if (node is VariableTreeNode) {
|
---|
163 | var variable = node as VariableTreeNode;
|
---|
164 | variable.Weight = double.Parse(dialog.variableWeightTextBox.Text);
|
---|
165 | variable.VariableName = dialog.variableNamesCombo.Text;
|
---|
166 | } else {
|
---|
167 | if (node.Symbol.MinimumArity <= parent.SubtreeCount && node.Symbol.MaximumArity >= parent.SubtreeCount) {
|
---|
168 | for (int i = parent.SubtreeCount - 1; i >= 0; --i) {
|
---|
169 | var child = parent.GetSubtree(i);
|
---|
170 | parent.RemoveSubtree(i);
|
---|
171 | node.AddSubtree(child);
|
---|
172 | }
|
---|
173 | }
|
---|
174 | // 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
|
---|
175 | }
|
---|
176 | if (parent.Symbol.MaximumArity > parent.SubtreeCount) {
|
---|
177 | parent.AddSubtree(node);
|
---|
178 | Tree = Tree;
|
---|
179 | }
|
---|
180 | OnSymbolicExpressionTreeChanged(sender, e);
|
---|
181 | }
|
---|
182 |
|
---|
183 | private void cutNodeToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
184 | lastOp = EditOp.CutNode;
|
---|
185 | tempNode = currSelected.SymbolicExpressionTreeNode;
|
---|
186 | var visualNode = visualTreeNodes[tempNode];
|
---|
187 | visualNode.LineColor = Color.LightGray;
|
---|
188 | visualNode.TextColor = Color.LightGray;
|
---|
189 | Repaint();
|
---|
190 | }
|
---|
191 |
|
---|
192 | private void cutSubtreeToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
193 | lastOp = EditOp.CutSubtree;
|
---|
194 | tempNode = currSelected.SymbolicExpressionTreeNode; // should never be null
|
---|
195 | foreach (var node in tempNode.IterateNodesPostfix()) {
|
---|
196 | var visualNode = visualTreeNodes[node];
|
---|
197 | visualNode.LineColor = Color.LightGray;
|
---|
198 | visualNode.TextColor = Color.LightGray;
|
---|
199 | if (node.SubtreeCount > 0) {
|
---|
200 | foreach (var subtree in node.Subtrees) {
|
---|
201 | var visualLine = visualLines[new Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>(node, subtree)];
|
---|
202 | visualLine.LineColor = Color.LightGray;
|
---|
203 | }
|
---|
204 | }
|
---|
205 | }
|
---|
206 | Repaint();
|
---|
207 | }
|
---|
208 |
|
---|
209 | private void copyNodeToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
210 | lastOp = EditOp.CopyNode;
|
---|
211 | tempNode = currSelected.SymbolicExpressionTreeNode;
|
---|
212 | var visualNode = visualTreeNodes[tempNode];
|
---|
213 | visualNode.LineColor = Color.LightGray;
|
---|
214 | visualNode.TextColor = Color.LightGray;
|
---|
215 | Repaint();
|
---|
216 | }
|
---|
217 |
|
---|
218 | private void copySubtreeToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
219 | lastOp = EditOp.CopySubtree;
|
---|
220 | tempNode = currSelected.SymbolicExpressionTreeNode;
|
---|
221 | foreach (var node in tempNode.IterateNodesPostfix()) {
|
---|
222 | var visualNode = visualTreeNodes[node];
|
---|
223 | visualNode.LineColor = Color.LightGray;
|
---|
224 | visualNode.TextColor = Color.LightGray;
|
---|
225 | if (node.SubtreeCount > 0) {
|
---|
226 | foreach (var subtree in node.Subtrees) {
|
---|
227 | var visualLine = visualLines[new Tuple<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode>(node, subtree)];
|
---|
228 | visualLine.LineColor = Color.LightGray;
|
---|
229 | }
|
---|
230 | }
|
---|
231 | }
|
---|
232 | Repaint();
|
---|
233 | }
|
---|
234 |
|
---|
235 | private void deleteNodeToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
236 | lastOp = EditOp.DeleteNode;
|
---|
237 | var node = currSelected.SymbolicExpressionTreeNode;
|
---|
238 | var parent = node.Parent;
|
---|
239 | if (parent.Symbol.MaximumArity >= node.SubtreeCount + parent.SubtreeCount - 1) { // -1 because tempNode will be removed
|
---|
240 | parent.RemoveSubtree(parent.IndexOfSubtree(node));
|
---|
241 | for (int i = node.SubtreeCount - 1; i >= 0; --i) {
|
---|
242 | var child = node.GetSubtree(i);
|
---|
243 | node.RemoveSubtree(i);
|
---|
244 | parent.AddSubtree(child);
|
---|
245 | }
|
---|
246 | } else {
|
---|
247 | // if the node cannot be deleted without breaking the tree, do nothing
|
---|
248 | // TODO: decide what to do
|
---|
249 | }
|
---|
250 | OnSymbolicExpressionTreeChanged(sender, e);
|
---|
251 | }
|
---|
252 |
|
---|
253 | private void deleteSubtreeToolStripMenuItem_Click(object sender, EventArgs e) {
|
---|
254 | lastOp = EditOp.DeleteSubtree;
|
---|
255 | var node = currSelected.SymbolicExpressionTreeNode;
|
---|
256 | var parent = node.Parent;
|
---|
257 | parent.RemoveSubtree(parent.IndexOfSubtree(node));
|
---|
258 | OnSymbolicExpressionTreeChanged(sender, e);
|
---|
259 | }
|
---|
260 |
|
---|
261 | private void pasteToolStripMenuItem_Clicked(object sender, EventArgs e) {
|
---|
262 | if (!(lastOp == EditOp.CopyNode || lastOp == EditOp.CopySubtree || lastOp == EditOp.CutNode || lastOp == EditOp.CutSubtree))
|
---|
263 | return;
|
---|
264 | // check if the copied/cut node (stored in the tempNode) can be inserted as a child of the current selected node
|
---|
265 | var node = currSelected.SymbolicExpressionTreeNode;
|
---|
266 | if (node is ConstantTreeNode || node is VariableTreeNode) return; // nothing to do
|
---|
267 | // check if the currently selected node can accept the copied node as a child
|
---|
268 | // no need to check the grammar, an arity check will do just fine here
|
---|
269 | if (node.Symbol.MaximumArity > node.SubtreeCount) {
|
---|
270 | switch (lastOp) {
|
---|
271 | case (EditOp.CutNode): {
|
---|
272 | // cut node
|
---|
273 | var parent = tempNode.Parent;
|
---|
274 | // arity checks to see if parent can accept node's children (we assume the grammar is already ok with that)
|
---|
275 | if (parent.Symbol.MaximumArity >= tempNode.SubtreeCount + parent.SubtreeCount - 1) {
|
---|
276 | // -1 because tempNode will be removed
|
---|
277 | parent.RemoveSubtree(parent.IndexOfSubtree(tempNode));
|
---|
278 | for (int i = tempNode.SubtreeCount - 1; i >= 0; --i) {
|
---|
279 | var child = tempNode.GetSubtree(i);
|
---|
280 | tempNode.RemoveSubtree(i);
|
---|
281 | parent.AddSubtree(child);
|
---|
282 | }
|
---|
283 | lastOp = EditOp.CopyNode;
|
---|
284 | }
|
---|
285 | break;
|
---|
286 | }
|
---|
287 | case (EditOp.CutSubtree): {
|
---|
288 | // cut subtree
|
---|
289 | tempNode.Parent.RemoveSubtree(tempNode.Parent.IndexOfSubtree(tempNode));
|
---|
290 | lastOp = EditOp.CopySubtree; // do this so the next paste will actually perform a copy
|
---|
291 | break;
|
---|
292 | }
|
---|
293 | case (EditOp.CopyNode): {
|
---|
294 | // copy node
|
---|
295 | var clone = tempNode.Clone() as ISymbolicExpressionTreeNode; // should never be null
|
---|
296 | clone.Parent = tempNode.Parent;
|
---|
297 | tempNode = clone;
|
---|
298 | if (tempNode != null && tempNode.SubtreeCount > 0) {
|
---|
299 | for (int i = tempNode.SubtreeCount - 1; i >= 0; --i)
|
---|
300 | tempNode.RemoveSubtree(i);
|
---|
301 | }
|
---|
302 | break;
|
---|
303 | }
|
---|
304 | case (EditOp.CopySubtree): {
|
---|
305 | // copy subtree
|
---|
306 | var clone = tempNode.Clone() as ISymbolicExpressionTreeNode;
|
---|
307 | if (clone != null) {
|
---|
308 | clone.Parent = tempNode.Parent;
|
---|
309 | tempNode = clone;
|
---|
310 | }
|
---|
311 | break;
|
---|
312 | }
|
---|
313 | }
|
---|
314 | node.AddSubtree(tempNode);
|
---|
315 | Tree = Tree;
|
---|
316 | // hack in order to trigger the reinitialization of the dictionaries after new nodes appeared in the graph
|
---|
317 | OnSymbolicExpressionTreeChanged(sender, e);
|
---|
318 | } else {
|
---|
319 | // throw exception or show a warning to the user about invalid arity
|
---|
320 | }
|
---|
321 | }
|
---|
322 |
|
---|
323 | private bool IsValid(ISymbolicExpressionTree tree) {
|
---|
324 | if (tree.IterateNodesPostfix().Any(node => node.SubtreeCount < node.Symbol.MinimumArity || node.SubtreeCount > node.Symbol.MaximumArity)) {
|
---|
325 | treeState = TreeState.Invalid;
|
---|
326 | return false;
|
---|
327 | }
|
---|
328 | treeState = TreeState.Valid;
|
---|
329 | return true;
|
---|
330 | }
|
---|
331 | }
|
---|
332 | }
|
---|