[3602] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[4722] | 24 | using HeuristicLab.Common;
|
---|
[3602] | 25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 26 |
|
---|
| 27 | namespace HeuristicLab.Core {
|
---|
| 28 | [StorableClass]
|
---|
| 29 | [Item("TypeCompatibilityConstraint", "A constraint that checks for compatible types.")]
|
---|
| 30 | public class TypeCompatibilityConstraint : Constraint {
|
---|
| 31 | [StorableConstructor]
|
---|
[4153] | 32 | protected TypeCompatibilityConstraint(bool deserializing) : base(deserializing) { }
|
---|
[4722] | 33 | protected TypeCompatibilityConstraint(TypeCompatibilityConstraint original, Cloner cloner) : base(original, cloner) { }
|
---|
| 34 | public TypeCompatibilityConstraint() : base() { }
|
---|
[3605] | 35 | public TypeCompatibilityConstraint(IItem constrainedValue, ConstraintOperation constraintOperation, Type constraintData)
|
---|
| 36 | : base(constrainedValue, constraintOperation, constraintData) {
|
---|
[3602] | 37 | }
|
---|
[3605] | 38 | public TypeCompatibilityConstraint(IItem constrainedValue, ConstraintOperation constraintOperation, Type constraintData, bool active)
|
---|
| 39 | : base(constrainedValue, constraintOperation, constraintData, active) {
|
---|
[3602] | 40 | }
|
---|
| 41 |
|
---|
| 42 | public new Type ConstraintData {
|
---|
| 43 | get { return (Type)base.ConstraintData; }
|
---|
| 44 | set { base.ConstraintData = value; }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | public override IEnumerable<ConstraintOperation> AllowedConstraintOperations {
|
---|
| 48 | get { return new ConstraintOperation[2] { ConstraintOperation.IsTypeCompatible, ConstraintOperation.IsTypeNotCompatible }; }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[4722] | 51 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 52 | return new TypeCompatibilityConstraint(this, cloner);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
[3602] | 55 | protected override bool Check(object constrainedMember) {
|
---|
| 56 | if (constrainedMember == null)
|
---|
| 57 | return false;
|
---|
| 58 |
|
---|
| 59 | Type constrainedMemberType = constrainedMember.GetType();
|
---|
| 60 | bool compareValue = ConstraintData.IsAssignableFrom(constrainedMemberType);
|
---|
| 61 | bool result;
|
---|
| 62 | if (ConstraintOperation == ConstraintOperation.IsTypeCompatible)
|
---|
| 63 | result = compareValue;
|
---|
| 64 | else if (ConstraintOperation == ConstraintOperation.IsTypeNotCompatible)
|
---|
| 65 | result = !compareValue;
|
---|
| 66 | else
|
---|
| 67 | throw new InvalidOperationException("Constraint operation " + this.ConstraintOperation + " is not defined for TypeCompatibilityConstraint.");
|
---|
| 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 = "The type of " + constrainedMember.ToString() + " must be " + ConstraintOperation.ToString() + " compatible to " + ConstraintData.ToString() + ".";
|
---|
| 77 | return result;
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | }
|
---|