Changeset 3602
- Timestamp:
- 05/04/10 13:38:35 (15 years ago)
- Location:
- trunk/sources
- Files:
-
- 6 added
- 1 deleted
- 2 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Core/3.3/Constraints/ComparisonConstraint.cs
r3601 r3602 24 24 using System.Linq; 25 25 using System.Text; 26 using HeuristicLab.Core;27 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 27 29 namespace HeuristicLab. Data{28 namespace HeuristicLab.Core { 30 29 [StorableClass] 31 30 [Item("ComparisonConstraint", "A constraint which checks for specified compare operation.")] 32 31 public class ComparisonConstraint : Constraint { 33 /// <summary> 34 /// Protected default constructor for constructor chaining and cloning. 35 /// </summary> 36 [StorableConstructor] 37 protected ComparisonConstraint() 32 public ComparisonConstraint() 38 33 : base() { 39 34 } 40 41 public ComparisonConstraint(IItem constrainedValue, ComparisonOperation comparisonOperation, object comparisonValue) 42 : base() { 43 this.ConstrainedValue = constrainedValue; 44 this.ComparisonOperation = comparisonOperation; 45 this.ComparisonValue = comparisonValue; 35 [StorableConstructor] 36 protected ComparisonConstraint(bool deserializing) { 37 } 38 public ComparisonConstraint(IItem constrainedValue, ConstraintOperation comparisonOperation, object comparisonValue) 39 : base(constrainedValue, comparisonOperation, comparisonValue) { 40 } 41 public ComparisonConstraint(IItem constrainedValue, ConstraintOperation comparisonOperation, object comparisonValue, bool active) 42 : base(constrainedValue, comparisonOperation, comparisonOperation, active) { 46 43 } 47 44 48 public override IEnumerable<Co mparisonOperation> AllowedComparisonOperations {49 get { return new List<ComparisonOperation>() { ComparisonOperation.Equal, ComparisonOperation.NotEqual, ComparisonOperation.Lesser, ComparisonOperation.LesserOrEqual, ComparisonOperation.Greater, ComparisonOperation.GreaterOrEqual }; }45 public override IEnumerable<ConstraintOperation> AllowedConstraintOperations { 46 get { return new ConstraintOperation[6] { ConstraintOperation.Equal, ConstraintOperation.NotEqual, ConstraintOperation.Lesser, ConstraintOperation.LesserOrEqual, ConstraintOperation.Greater, ConstraintOperation.GreaterOrEqual }; } 50 47 } 51 48 52 49 protected override bool Check(object constrainedMember) { 50 if (constrainedMember == null) 51 return false; 52 53 53 IComparable comparableMember = constrainedMember as IComparable; 54 54 if (comparableMember == null) 55 55 throw new InvalidOperationException("Constrained member must be of type IComparable to be used with ComparisonConstraint."); 56 56 57 int compareResult = comparableMember.CompareTo(this.Co mparisonValue);57 int compareResult = comparableMember.CompareTo(this.ConstraintData); 58 58 bool result = false; 59 if (this.Co mparisonOperation == ComparisonOperation.Lesser)59 if (this.ConstraintOperation == ConstraintOperation.Lesser) 60 60 result = compareResult < 0; 61 else if (this.Co mparisonOperation == ComparisonOperation.LesserOrEqual)61 else if (this.ConstraintOperation == ConstraintOperation.LesserOrEqual) 62 62 result = compareResult <= 0; 63 else if (this.Co mparisonOperation == ComparisonOperation.Equal)63 else if (this.ConstraintOperation == ConstraintOperation.Equal) 64 64 result = compareResult == 0; 65 else if (this.Co mparisonOperation == ComparisonOperation.GreaterOrEqual)65 else if (this.ConstraintOperation == ConstraintOperation.GreaterOrEqual) 66 66 result = compareResult >= 0; 67 else if (this.Co mparisonOperation == ComparisonOperation.Greater)67 else if (this.ConstraintOperation == ConstraintOperation.Greater) 68 68 result = compareResult > 0; 69 else if (this.Co mparisonOperation == ComparisonOperation.NotEqual)69 else if (this.ConstraintOperation == ConstraintOperation.NotEqual) 70 70 result = compareResult != 0; 71 71 else 72 throw new InvalidOperationException("Co mpareOperation " + this.ComparisonOperation + " is not defined for ComparisonConstraint.");72 throw new InvalidOperationException("Constraint operation " + this.ConstraintOperation + " is not defined for ComparisonConstraint."); 73 73 74 return result; 75 } 76 77 protected override bool Check(object constrainedMember, out string errorMessage) { 78 bool result = Check(constrainedMember); 79 errorMessage = string.Empty; 80 if (!result) 81 errorMessage = constrainedMember.ToString() + " must be " + ConstraintOperation.ToString() + " than " + ConstraintData.ToString() + "."; 74 82 return result; 75 83 } -
trunk/sources/HeuristicLab.Core/3.3/Constraints/Constraint.cs
r3601 r3602 24 24 using System.Linq; 25 25 using System.Text; 26 using HeuristicLab.Core;27 26 using HeuristicLab.Common; 28 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 29 28 30 namespace HeuristicLab. Data{29 namespace HeuristicLab.Core { 31 30 [StorableClass] 32 31 public abstract class Constraint : Item, IConstraint { 33 /// <summary>34 /// Protected default constructor for constructor chaining, cloning and persisting of constraints.35 /// </summary>32 protected Constraint() { 33 this.Active = false; 34 } 36 35 [StorableConstructor] 37 protected Constraint() { 38 this.Active = true; 36 protected Constraint(bool deserializing) { 39 37 } 40 41 public Constraint(IItem constrainedValue,ComparisonOperation comparisonOperation, object comparisonValue) 38 protected Constraint(IItem constrainedValue, ConstraintOperation comparisonOperation, object comparisonValue) 42 39 : this() { 43 40 this.ConstrainedValue = constrainedValue; 44 this.ComparisonOperation = comparisonOperation; 45 this.ComparisonValue = comparisonValue; 41 this.ConstraintOperation = comparisonOperation; 42 this.ConstraintData = comparisonValue; 43 } 44 protected Constraint(IItem constrainedValue, ConstraintOperation comparisonOperation, object comparisonValue, bool active) { 45 this.ConstrainedValue = constrainedValue; 46 this.ConstraintOperation = comparisonOperation; 47 this.ConstraintData = comparisonValue; 48 this.Active = active; 46 49 } 47 50 … … 73 76 74 77 [Storable] 75 private object co mparisonValue;76 public object Co mparisonValue{77 get { return this.co mparisonValue; }78 private object constraintData; 79 public object ConstraintData { 80 get { return this.constraintData; } 78 81 set { 79 if (this.co mparisonValue!= value) {80 this.co mparisonValue= value;81 this.OnCo mparisonValueChanged();82 if (this.constraintData != value) { 83 this.constraintData = value; 84 this.OnConstraintDataChanged(); 82 85 this.OnToStringChanged(); 83 86 } … … 85 88 } 86 89 87 public abstract IEnumerable<Co mparisonOperation> AllowedComparisonOperations { get; }90 public abstract IEnumerable<ConstraintOperation> AllowedConstraintOperations { get; } 88 91 [Storable] 89 private Co mparisonOperation comparisonOperation;90 public Co mparisonOperation ComparisonOperation {91 get { return this.co mparisonOperation; }92 private ConstraintOperation constraintOperation; 93 public ConstraintOperation ConstraintOperation { 94 get { return this.constraintOperation; } 92 95 set { 93 96 if (value == null) 94 97 throw new ArgumentNullException("Comparison operation cannot be null."); 95 if (!AllowedCo mparisonOperations.Contains(value))98 if (!AllowedConstraintOperations.Contains(value)) 96 99 throw new ArgumentException("Comparison operation is not contained in the allowed ComparisonOperations."); 97 if (this.co mparisonOperation != value) {98 this.co mparisonOperation = value;99 this.OnCo mparisonOperationChanged();100 if (this.constraintOperation != value) { 101 this.constraintOperation = value; 102 this.OnConstraintOperationChanged(); 100 103 this.OnToStringChanged(); 101 104 } … … 119 122 protected abstract bool Check(object constrainedMember); 120 123 124 public bool Check(out string errorMessage) { 125 errorMessage = string.Empty; 126 if (!Active) 127 return true; 128 129 IItem constrainedMember = this.GetConstrainedMember(); 130 return this.Check(constrainedMember, out errorMessage); 131 } 132 133 protected abstract bool Check(object constrainedMember, out string errorMessage); 134 121 135 #region events 122 136 public event EventHandler ActiveChanged; … … 127 141 } 128 142 129 public event EventHandler Co mparisonValueChanged;130 protected virtual void OnCo mparisonValueChanged() {131 EventHandler handler = Co mparisonValueChanged;143 public event EventHandler ConstraintDataChanged; 144 protected virtual void OnConstraintDataChanged() { 145 EventHandler handler = ConstraintDataChanged; 132 146 if (handler != null) 133 147 ActiveChanged(this, EventArgs.Empty); 134 148 } 135 149 136 public event EventHandler Co mparisonOperationChanged;137 protected virtual void OnCo mparisonOperationChanged() {138 EventHandler handler = Co mparisonOperationChanged;150 public event EventHandler ConstraintOperationChanged; 151 protected virtual void OnConstraintOperationChanged() { 152 EventHandler handler = ConstraintOperationChanged; 139 153 if (handler != null) 140 154 ActiveChanged(this, EventArgs.Empty); … … 151 165 return "Could not determine constraint value."; 152 166 153 s += " " + comparisonOperation.ToString() + " ";167 s += " " + ConstraintOperation.ToString() + " "; 154 168 155 if (co mparisonValue!= null)156 s += co mparisonValue.ToString();169 if (constraintData != null) 170 s += constraintData.ToString(); 157 171 else 158 172 s += "null"; … … 166 180 clone.constrainedValue = (IItem)cloner.Clone(this.constrainedValue); 167 181 168 IItem comparisonItem = this.comparisonValue as IItem; 169 if (comparisonItem != null) 170 clone.comparisonValue = (IItem)cloner.Clone(comparisonItem); 182 IItem constraintDataItem = this.constraintData as IItem; 183 ICloneable constraintDataCloneable = this.constraintData as ICloneable; 184 if (constraintDataItem != null) 185 clone.constraintData = cloner.Clone(constraintDataItem); 186 else if (constraintDataCloneable != null) 187 clone.constraintData = constraintDataCloneable.Clone(); 171 188 else 172 clone.comparisonValue = comparisonValue; 173 clone.comparisonOperation = this.comparisonOperation; 189 clone.constraintData = constraintData; 190 191 clone.constraintOperation = this.constraintOperation; 174 192 175 193 return clone; -
trunk/sources/HeuristicLab.Core/3.3/HeuristicLab.Core-3.3.csproj
r3565 r3602 125 125 <Compile Include="Collections\ValueParameterCollection.cs" /> 126 126 <Compile Include="Collections\VariableCollection.cs" /> 127 <Compile Include="Constraints\ComparisonConstraint.cs" /> 128 <Compile Include="Constraints\Constraint.cs" /> 129 <Compile Include="Collections\ConstraintCollection.cs" /> 130 <Compile Include="Constraints\ConstraintOperation.cs" /> 131 <Compile Include="Constraints\EqualityConstraint.cs" /> 132 <Compile Include="Constraints\IConstraint.cs" /> 133 <Compile Include="Constraints\TypeCompatibilityConstraint.cs" /> 127 134 <Compile Include="Interfaces\ICheckedItemList.cs" /> 128 135 <Compile Include="Interfaces\ICheckedItemCollection.cs" /> -
trunk/sources/HeuristicLab.Data/3.3/HeuristicLab.Data-3.3.csproj
r3592 r3602 105 105 <Compile Include="Comparison.cs" /> 106 106 <Compile Include="ComparisonType.cs" /> 107 <Compile Include="Constraints\ComparisonOperation.cs" />108 <Compile Include="Constraints\TypeConstraint.cs" />109 <Compile Include="Constraints\ComparisonConstraint.cs" />110 <Compile Include="Constraints\Constraint.cs" />111 <Compile Include="Constraints\EqualityConstraint.cs" />112 <Compile Include="Constraints\IConstraint.cs" />113 107 <Compile Include="PercentValue.cs" /> 114 108 <Compile Include="DateTimeValue.cs" />
Note: See TracChangeset
for help on using the changeset viewer.