Free cookie consent management tool by TermsFeed Policy Generator

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

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

refactored cloning of constraints (ticket #996)

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