Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionGrammarAllowedChildSymbolsControl.cs @ 6493

Last change on this file since 6493 was 6493, checked in by mkommend, 13 years ago

#1479: added functional improvements to SymbolicExpressionGrammar editor.

File size: 15.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Collections.Generic;
24using System.Drawing;
25using System.Linq;
26using System.Windows.Forms;
27using HeuristicLab.Collections;
28using HeuristicLab.Common;
29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views {
32  public sealed partial class SymbolicExpressionGrammarAllowedChildSymbolsControl : UserControl {
33    private ObservableList<ISymbolicExpressionTreeNode> selectedSymbolicExpressionTreeNodes;
34
35    public SymbolicExpressionGrammarAllowedChildSymbolsControl() {
36      InitializeComponent();
37      selectedSymbolicExpressionTreeNodes = new ObservableList<ISymbolicExpressionTreeNode>();
38    }
39
40    private ISymbolicExpressionGrammar grammar;
41    public ISymbolicExpressionGrammar Grammar {
42      get { return grammar; }
43      set {
44        if (grammar != value) {
45          if (grammar != null) DeregisterGrammarEvents();
46          grammar = value;
47          if (grammar != null) RegisterGrammarEvents();
48          OnGrammarChanged();
49        }
50      }
51    }
52
53    private ISymbol symbol;
54    public ISymbol Symbol {
55      get { return symbol; }
56      set {
57        if (symbol != value) {
58          if (value != null && grammar == null) throw new InvalidOperationException("grammar is null");
59          if (value != null && !grammar.ContainsSymbol(value)) throw new ArgumentException("grammar does not contain symbol.");
60          symbol = value;
61          OnSymbolChanged();
62        }
63      }
64    }
65
66    private void RegisterGrammarEvents() {
67      grammar.Changed += new EventHandler(Grammar_Changed);
68    }
69    private void DeregisterGrammarEvents() {
70      grammar.Changed -= new EventHandler(Grammar_Changed);
71    }
72
73    private void Grammar_Changed(object sender, EventArgs e) {
74      if (Grammar == null) return;
75      if (Symbol == null) return;
76      if (Symbol != null && !Grammar.ContainsSymbol(Symbol)) Symbol = null;
77      else BuildAllowedChildSymbolsTree();
78    }
79
80    private void OnGrammarChanged() {
81      if (Grammar == null) {
82        symbolicExpressionTreeChart.Tree = null;
83        Symbol = null;
84      }
85    }
86    private void OnSymbolChanged() {
87      if (Symbol == null) symbolicExpressionTreeChart.Tree = null;
88      else BuildAllowedChildSymbolsTree();
89    }
90
91    private void BuildAllowedChildSymbolsTree() {
92      var tree = new SymbolicExpressionTree(new SymbolicExpressionTreeNode(Symbol));
93
94      symbolicExpressionTreeChart.SuspendRepaint = true;
95      if (Grammar.GetMaximumSubtreeCount(Symbol) > 0) {
96        for (int i = 0; i < Grammar.GetMaximumSubtreeCount(Symbol); i++) {
97          var node = new DummySymbol("Subtree " + i).CreateTreeNode();
98          foreach (var childSymbol in Grammar.GetAllowedChildSymbols(Symbol, i)) {
99            node.AddSubtree(new SymbolicExpressionTreeNode(childSymbol));
100          }
101          tree.Root.AddSubtree(node);
102        }
103      }
104      symbolicExpressionTreeChart.Tree = tree;
105
106      foreach (var subtreeNode in tree.Root.Subtrees) {
107        foreach (var allowedChildNode in subtreeNode.Subtrees) {
108          var visualLine = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNodeConnection(subtreeNode, allowedChildNode);
109          visualLine.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
110        }
111      }
112
113      for (int i = Grammar.GetMinimumSubtreeCount(symbol); i < Grammar.GetMaximumSubtreeCount(symbol); i++) {
114        var subtreeNode = tree.Root.GetSubtree(i);
115        var visualTreeNode = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(subtreeNode);
116        visualTreeNode.TextColor = Color.Gray;
117        visualTreeNode.LineColor = Color.LightGray;
118
119        var visualLine = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNodeConnection(tree.Root, subtreeNode);
120        visualLine.LineColor = Color.LightGray;
121
122        foreach (var allowedChildNode in subtreeNode.Subtrees) {
123          visualTreeNode = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(allowedChildNode);
124          visualTreeNode.TextColor = Color.Gray;
125          visualTreeNode.LineColor = Color.LightGray;
126
127          visualLine = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNodeConnection(subtreeNode, allowedChildNode);
128          visualLine.LineColor = Color.LightGray;
129        }
130      }
131
132      symbolicExpressionTreeChart.SuspendRepaint = false;
133      UpdateSelectedSymbolicExpressionTreeNodes();
134    }
135
136    private void UpdateSelectedSymbolicExpressionTreeNodes() {
137      foreach (var node in symbolicExpressionTreeChart.Tree.IterateNodesPrefix()) {
138        var visualNode = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(node);
139        if (!selectedSymbolicExpressionTreeNodes.Contains(node)) visualNode.FillColor = Color.White;
140        else visualNode.FillColor = Color.LightSteelBlue;
141      }
142      symbolicExpressionTreeChart.Repaint();
143    }
144
145    private void symbolicExpressionTreeChart_SymbolicExpressionTreeNodeClicked(object sender, MouseEventArgs e) {
146      if ((Control.ModifierKeys & Keys.Control) == 0)
147        selectedSymbolicExpressionTreeNodes.Clear();
148
149      VisualSymbolicExpressionTreeNode clickedNode = (VisualSymbolicExpressionTreeNode)sender;
150      var selectedNode = clickedNode.SymbolicExpressionTreeNode;
151      if (selectedNode.SubtreeCount == 0) {
152        if (!selectedSymbolicExpressionTreeNodes.Contains(selectedNode))
153          selectedSymbolicExpressionTreeNodes.Add(selectedNode);
154        else
155          selectedSymbolicExpressionTreeNodes.Remove(selectedNode);
156      }
157
158      UpdateSelectedSymbolicExpressionTreeNodes();
159    }
160
161    private void symbolicExpressionTreeChart_KeyDown(object sender, KeyEventArgs e) {
162      if (e.KeyCode == Keys.Delete) {
163        var root = symbolicExpressionTreeChart.Tree.Root;
164        Grammar.StartGrammarManipulation();
165        foreach (var node in selectedSymbolicExpressionTreeNodes) {
166          int argIndex = root.IndexOfSubtree(node.Parent);
167          Grammar.RemoveAllowedChildSymbol(root.Symbol, node.Symbol, argIndex);
168        }
169
170        selectedSymbolicExpressionTreeNodes.Clear();
171        Grammar.FinishedGrammarManipulation();
172      }
173    }
174
175    #region drag & drop operations
176    private bool validDragOperation;
177    private void symbolicExpressionTreeChart_DragEnter(object sender, DragEventArgs e) {
178      validDragOperation = false;
179      var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
180      var symbol = data as ISymbol;
181      var symbols = data as IEnumerable<ISymbol>;
182      if (symbol != null && !(symbol is IReadOnlySymbol) && Grammar.ContainsSymbol(symbol)) validDragOperation = true;
183      else if (symbols != null && symbols.All(s => !(symbol is IReadOnlySymbol) && Grammar.ContainsSymbol(s))) validDragOperation = true;
184    }
185
186    private void symbolicExpressionTreeChart_DragOver(object sender, DragEventArgs e) {
187      e.Effect = DragDropEffects.None;
188      if (validDragOperation) {
189        Point coordinates = symbolicExpressionTreeChart.PointToClient(new Point(e.X, e.Y));
190        var visualNode = symbolicExpressionTreeChart.FindVisualSymbolicExpressionTreeNodeAt(coordinates.X, coordinates.Y);
191        if (visualNode != null) {
192          var node = visualNode.SymbolicExpressionTreeNode;
193          var root = symbolicExpressionTreeChart.Tree.Root;
194          if (root.Symbol is ProgramRootSymbol) return;
195          if (node == root || node.Parent == root) e.Effect = DragDropEffects.Copy;
196        }
197      }
198    }
199
200    private void symbolicExpressionTreeChart_DragDrop(object sender, DragEventArgs e) {
201      Point coordinates = symbolicExpressionTreeChart.PointToClient(new Point(e.X, e.Y));
202      var node = symbolicExpressionTreeChart.FindVisualSymbolicExpressionTreeNodeAt(coordinates.X, coordinates.Y);
203      var root = symbolicExpressionTreeChart.Tree.Root;
204
205      var data = e.Data.GetData(HeuristicLab.Common.Constants.DragDropDataFormat);
206      var symbol = data as ISymbol;
207      var symbols = data as IEnumerable<ISymbol>;
208
209      if (node.SymbolicExpressionTreeNode == root) {
210        if (symbol != null)
211          Grammar.AddAllowedChildSymbol(root.Symbol, symbol);
212        else if (symbols != null)
213          foreach (var s in symbols) Grammar.AddAllowedChildSymbol(root.Symbol, s);
214      } else {
215        int argumentIndex = root.IndexOfSubtree(node.SymbolicExpressionTreeNode);
216        if (symbol != null)
217          Grammar.AddAllowedChildSymbol(root.Symbol, symbol, argumentIndex);
218        else if (symbols != null)
219          foreach (var s in symbols) Grammar.AddAllowedChildSymbol(root.Symbol, s, argumentIndex);
220      }
221      BuildAllowedChildSymbolsTree();
222    }
223    #endregion
224
225    #region draw and handle root node with buttons to manipulate the subtree count
226    private RectangleF increaseMinimumSubtreeCountRectangle;
227    private RectangleF decreaseMinimumSubtreeCountRectangle;
228    private RectangleF increaseMaximumSubtreeCountRectangle;
229    private RectangleF decreaseMaximumSubtreeCountRectangle;
230    private void allowedChildSymbolsControl_Paint(object sender, PaintEventArgs e) {
231      increaseMinimumSubtreeCountRectangle = RectangleF.Empty;
232      decreaseMinimumSubtreeCountRectangle = RectangleF.Empty;
233      increaseMaximumSubtreeCountRectangle = RectangleF.Empty;
234      decreaseMaximumSubtreeCountRectangle = RectangleF.Empty;
235
236      if (Grammar == null) return;
237      if (symbolicExpressionTreeChart.Tree == null) return;
238
239      var rootNode = symbolicExpressionTreeChart.Tree.Root;
240      var visualRootNode = symbolicExpressionTreeChart.GetVisualSymbolicExpressionTreeNode(rootNode);
241      var graphics = e.Graphics;
242
243      if (rootNode.Symbol is IReadOnlySymbol) return;
244      if (rootNode.Symbol.MinimumArity == rootNode.Symbol.MaximumArity) return;
245
246      using (Pen pen = new Pen(Color.Black)) {
247        using (Font font = new Font("Times New Roman", 8)) {
248          var stringFormat = new StringFormat();
249          stringFormat.Alignment = StringAlignment.Center;
250          stringFormat.LineAlignment = StringAlignment.Center;
251          int spacing = 5;
252          int size = (visualRootNode.Height - spacing * 3) / 2;
253
254          increaseMinimumSubtreeCountRectangle = new RectangleF(visualRootNode.X - spacing - size, visualRootNode.Y + spacing, size, size);
255          decreaseMinimumSubtreeCountRectangle = new RectangleF(visualRootNode.X - spacing - size, visualRootNode.Y + size + 2 * spacing, size, size);
256          increaseMaximumSubtreeCountRectangle = new RectangleF(visualRootNode.X + visualRootNode.Width + spacing, visualRootNode.Y + spacing, size, size);
257          decreaseMaximumSubtreeCountRectangle = new RectangleF(visualRootNode.X + visualRootNode.Width + spacing, visualRootNode.Y + size + 2 * spacing, size, size);
258
259          pen.Color = Grammar.GetMaximumSubtreeCount(rootNode.Symbol) == Grammar.GetMinimumSubtreeCount(rootNode.Symbol) ? Color.LightGray : Color.Black;
260          graphics.DrawString("+", font, pen.Brush, increaseMinimumSubtreeCountRectangle, stringFormat);
261          graphics.DrawRectangle(pen, Rectangle.Round(increaseMinimumSubtreeCountRectangle));
262          if (pen.Color == Color.LightGray) increaseMinimumSubtreeCountRectangle = RectangleF.Empty;
263
264          pen.Color = Grammar.GetMinimumSubtreeCount(rootNode.Symbol) == rootNode.Symbol.MinimumArity ? Color.LightGray : Color.Black;
265          graphics.DrawString("-", font, pen.Brush, decreaseMinimumSubtreeCountRectangle, stringFormat);
266          graphics.DrawRectangle(pen, Rectangle.Round(decreaseMinimumSubtreeCountRectangle));
267          if (pen.Color == Color.LightGray) decreaseMinimumSubtreeCountRectangle = RectangleF.Empty;
268
269          pen.Color = Grammar.GetMaximumSubtreeCount(rootNode.Symbol) == rootNode.Symbol.MaximumArity ? Color.LightGray : Color.Black;
270          graphics.DrawRectangle(pen, Rectangle.Round(increaseMaximumSubtreeCountRectangle));
271          graphics.DrawString("+", font, pen.Brush, increaseMaximumSubtreeCountRectangle, stringFormat);
272          if (pen.Color == Color.LightGray) increaseMaximumSubtreeCountRectangle = RectangleF.Empty;
273
274          pen.Color = Grammar.GetMaximumSubtreeCount(rootNode.Symbol) == Grammar.GetMinimumSubtreeCount(rootNode.Symbol) ? Color.LightGray : Color.Black;
275          graphics.DrawRectangle(pen, Rectangle.Round(decreaseMaximumSubtreeCountRectangle));
276          graphics.DrawString("-", font, pen.Brush, decreaseMaximumSubtreeCountRectangle, stringFormat);
277          if (pen.Color == Color.LightGray) decreaseMaximumSubtreeCountRectangle = RectangleF.Empty;
278        }
279      }
280    }
281
282    private void allowedChildSymbolsControl_MouseDown(object sender, MouseEventArgs e) {
283      if (Grammar == null) return;
284      if (symbolicExpressionTreeChart.Tree == null) return;
285
286      var pointF = new PointF(e.X, e.Y);
287      var rootSymbol = symbolicExpressionTreeChart.Tree.Root.Symbol;
288      int minimumSubtreeCount = Grammar.GetMinimumSubtreeCount(rootSymbol);
289      int maximumSubtreecount = Grammar.GetMaximumSubtreeCount(rootSymbol);
290
291      bool changed = true;
292      if (increaseMinimumSubtreeCountRectangle.Contains(pointF))
293        Grammar.SetSubtreeCount(rootSymbol, minimumSubtreeCount + 1, maximumSubtreecount);
294      else if (decreaseMinimumSubtreeCountRectangle.Contains(pointF))
295        Grammar.SetSubtreeCount(rootSymbol, minimumSubtreeCount - 1, maximumSubtreecount);
296      else if (increaseMaximumSubtreeCountRectangle.Contains(pointF))
297        Grammar.SetSubtreeCount(rootSymbol, minimumSubtreeCount, maximumSubtreecount + 1);
298      else if (decreaseMaximumSubtreeCountRectangle.Contains(pointF))
299        Grammar.SetSubtreeCount(rootSymbol, minimumSubtreeCount, maximumSubtreecount - 1);
300      else
301        changed = false;
302
303      if (changed) BuildAllowedChildSymbolsTree();
304    }
305    #endregion
306
307  }
308
309  [NonDiscoverableType]
310  internal class DummySymbol : Symbol {
311    private const int minimumArity = 1;
312    private const int maximumArity = byte.MaxValue;
313
314    public override int MinimumArity {
315      get { return minimumArity; }
316    }
317    public override int MaximumArity {
318      get { return maximumArity; }
319    }
320
321    public DummySymbol(DummySymbol original, Cloner cloner) : base(original, cloner) { }
322    public DummySymbol(string name) : base(name, "DummySymbol for views") { }
323    public override IDeepCloneable Clone(Cloner cloner) { return new DummySymbol(this, cloner); }
324  }
325}
Note: See TracBrowser for help on using the repository browser.