Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Core/3.3/Constraints/EqualityConstraint.cs @ 18005

Last change on this file since 18005 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 3.3 KB
RevLine 
[3602]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3602]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;
[4722]24using HeuristicLab.Common;
[16565]25using HEAL.Attic;
[3602]26
27namespace HeuristicLab.Core {
[16565]28  [StorableType("9E83A84E-39A1-4DEB-90EB-DEB5A457D77D")]
[3602]29  [Item("EqualityConstraint", "A constraint which checks for equality.")]
30  public class EqualityConstraint : Constraint {
31    [StorableConstructor]
[16565]32    protected EqualityConstraint(StorableConstructorFlag _) : base(_) { }
[4722]33    protected EqualityConstraint(EqualityConstraint original, Cloner cloner) : base(original, cloner) { }
[4153]34    public EqualityConstraint() : base() { }
[3605]35    public EqualityConstraint(IItem constrainedValue, ConstraintOperation constraintOperation, object constraintData)
36      : base(constrainedValue, constraintOperation, constraintData) {
[3602]37    }
[3605]38    public EqualityConstraint(IItem constrainedValue, ConstraintOperation constraintOperation, object constraintData, bool active)
39      : base(constrainedValue, constraintOperation, constraintData, active) {
[3602]40    }
41
42    public override IEnumerable<ConstraintOperation> AllowedConstraintOperations {
43      get { return new ConstraintOperation[2] { ConstraintOperation.Equal, ConstraintOperation.NotEqual }; }
44    }
45
[4722]46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new EqualityConstraint(this, cloner);
48    }
49
[3602]50    protected override bool Check(object constrainedMember) {
51      if (constrainedMember == null)
52        return false;
53
54      IComparable comparableMember = constrainedMember as IComparable;
55      bool compareValue;
56      if (comparableMember != null && this.ConstraintData != null)
57        compareValue = comparableMember.CompareTo(this.ConstraintData) == 0;
58      else
59        compareValue = constrainedMember.Equals(this.ConstraintData);
60
61      bool result;
62      if (ConstraintOperation == ConstraintOperation.Equal)
63        result = compareValue;
64      else if (ConstraintOperation == ConstraintOperation.NotEqual)
65        result = !compareValue;
66      else
67        throw new InvalidOperationException("Constraint operation " + this.ConstraintOperation + " is not defined for TypeConstraint.");
68
69      return result;
70    }
71
72    protected override bool Check(object constrainedMember, out string errorMessage) {
73      bool result = Check(constrainedMember);
74      errorMessage = string.Empty;
75      if (!result)
76        errorMessage = constrainedMember.ToString() + " must be " + ConstraintOperation.ToString() + " to " + ConstraintData.ToString() + ".";
77      return result;
78    }
79  }
80}
Note: See TracBrowser for help on using the repository browser.