source: branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/StructureTemplate/StructureTemplateView.cs @ 18151

Last change on this file since 18151 was 18151, checked in by dpiringe, 7 months ago

#3136

  • fixed eventhandler reregister after deserialisazion/cloning
  • added a test case for StructuredSymbolicRegressionSingleObjectiveProblem
  • changed the usage of a Dictionary to List
File size: 4.2 KB
Line 
1 using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Linq;
5using System.Text;
6using System.Windows.Forms;
7using HeuristicLab.Collections;
8using HeuristicLab.Core;
9using HeuristicLab.Data;
10using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
11using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
12using HeuristicLab.MainForm;
13using HeuristicLab.MainForm.WindowsForms;
14
15namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
16
17  [View("StructureTemplate View")]
18  [Content(typeof(StructureTemplate), true)]
19  public partial class StructureTemplateView : AsynchronousContentView {
20    public new StructureTemplate Content {
21      get => (StructureTemplate)base.Content;
22      set => base.Content = value;
23    }
24
25    public StructureTemplateView() {
26      InitializeComponent();
27      infoLabel.Text = "";
28      this.Resize += StructureTemplateViewResize;
29      splitContainer.SplitterMoved += SplitContainerSplitterMoved;
30      treeChart.SymbolicExpressionTreeNodeClicked += SymbolicExpressionTreeNodeClicked;
31     
32    }
33
34    private void SplitContainerSplitterMoved(object sender, EventArgs e) {
35      PaintTree();
36    }
37
38    private void StructureTemplateViewResize(object sender, EventArgs e) {
39      PaintTree();
40    }
41
42    private void SymbolicExpressionTreeNodeClicked(object sender, MouseEventArgs e) {
43      var visualTreeNode = sender as VisualTreeNode<ISymbolicExpressionTreeNode>;
44      if(visualTreeNode != null) {
45        var subFunctionTreeNode = visualTreeNode.Content as SubFunctionTreeNode;
46        var selectedSubFunction = Content.SubFunctions.Where(x => x.Name == subFunctionTreeNode.Name).FirstOrDefault();
47        if(subFunctionTreeNode != null && selectedSubFunction != null)
48          viewHost.Content = selectedSubFunction;
49      }
50    }
51
52    protected override void OnContentChanged() {
53      base.OnContentChanged();
54      if (Content == null) return;
55      expressionInput.Text = Content.Template;
56      linearScalingCheckBox.Checked = Content.ApplyLinearScaling;
57      PaintTree();
58      infoLabel.Text = "";
59    }
60
61    private void ParseButtonClick(object sender, EventArgs e) {
62      Parse();
63    }
64
65    private void ExpressionInputTextChanged(object sender, EventArgs e) {
66      infoLabel.Text = "Unparsed changes! Press parse button to save changes.";
67      infoLabel.ForeColor = Color.DarkOrange;
68    }
69
70    private void PaintTree() {
71      if(Content != null && Content.Tree != null) {
72        treeChart.Tree = Content.Tree;
73        foreach (var n in Content.Tree.IterateNodesPrefix()) {
74          if (n.Symbol is SubFunctionSymbol) {
75            var visualNode = treeChart.GetVisualSymbolicExpressionTreeNode(n);
76            visualNode.FillColor = Color.LightCyan;
77            visualNode.LineColor = Color.SlateGray;
78          }
79        }
80        treeChart.RepaintNodes();
81      }
82    }
83
84    private void ExpressionInputKeyUp(object sender, KeyEventArgs e) {
85      if (e.KeyCode == Keys.Enter)
86        Parse();
87    }
88
89    private void Parse() {
90      viewHost.Content = null; // reset active detail view
91      if (!string.IsNullOrEmpty(expressionInput.Text)) {
92        try {
93          Content.Template = expressionInput.Text;
94          PaintTree();
95          infoLabel.Text = "Template structure successfully parsed.";
96          infoLabel.ForeColor = Color.DarkGreen;
97        } catch (AggregateException ex) {
98          infoLabel.Text = string.Join("\n", ex.InnerExceptions.Select(x => x.Message));
99          infoLabel.ForeColor = Color.DarkRed;
100        } catch (Exception ex) {
101          infoLabel.Text = ex.Message;
102          infoLabel.ForeColor = Color.DarkRed;
103        }
104      }
105    }
106
107    private void LinearScalingCheckBoxCheckStateChanged(object sender, EventArgs e) {
108      Content.ApplyLinearScaling = linearScalingCheckBox.Checked;
109      PaintTree();
110    }
111
112    private void helpButton_DoubleClick(object sender, EventArgs e) {
113      using (InfoBox dialog = new InfoBox("Help for structure template",
114        "HeuristicLab.Problems.DataAnalysis.Symbolic.Views.Resources.structureTemplateHelp.rtf",
115        this)) {
116        dialog.ShowDialog(this);
117      }
118    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.