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