Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.EvolutionTracking/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views/3.4/SymbolicExpressionTreeTile.cs @ 10524

Last change on this file since 10524 was 10524, checked in by bburlacu, 10 years ago

#1772: Reverse-merged erroneous commit (booked on ticked #2076), and merged latest trunk changes.

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.Collections.Generic;
23using System.Drawing;
24using System.Linq;
25using HeuristicLab.Visualization;
26
27using Rectangle = HeuristicLab.Visualization.Rectangle;
28
29namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views {
30  public class SymbolicExpressionTreeTile : Group {
31    public double PreferredNodeWidth { get; set; }
32    public double PreferredNodeHeight { get; set; }
33    private ISymbolicExpressionTree symbolicExpressionTree;
34    public ISymbolicExpressionTree SymbolicExpressionTree {
35      get { return symbolicExpressionTree; }
36      set {
37        symbolicExpressionTree = value;
38        GeneratePrimitives(PreferredNodeWidth, PreferredNodeHeight);
39      }
40    }
41
42    public ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode> LayoutEngine { get; set; }
43    public SymbolicExpressionTreeTile(IChart chart) : base(chart) { }
44    public SymbolicExpressionTreeTile(IChart chart, ISymbolicExpressionTree tree)
45      : base(chart) {
46      SymbolicExpressionTree = tree;
47      PreferredNodeWidth = 80;
48      PreferredNodeHeight = 40;
49    }
50    private void GeneratePrimitives(double preferredNodeWidth, double preferredNodeHeight) {
51      Clear();
52      LayoutEngine.Initialize(SymbolicExpressionTree.Root, node => node.Subtrees);
53      LayoutEngine.CalculateLayout();
54
55      var primitivesMap = new Dictionary<ISymbolicExpressionTreeNode, IPrimitive>(); // both Ellipse and Rectangle are derived from the RectangularPrimitiveBase
56      var font = new Font(FontFamily.GenericSansSerif, 10, GraphicsUnit.Pixel);
57
58      var visualNodes = LayoutEngine.GetVisualNodes().ToList();
59      var visualNodeMap = visualNodes.ToDictionary(x => x.Content, x => x);
60
61      foreach (var visualNode in visualNodes) {
62        var lowerLeft = new PointD(visualNode.X, visualNode.Y);
63        var upperRight = new PointD(visualNode.X + preferredNodeWidth, visualNode.Y + preferredNodeHeight);
64        var node = visualNode.Content;
65        RectangularPrimitiveBase rectangularPrimitive;
66        if (node.SubtreeCount == 0) {
67          rectangularPrimitive = new Rectangle(Chart, lowerLeft, upperRight) { Font = font, Text = visualNode.Content.ToString() };
68        } else {
69          rectangularPrimitive = new Ellipse(Chart, lowerLeft, upperRight) { Font = font, Text = visualNode.Content.ToString() };
70        }
71
72        this.Add(rectangularPrimitive);
73        primitivesMap.Add(node, rectangularPrimitive);
74      }
75
76      foreach (var node in visualNodes.Where(n => n.Content.SubtreeCount > 0)) {
77        var parent = node.Content;
78        foreach (var child in parent.Subtrees.Select(x => visualNodeMap[x])) {
79          var start = new PointD(node.X + preferredNodeWidth / 2, node.Y + preferredNodeHeight);
80          var end = new PointD(child.X + preferredNodeWidth / 2, child.Y);
81          var line = new Line(this.Chart, start, end);
82          this.Add(line);
83        }
84      }
85    }
86  }
87}
Note: See TracBrowser for help on using the repository browser.