Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization/3.3/RunCollectionConstraints/RunCollectionComparisonConstraint.cs @ 4157

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

fixed cloning of RunCollectionConstraints (ticket #1131)

File size: 5.6 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.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Optimization {
30  [StorableClass]
31  [Item("RunCollectionComparisonConstraint", "A constraint which compares the members of the contained runs with the constraint data.")]
32  public class RunCollectionComparisonConstraint : ComparisonConstraint, IRunCollectionConstraint {
33    [StorableConstructor]
34    protected RunCollectionComparisonConstraint(bool deserializing) : base(deserializing) { }
35
36    public RunCollectionComparisonConstraint() : base() { }
37    public RunCollectionComparisonConstraint(RunCollection constrainedValue, ConstraintOperation constraintOperation, object constraintData)
38      : base(constrainedValue, constraintOperation, constraintData) {
39    }
40    public RunCollectionComparisonConstraint(RunCollection constrainedValue, ConstraintOperation constraintOperation, object constraintData, bool active)
41      : base(constrainedValue, constraintOperation, constraintData, active) {
42    }
43
44    public new RunCollection ConstrainedValue {
45      get { return (RunCollection)base.ConstrainedValue; }
46      set { base.ConstrainedValue = value; }
47    }
48
49    public new IStringConvertibleValue ConstraintData {
50      get { return (IStringConvertibleValue)base.ConstraintData; }
51      set {
52        if (!(value is IComparable))
53          throw new ArgumentException("Only IComparables allowed for ConstraintData");
54        base.ConstraintData = value;
55      }
56    }
57
58    [Storable]
59    private string constraintColumn;
60    public string ConstraintColumn {
61      get { return constraintColumn; }
62      set {
63        if (!((IStringConvertibleMatrix)ConstrainedValue).ColumnNames.Contains(value))
64          throw new ArgumentException("Could not set ConstraintData to not existing column index.");
65        if (constraintColumn != value) {
66          constraintColumn = value;
67          this.OnConstraintColumnChanged();
68          this.OnToStringChanged();
69        }
70      }
71    }
72
73    public event EventHandler ConstraintColumnChanged;
74    protected virtual void OnConstraintColumnChanged() {
75      EventHandler handler = ConstraintColumnChanged;
76      if (handler != null)
77        handler(this, EventArgs.Empty);
78    }
79
80    protected override void OnConstrainedValueChanged() {
81      base.OnConstrainedValueChanged();
82      IStringConvertibleMatrix matrix = (IStringConvertibleMatrix)ConstrainedValue;
83      if (constraintColumn == null && ConstrainedValue != null && matrix.Columns != 0)
84        constraintColumn = matrix.ColumnNames.ElementAt(0);
85    }
86
87    protected override bool Check(object constrainedMember) {
88      if (!Active)
89        return true;
90
91      foreach (IRun run in ConstrainedValue.Where(r => r.Visible)) {
92        IItem item = ConstrainedValue.GetValue(run, constraintColumn);
93        if (!base.Check(item))
94          run.Visible = false;
95      }
96      return true;
97    }
98
99    protected override bool Check(object constrainedMember, out string errorMessage) {
100      errorMessage = string.Empty;
101      if (!Active)
102        return true;
103
104      foreach (IRun run in ConstrainedValue.Where(r => r.Visible)) {
105        IItem item = ConstrainedValue.GetValue(run, constraintColumn);
106        if (!base.Check(item))
107          run.Visible = false;
108      }
109      return true;
110    }
111
112    public override string ToString() {
113      string s = string.Empty;
114      IStringConvertibleMatrix matrix = ConstrainedValue;
115      if (matrix != null && matrix.ColumnNames.Count() != 0)
116        s += constraintColumn + " ";
117      else
118        return "ComparisonConstraint";
119
120      if (ConstraintOperation != null)
121        s += ConstraintOperation.ToString() + " ";
122
123      if (ConstraintData != null)
124        s += ConstraintData.GetValue();
125      else
126        s += "null";
127
128      return s;
129    }
130
131    public override IDeepCloneable Clone(HeuristicLab.Common.Cloner cloner) {
132      RunCollectionComparisonConstraint clone = (RunCollectionComparisonConstraint)base.Clone(cloner);
133
134      IItem constraintDataItem = this.ConstraintData as IItem;
135      ICloneable constraintDataCloneable = this.ConstraintData as ICloneable;
136      if (constraintDataItem != null)
137        clone.ConstraintData = (IStringConvertibleValue)cloner.Clone(constraintDataItem);
138      else if (constraintDataCloneable != null)
139        clone.ConstraintData = (IStringConvertibleValue)constraintDataCloneable.Clone();
140      else
141        clone.ConstraintData = this.ConstraintData;
142
143      clone.ConstraintOperation = this.ConstraintOperation;
144      clone.constraintColumn = this.constraintColumn;
145
146      return clone;
147    }
148  }
149}
Note: See TracBrowser for help on using the repository browser.