Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Core/3.3/Constraints/Constraint.cs @ 12679

Last change on this file since 12679 was 12009, checked in by ascheibe, 10 years ago

#2212 updated copyright year

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