Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/Constraints/ComparisonConstraint.cs @ 4068

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

Sorted usings and removed unused usings in entire solution (#1094)

File size: 3.7 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.Persistence.Default.CompositeSerializers.Storable;
25
26namespace HeuristicLab.Core {
27  [StorableClass]
28  [Item("ComparisonConstraint", "A constraint which checks for specified compare operation.")]
29  public class ComparisonConstraint : Constraint {
30    public ComparisonConstraint()
31      : base() {
32    }
33    [StorableConstructor]
34    protected ComparisonConstraint(bool deserializing) {
35    }
36    public ComparisonConstraint(IItem constrainedValue, ConstraintOperation comparisonOperation, object comparisonValue)
37      : base(constrainedValue, comparisonOperation, comparisonValue) {
38    }
39    public ComparisonConstraint(IItem constrainedValue, ConstraintOperation comparisonOperation, object comparisonValue, bool active)
40      : base(constrainedValue, comparisonOperation, comparisonOperation, active) {
41    }
42
43    public override IEnumerable<ConstraintOperation> AllowedConstraintOperations {
44      get { return new ConstraintOperation[6] { ConstraintOperation.Less, ConstraintOperation.LessOrEqual, ConstraintOperation.Equal, ConstraintOperation.GreaterOrEqual, ConstraintOperation.Greater, ConstraintOperation.NotEqual }; }
45    }
46
47    protected override bool Check(object constrainedMember) {
48      if (constrainedMember == null)
49        return false;
50
51      IComparable comparableMember = constrainedMember as IComparable;
52      if (comparableMember == null)
53        throw new InvalidOperationException("Constrained member must be of type IComparable to be used with ComparisonConstraint.");
54
55      int compareResult = comparableMember.CompareTo(this.ConstraintData);
56      bool result = false;
57      if (this.ConstraintOperation == ConstraintOperation.Less)
58        result = compareResult < 0;
59      else if (this.ConstraintOperation == ConstraintOperation.LessOrEqual)
60        result = compareResult <= 0;
61      else if (this.ConstraintOperation == ConstraintOperation.Equal)
62        result = compareResult == 0;
63      else if (this.ConstraintOperation == ConstraintOperation.GreaterOrEqual)
64        result = compareResult >= 0;
65      else if (this.ConstraintOperation == ConstraintOperation.Greater)
66        result = compareResult > 0;
67      else if (this.ConstraintOperation == ConstraintOperation.NotEqual)
68        result = compareResult != 0;
69      else
70        throw new InvalidOperationException("Constraint operation " + this.ConstraintOperation + " is not defined for ComparisonConstraint.");
71
72      return result;
73    }
74
75    protected override bool Check(object constrainedMember, out string errorMessage) {
76      bool result = Check(constrainedMember);
77      errorMessage = string.Empty;
78      if (!result)
79        errorMessage = constrainedMember.ToString() + " must be " + ConstraintOperation.ToString() + " than " + ConstraintData.ToString() + ".";
80      return result;
81    }
82  }
83}
Note: See TracBrowser for help on using the repository browser.