Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Views/SymbolicExpressionTreeView.cs @ 3237

Last change on this file since 3237 was 3237, checked in by gkronber, 14 years ago

Worked on symbolic expression tree encoding.
Added view for expression trees (simple textual view in s-exp format).
Implemented SubtreeCrossover.
Fixed bugs in ProbabilisticTreeCreator.
#937 (Data types and operators for symbolic expression tree encoding)

File size: 2.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.Data;
6using System.Linq;
7using System.Text;
8using System.Windows.Forms;
9using HeuristicLab.MainForm;
10using HeuristicLab.MainForm.WindowsForms;
11
12namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views {
13  [View("SymbolicExpressionTree View")]
14  [Content(typeof(SymbolicExpressionTree), true)]
15  public partial class SymbolicExpressionTreeView : AsynchronousContentView {
16    public new SymbolicExpressionTree Content {
17      get { return (SymbolicExpressionTree)base.Content; }
18      set { base.Content = value; }
19    }
20
21    public SymbolicExpressionTreeView() {
22      InitializeComponent();
23      Caption = "SymbolicExpressionTree View";
24    }
25
26    public SymbolicExpressionTreeView(SymbolicExpressionTree content)
27      : this() {
28      Content = content;
29    }
30
31
32    protected override void OnContentChanged() {
33      base.OnContentChanged();
34      if (Content == null) {
35        Caption = "SymbolicExpressionTree View";
36        textBox.Text = string.Empty;
37        textBox.Enabled = false;
38      } else {
39        textBox.Text = SymbolicExpression(Content.Root, 0);
40        textBox.Enabled = true;
41      }
42    }
43
44    private static string SymbolicExpression(SymbolicExpressionTreeNode node, int indentLength) {
45      StringBuilder strBuilder = new StringBuilder();
46      strBuilder.Append(' ', indentLength); strBuilder.Append("(");
47      // internal nodes or leaf nodes?
48      if (node.SubTrees.Count > 0) {
49        // symbol on same line as '('
50        strBuilder.AppendLine(node.Symbol.Name);
51        // each subtree expression on a new line
52        // and closing ')' also on new line
53        foreach (var subtree in node.SubTrees) {
54          strBuilder.AppendLine(SymbolicExpression(subtree, indentLength + 2));
55        }
56        strBuilder.Append(' ', indentLength); strBuilder.Append(")");
57      } else {
58        // symbol in the same line with as '(' and ')'
59        strBuilder.Append(node.Symbol.Name);
60        strBuilder.Append(")");
61      }
62      return strBuilder.ToString();
63    }
64  }
65}
Note: See TracBrowser for help on using the repository browser.