Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2895_PushGP_GenealogyAnalysis/HeuristicLab.Problems.ProgramSynthesis.Views/PushProgramTreeView.cs @ 16726

Last change on this file since 16726 was 15771, checked in by bburlacu, 7 years ago

#2895: Add solution skeleton for PushGP with genealogy analysis.

File size: 1.7 KB
Line 
1using System.Windows.Forms;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Problems.ProgramSynthesis;
5
6namespace HeuristicLab.Problems.ProgramSynthesis.Views {
7  public partial class PushProgramTreeView : TreeView {
8    private const string PushProgramStringFormat = "List {0}@{1}";
9    private const string EmptyList = "Empty List";
10
11    public PushProgramTreeView() {
12      InitializeComponent();
13
14      ShowNodeToolTips = true;
15    }
16
17    public void LoadExpressions(IEnumerable<Expression> expressions) {
18      Nodes.Clear();
19      AddExpressions(expressions);
20    }
21
22    public void AddExpressions(IEnumerable<Expression> expressions) {
23      var nodes = expressions.Select(ToTreeNode).ToArray();
24      Nodes.AddRange(nodes);
25    }
26
27    private TreeNode ToTreeNode(Expression expression) {
28      var node = new TreeNode {
29        Text = GetNodeText(expression),
30        Tag = expression,
31      };
32
33      if (expression.IsProgram) {
34        var program = (PushProgram)expression;
35        var subNodes = program.Expressions.Reverse().Select(ToTreeNode).ToArray();
36
37        node.Nodes.AddRange(subNodes);
38      } else {
39        var type = expression.GetType();
40        var attribute = ExpressionTable.TypeToAttributeTable[type];
41
42        node.ToolTipText = attribute.Description;
43      }
44
45      return node;
46    }
47
48    private string GetNodeText(Expression expression) {
49      if (expression.IsProgram) {
50        var program = (PushProgram)expression;
51
52        if (program.IsEmpty)
53          return EmptyList;
54
55        return string.Format(PushProgramStringFormat, program.TotalInstructionCount, program.Depth);
56      }
57
58      return expression.StringRepresentation;
59    }
60  }
61}
Note: See TracBrowser for help on using the repository browser.