#region License Information /* HeuristicLab * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Core { [StorableClass] [Item("ComparisonConstraint", "A constraint which checks for specified compare operation.")] public class ComparisonConstraint : Constraint { public ComparisonConstraint() : base() { } [StorableConstructor] protected ComparisonConstraint(bool deserializing) { } public ComparisonConstraint(IItem constrainedValue, ConstraintOperation comparisonOperation, object comparisonValue) : base(constrainedValue, comparisonOperation, comparisonValue) { } public ComparisonConstraint(IItem constrainedValue, ConstraintOperation comparisonOperation, object comparisonValue, bool active) : base(constrainedValue, comparisonOperation, comparisonOperation, active) { } public override IEnumerable AllowedConstraintOperations { get { return new ConstraintOperation[6] { ConstraintOperation.Lesser, ConstraintOperation.LesserOrEqual, ConstraintOperation.Equal, ConstraintOperation.GreaterOrEqual, ConstraintOperation.Greater, ConstraintOperation.NotEqual }; } } protected override bool Check(object constrainedMember) { if (constrainedMember == null) return false; IComparable comparableMember = constrainedMember as IComparable; if (comparableMember == null) throw new InvalidOperationException("Constrained member must be of type IComparable to be used with ComparisonConstraint."); int compareResult = comparableMember.CompareTo(this.ConstraintData); bool result = false; if (this.ConstraintOperation == ConstraintOperation.Lesser) result = compareResult < 0; else if (this.ConstraintOperation == ConstraintOperation.LesserOrEqual) result = compareResult <= 0; else if (this.ConstraintOperation == ConstraintOperation.Equal) result = compareResult == 0; else if (this.ConstraintOperation == ConstraintOperation.GreaterOrEqual) result = compareResult >= 0; else if (this.ConstraintOperation == ConstraintOperation.Greater) result = compareResult > 0; else if (this.ConstraintOperation == ConstraintOperation.NotEqual) result = compareResult != 0; else throw new InvalidOperationException("Constraint operation " + this.ConstraintOperation + " is not defined for ComparisonConstraint."); return result; } protected override bool Check(object constrainedMember, out string errorMessage) { bool result = Check(constrainedMember); errorMessage = string.Empty; if (!result) errorMessage = constrainedMember.ToString() + " must be " + ConstraintOperation.ToString() + " than " + ConstraintData.ToString() + "."; return result; } } }