Free cookie consent management tool by TermsFeed Policy Generator

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

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

corrected RunCollectionConstraints and the according views (ticket#970)

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          this.OnToStringChanged();
73        }
74      }
75    }
76
77    public event EventHandler ConstraintColumnChanged;
78    protected virtual void OnConstraintColumnChanged() {
79      EventHandler handler = ConstraintColumnChanged;
80      if (handler != null)
81        handler(this, EventArgs.Empty);
82    }
83
84    protected override bool Check(object constrainedMember) {
85      if (!Active)
86        return true;
87
88      foreach (IRun run in ConstrainedValue.Where(r => r.Visible)) {
89        IItem item = ConstrainedValue.GetValue(run, constraintColumn);
90        if (!base.Check(item))
91          run.Visible = false;
92      }
93      return true;
94    }
95
96    protected override bool Check(object constrainedMember, out string errorMessage) {
97      errorMessage = string.Empty;
98      if (!Active)
99        return true;
100
101      foreach (IRun run in ConstrainedValue.Where(r => r.Visible)) {
102        IItem item = ConstrainedValue.GetValue(run, constraintColumn);
103        if (!base.Check(item))
104          run.Visible = false;
105      }
106      return true;
107    }
108
109    public override string ToString() {
110      string s = string.Empty;
111      IStringConvertibleMatrix matrix = ConstrainedValue;
112      if (matrix != null && matrix.ColumnNames.Count() != 0)
113        s += matrix.ColumnNames.ElementAt(constraintColumn) + " ";
114      else
115        return "ComparisonConstraint";
116
117      if (ConstraintOperation != null)
118        s += ConstraintOperation.ToString() + " ";
119
120      if (ConstraintData != null)
121        s += ConstraintData.GetValue();
122      else
123        s += "null";
124
125      return s;
126    }
127
128    public override IDeepCloneable Clone(HeuristicLab.Common.Cloner cloner) {
129      RunCollectionComparisonConstraint clone = (RunCollectionComparisonConstraint)base.Clone(cloner);
130      clone.ConstrainedValue = null;
131
132      IItem constraintDataItem = this.ConstraintData as IItem;
133      ICloneable constraintDataCloneable = this.ConstraintData as ICloneable;
134      if (constraintDataItem != null)
135        clone.ConstraintData = (IStringConvertibleValue)cloner.Clone(constraintDataItem);
136      else if (constraintDataCloneable != null)
137        clone.ConstraintData = (IStringConvertibleValue)constraintDataCloneable.Clone();
138      else
139        clone.ConstraintData = this.ConstraintData;
140
141      clone.ConstraintOperation = this.ConstraintOperation;
142      clone.constraintColumn = this.constraintColumn;
143
144      return clone;
145    }
146  }
147}
Note: See TracBrowser for help on using the repository browser.