1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 | using System;
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Drawing;
|
---|
24 | using HeuristicLab.Collections;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.MainForm;
|
---|
27 | using HeuristicLab.MainForm.WindowsForms;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Problems.DataAnalysis.Views {
|
---|
30 | [View("ParsedConstraint View")]
|
---|
31 | [Content(typeof(ProblemDataConstraint), true)]
|
---|
32 | public partial class ProblemDataConstraintView : AsynchronousContentView {
|
---|
33 | private readonly CheckedItemList<IntervalConstraint> intervalConstraints = new CheckedItemList<IntervalConstraint>();
|
---|
34 | public new ProblemDataConstraint Content {
|
---|
35 | get => (ProblemDataConstraint)base.Content;
|
---|
36 | set => base.Content = value;
|
---|
37 | }
|
---|
38 |
|
---|
39 | public ProblemDataConstraintView() {
|
---|
40 | InitializeComponent();
|
---|
41 | errorOutput.Text = "";
|
---|
42 | }
|
---|
43 |
|
---|
44 | protected override void OnContentChanged() {
|
---|
45 | base.OnContentChanged();
|
---|
46 | UpdateControl();
|
---|
47 | }
|
---|
48 |
|
---|
49 | protected override void RegisterContentEvents() {
|
---|
50 | base.RegisterContentEvents();
|
---|
51 | intervalConstraints.CheckedItemsChanged += constraint_CheckedItemChanged;
|
---|
52 | intervalConstraints.ItemsAdded += constraints_Updated;
|
---|
53 | intervalConstraints.ItemsRemoved += constraint_removed;
|
---|
54 | Content.Changed += Content_Changed;
|
---|
55 | }
|
---|
56 |
|
---|
57 | protected override void DeregisterContentEvents() {
|
---|
58 | intervalConstraints.CheckedItemsChanged -= constraint_CheckedItemChanged;
|
---|
59 | intervalConstraints.ItemsAdded -= constraints_Updated;
|
---|
60 | intervalConstraints.ItemsRemoved -= constraint_removed;
|
---|
61 | Content.Changed -= Content_Changed;
|
---|
62 | base.DeregisterContentEvents();
|
---|
63 | }
|
---|
64 |
|
---|
65 | protected override void SetEnabledStateOfControls() {
|
---|
66 | constraintsInput.Enabled = Content != null && !Locked && !ReadOnly;
|
---|
67 | }
|
---|
68 |
|
---|
69 |
|
---|
70 | private IEnumerable<IntervalConstraint> ParseConstraints(string input) {
|
---|
71 | return IntervalConstraintsParser.ParseInput(input, Content.ProblemData.TargetVariable,
|
---|
72 | Content.ProblemData.AllowedInputVariables);
|
---|
73 | }
|
---|
74 |
|
---|
75 | private void parseBtn_Click(object sender, EventArgs e) {
|
---|
76 | if (constraintsInput.Text != null) {
|
---|
77 | intervalConstraints.Clear();
|
---|
78 | try {
|
---|
79 | var parsedConstraints = ParseConstraints(constraintsInput.Text);
|
---|
80 | Content.Constraints = parsedConstraints;
|
---|
81 | Content.Input = constraintsInput.Text;
|
---|
82 | errorOutput.ForeColor = Color.DarkGreen;
|
---|
83 | errorOutput.Text = "Constraints successfully parsed.";
|
---|
84 | //Catch the exception from the constraints parser and show it in the error dialog
|
---|
85 | } catch(ArgumentException ex) {
|
---|
86 | Content.Constraints = new List<IntervalConstraint>();
|
---|
87 | errorOutput.ForeColor = Color.DarkRed;
|
---|
88 | errorOutput.Text = ex.Message.Replace("Parameter name", "@Line");
|
---|
89 | }
|
---|
90 | } else {
|
---|
91 | errorOutput.Text = "No constraints were found!";
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | private void UpdateControl() {
|
---|
96 | if (Content == null) {
|
---|
97 | intervalConstraints.Clear();
|
---|
98 | intervalConstraintsView.Content = intervalConstraints;
|
---|
99 | } else {
|
---|
100 | intervalConstraints.Clear();
|
---|
101 | constraintsInput.Text = Content.Input;
|
---|
102 | foreach (var constraint in Content.Constraints) {
|
---|
103 | intervalConstraints.Add(constraint, constraint.Enabled);
|
---|
104 | }
|
---|
105 |
|
---|
106 | intervalConstraintsView.Content = intervalConstraints;
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | private void constraint_Changed(object sender, EventArgs e) {
|
---|
111 | var constraint = (IntervalConstraint)sender;
|
---|
112 | intervalConstraints.SetItemCheckedState(constraint, constraint.Enabled);
|
---|
113 | }
|
---|
114 |
|
---|
115 | private void constraints_Updated(object sender, CollectionItemsChangedEventArgs<IndexedItem<IntervalConstraint>> e) {
|
---|
116 | foreach (var addedItem in e.Items) {
|
---|
117 | addedItem.Value.Changed += constraint_Changed;
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | private void constraint_removed(object sender, CollectionItemsChangedEventArgs<IndexedItem<IntervalConstraint>> e) {
|
---|
122 | foreach (var removedItem in e.Items) {
|
---|
123 | removedItem.Value.Changed -= constraint_Changed;
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | private void constraint_CheckedItemChanged(object sender,
|
---|
128 | CollectionItemsChangedEventArgs<IndexedItem<IntervalConstraint>> e) {
|
---|
129 | ICheckedItemList<IntervalConstraint> checkedItemList = (ICheckedItemList<IntervalConstraint>)sender;
|
---|
130 | foreach (var indexedItem in e.Items) {
|
---|
131 | indexedItem.Value.Enabled = checkedItemList.ItemChecked(indexedItem.Value);
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | private void constraintsInput_TextChanged(object sender, EventArgs e) {
|
---|
136 | if (Content.Input != constraintsInput.Text) {
|
---|
137 | errorOutput.ForeColor = Color.DarkOrange;
|
---|
138 | errorOutput.Text = "Unparsed changes! Press parse button to save changes.";
|
---|
139 | Content.Input = constraintsInput.Text;
|
---|
140 | } else {
|
---|
141 | errorOutput.Text = "";
|
---|
142 | }
|
---|
143 |
|
---|
144 | }
|
---|
145 |
|
---|
146 | private void Content_Changed(object sender, EventArgs e) {
|
---|
147 | UpdateControl();
|
---|
148 | }
|
---|
149 |
|
---|
150 | }
|
---|
151 | }
|
---|