[3592] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15584] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3592] | 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.Collections.Generic;
|
---|
[4722] | 24 | using HeuristicLab.Common;
|
---|
[3592] | 25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 26 |
|
---|
[3602] | 27 | namespace HeuristicLab.Core {
|
---|
[3592] | 28 | [StorableClass]
|
---|
| 29 | [Item("ComparisonConstraint", "A constraint which checks for specified compare operation.")]
|
---|
| 30 | public class ComparisonConstraint : Constraint {
|
---|
[3602] | 31 | [StorableConstructor]
|
---|
[4153] | 32 | protected ComparisonConstraint(bool deserializing) : base(deserializing) { }
|
---|
[4722] | 33 | protected ComparisonConstraint(ComparisonConstraint original, Cloner cloner) : base(original, cloner) { }
|
---|
[4153] | 34 | public ComparisonConstraint() : base() { }
|
---|
[3602] | 35 | public ComparisonConstraint(IItem constrainedValue, ConstraintOperation comparisonOperation, object comparisonValue)
|
---|
| 36 | : base(constrainedValue, comparisonOperation, comparisonValue) {
|
---|
| 37 | }
|
---|
| 38 | public ComparisonConstraint(IItem constrainedValue, ConstraintOperation comparisonOperation, object comparisonValue, bool active)
|
---|
[11137] | 39 | : base(constrainedValue, comparisonOperation, comparisonValue, active) {
|
---|
[3602] | 40 | }
|
---|
[3592] | 41 |
|
---|
[3602] | 42 | public override IEnumerable<ConstraintOperation> AllowedConstraintOperations {
|
---|
[3630] | 43 | get { return new ConstraintOperation[6] { ConstraintOperation.Less, ConstraintOperation.LessOrEqual, ConstraintOperation.Equal, ConstraintOperation.GreaterOrEqual, ConstraintOperation.Greater, ConstraintOperation.NotEqual }; }
|
---|
[3592] | 44 | }
|
---|
| 45 |
|
---|
[4722] | 46 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 47 | return new ComparisonConstraint(this, cloner);
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[3592] | 50 | protected override bool Check(object constrainedMember) {
|
---|
[3602] | 51 | if (constrainedMember == null)
|
---|
| 52 | return false;
|
---|
| 53 |
|
---|
[3592] | 54 | IComparable comparableMember = constrainedMember as IComparable;
|
---|
| 55 | if (comparableMember == null)
|
---|
| 56 | throw new InvalidOperationException("Constrained member must be of type IComparable to be used with ComparisonConstraint.");
|
---|
| 57 |
|
---|
[3602] | 58 | int compareResult = comparableMember.CompareTo(this.ConstraintData);
|
---|
[3592] | 59 | bool result = false;
|
---|
[3630] | 60 | if (this.ConstraintOperation == ConstraintOperation.Less)
|
---|
[3592] | 61 | result = compareResult < 0;
|
---|
[3630] | 62 | else if (this.ConstraintOperation == ConstraintOperation.LessOrEqual)
|
---|
[3592] | 63 | result = compareResult <= 0;
|
---|
[3602] | 64 | else if (this.ConstraintOperation == ConstraintOperation.Equal)
|
---|
[3592] | 65 | result = compareResult == 0;
|
---|
[3602] | 66 | else if (this.ConstraintOperation == ConstraintOperation.GreaterOrEqual)
|
---|
[3592] | 67 | result = compareResult >= 0;
|
---|
[3602] | 68 | else if (this.ConstraintOperation == ConstraintOperation.Greater)
|
---|
[3592] | 69 | result = compareResult > 0;
|
---|
[3602] | 70 | else if (this.ConstraintOperation == ConstraintOperation.NotEqual)
|
---|
[3592] | 71 | result = compareResult != 0;
|
---|
| 72 | else
|
---|
[3602] | 73 | throw new InvalidOperationException("Constraint operation " + this.ConstraintOperation + " is not defined for ComparisonConstraint.");
|
---|
[3592] | 74 |
|
---|
| 75 | return result;
|
---|
| 76 | }
|
---|
[3602] | 77 |
|
---|
| 78 | protected override bool Check(object constrainedMember, out string errorMessage) {
|
---|
| 79 | bool result = Check(constrainedMember);
|
---|
| 80 | errorMessage = string.Empty;
|
---|
| 81 | if (!result)
|
---|
| 82 | errorMessage = constrainedMember.ToString() + " must be " + ConstraintOperation.ToString() + " than " + ConstraintData.ToString() + ".";
|
---|
| 83 | return result;
|
---|
| 84 | }
|
---|
[3592] | 85 | }
|
---|
| 86 | }
|
---|