Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RemoveBackwardsCompatibility/HeuristicLab.Optimization/3.3/RunCollectionConstraints/RunCollectionComparisonConstraint.cs

Last change on this file was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

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