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