Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/StructureTemplate/StructureTemplateView.cs @ 18232

Last change on this file since 18232 was 18164, checked in by dpiringe, 3 years ago

#3136

  • fixed a bug in StructureTemplateView -> only nodes of type SubFunctionTreeNode are selectable
  • added a way to keep old sub functions after parsing a new expression
    • overwrote some basic object methods for SubFunction to keep it simple
    • only old sub functions, which match the name and signature of the new ones, are saved; examples:
      • old: f(x), new: f(x) -> keep old
      • old: f(x1), new: f(x1, x2) -> use new
      • old: f1(x), new f2(x) -> use new
File size: 4.5 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      errorLabel.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 && visualTreeNode.Content is SubFunctionTreeNode subFunctionTreeNode) {
45        var selectedSubFunction = Content.SubFunctions.Where(x => x.Name == subFunctionTreeNode.Name).FirstOrDefault();
46        if(subFunctionTreeNode != null && selectedSubFunction != null)
47          viewHost.Content = selectedSubFunction;
48      }
49    }
50
51    protected override void SetEnabledStateOfControls() {
52      base.SetEnabledStateOfControls();
53      parseButton.Enabled = Content != null && !Locked && !ReadOnly;
54      linearScalingCheckBox.Enabled = Content != null && !Locked && !ReadOnly;
55      expressionInput.Enabled = Content != null && !Locked && !ReadOnly;
56    }
57
58    protected override void OnContentChanged() {
59      base.OnContentChanged();
60      if (Content == null) return;
61      expressionInput.Text = Content.Template;
62      linearScalingCheckBox.Checked = Content.ApplyLinearScaling;
63      PaintTree();
64      errorLabel.Text = "";
65    }
66
67    private void ParseButtonClick(object sender, EventArgs e) {
68      Parse();
69    }
70
71    private void ExpressionInputTextChanged(object sender, EventArgs e) {
72      errorLabel.Text = "Unparsed changes! Press parse button to save changes.";
73      errorLabel.ForeColor = Color.DarkOrange;
74    }
75
76    private void PaintTree() {
77      if(Content != null && Content.Tree != null) {
78        treeChart.Tree = Content.Tree;
79        foreach (var n in Content.Tree.IterateNodesPrefix()) {
80          if (n.Symbol is SubFunctionSymbol) {
81            var visualNode = treeChart.GetVisualSymbolicExpressionTreeNode(n);
82            visualNode.FillColor = Color.LightCyan;
83            visualNode.LineColor = Color.SlateGray;
84          }
85        }
86        treeChart.RepaintNodes();
87      }
88    }
89
90    private void ExpressionInputKeyUp(object sender, KeyEventArgs e) {
91      if (e.KeyCode == Keys.Enter)
92        Parse();
93    }
94
95    private void Parse() {
96      viewHost.Content = null; // reset active detail view
97      if (!string.IsNullOrEmpty(expressionInput.Text)) {
98        try {
99          Content.Template = expressionInput.Text;
100          PaintTree();
101          errorLabel.Text = "Template structure successfully parsed.";
102          errorLabel.ForeColor = Color.DarkGreen;
103        } catch (AggregateException ex) {
104          errorLabel.Text = string.Join("\n", ex.InnerExceptions.Select(x => x.Message));
105          errorLabel.ForeColor = Color.DarkRed;
106        } catch (Exception ex) {
107          errorLabel.Text = ex.Message;
108          errorLabel.ForeColor = Color.DarkRed;
109        }
110      }
111    }
112
113    private void LinearScalingCheckBoxCheckStateChanged(object sender, EventArgs e) {
114      Content.ApplyLinearScaling = linearScalingCheckBox.Checked;
115      PaintTree();
116    }
117
118    private void HelpButtonDoubleClick(object sender, EventArgs e) {
119      using (InfoBox dialog = new InfoBox("Help for structure template",
120        "HeuristicLab.Problems.DataAnalysis.Symbolic.Views.Resources.structureTemplateHelp.rtf",
121        this)) {
122        dialog.ShowDialog(this);
123      }
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.