[3614] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16057] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3614] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Linq;
|
---|
[4068] | 24 | using HeuristicLab.Common;
|
---|
[3614] | 25 | using HeuristicLab.Core;
|
---|
[4068] | 26 | using HeuristicLab.Data;
|
---|
[3614] | 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Optimization {
|
---|
| 30 | [StorableClass]
|
---|
| 31 | [Item("RunCollectionEqualityConstraint", "A constraint which checks the members of the contained runs for equality to the constraint data.")]
|
---|
[6673] | 32 | public class RunCollectionEqualityConstraint : EqualityConstraint, IRunCollectionColumnConstraint {
|
---|
[4157] | 33 | [StorableConstructor]
|
---|
| 34 | protected RunCollectionEqualityConstraint(bool deserializing) : base(deserializing) { }
|
---|
| 35 |
|
---|
[4722] | 36 | protected RunCollectionEqualityConstraint(RunCollectionEqualityConstraint original, Cloner cloner)
|
---|
| 37 | : base(original, cloner) {
|
---|
| 38 | ConstraintData = original.ConstraintData;
|
---|
| 39 | ConstraintOperation = original.ConstraintOperation;
|
---|
| 40 | constraintColumn = original.constraintColumn;
|
---|
| 41 | }
|
---|
| 42 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 43 | return new RunCollectionEqualityConstraint(this, cloner);
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[3614] | 46 | public RunCollectionEqualityConstraint()
|
---|
| 47 | : base() {
|
---|
| 48 | this.ConstraintData = string.Empty;
|
---|
| 49 | }
|
---|
| 50 | public RunCollectionEqualityConstraint(RunCollection constrainedValue, ConstraintOperation constraintOperation, string constraintData)
|
---|
| 51 | : base(constrainedValue, constraintOperation, constraintData) {
|
---|
| 52 | }
|
---|
| 53 | public RunCollectionEqualityConstraint(RunCollection constrainedValue, ConstraintOperation constraintOperation, string constraintData, bool active)
|
---|
| 54 | : base(constrainedValue, constraintOperation, constraintData, active) {
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | public new RunCollection ConstrainedValue {
|
---|
| 58 | get { return (RunCollection)base.ConstrainedValue; }
|
---|
| 59 | set { base.ConstrainedValue = value; }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | public new string ConstraintData {
|
---|
[3617] | 63 | get { return (string)base.ConstraintData; }
|
---|
| 64 | set { base.ConstraintData = value; }
|
---|
[3614] | 65 | }
|
---|
| 66 |
|
---|
| 67 | [Storable]
|
---|
[3717] | 68 | private string constraintColumn;
|
---|
| 69 | public string ConstraintColumn {
|
---|
[3614] | 70 | get { return constraintColumn; }
|
---|
| 71 | set {
|
---|
[3717] | 72 | if (!((IStringConvertibleMatrix)ConstrainedValue).ColumnNames.Contains(value))
|
---|
[3614] | 73 | throw new ArgumentException("Could not set ConstraintData to not existing column index.");
|
---|
| 74 | if (constraintColumn != value) {
|
---|
| 75 | constraintColumn = value;
|
---|
| 76 | this.OnConstraintColumnChanged();
|
---|
| 77 | this.OnToStringChanged();
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | public event EventHandler ConstraintColumnChanged;
|
---|
| 83 | protected virtual void OnConstraintColumnChanged() {
|
---|
| 84 | EventHandler handler = ConstraintColumnChanged;
|
---|
| 85 | if (handler != null)
|
---|
| 86 | handler(this, EventArgs.Empty);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[3717] | 89 | protected override void OnConstrainedValueChanged() {
|
---|
| 90 | base.OnConstrainedValueChanged();
|
---|
| 91 | IStringConvertibleMatrix matrix = (IStringConvertibleMatrix)ConstrainedValue;
|
---|
| 92 | if (constraintColumn == null && ConstrainedValue != null && matrix.Columns != 0)
|
---|
| 93 | constraintColumn = matrix.ColumnNames.ElementAt(0);
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[3614] | 96 | protected override bool Check(object constrainedMember) {
|
---|
| 97 | if (!Active)
|
---|
| 98 | return true;
|
---|
| 99 |
|
---|
| 100 | foreach (IRun run in ConstrainedValue.Where(r => r.Visible)) {
|
---|
| 101 | IItem item = ConstrainedValue.GetValue(run, constraintColumn);
|
---|
[6678] | 102 | if (item != null && !base.Check(item.ToString()))
|
---|
[3614] | 103 | run.Visible = false;
|
---|
| 104 | }
|
---|
| 105 | return true;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | protected override bool Check(object constrainedMember, out string errorMessage) {
|
---|
| 109 | errorMessage = string.Empty;
|
---|
| 110 | if (!Active)
|
---|
| 111 | return true;
|
---|
| 112 |
|
---|
| 113 | foreach (IRun run in ConstrainedValue.Where(r => r.Visible)) {
|
---|
| 114 | IItem item = ConstrainedValue.GetValue(run, constraintColumn);
|
---|
| 115 | if (!base.Check(item))
|
---|
| 116 | run.Visible = false;
|
---|
| 117 | }
|
---|
| 118 | return true;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | public override string ToString() {
|
---|
| 122 | string s = string.Empty;
|
---|
| 123 | IStringConvertibleMatrix matrix = ConstrainedValue;
|
---|
| 124 | if (matrix != null && matrix.ColumnNames.Count() != 0)
|
---|
[3717] | 125 | s += constraintColumn + " ";
|
---|
[3614] | 126 | else
|
---|
| 127 | return "EqualityConstraint";
|
---|
| 128 |
|
---|
| 129 | if (ConstraintOperation != null)
|
---|
| 130 | s += ConstraintOperation.ToString() + " ";
|
---|
| 131 |
|
---|
| 132 | if (!string.IsNullOrEmpty(ConstraintData))
|
---|
| 133 | s += ConstraintData;
|
---|
| 134 | else
|
---|
| 135 | s += "null";
|
---|
| 136 |
|
---|
| 137 | return s;
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | }
|
---|