Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TimeSeries/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionGrammarAllowedChildSymbolsControl.cs @ 7098

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

#1479: Hid the ProgramRootSymbol and the DefunSymbol in SymbolicExpressionGrammarEditorView.

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