Free cookie consent management tool by TermsFeed Policy Generator

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

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

corrected ctors of core constraints (ticket #996)

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