Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.0/HeuristicLab.Core/3.3/Constraints/TypeCompatibilityConstraint.cs @ 14620

Last change on this file since 14620 was 3605, checked in by mkommend, 14 years ago

corrected ctors of core constraints (ticket #996)

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