Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Core/3.3/Constraints/ComparisonConstraint.cs @ 10783

Last change on this file since 10783 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

File size: 4.0 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using HeuristicLab.Common;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Core {
28  [StorableClass]
29  [Item("ComparisonConstraint", "A constraint which checks for specified compare operation.")]
30  public class ComparisonConstraint : Constraint {
31    [StorableConstructor]
32    protected ComparisonConstraint(bool deserializing) : base(deserializing) { }
33    protected ComparisonConstraint(ComparisonConstraint original, Cloner cloner) : base(original, cloner) { }
34    public ComparisonConstraint() : base() { }
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)
39      : base(constrainedValue, comparisonOperation, comparisonOperation, active) {
40    }
41
42    public override IEnumerable<ConstraintOperation> AllowedConstraintOperations {
43      get { return new ConstraintOperation[6] { ConstraintOperation.Less, ConstraintOperation.LessOrEqual, ConstraintOperation.Equal, ConstraintOperation.GreaterOrEqual, ConstraintOperation.Greater, ConstraintOperation.NotEqual }; }
44    }
45
46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new ComparisonConstraint(this, cloner);
48    }
49
50    protected override bool Check(object constrainedMember) {
51      if (constrainedMember == null)
52        return false;
53
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
58      int compareResult = comparableMember.CompareTo(this.ConstraintData);
59      bool result = false;
60      if (this.ConstraintOperation == ConstraintOperation.Less)
61        result = compareResult < 0;
62      else if (this.ConstraintOperation == ConstraintOperation.LessOrEqual)
63        result = compareResult <= 0;
64      else if (this.ConstraintOperation == ConstraintOperation.Equal)
65        result = compareResult == 0;
66      else if (this.ConstraintOperation == ConstraintOperation.GreaterOrEqual)
67        result = compareResult >= 0;
68      else if (this.ConstraintOperation == ConstraintOperation.Greater)
69        result = compareResult > 0;
70      else if (this.ConstraintOperation == ConstraintOperation.NotEqual)
71        result = compareResult != 0;
72      else
73        throw new InvalidOperationException("Constraint operation " + this.ConstraintOperation + " is not defined for ComparisonConstraint.");
74
75      return result;
76    }
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    }
85  }
86}
Note: See TracBrowser for help on using the repository browser.