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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Drawing;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Visualization;
|
---|
26 |
|
---|
27 | using Rectangle = HeuristicLab.Visualization.Rectangle;
|
---|
28 |
|
---|
29 | namespace 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 | private Dictionary<IPrimitive, ISymbolicExpressionTreeNode> primitiveMap;
|
---|
44 |
|
---|
45 | public SymbolicExpressionTreeTile(IChart chart)
|
---|
46 | : base(chart) {
|
---|
47 | primitiveMap = new Dictionary<IPrimitive, ISymbolicExpressionTreeNode>();
|
---|
48 | }
|
---|
49 | public SymbolicExpressionTreeTile(IChart chart, ISymbolicExpressionTree tree)
|
---|
50 | : this(chart) {
|
---|
51 | SymbolicExpressionTree = tree;
|
---|
52 | PreferredNodeWidth = 80;
|
---|
53 | PreferredNodeHeight = 40;
|
---|
54 | }
|
---|
55 | private void GeneratePrimitives(double preferredNodeWidth, double preferredNodeHeight) {
|
---|
56 | Clear();
|
---|
57 | ISymbolicExpressionTreeNode root = SymbolicExpressionTree.Root;
|
---|
58 | if (root.Symbol is ProgramRootSymbol && root.SubtreeCount == 1) { root = root.GetSubtree(0); }
|
---|
59 | var visualNodes = LayoutEngine.CalculateLayout(root);
|
---|
60 |
|
---|
61 | var font = new Font(FontFamily.GenericSansSerif, 10, GraphicsUnit.Pixel);
|
---|
62 |
|
---|
63 | var visualNodeMap = visualNodes.ToDictionary(x => x.Content, x => x);
|
---|
64 |
|
---|
65 | foreach (var visualNode in visualNodes) {
|
---|
66 | var lowerLeft = new PointD(visualNode.X, visualNode.Y);
|
---|
67 | var upperRight = new PointD(visualNode.X + preferredNodeWidth, visualNode.Y + preferredNodeHeight);
|
---|
68 | var node = visualNode.Content;
|
---|
69 | RectangularPrimitiveBase rectangularPrimitive;
|
---|
70 | if (node.SubtreeCount == 0) {
|
---|
71 | rectangularPrimitive = new Rectangle(Chart, lowerLeft, upperRight) { Font = font, Text = visualNode.Content.ToString() };
|
---|
72 | } else {
|
---|
73 | rectangularPrimitive = new Ellipse(Chart, lowerLeft, upperRight) { Font = font, Text = visualNode.Content.ToString() };
|
---|
74 | }
|
---|
75 |
|
---|
76 | this.Add(rectangularPrimitive);
|
---|
77 | }
|
---|
78 |
|
---|
79 | foreach (var node in visualNodes.Where(n => n.Content.SubtreeCount > 0)) {
|
---|
80 | var parent = node.Content;
|
---|
81 | foreach (var child in parent.Subtrees.Select(x => visualNodeMap[x])) {
|
---|
82 | var start = new PointD(node.X + preferredNodeWidth / 2, node.Y + preferredNodeHeight);
|
---|
83 | var end = new PointD(child.X + preferredNodeWidth / 2, child.Y);
|
---|
84 | var line = new Line(this.Chart, start, end);
|
---|
85 | this.Add(line);
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|