[10514] | 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 |
|
---|
[10524] | 27 | using Rectangle = HeuristicLab.Visualization.Rectangle;
|
---|
| 28 |
|
---|
[10514] | 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 | 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();
|
---|
[10524] | 52 | LayoutEngine.Initialize(SymbolicExpressionTree.Root, node => node.Subtrees);
|
---|
[10514] | 53 | LayoutEngine.CalculateLayout();
|
---|
| 54 |
|
---|
[10524] | 55 | var primitivesMap = new Dictionary<ISymbolicExpressionTreeNode, IPrimitive>(); // both Ellipse and Rectangle are derived from the RectangularPrimitiveBase
|
---|
[10514] | 56 | var font = new Font(FontFamily.GenericSansSerif, 10, GraphicsUnit.Pixel);
|
---|
| 57 |
|
---|
[10524] | 58 | var visualNodes = LayoutEngine.GetVisualNodes().ToList();
|
---|
| 59 | var visualNodeMap = visualNodes.ToDictionary(x => x.Content, x => x);
|
---|
[10517] | 60 |
|
---|
[10524] | 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;
|
---|
[10517] | 65 | RectangularPrimitiveBase rectangularPrimitive;
|
---|
[10524] | 66 | if (node.SubtreeCount == 0) {
|
---|
| 67 | rectangularPrimitive = new Rectangle(Chart, lowerLeft, upperRight) { Font = font, Text = visualNode.Content.ToString() };
|
---|
[10514] | 68 | } else {
|
---|
[10524] | 69 | rectangularPrimitive = new Ellipse(Chart, lowerLeft, upperRight) { Font = font, Text = visualNode.Content.ToString() };
|
---|
[10514] | 70 | }
|
---|
[10517] | 71 |
|
---|
| 72 | this.Add(rectangularPrimitive);
|
---|
| 73 | primitivesMap.Add(node, rectangularPrimitive);
|
---|
[10514] | 74 | }
|
---|
| 75 |
|
---|
[10524] | 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])) {
|
---|
[10514] | 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 | }
|
---|