Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization/3.3/RunCollectionConstraints/RunCollectionTypeCompatiblityConstraint.cs @ 4157

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

fixed cloning of RunCollectionConstraints (ticket #1131)

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