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