Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionGrammarAllowedChildSymbolsControl.cs @ 10308

Last change on this file since 10308 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

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