source: branches/HeuristicLab.ReingoldTilfordTreeLayout/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/LayoutEngines/SymbolicExpressionTreeLayoutAdapter.cs @ 10487

Last change on this file since 10487 was 10487, checked in by bburlacu, 6 years ago

#2076: Fixed image save to file in the SymbolicExpressionTreeChart. Removed commented code from SymbolicExpressionTreeLayoutAdapter.

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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;
24
25namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
26  // adapter class that provides some conversion methods from symbolic expression trees to layout nodes (preserving the tree structure)
27  public class SymbolicExpressionTreeLayoutAdapter : ILayoutAdapter<ISymbolicExpressionTreeNode> {
28    // default conversion function between ISymbolicExpressionTreeNode and LayoutNode<ISymbolicExpressionTree>
29    LayoutNode<ISymbolicExpressionTreeNode> defaultConvert(ISymbolicExpressionTreeNode node) {
30      var layoutNode = new LayoutNode<ISymbolicExpressionTreeNode> { Content = node };
31      layoutNode.Ancestor = layoutNode;
32      return layoutNode;
33    }
34
35    public IEnumerable<LayoutNode<ISymbolicExpressionTreeNode>> Convert(ISymbolicExpressionTree tree, Func<ISymbolicExpressionTreeNode, LayoutNode<ISymbolicExpressionTreeNode>> convertFunc = null) {
36      return Convert(tree.Root, convertFunc);
37    }
38    // translate the symbolic expression tree structure to a layout node tree structure
39    // return an enumerable containing all the layout nodes
40    public IEnumerable<LayoutNode<ISymbolicExpressionTreeNode>> Convert(ISymbolicExpressionTreeNode root, Func<ISymbolicExpressionTreeNode, LayoutNode<ISymbolicExpressionTreeNode>> convertFunc = null) {
41      var rootLayoutNode = convertFunc == null ? defaultConvert(root) : convertFunc(root);
42      rootLayoutNode.Ancestor = rootLayoutNode;
43
44      if (root.SubtreeCount > 0) {
45        rootLayoutNode.Children = new List<LayoutNode<ISymbolicExpressionTreeNode>>(root.SubtreeCount);
46        Expand(rootLayoutNode, convertFunc);
47      }
48
49      var list = new List<LayoutNode<ISymbolicExpressionTreeNode>> { rootLayoutNode };
50      int i = 0;
51      while (i < list.Count) {
52        if (list[i].Children != null) list.AddRange(list[i].Children);
53        ++i;
54      }
55      return list;
56    }
57
58    private void Expand(LayoutNode<ISymbolicExpressionTreeNode> layoutNode, Func<ISymbolicExpressionTreeNode, LayoutNode<ISymbolicExpressionTreeNode>> convertFunc = null) {
59      if (layoutNode.Children == null) return;
60      for (int i = 0; i < layoutNode.Content.SubtreeCount; ++i) {
61        var subtree = layoutNode.Content.GetSubtree(i);
62        var childLayoutNode = convertFunc == null ? defaultConvert(subtree) : convertFunc(subtree);
63        childLayoutNode.Parent = layoutNode;
64        childLayoutNode.Number = i;
65        childLayoutNode.Level = layoutNode.Level + 1;
66        childLayoutNode.Ancestor = childLayoutNode;
67        childLayoutNode.Children = subtree.SubtreeCount > 0 ? new List<LayoutNode<ISymbolicExpressionTreeNode>>(subtree.SubtreeCount) : null;
68        layoutNode.Children.Add(childLayoutNode);
69        Expand(childLayoutNode, convertFunc);
70      }
71    }
72  }
73}
Note: See TracBrowser for help on using the repository browser.