Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/Constraints/Constraint.cs @ 4345

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

Removed cloning the active flag of Constraints (ticket #1143).

File size: 6.9 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 HeuristicLab.Common;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Core {
29  [StorableClass]
30  public abstract class Constraint : Item, IConstraint {
31    [StorableConstructor]
32    protected Constraint(bool deserializing) : base(deserializing) { }
33
34    protected Constraint() {
35      this.Active = false;
36      if (AllowedConstraintOperations != null && AllowedConstraintOperations.Count() != 0)
37        this.ConstraintOperation = AllowedConstraintOperations.ElementAt(0);
38    }
39    protected Constraint(IItem constrainedValue, ConstraintOperation constraintOperation, object constraintData)
40      : this() {
41      this.ConstrainedValue = constrainedValue;
42      this.ConstraintOperation = constraintOperation;
43      this.ConstraintData = constraintData;
44    }
45    protected Constraint(IItem constrainedValue, ConstraintOperation constraintOperation, object constraintData, bool active) {
46      this.ConstrainedValue = constrainedValue;
47      this.ConstraintOperation = constraintOperation;
48      this.ConstraintData = constraintData;
49      this.Active = active;
50    }
51
52    [Storable]
53    private bool active;
54    public bool Active {
55      get { return this.active; }
56      set {
57        if (this.active != value) {
58          this.active = value;
59          this.OnActiveChanged();
60        }
61      }
62    }
63
64    [Storable]
65    private IItem constrainedValue;
66    public IItem ConstrainedValue {
67      get { return this.constrainedValue; }
68      set {
69        if (value == null)
70          throw new ArgumentNullException("Constraint value cannot be null.");
71        if (this.constrainedValue != value) {
72          this.constrainedValue = value;
73          this.OnConstrainedValueChanged();
74          this.OnToStringChanged();
75        }
76      }
77    }
78
79    [Storable]
80    private object constraintData;
81    public object ConstraintData {
82      get { return this.constraintData; }
83      set {
84        if (this.constraintData != value) {
85          this.constraintData = value;
86          this.OnConstraintDataChanged();
87          this.OnToStringChanged();
88        }
89      }
90    }
91
92    public abstract IEnumerable<ConstraintOperation> AllowedConstraintOperations { get; }
93    [Storable]
94    private ConstraintOperation constraintOperation;
95    public ConstraintOperation ConstraintOperation {
96      get { return this.constraintOperation; }
97      set {
98        if (value == null)
99          throw new ArgumentNullException("Comparison operation cannot be null.");
100        if (!AllowedConstraintOperations.Contains(value))
101          throw new ArgumentException("Comparison operation is not contained in the allowed ComparisonOperations.");
102        if (this.constraintOperation != value) {
103          this.constraintOperation = value;
104          this.OnConstraintOperationChanged();
105          this.OnToStringChanged();
106        }
107      }
108    }
109
110    /// <summary>
111    /// This method is called to determine which member of the constrained value should be compared.
112    /// </summary>
113    /// <returns></returns>
114    protected virtual IItem GetConstrainedMember() {
115      return this.constrainedValue;
116    }
117    protected abstract bool Check(object constrainedMember);
118    protected abstract bool Check(object constrainedMember, out string errorMessage);
119
120    public bool Check() {
121      if (!Active)
122        return true;
123
124      IItem constrainedMember = this.GetConstrainedMember();
125      return this.Check(constrainedMember);
126    }
127    public bool Check(out string errorMessage) {
128      errorMessage = string.Empty;
129      if (!Active)
130        return true;
131
132      IItem constrainedMember = this.GetConstrainedMember();
133      return this.Check(constrainedMember, out errorMessage);
134    }
135
136    #region events
137    public event EventHandler ActiveChanged;
138    protected virtual void OnActiveChanged() {
139      EventHandler handler = ActiveChanged;
140      if (handler != null)
141        handler(this, EventArgs.Empty);
142    }
143
144    public event EventHandler ConstrainedValueChanged;
145    protected virtual void OnConstrainedValueChanged() {
146      EventHandler handler = ConstrainedValueChanged;
147      if (handler != null)
148        handler(this, EventArgs.Empty);
149    }
150
151    public event EventHandler ConstraintDataChanged;
152    protected virtual void OnConstraintDataChanged() {
153      EventHandler handler = ConstraintDataChanged;
154      if (handler != null)
155        handler(this, EventArgs.Empty);
156    }
157
158    public event EventHandler ConstraintOperationChanged;
159    protected virtual void OnConstraintOperationChanged() {
160      EventHandler handler = ConstraintOperationChanged;
161      if (handler != null)
162        handler(this, EventArgs.Empty);
163    }
164    #endregion
165
166    #region overriden item methods
167    public override string ToString() {
168      IItem constrainedMember = GetConstrainedMember();
169      string s = string.Empty;
170      if (constrainedMember != null)
171        s += constrainedMember.ToString() + " ";
172
173      if (constraintOperation != null)
174        s += ConstraintOperation.ToString() + " ";
175
176      if (constraintData != null)
177        s += constraintData.ToString();
178      else
179        s += "null";
180
181      s += ".";
182      return s;
183    }
184
185    public override IDeepCloneable Clone(HeuristicLab.Common.Cloner cloner) {
186      Constraint clone = (Constraint)base.Clone(cloner);
187      clone.constrainedValue = null;  //mkommend: intentionally set to null;
188
189      IItem constraintDataItem = this.constraintData as IItem;
190      ICloneable constraintDataCloneable = this.constraintData as ICloneable;
191      if (constraintDataItem != null)
192        clone.constraintData = cloner.Clone(constraintDataItem);
193      else if (constraintDataCloneable != null)
194        clone.constraintData = constraintDataCloneable.Clone();
195      else
196        clone.constraintData = constraintData;
197
198      clone.constraintOperation = this.constraintOperation;
199
200      return clone;
201    }
202    #endregion
203  }
204}
Note: See TracBrowser for help on using the repository browser.