Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/3.3/Constraints/Constraint.cs @ 3598

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

changed ctors of constraints (ticket #996)

File size: 5.7 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.Core;
27using HeuristicLab.Common;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Data {
31  [StorableClass]
32  public abstract class Constraint : Item, IConstraint {
33    /// <summary>
34    /// Protected default constructor for constructor chaining, cloning and persisting of constraints.
35    /// </summary>
36    [StorableConstructor]
37    protected Constraint() {
38      this.Active = true;
39    }
40
41    public Constraint(IItem constrainedValue,ComparisonOperation comparisonOperation, object comparisonValue)
42      : this() {
43      this.ConstrainedValue = constrainedValue;
44      this.ComparisonOperation = comparisonOperation;
45      this.ComparisonValue = comparisonValue;
46    }
47
48    [Storable]
49    private bool active;
50    public bool Active {
51      get { return this.active; }
52      set {
53        if (this.active != value) {
54          this.active = value;
55          this.OnActiveChanged();
56        }
57      }
58    }
59
60    [Storable]
61    private IItem constrainedValue;
62    public IItem ConstrainedValue {
63      get { return this.constrainedValue; }
64      protected set {
65        if (value == null)
66          throw new ArgumentNullException("Constraint value cannot be null.");
67        if (this.constrainedValue != value) {
68          this.constrainedValue = value;
69          this.OnToStringChanged();
70        }
71      }
72    }
73
74    [Storable]
75    private object comparisonValue;
76    public object ComparisonValue {
77      get { return this.comparisonValue; }
78      set {
79        if (this.comparisonValue != value) {
80          this.comparisonValue = value;
81          this.OnComparisonValueChanged();
82          this.OnToStringChanged();
83        }
84      }
85    }
86
87    public abstract IEnumerable<ComparisonOperation> AllowedComparisonOperations { get; }
88    [Storable]
89    private ComparisonOperation comparisonOperation;
90    public ComparisonOperation ComparisonOperation {
91      get { return this.comparisonOperation; }
92      set {
93        if (value == null)
94          throw new ArgumentNullException("Comparison operation cannot be null.");
95        if (!AllowedComparisonOperations.Contains(value))
96          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          this.OnToStringChanged();
101        }
102      }
103    }
104
105    /// <summary>
106    /// This method is called to determine which member of the constrained value should be compared.
107    /// </summary>
108    /// <returns></returns>
109    protected virtual IItem GetConstrainedMember() {
110      return this.constrainedValue;
111    }
112    public bool Check() {
113      if (!Active)
114        return true;
115
116      IItem constrainedMember = this.GetConstrainedMember();
117      return this.Check(constrainedMember);
118    }
119    protected abstract bool Check(object constrainedMember);
120
121    #region events
122    public event EventHandler ActiveChanged;
123    protected virtual void OnActiveChanged() {
124      EventHandler handler = ActiveChanged;
125      if (handler != null)
126        ActiveChanged(this, EventArgs.Empty);
127    }
128
129    public event EventHandler ComparisonValueChanged;
130    protected virtual void OnComparisonValueChanged() {
131      EventHandler handler = ComparisonValueChanged;
132      if (handler != null)
133        ActiveChanged(this, EventArgs.Empty);
134    }
135
136    public event EventHandler ComparisonOperationChanged;
137    protected virtual void OnComparisonOperationChanged() {
138      EventHandler handler = ComparisonOperationChanged;
139      if (handler != null)
140        ActiveChanged(this, EventArgs.Empty);
141    }
142    #endregion
143
144    #region overriden item methods
145    public override string ToString() {
146      IItem constrainedValue = GetConstrainedMember();
147      string s = string.Empty;
148      if (constrainedValue != null)
149        s += constrainedValue.ToString();
150      else
151        return "Could not determine constraint value.";
152
153      s += " " + comparisonOperation.ToString() + " ";
154
155      if (comparisonValue != null)
156        s += comparisonValue.ToString();
157      else
158        s += "null";
159
160      s += ".";
161      return s;
162    }
163
164    public override IDeepCloneable Clone(HeuristicLab.Common.Cloner cloner) {
165      Constraint clone = (Constraint)base.Clone(cloner);
166      clone.constrainedValue = (IItem)cloner.Clone(this.constrainedValue);
167
168      IItem comparisonItem = this.comparisonValue as IItem;
169      if (comparisonItem != null)
170        clone.comparisonValue = (IItem)cloner.Clone(comparisonItem);
171      else
172        clone.comparisonValue = comparisonValue;
173      clone.comparisonOperation = this.comparisonOperation;
174
175      return clone;
176    }
177    #endregion
178  }
179}
Note: See TracBrowser for help on using the repository browser.