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