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("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 | 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 bool Check(object constrainedMember) {
|
---|
75 | if (!Active)
|
---|
76 | return true;
|
---|
77 |
|
---|
78 | foreach (IRun run in ConstrainedValue.Where(r => r.Visible)) {
|
---|
79 | IItem item = ConstrainedValue.GetValue(run, constraintColumn);
|
---|
80 | if (item == null || !base.Check(item))
|
---|
81 | run.Visible = false;
|
---|
82 | }
|
---|
83 | return true;
|
---|
84 | }
|
---|
85 |
|
---|
86 | protected override bool Check(object constrainedMember, out string errorMessage) {
|
---|
87 | errorMessage = string.Empty;
|
---|
88 | if (!Active)
|
---|
89 | return true;
|
---|
90 |
|
---|
91 | foreach (IRun run in ConstrainedValue.Where(r => r.Visible)) {
|
---|
92 | IItem item = ConstrainedValue.GetValue(run, constraintColumn);
|
---|
93 | if (!base.Check(item))
|
---|
94 | run.Visible = false;
|
---|
95 | }
|
---|
96 | return true;
|
---|
97 | }
|
---|
98 |
|
---|
99 | public override string ToString() {
|
---|
100 | string s = string.Empty;
|
---|
101 | IStringConvertibleMatrix matrix = ConstrainedValue;
|
---|
102 | if (matrix != null && matrix.ColumnNames.Count() != 0)
|
---|
103 | s += matrix.ColumnNames.ElementAt(constraintColumn) + " ";
|
---|
104 | else
|
---|
105 | return "TypeCompabitilityConstraint";
|
---|
106 |
|
---|
107 | if (ConstraintOperation != null)
|
---|
108 | s += ConstraintOperation.ToString() + " ";
|
---|
109 |
|
---|
110 | if (ConstraintData != null)
|
---|
111 | s += ConstraintData;
|
---|
112 | else
|
---|
113 | s += "null";
|
---|
114 |
|
---|
115 | return s;
|
---|
116 | }
|
---|
117 |
|
---|
118 | public override IDeepCloneable Clone(HeuristicLab.Common.Cloner cloner) {
|
---|
119 | RunCollectionTypeCompatibilityConstraint clone = (RunCollectionTypeCompatibilityConstraint)base.Clone(cloner);
|
---|
120 | clone.ConstrainedValue = null;
|
---|
121 | clone.ConstraintData = this.ConstraintData;
|
---|
122 | clone.ConstraintOperation = this.ConstraintOperation;
|
---|
123 | clone.constraintColumn = this.constraintColumn;
|
---|
124 |
|
---|
125 | return clone;
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|