[3592] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3592] | 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 HeuristicLab.Common;
|
---|
[16565] | 26 | using HEAL.Attic;
|
---|
[3592] | 27 |
|
---|
[3602] | 28 | namespace HeuristicLab.Core {
|
---|
[16565] | 29 | [StorableType("02F8AFE2-BF67-4378-9E38-B18FF4D63609")]
|
---|
[3592] | 30 | public abstract class Constraint : Item, IConstraint {
|
---|
[4153] | 31 | [StorableConstructor]
|
---|
[16565] | 32 | protected Constraint(StorableConstructorFlag _) : base(_) { }
|
---|
[4722] | 33 | protected Constraint(Constraint original, Cloner cloner)
|
---|
| 34 | : base(original, cloner) {
|
---|
| 35 | constrainedValue = null; //mkommend: intentionally set to null;
|
---|
[4153] | 36 |
|
---|
[4722] | 37 | IDeepCloneable constraintDataDeepCloneable = original.constraintData as IDeepCloneable;
|
---|
| 38 | ICloneable constraintDataCloneable = original.constraintData as ICloneable;
|
---|
| 39 | if (constraintDataDeepCloneable != null)
|
---|
| 40 | constraintData = cloner.Clone(constraintDataDeepCloneable);
|
---|
| 41 | else if (constraintDataCloneable != null)
|
---|
| 42 | constraintData = constraintDataCloneable.Clone();
|
---|
| 43 | else
|
---|
| 44 | constraintData = original.constraintData;
|
---|
| 45 |
|
---|
| 46 | constraintOperation = original.constraintOperation;
|
---|
| 47 | }
|
---|
[3592] | 48 | protected Constraint() {
|
---|
[3602] | 49 | this.Active = false;
|
---|
[6681] | 50 | if (AllowedConstraintOperations != null && AllowedConstraintOperations.Any())
|
---|
[3613] | 51 | this.ConstraintOperation = AllowedConstraintOperations.ElementAt(0);
|
---|
[3592] | 52 | }
|
---|
[3605] | 53 | protected Constraint(IItem constrainedValue, ConstraintOperation constraintOperation, object constraintData)
|
---|
[3592] | 54 | : this() {
|
---|
| 55 | this.ConstrainedValue = constrainedValue;
|
---|
[3605] | 56 | this.ConstraintOperation = constraintOperation;
|
---|
| 57 | this.ConstraintData = constraintData;
|
---|
[3592] | 58 | }
|
---|
[3605] | 59 | protected Constraint(IItem constrainedValue, ConstraintOperation constraintOperation, object constraintData, bool active) {
|
---|
[3602] | 60 | this.ConstrainedValue = constrainedValue;
|
---|
[3605] | 61 | this.ConstraintOperation = constraintOperation;
|
---|
| 62 | this.ConstraintData = constraintData;
|
---|
[3602] | 63 | this.Active = active;
|
---|
| 64 | }
|
---|
[3592] | 65 |
|
---|
| 66 | [Storable]
|
---|
| 67 | private bool active;
|
---|
| 68 | public bool Active {
|
---|
| 69 | get { return this.active; }
|
---|
| 70 | set {
|
---|
| 71 | if (this.active != value) {
|
---|
| 72 | this.active = value;
|
---|
| 73 | this.OnActiveChanged();
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | [Storable]
|
---|
| 79 | private IItem constrainedValue;
|
---|
| 80 | public IItem ConstrainedValue {
|
---|
| 81 | get { return this.constrainedValue; }
|
---|
[3613] | 82 | set {
|
---|
[3592] | 83 | if (value == null)
|
---|
| 84 | throw new ArgumentNullException("Constraint value cannot be null.");
|
---|
| 85 | if (this.constrainedValue != value) {
|
---|
| 86 | this.constrainedValue = value;
|
---|
[3613] | 87 | this.OnConstrainedValueChanged();
|
---|
[3592] | 88 | this.OnToStringChanged();
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | [Storable]
|
---|
[3602] | 94 | private object constraintData;
|
---|
| 95 | public object ConstraintData {
|
---|
| 96 | get { return this.constraintData; }
|
---|
[3592] | 97 | set {
|
---|
[3602] | 98 | if (this.constraintData != value) {
|
---|
| 99 | this.constraintData = value;
|
---|
| 100 | this.OnConstraintDataChanged();
|
---|
[3592] | 101 | this.OnToStringChanged();
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[3602] | 106 | public abstract IEnumerable<ConstraintOperation> AllowedConstraintOperations { get; }
|
---|
[3592] | 107 | [Storable]
|
---|
[3602] | 108 | private ConstraintOperation constraintOperation;
|
---|
| 109 | public ConstraintOperation ConstraintOperation {
|
---|
| 110 | get { return this.constraintOperation; }
|
---|
[3592] | 111 | set {
|
---|
| 112 | if (value == null)
|
---|
| 113 | throw new ArgumentNullException("Comparison operation cannot be null.");
|
---|
[3602] | 114 | if (!AllowedConstraintOperations.Contains(value))
|
---|
[3592] | 115 | throw new ArgumentException("Comparison operation is not contained in the allowed ComparisonOperations.");
|
---|
[3602] | 116 | if (this.constraintOperation != value) {
|
---|
| 117 | this.constraintOperation = value;
|
---|
| 118 | this.OnConstraintOperationChanged();
|
---|
[3592] | 119 | this.OnToStringChanged();
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | /// <summary>
|
---|
| 125 | /// This method is called to determine which member of the constrained value should be compared.
|
---|
| 126 | /// </summary>
|
---|
| 127 | /// <returns></returns>
|
---|
| 128 | protected virtual IItem GetConstrainedMember() {
|
---|
| 129 | return this.constrainedValue;
|
---|
| 130 | }
|
---|
[4153] | 131 | protected abstract bool Check(object constrainedMember);
|
---|
| 132 | protected abstract bool Check(object constrainedMember, out string errorMessage);
|
---|
| 133 |
|
---|
[3592] | 134 | public bool Check() {
|
---|
| 135 | if (!Active)
|
---|
| 136 | return true;
|
---|
| 137 |
|
---|
| 138 | IItem constrainedMember = this.GetConstrainedMember();
|
---|
| 139 | return this.Check(constrainedMember);
|
---|
| 140 | }
|
---|
[3602] | 141 | public bool Check(out string errorMessage) {
|
---|
| 142 | errorMessage = string.Empty;
|
---|
| 143 | if (!Active)
|
---|
| 144 | return true;
|
---|
| 145 |
|
---|
| 146 | IItem constrainedMember = this.GetConstrainedMember();
|
---|
| 147 | return this.Check(constrainedMember, out errorMessage);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[3592] | 150 | #region events
|
---|
| 151 | public event EventHandler ActiveChanged;
|
---|
| 152 | protected virtual void OnActiveChanged() {
|
---|
| 153 | EventHandler handler = ActiveChanged;
|
---|
[4722] | 154 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3592] | 155 | }
|
---|
| 156 |
|
---|
[3613] | 157 | public event EventHandler ConstrainedValueChanged;
|
---|
| 158 | protected virtual void OnConstrainedValueChanged() {
|
---|
| 159 | EventHandler handler = ConstrainedValueChanged;
|
---|
[4722] | 160 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3613] | 161 | }
|
---|
| 162 |
|
---|
[3602] | 163 | public event EventHandler ConstraintDataChanged;
|
---|
| 164 | protected virtual void OnConstraintDataChanged() {
|
---|
| 165 | EventHandler handler = ConstraintDataChanged;
|
---|
[4722] | 166 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3592] | 167 | }
|
---|
| 168 |
|
---|
[3602] | 169 | public event EventHandler ConstraintOperationChanged;
|
---|
| 170 | protected virtual void OnConstraintOperationChanged() {
|
---|
| 171 | EventHandler handler = ConstraintOperationChanged;
|
---|
[4722] | 172 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
[3592] | 173 | }
|
---|
| 174 | #endregion
|
---|
| 175 |
|
---|
| 176 | #region overriden item methods
|
---|
| 177 | public override string ToString() {
|
---|
[3613] | 178 | IItem constrainedMember = GetConstrainedMember();
|
---|
[3592] | 179 | string s = string.Empty;
|
---|
[3613] | 180 | if (constrainedMember != null)
|
---|
| 181 | s += constrainedMember.ToString() + " ";
|
---|
[3592] | 182 |
|
---|
[3613] | 183 | if (constraintOperation != null)
|
---|
| 184 | s += ConstraintOperation.ToString() + " ";
|
---|
[3592] | 185 |
|
---|
[3602] | 186 | if (constraintData != null)
|
---|
| 187 | s += constraintData.ToString();
|
---|
[3592] | 188 | else
|
---|
| 189 | s += "null";
|
---|
| 190 |
|
---|
| 191 | s += ".";
|
---|
| 192 | return s;
|
---|
| 193 | }
|
---|
| 194 | #endregion
|
---|
| 195 | }
|
---|
| 196 | }
|
---|