Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3073_IA_constraint_splitting/HeuristicLab.Problems.DataAnalysis.Views/3.4/ProblemDataConstraintView.cs @ 18242

Last change on this file since 18242 was 17607, checked in by chaider, 5 years ago

#3073 Added classes/views to define constraints

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