Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization/3.3/RunCollectionConstraints/RunCollectionContentConstraint.cs @ 12246

Last change on this file since 12246 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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
22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Collections;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Optimization {
30  [StorableClass]
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]
47    protected RunCollectionContentConstraint(bool deserializing) : base(deserializing) { }
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}
Note: See TracBrowser for help on using the repository browser.