[15017] | 1 | using System.Windows.Forms;
|
---|
[15771] | 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using HeuristicLab.Problems.ProgramSynthesis;
|
---|
[15017] | 5 |
|
---|
[15771] | 6 | namespace HeuristicLab.Problems.ProgramSynthesis.Views {
|
---|
[15017] | 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();
|
---|
[15032] | 13 |
|
---|
| 14 | ShowNodeToolTips = true;
|
---|
[15017] | 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),
|
---|
[15032] | 30 | Tag = expression,
|
---|
[15017] | 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);
|
---|
[15032] | 38 | } else {
|
---|
| 39 | var type = expression.GetType();
|
---|
| 40 | var attribute = ExpressionTable.TypeToAttributeTable[type];
|
---|
| 41 |
|
---|
| 42 | node.ToolTipText = attribute.Description;
|
---|
[15017] | 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 |
|
---|
[15366] | 55 | return string.Format(PushProgramStringFormat, program.TotalInstructionCount, program.Depth);
|
---|
[15017] | 56 | }
|
---|
| 57 |
|
---|
| 58 | return expression.StringRepresentation;
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 | }
|
---|