Changeset 3602


Ignore:
Timestamp:
05/04/10 13:38:35 (12 years ago)
Author:
mkommend
Message:

implemented comments by swa and moved constraints from data to core (ticket #996)

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  
    2424using System.Linq;
    2525using System.Text;
    26 using HeuristicLab.Core;
    2726using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2827
    29 namespace HeuristicLab.Data {
     28namespace HeuristicLab.Core {
    3029  [StorableClass]
    3130  [Item("ComparisonConstraint", "A constraint which checks for specified compare operation.")]
    3231  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()
    3833      : base() {
    3934    }
    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) {
    4643    }
    4744
    48     public override IEnumerable<ComparisonOperation> 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 }; }
    5047    }
    5148
    5249    protected override bool Check(object constrainedMember) {
     50      if (constrainedMember == null)
     51        return false;
     52
    5353      IComparable comparableMember = constrainedMember as IComparable;
    5454      if (comparableMember == null)
    5555        throw new InvalidOperationException("Constrained member must be of type IComparable to be used with ComparisonConstraint.");
    5656
    57       int compareResult = comparableMember.CompareTo(this.ComparisonValue);
     57      int compareResult = comparableMember.CompareTo(this.ConstraintData);
    5858      bool result = false;
    59       if (this.ComparisonOperation == ComparisonOperation.Lesser)
     59      if (this.ConstraintOperation == ConstraintOperation.Lesser)
    6060        result = compareResult < 0;
    61       else if (this.ComparisonOperation == ComparisonOperation.LesserOrEqual)
     61      else if (this.ConstraintOperation == ConstraintOperation.LesserOrEqual)
    6262        result = compareResult <= 0;
    63       else if (this.ComparisonOperation == ComparisonOperation.Equal)
     63      else if (this.ConstraintOperation == ConstraintOperation.Equal)
    6464        result = compareResult == 0;
    65       else if (this.ComparisonOperation == ComparisonOperation.GreaterOrEqual)
     65      else if (this.ConstraintOperation == ConstraintOperation.GreaterOrEqual)
    6666        result = compareResult >= 0;
    67       else if (this.ComparisonOperation == ComparisonOperation.Greater)
     67      else if (this.ConstraintOperation == ConstraintOperation.Greater)
    6868        result = compareResult > 0;
    69       else if (this.ComparisonOperation == ComparisonOperation.NotEqual)
     69      else if (this.ConstraintOperation == ConstraintOperation.NotEqual)
    7070        result = compareResult != 0;
    7171      else
    72         throw new InvalidOperationException("CompareOperation " + this.ComparisonOperation + " is not defined for ComparisonConstraint.");
     72        throw new InvalidOperationException("Constraint operation " + this.ConstraintOperation + " is not defined for ComparisonConstraint.");
    7373
     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() + ".";
    7482      return result;
    7583    }
  • trunk/sources/HeuristicLab.Core/3.3/Constraints/Constraint.cs

    r3601 r3602  
    2424using System.Linq;
    2525using System.Text;
    26 using HeuristicLab.Core;
    2726using HeuristicLab.Common;
    2827using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2928
    30 namespace HeuristicLab.Data {
     29namespace HeuristicLab.Core {
    3130  [StorableClass]
    3231  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    }
    3635    [StorableConstructor]
    37     protected Constraint() {
    38       this.Active = true;
     36    protected Constraint(bool deserializing) {
    3937    }
    40 
    41     public Constraint(IItem constrainedValue,ComparisonOperation comparisonOperation, object comparisonValue)
     38    protected Constraint(IItem constrainedValue, ConstraintOperation comparisonOperation, object comparisonValue)
    4239      : this() {
    4340      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;
    4649    }
    4750
     
    7376
    7477    [Storable]
    75     private object comparisonValue;
    76     public object ComparisonValue {
    77       get { return this.comparisonValue; }
     78    private object constraintData;
     79    public object ConstraintData {
     80      get { return this.constraintData; }
    7881      set {
    79         if (this.comparisonValue != value) {
    80           this.comparisonValue = value;
    81           this.OnComparisonValueChanged();
     82        if (this.constraintData != value) {
     83          this.constraintData = value;
     84          this.OnConstraintDataChanged();
    8285          this.OnToStringChanged();
    8386        }
     
    8588    }
    8689
    87     public abstract IEnumerable<ComparisonOperation> AllowedComparisonOperations { get; }
     90    public abstract IEnumerable<ConstraintOperation> AllowedConstraintOperations { get; }
    8891    [Storable]
    89     private ComparisonOperation comparisonOperation;
    90     public ComparisonOperation ComparisonOperation {
    91       get { return this.comparisonOperation; }
     92    private ConstraintOperation constraintOperation;
     93    public ConstraintOperation ConstraintOperation {
     94      get { return this.constraintOperation; }
    9295      set {
    9396        if (value == null)
    9497          throw new ArgumentNullException("Comparison operation cannot be null.");
    95         if (!AllowedComparisonOperations.Contains(value))
     98        if (!AllowedConstraintOperations.Contains(value))
    9699          throw new ArgumentException("Comparison operation is not contained in the allowed ComparisonOperations.");
    97         if (this.comparisonOperation != value) {
    98           this.comparisonOperation = value;
    99           this.OnComparisonOperationChanged();
     100        if (this.constraintOperation != value) {
     101          this.constraintOperation = value;
     102          this.OnConstraintOperationChanged();
    100103          this.OnToStringChanged();
    101104        }
     
    119122    protected abstract bool Check(object constrainedMember);
    120123
     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
    121135    #region events
    122136    public event EventHandler ActiveChanged;
     
    127141    }
    128142
    129     public event EventHandler ComparisonValueChanged;
    130     protected virtual void OnComparisonValueChanged() {
    131       EventHandler handler = ComparisonValueChanged;
     143    public event EventHandler ConstraintDataChanged;
     144    protected virtual void OnConstraintDataChanged() {
     145      EventHandler handler = ConstraintDataChanged;
    132146      if (handler != null)
    133147        ActiveChanged(this, EventArgs.Empty);
    134148    }
    135149
    136     public event EventHandler ComparisonOperationChanged;
    137     protected virtual void OnComparisonOperationChanged() {
    138       EventHandler handler = ComparisonOperationChanged;
     150    public event EventHandler ConstraintOperationChanged;
     151    protected virtual void OnConstraintOperationChanged() {
     152      EventHandler handler = ConstraintOperationChanged;
    139153      if (handler != null)
    140154        ActiveChanged(this, EventArgs.Empty);
     
    151165        return "Could not determine constraint value.";
    152166
    153       s += " " + comparisonOperation.ToString() + " ";
     167      s += " " + ConstraintOperation.ToString() + " ";
    154168
    155       if (comparisonValue != null)
    156         s += comparisonValue.ToString();
     169      if (constraintData != null)
     170        s += constraintData.ToString();
    157171      else
    158172        s += "null";
     
    166180      clone.constrainedValue = (IItem)cloner.Clone(this.constrainedValue);
    167181
    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();
    171188      else
    172         clone.comparisonValue = comparisonValue;
    173       clone.comparisonOperation = this.comparisonOperation;
     189        clone.constraintData = constraintData;
     190
     191      clone.constraintOperation = this.constraintOperation;
    174192
    175193      return clone;
  • trunk/sources/HeuristicLab.Core/3.3/HeuristicLab.Core-3.3.csproj

    r3565 r3602  
    125125    <Compile Include="Collections\ValueParameterCollection.cs" />
    126126    <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" />
    127134    <Compile Include="Interfaces\ICheckedItemList.cs" />
    128135    <Compile Include="Interfaces\ICheckedItemCollection.cs" />
  • trunk/sources/HeuristicLab.Data/3.3/HeuristicLab.Data-3.3.csproj

    r3592 r3602  
    105105    <Compile Include="Comparison.cs" />
    106106    <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" />
    113107    <Compile Include="PercentValue.cs" />
    114108    <Compile Include="DateTimeValue.cs" />
Note: See TracChangeset for help on using the changeset viewer.