Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization/3.3/RunCollectionConstraints/RunCollectionEqualityConstraint.cs @ 3634

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

refactored cloning of constraints (ticket #996)

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