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