Free cookie consent management tool by TermsFeed Policy Generator

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

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

refactored cloning of constraints (ticket #996)

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