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

Last change on this file since 18071 was 18071, checked in by dpiringe, 9 months ago

#3136

  • added linear scaling logic in Evaluate and (for UI reasons) Analyze
  • added logic forSubFunctionSymbol (modified OpCodes) -> the SubFunctionTreeNode is display in the tree but has no effect on evaluation (works like a flag)
    • works now with SymbolicDataAnalysisExpressionTreeInterpreter
  • default grammar for SubFunction is now ArithmeticExpressionGrammar instead of LinearScalingGrammar
File size: 3.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Text;
5using System.Windows.Forms;
6using HeuristicLab.Collections;
7using HeuristicLab.Core;
8using HeuristicLab.Data;
9using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
10using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
11using HeuristicLab.MainForm;
12using HeuristicLab.MainForm.WindowsForms;
13
14namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Views {
15
16  [View("StructureTemplate View")]
17  [Content(typeof(StructureTemplate), true)]
18  public partial class StructureTemplateView : AsynchronousContentView {
19    public new StructureTemplate Content {
20      get => (StructureTemplate)base.Content;
21      set => base.Content = value;
22    }
23
24    public StructureTemplateView() {
25      InitializeComponent();
26      infoLabel.Text = "";
27      treeChart.SymbolicExpressionTreeNodeClicked += SymbolicExpressionTreeNodeClicked;
28     
29    }
30
31    private void SymbolicExpressionTreeNodeClicked(object sender, MouseEventArgs e) {
32      var visualTreeNode = sender as VisualTreeNode<ISymbolicExpressionTreeNode>;
33      if(visualTreeNode != null) {
34        var subFunctionTreeNode = visualTreeNode.Content as SubFunctionTreeNode;
35        if(subFunctionTreeNode != null && Content.SubFunctions.TryGetValue(subFunctionTreeNode.Name, out SubFunction subFunction))
36          viewHost.Content = subFunction;
37      }
38    }
39
40    protected override void OnContentChanged() {
41      base.OnContentChanged();
42      if (Content == null) return;
43      expressionInput.Text = Content.Template;
44      linearScalingCheckBox.Checked = Content.ApplyLinearScaling;
45      PaintTree();
46      infoLabel.Text = "";
47    }
48
49    private void ParseButtonClick(object sender, EventArgs e) {
50      Parse();
51    }
52
53    private void ExpressionInputTextChanged(object sender, EventArgs e) {
54      infoLabel.Text = "Unparsed changes! Press parse button to save changes.";
55      infoLabel.ForeColor = Color.DarkOrange;
56    }
57
58    private void PaintTree() {
59      if(Content != null && Content.Tree != null) {
60        treeChart.Tree = Content.Tree;
61        foreach (var n in Content.Tree.IterateNodesPrefix()) {
62          if (n.Symbol is SubFunctionSymbol) {
63            var visualNode = treeChart.GetVisualSymbolicExpressionTreeNode(n);
64            visualNode.FillColor = Color.LightCyan;
65            visualNode.LineColor = Color.SlateGray;
66          }
67        }
68        treeChart.RepaintNodes();
69      }
70    }
71
72    private void ExpressionInputKeyUp(object sender, KeyEventArgs e) {
73      if (e.KeyCode == Keys.Enter)
74        Parse();
75    }
76
77    private void Parse() {
78      if (!string.IsNullOrEmpty(expressionInput.Text)) {
79        try {
80          Content.Template = expressionInput.Text;
81          PaintTree();
82          infoLabel.Text = "Template structure successfully parsed.";
83          infoLabel.ForeColor = Color.DarkGreen;
84        } catch (Exception ex) {
85          infoLabel.Text = ex.Message;
86          infoLabel.ForeColor = Color.DarkRed;
87        }
88      }
89    }
90
91    private void LinearScalingCheckBoxCheckStateChanged(object sender, EventArgs e) {
92      Content.ApplyLinearScaling = linearScalingCheckBox.Checked;
93      PaintTree();
94    }
95  }
96}
Note: See TracBrowser for help on using the repository browser.