[6673] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16453] | 3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6673] | 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.Collections.Generic;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Collections;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
[16462] | 27 | using HEAL.Fossil;
|
---|
[6673] | 28 |
|
---|
| 29 | namespace HeuristicLab.Optimization {
|
---|
[16462] | 30 | [StorableType("E155F5BA-C93F-43F2-A513-A79AEF8F0043")]
|
---|
[6673] | 31 | public class RunCollectionContentConstraint : Constraint, IRunCollectionConstraint {
|
---|
| 32 | public new RunCollection ConstrainedValue {
|
---|
| 33 | get { return (RunCollection)base.ConstrainedValue; }
|
---|
| 34 | set { base.ConstrainedValue = value; }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | public new ItemSet<IRun> ConstraintData {
|
---|
| 38 | get { return (ItemSet<IRun>)base.ConstraintData; }
|
---|
| 39 | set { base.ConstraintData = value; }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | public override IEnumerable<ConstraintOperation> AllowedConstraintOperations {
|
---|
| 43 | get { return new List<ConstraintOperation>() { ConstraintOperation.Equal }; }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | [StorableConstructor]
|
---|
[16462] | 47 | protected RunCollectionContentConstraint(StorableConstructorFlag _) : base(_) { }
|
---|
[6673] | 48 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 49 | private void AfterDeserialization() {
|
---|
| 50 | RegisterConstraintDataEvents();
|
---|
| 51 | }
|
---|
| 52 | protected RunCollectionContentConstraint(RunCollectionContentConstraint original, Cloner cloner)
|
---|
| 53 | : base(original, cloner) {
|
---|
| 54 | RegisterConstraintDataEvents();
|
---|
| 55 | }
|
---|
| 56 | public override IDeepCloneable Clone(Cloner cloner) { return new RunCollectionContentConstraint(this, cloner); }
|
---|
| 57 |
|
---|
| 58 | public RunCollectionContentConstraint()
|
---|
| 59 | : base() {
|
---|
| 60 | ConstraintData = new ItemSet<IRun>();
|
---|
| 61 | }
|
---|
| 62 | public RunCollectionContentConstraint(RunCollection constrainedValue, IObservableSet<IRun> constraintData)
|
---|
| 63 | : base(constrainedValue, ConstraintOperation.Equal, constraintData) {
|
---|
| 64 | }
|
---|
| 65 | public RunCollectionContentConstraint(RunCollection constrainedValue, IObservableSet<IRun> constraintData, bool active)
|
---|
| 66 | : base(constrainedValue, ConstraintOperation.Equal, constraintData, active) {
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | protected override void OnConstraintDataChanged() {
|
---|
| 70 | RegisterConstraintDataEvents();
|
---|
| 71 | base.OnConstraintDataChanged();
|
---|
| 72 | }
|
---|
| 73 | private void RegisterConstraintDataEvents() {
|
---|
| 74 | if (ConstraintData != null) {
|
---|
| 75 | ConstraintData.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(ConstraintData_ItemsAdded);
|
---|
| 76 | ConstraintData.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(ConstraintData_ItemsRemoved);
|
---|
| 77 | ConstraintData.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(ConstraintData_CollectionReset);
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | private void ConstraintData_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
| 81 | base.OnConstraintDataChanged();
|
---|
| 82 | }
|
---|
| 83 | private void ConstraintData_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
| 84 | base.OnConstraintDataChanged();
|
---|
| 85 | }
|
---|
| 86 | private void ConstraintData_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
|
---|
| 87 | base.OnConstraintDataChanged();
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | protected override bool Check(object constrainedMember) {
|
---|
| 91 | if (!Active) return true;
|
---|
| 92 |
|
---|
| 93 | foreach (IRun run in ConstrainedValue.Where(r => r.Visible))
|
---|
| 94 | run.Visible = !ConstraintData.Contains(run);
|
---|
| 95 | return true;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | protected override bool Check(object constrainedMember, out string errorMessage) {
|
---|
| 99 | errorMessage = string.Empty;
|
---|
| 100 | if (!Active) return true;
|
---|
| 101 |
|
---|
| 102 | foreach (IRun run in ConstrainedValue.Where(r => r.Visible))
|
---|
| 103 | run.Visible = !ConstraintData.Contains(run);
|
---|
| 104 | return true;
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 | }
|
---|