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