Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 18134 was 18134, checked in by dpiringe, 2 years ago

#3136

  • added a new information box for StructureTemplate in StructureTemplateView with an extended description about structure templates
File size: 4.1 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        if(subFunctionTreeNode != null && Content.SubFunctions.TryGetValue(subFunctionTreeNode.Name, out SubFunction subFunction))
47          viewHost.Content = subFunction;
48      }
49    }
50
51    protected override void OnContentChanged() {
52      base.OnContentChanged();
53      if (Content == null) return;
54      expressionInput.Text = Content.Template;
55      linearScalingCheckBox.Checked = Content.ApplyLinearScaling;
56      PaintTree();
57      infoLabel.Text = "";
58    }
59
60    private void ParseButtonClick(object sender, EventArgs e) {
61      Parse();
62    }
63
64    private void ExpressionInputTextChanged(object sender, EventArgs e) {
65      infoLabel.Text = "Unparsed changes! Press parse button to save changes.";
66      infoLabel.ForeColor = Color.DarkOrange;
67    }
68
69    private void PaintTree() {
70      if(Content != null && Content.Tree != null) {
71        treeChart.Tree = Content.Tree;
72        foreach (var n in Content.Tree.IterateNodesPrefix()) {
73          if (n.Symbol is SubFunctionSymbol) {
74            var visualNode = treeChart.GetVisualSymbolicExpressionTreeNode(n);
75            visualNode.FillColor = Color.LightCyan;
76            visualNode.LineColor = Color.SlateGray;
77          }
78        }
79        treeChart.RepaintNodes();
80      }
81    }
82
83    private void ExpressionInputKeyUp(object sender, KeyEventArgs e) {
84      if (e.KeyCode == Keys.Enter)
85        Parse();
86    }
87
88    private void Parse() {
89      viewHost.Content = null; // reset active detail view
90      if (!string.IsNullOrEmpty(expressionInput.Text)) {
91        try {
92          Content.Template = expressionInput.Text;
93          PaintTree();
94          infoLabel.Text = "Template structure successfully parsed.";
95          infoLabel.ForeColor = Color.DarkGreen;
96        } catch (AggregateException ex) {
97          infoLabel.Text = string.Join("\n", ex.InnerExceptions.Select(x => x.Message));
98          infoLabel.ForeColor = Color.DarkRed;
99        } catch (Exception ex) {
100          infoLabel.Text = ex.Message;
101          infoLabel.ForeColor = Color.DarkRed;
102        }
103      }
104    }
105
106    private void LinearScalingCheckBoxCheckStateChanged(object sender, EventArgs e) {
107      Content.ApplyLinearScaling = linearScalingCheckBox.Checked;
108      PaintTree();
109    }
110
111    private void helpButton_DoubleClick(object sender, EventArgs e) {
112      using (InfoBox dialog = new InfoBox("Help for structure template",
113        "HeuristicLab.Problems.DataAnalysis.Symbolic.Views.Resources.structureTemplateHelp.rtf",
114        this)) {
115        dialog.ShowDialog(this);
116      }
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.