[16800] | 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;
|
---|
[16787] | 22 | using System.Collections.Generic;
|
---|
[17250] | 23 | using System.Drawing;
|
---|
[16777] | 24 | using HeuristicLab.Collections;
|
---|
[16772] | 25 | using HeuristicLab.Core;
|
---|
[16756] | 26 | using HeuristicLab.MainForm;
|
---|
| 27 | using HeuristicLab.MainForm.WindowsForms;
|
---|
| 28 |
|
---|
[16830] | 29 | namespace HeuristicLab.Problems.DataAnalysis.Views {
|
---|
[17536] | 30 | [View("ProblemDataConstraint View")]
|
---|
[16896] | 31 | [Content(typeof(ProblemDataConstraint), true)]
|
---|
[17146] | 32 | public partial class ProblemDataConstraintView : AsynchronousContentView {
|
---|
| 33 | private readonly CheckedItemList<IntervalConstraint> intervalConstraints = new CheckedItemList<IntervalConstraint>();
|
---|
[16896] | 34 | public new ProblemDataConstraint Content {
|
---|
| 35 | get => (ProblemDataConstraint)base.Content;
|
---|
[16756] | 36 | set => base.Content = value;
|
---|
| 37 | }
|
---|
[16773] | 38 |
|
---|
[17146] | 39 | public ProblemDataConstraintView() {
|
---|
[16756] | 40 | InitializeComponent();
|
---|
[16772] | 41 | errorOutput.Text = "";
|
---|
[16756] | 42 | }
|
---|
| 43 |
|
---|
| 44 | protected override void OnContentChanged() {
|
---|
| 45 | base.OnContentChanged();
|
---|
[16776] | 46 | UpdateControl();
|
---|
[16756] | 47 | }
|
---|
[16870] | 48 |
|
---|
| 49 | protected override void RegisterContentEvents() {
|
---|
| 50 | base.RegisterContentEvents();
|
---|
| 51 | intervalConstraints.CheckedItemsChanged += constraint_CheckedItemChanged;
|
---|
| 52 | intervalConstraints.ItemsAdded += constraints_Updated;
|
---|
[16938] | 53 | intervalConstraints.ItemsRemoved += constraint_removed;
|
---|
| 54 | Content.Changed += Content_Changed;
|
---|
[16870] | 55 | }
|
---|
| 56 |
|
---|
| 57 | protected override void DeregisterContentEvents() {
|
---|
| 58 | intervalConstraints.CheckedItemsChanged -= constraint_CheckedItemChanged;
|
---|
| 59 | intervalConstraints.ItemsAdded -= constraints_Updated;
|
---|
[16918] | 60 | intervalConstraints.ItemsRemoved -= constraint_removed;
|
---|
[16938] | 61 | Content.Changed -= Content_Changed;
|
---|
[16870] | 62 | base.DeregisterContentEvents();
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[16963] | 65 | protected override void SetEnabledStateOfControls() {
|
---|
| 66 | constraintsInput.Enabled = Content != null && !Locked && !ReadOnly;
|
---|
| 67 | }
|
---|
[16870] | 68 |
|
---|
[16963] | 69 |
|
---|
[16787] | 70 | private IEnumerable<IntervalConstraint> ParseConstraints(string input) {
|
---|
[17249] | 71 | return IntervalConstraintsParser.ParseInput(input, Content.ProblemData.TargetVariable,
|
---|
| 72 | Content.ProblemData.AllowedInputVariables);
|
---|
[16756] | 73 | }
|
---|
| 74 |
|
---|
| 75 | private void parseBtn_Click(object sender, EventArgs e) {
|
---|
| 76 | if (constraintsInput.Text != null) {
|
---|
[16870] | 77 | intervalConstraints.Clear();
|
---|
[17250] | 78 | try {
|
---|
| 79 | var parsedConstraints = ParseConstraints(constraintsInput.Text);
|
---|
| 80 | Content.Constraints = parsedConstraints;
|
---|
| 81 | Content.Input = constraintsInput.Text;
|
---|
[17256] | 82 | Content.InfoText = "Constraints successfully parsed.";
|
---|
| 83 | Content.InfoColor = Color.DarkGreen;
|
---|
[17250] | 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>();
|
---|
[17256] | 87 | Content.InfoText = ex.Message.Replace("Parameter name", "@Line");
|
---|
| 88 | Content.InfoColor = Color.DarkRed;
|
---|
[17250] | 89 | }
|
---|
[16772] | 90 | } else {
|
---|
| 91 | errorOutput.Text = "No constraints were found!";
|
---|
[16756] | 92 | }
|
---|
| 93 | }
|
---|
[16772] | 94 |
|
---|
[16776] | 95 | private void UpdateControl() {
|
---|
| 96 | if (Content == null) {
|
---|
[16870] | 97 | intervalConstraints.Clear();
|
---|
[17146] | 98 | intervalConstraintsView.Content = intervalConstraints;
|
---|
[16776] | 99 | } else {
|
---|
[16870] | 100 | intervalConstraints.Clear();
|
---|
[16776] | 101 | constraintsInput.Text = Content.Input;
|
---|
[17256] | 102 | errorOutput.ForeColor = Content.InfoColor;
|
---|
| 103 | errorOutput.Text = Content.InfoText;
|
---|
[16776] | 104 | foreach (var constraint in Content.Constraints) {
|
---|
[16800] | 105 | intervalConstraints.Add(constraint, constraint.Enabled);
|
---|
[16776] | 106 | }
|
---|
[16787] | 107 |
|
---|
[17146] | 108 | intervalConstraintsView.Content = intervalConstraints;
|
---|
[16776] | 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[16787] | 112 | private void constraint_Changed(object sender, EventArgs e) {
|
---|
[17146] | 113 | var constraint = (IntervalConstraint)sender;
|
---|
[16800] | 114 | intervalConstraints.SetItemCheckedState(constraint, constraint.Enabled);
|
---|
[16777] | 115 | }
|
---|
| 116 |
|
---|
[16870] | 117 | private void constraints_Updated(object sender, CollectionItemsChangedEventArgs<IndexedItem<IntervalConstraint>> e) {
|
---|
| 118 | foreach (var addedItem in e.Items) {
|
---|
| 119 | addedItem.Value.Changed += constraint_Changed;
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[16918] | 123 | private void constraint_removed(object sender, CollectionItemsChangedEventArgs<IndexedItem<IntervalConstraint>> e) {
|
---|
| 124 | foreach (var removedItem in e.Items) {
|
---|
| 125 | removedItem.Value.Changed -= constraint_Changed;
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[16787] | 129 | private void constraint_CheckedItemChanged(object sender,
|
---|
[16777] | 130 | CollectionItemsChangedEventArgs<IndexedItem<IntervalConstraint>> e) {
|
---|
[17146] | 131 | ICheckedItemList<IntervalConstraint> checkedItemList = (ICheckedItemList<IntervalConstraint>)sender;
|
---|
[16777] | 132 | foreach (var indexedItem in e.Items) {
|
---|
[16800] | 133 | indexedItem.Value.Enabled = checkedItemList.ItemChecked(indexedItem.Value);
|
---|
[16777] | 134 | }
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[16787] | 137 | private void constraintsInput_TextChanged(object sender, EventArgs e) {
|
---|
[17250] | 138 | if (Content.Input != constraintsInput.Text) {
|
---|
[17256] | 139 | Content.Input = constraintsInput.Text;
|
---|
| 140 | Content.InfoText = "Unparsed changes! Press parse button to save changes.";
|
---|
| 141 | Content.InfoColor = Color.DarkOrange;
|
---|
[17249] | 142 | } else {
|
---|
| 143 | errorOutput.Text = "";
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[16787] | 146 | }
|
---|
| 147 |
|
---|
[16938] | 148 | private void Content_Changed(object sender, EventArgs e) {
|
---|
| 149 | UpdateControl();
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[16756] | 152 | }
|
---|
| 153 | }
|
---|