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.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Common;
|
---|
30 |
|
---|
31 | namespace 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 string constraintColumn;
|
---|
61 | public string ConstraintColumn {
|
---|
62 | get { return constraintColumn; }
|
---|
63 | set {
|
---|
64 | if (!((IStringConvertibleMatrix)ConstrainedValue).ColumnNames.Contains(value))
|
---|
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 void OnConstrainedValueChanged() {
|
---|
82 | base.OnConstrainedValueChanged();
|
---|
83 | IStringConvertibleMatrix matrix = (IStringConvertibleMatrix)ConstrainedValue;
|
---|
84 | if (constraintColumn == null && ConstrainedValue != null && matrix.Columns != 0)
|
---|
85 | constraintColumn = matrix.ColumnNames.ElementAt(0);
|
---|
86 | }
|
---|
87 |
|
---|
88 | protected override bool Check(object constrainedMember) {
|
---|
89 | if (!Active)
|
---|
90 | return true;
|
---|
91 |
|
---|
92 | foreach (IRun run in ConstrainedValue.Where(r => r.Visible)) {
|
---|
93 | IItem item = ConstrainedValue.GetValue(run, constraintColumn);
|
---|
94 | if (!base.Check(item.ToString()))
|
---|
95 | run.Visible = false;
|
---|
96 | }
|
---|
97 | return true;
|
---|
98 | }
|
---|
99 |
|
---|
100 | protected override bool Check(object constrainedMember, out string errorMessage) {
|
---|
101 | errorMessage = string.Empty;
|
---|
102 | if (!Active)
|
---|
103 | return true;
|
---|
104 |
|
---|
105 | foreach (IRun run in ConstrainedValue.Where(r => r.Visible)) {
|
---|
106 | IItem item = ConstrainedValue.GetValue(run, constraintColumn);
|
---|
107 | if (!base.Check(item))
|
---|
108 | run.Visible = false;
|
---|
109 | }
|
---|
110 | return true;
|
---|
111 | }
|
---|
112 |
|
---|
113 | public override string ToString() {
|
---|
114 | string s = string.Empty;
|
---|
115 | IStringConvertibleMatrix matrix = ConstrainedValue;
|
---|
116 | if (matrix != null && matrix.ColumnNames.Count() != 0)
|
---|
117 | s += constraintColumn + " ";
|
---|
118 | else
|
---|
119 | return "EqualityConstraint";
|
---|
120 |
|
---|
121 | if (ConstraintOperation != null)
|
---|
122 | s += ConstraintOperation.ToString() + " ";
|
---|
123 |
|
---|
124 | if (!string.IsNullOrEmpty(ConstraintData))
|
---|
125 | s += ConstraintData;
|
---|
126 | else
|
---|
127 | s += "null";
|
---|
128 |
|
---|
129 | return s;
|
---|
130 | }
|
---|
131 |
|
---|
132 | public override IDeepCloneable Clone(HeuristicLab.Common.Cloner cloner) {
|
---|
133 | RunCollectionEqualityConstraint clone = (RunCollectionEqualityConstraint)base.Clone(cloner);
|
---|
134 | clone.ConstrainedValue = null;
|
---|
135 | clone.ConstraintData = this.ConstraintData;
|
---|
136 | clone.ConstraintOperation = this.ConstraintOperation;
|
---|
137 | clone.constraintColumn = this.constraintColumn;
|
---|
138 |
|
---|
139 | return clone;
|
---|
140 | }
|
---|
141 | }
|
---|
142 | }
|
---|