1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Drawing;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Text;
|
---|
6 | using System.Windows.Forms;
|
---|
7 | using HeuristicLab.Collections;
|
---|
8 | using HeuristicLab.Core;
|
---|
9 | using HeuristicLab.Data;
|
---|
10 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
11 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
|
---|
12 | using HeuristicLab.MainForm;
|
---|
13 | using HeuristicLab.MainForm.WindowsForms;
|
---|
14 |
|
---|
15 | namespace 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 | }
|
---|