Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization/3.3/RunCollectionConstraints/RunCollectionTypeCompatiblityConstraint.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.0 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 string constraintColumn;
54    public string ConstraintColumn {
55      get { return constraintColumn; }
56      set {
57        if (!((IStringConvertibleMatrix)ConstrainedValue).ColumnNames.Contains(value))
58          throw new ArgumentException("Could not set ConstraintData to not existing column index.");
59        if (constraintColumn != value) {
60          constraintColumn = value;
61          this.OnConstraintColumnChanged();
62          this.OnToStringChanged();
63        }
64      }
65    }
66
67    public event EventHandler ConstraintColumnChanged;
68    protected virtual void OnConstraintColumnChanged() {
69      EventHandler handler = ConstraintColumnChanged;
70      if (handler != null)
71        handler(this, EventArgs.Empty);
72    }
73
74    protected override void OnConstrainedValueChanged() {
75      base.OnConstrainedValueChanged();
76      IStringConvertibleMatrix matrix = (IStringConvertibleMatrix)ConstrainedValue;
77      if (constraintColumn == null && ConstrainedValue != null && matrix.Columns != 0)
78        constraintColumn = matrix.ColumnNames.ElementAt(0);
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 (item == null || !base.Check(item))
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 += constraintColumn + " ";
111      else
112        return "TypeCompabitilityConstraint";
113
114      if (ConstraintOperation != null)
115        s += ConstraintOperation.ToString() + " ";
116
117      if (ConstraintData != null)
118        s += ConstraintData;
119      else
120        s += "null";
121
122      return s;
123    }
124
125    public override IDeepCloneable Clone(HeuristicLab.Common.Cloner cloner) {
126      RunCollectionTypeCompatibilityConstraint clone = (RunCollectionTypeCompatibilityConstraint)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.