Free cookie consent management tool by TermsFeed Policy Generator

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

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

Location:
trunk/sources/HeuristicLab.Core/3.3/Constraints
Files:
1 added
1 copied

Legend:

Unmodified
Added
Removed
  • 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;
Note: See TracChangeset for help on using the changeset viewer.