Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.1/HeuristicLab.Core/3.3/Constraints/TypeCompatibilityConstraint.cs @ 4537

Last change on this file since 4537 was 4153, checked in by mkommend, 14 years ago

chained storable ctors for constraints (ticket #1129)

File size: 3.2 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("TypeCompatibilityConstraint", "A constraint that checks for compatible types.")]
29  public class TypeCompatibilityConstraint : Constraint {
30    public TypeCompatibilityConstraint() {
31    }
32    [StorableConstructor]
33    protected TypeCompatibilityConstraint(bool deserializing) : base(deserializing) { }
34    public TypeCompatibilityConstraint(IItem constrainedValue, ConstraintOperation constraintOperation, Type constraintData)
35      : base(constrainedValue, constraintOperation, constraintData) {
36    }
37    public TypeCompatibilityConstraint(IItem constrainedValue, ConstraintOperation constraintOperation, Type constraintData, bool active)
38      : base(constrainedValue, constraintOperation, constraintData, active) {
39    }
40
41    public new Type ConstraintData {
42      get { return (Type)base.ConstraintData; }
43      set { base.ConstraintData = value; }
44    }
45
46    public override IEnumerable<ConstraintOperation> AllowedConstraintOperations {
47      get { return new ConstraintOperation[2] { ConstraintOperation.IsTypeCompatible, ConstraintOperation.IsTypeNotCompatible }; }
48    }
49
50    protected override bool Check(object constrainedMember) {
51      if (constrainedMember == null)
52        return false;
53
54      Type constrainedMemberType = constrainedMember.GetType();
55      bool compareValue = ConstraintData.IsAssignableFrom(constrainedMemberType);
56      bool result;
57      if (ConstraintOperation == ConstraintOperation.IsTypeCompatible)
58        result = compareValue;
59      else if (ConstraintOperation == ConstraintOperation.IsTypeNotCompatible)
60        result = !compareValue;
61      else
62        throw new InvalidOperationException("Constraint operation " + this.ConstraintOperation + " is not defined for TypeCompatibilityConstraint.");
63
64      return result;
65    }
66
67    protected override bool Check(object constrainedMember, out string errorMessage) {
68      bool result = Check(constrainedMember);
69      errorMessage = string.Empty;
70      if (!result)
71        errorMessage = "The type of " + constrainedMember.ToString() + " must be " + ConstraintOperation.ToString() + " compatible to " + ConstraintData.ToString() + ".";
72      return result;
73    }
74  }
75}
Note: See TracBrowser for help on using the repository browser.