1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.ComponentModel;
|
---|
25 | using System.Drawing;
|
---|
26 | using System.Data;
|
---|
27 | using System.Linq;
|
---|
28 | using System.Text;
|
---|
29 | using System.Windows.Forms;
|
---|
30 | using HeuristicLab.MainForm;
|
---|
31 | using HeuristicLab.MainForm.WindowsForms;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views {
|
---|
34 | [View("SymbolicExpression View")]
|
---|
35 | [Content(typeof(SymbolicExpressionTree), false)]
|
---|
36 | public partial class SymbolicExpressionView : AsynchronousContentView {
|
---|
37 | public new SymbolicExpressionTree Content {
|
---|
38 | get { return (SymbolicExpressionTree)base.Content; }
|
---|
39 | set { base.Content = value; }
|
---|
40 | }
|
---|
41 |
|
---|
42 | public SymbolicExpressionView() {
|
---|
43 | InitializeComponent();
|
---|
44 | Caption = "SymbolicExpression View";
|
---|
45 | }
|
---|
46 |
|
---|
47 | protected override void OnContentChanged() {
|
---|
48 | base.OnContentChanged();
|
---|
49 | if (Content == null) {
|
---|
50 | Caption = "SymbolicExpression View";
|
---|
51 | textBox.Text = string.Empty;
|
---|
52 | } else {
|
---|
53 | textBox.Text = SymbolicExpression(Content.Root, 0);
|
---|
54 | }
|
---|
55 | SetEnabledStateOfControls();
|
---|
56 | }
|
---|
57 |
|
---|
58 | protected override void OnReadOnlyChanged() {
|
---|
59 | base.OnReadOnlyChanged();
|
---|
60 | SetEnabledStateOfControls();
|
---|
61 | }
|
---|
62 |
|
---|
63 | private void SetEnabledStateOfControls() {
|
---|
64 | textBox.Enabled = Content != null;
|
---|
65 | textBox.ReadOnly = ReadOnly;
|
---|
66 | }
|
---|
67 |
|
---|
68 | private static string SymbolicExpression(SymbolicExpressionTreeNode node, int indentLength) {
|
---|
69 | StringBuilder strBuilder = new StringBuilder();
|
---|
70 | strBuilder.Append(' ', indentLength); strBuilder.Append("(");
|
---|
71 | // internal nodes or leaf nodes?
|
---|
72 | if (node.SubTrees.Count > 0) {
|
---|
73 | // symbol on same line as '('
|
---|
74 | strBuilder.AppendLine(node.ToString());
|
---|
75 | // each subtree expression on a new line
|
---|
76 | // and closing ')' also on new line
|
---|
77 | foreach (var subtree in node.SubTrees) {
|
---|
78 | strBuilder.AppendLine(SymbolicExpression(subtree, indentLength + 2));
|
---|
79 | }
|
---|
80 | strBuilder.Append(' ', indentLength); strBuilder.Append(")");
|
---|
81 | } else {
|
---|
82 | // symbol in the same line with as '(' and ')'
|
---|
83 | strBuilder.Append(node.ToString());
|
---|
84 | strBuilder.Append(")");
|
---|
85 | }
|
---|
86 | return strBuilder.ToString();
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|