Free cookie consent management tool by TermsFeed Policy Generator

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

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

sorting of column names in RunCollection and fixed errors in RunCollectionConstraints (ticket #970)

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.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 string constraintColumn;
64    public string ConstraintColumn {
65      get { return constraintColumn; }
66      set {
67        if (!((IStringConvertibleMatrix)ConstrainedValue).ColumnNames.Contains(value))
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 void OnConstrainedValueChanged() {
85      base.OnConstrainedValueChanged();
86      IStringConvertibleMatrix matrix = (IStringConvertibleMatrix)ConstrainedValue;
87      if (constraintColumn == null && ConstrainedValue != null && matrix.Columns != 0)
88        constraintColumn = matrix.ColumnNames.ElementAt(0);
89    }
90
91    protected override bool Check(object constrainedMember) {
92      if (!Active)
93        return true;
94
95      foreach (IRun run in ConstrainedValue.Where(r => r.Visible)) {
96        IItem item = ConstrainedValue.GetValue(run, constraintColumn);
97        if (!base.Check(item))
98          run.Visible = false;
99      }
100      return true;
101    }
102
103    protected override bool Check(object constrainedMember, out string errorMessage) {
104      errorMessage = string.Empty;
105      if (!Active)
106        return true;
107
108      foreach (IRun run in ConstrainedValue.Where(r => r.Visible)) {
109        IItem item = ConstrainedValue.GetValue(run, constraintColumn);
110        if (!base.Check(item))
111          run.Visible = false;
112      }
113      return true;
114    }
115
116    public override string ToString() {
117      string s = string.Empty;
118      IStringConvertibleMatrix matrix = ConstrainedValue;
119      if (matrix != null && matrix.ColumnNames.Count() != 0)
120        s += constraintColumn + " ";
121      else
122        return "ComparisonConstraint";
123
124      if (ConstraintOperation != null)
125        s += ConstraintOperation.ToString() + " ";
126
127      if (ConstraintData != null)
128        s += ConstraintData.GetValue();
129      else
130        s += "null";
131
132      return s;
133    }
134
135    public override IDeepCloneable Clone(HeuristicLab.Common.Cloner cloner) {
136      RunCollectionComparisonConstraint clone = (RunCollectionComparisonConstraint)base.Clone(cloner);
137      clone.ConstrainedValue = null;
138
139      IItem constraintDataItem = this.ConstraintData as IItem;
140      ICloneable constraintDataCloneable = this.ConstraintData as ICloneable;
141      if (constraintDataItem != null)
142        clone.ConstraintData = (IStringConvertibleValue)cloner.Clone(constraintDataItem);
143      else if (constraintDataCloneable != null)
144        clone.ConstraintData = (IStringConvertibleValue)constraintDataCloneable.Clone();
145      else
146        clone.ConstraintData = this.ConstraintData;
147
148      clone.ConstraintOperation = this.ConstraintOperation;
149      clone.constraintColumn = this.constraintColumn;
150
151      return clone;
152    }
153  }
154}
Note: See TracBrowser for help on using the repository browser.