Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization/3.3/Problems/Problem.cs @ 7440

Last change on this file since 7440 was 7431, checked in by abeham, 12 years ago

#1769

  • removed private field and stored the collection in the parameter only
  • forwarded calls to Operators to the parameter
  • put StorableOperators property out of use as the collection is persisted through the parameters' persistence
File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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;
23using System.Collections.Generic;
24using System.Drawing;
25using HeuristicLab.Collections;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Optimization {
32  [Item("Problem", "Represents the base class for a problem.")]
33  [StorableClass]
34  public abstract class Problem : ParameterizedNamedItem, IProblem {
35    private static readonly string OperatorsParameterName = "Operators";
36
37    public IFixedValueParameter<OperatorCollection> OperatorsParameter {
38      get { return (IFixedValueParameter<OperatorCollection>)Parameters[OperatorsParameterName]; }
39    }
40
41    public static new Image StaticItemImage {
42      get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
43    }
44
45    [StorableConstructor]
46    protected Problem(bool deserializing) : base(deserializing) { }
47    protected Problem(Problem original, Cloner cloner)
48      : base(original, cloner) {
49      RegisterEventHandlers();
50    }
51
52    protected Problem()
53      : base() {
54      Parameters.Add(new FixedValueParameter<OperatorCollection>(OperatorsParameterName, "The operators that the problem provides to the algorithms.", new OperatorCollection(), false));
55      OperatorsParameter.Hidden = true;
56      RegisterEventHandlers();
57    }
58
59    [StorableHook(HookType.AfterDeserialization)]
60    private void AfterDeserialization() {
61      RegisterEventHandlers();
62    }
63
64    private void RegisterEventHandlers() {
65      Operators.ItemsAdded += new CollectionItemsChangedEventHandler<IOperator>(Operators_Changed);
66      Operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IOperator>(Operators_Changed);
67      Operators.CollectionReset += new CollectionItemsChangedEventHandler<IOperator>(Operators_Changed);
68    }
69
70    #region properties
71    // BackwardsCompatibility3.3
72    #region Backwards compatible code, remove with 3.4
73    [Storable(Name = "Operators")]
74    private IEnumerable<IOperator> StorableOperators {
75      get { return null; }
76      set {
77        if (!Parameters.ContainsKey(OperatorsParameterName)) {
78          Parameters.Add(new FixedValueParameter<OperatorCollection>(OperatorsParameterName, "The operators that the problem provides to the algorithms.", new OperatorCollection(value), false));
79          OperatorsParameter.Hidden = true;
80        }
81      }
82    }
83    #endregion
84    protected OperatorCollection Operators {
85      get {
86        // BackwardsCompatibility3.3
87        #region Backwards compatible code, remove with 3.4
88        if (!Parameters.ContainsKey(OperatorsParameterName)) {
89          Parameters.Add(new FixedValueParameter<OperatorCollection>(OperatorsParameterName, "The operators that the problem provides to the algorithms.", new OperatorCollection(), false));
90          OperatorsParameter.Hidden = true;
91        }
92        #endregion
93        return OperatorsParameter.Value;
94      }
95    }
96    IEnumerable<IOperator> IProblem.Operators { get { return Operators; } }
97    #endregion
98
99    #region events
100    private void Operators_Changed(object sender, EventArgs e) {
101      OnOperatorsChanged();
102    }
103    public event EventHandler OperatorsChanged;
104    protected virtual void OnOperatorsChanged() {
105      EventHandler handler = OperatorsChanged;
106      if (handler != null)
107        handler(this, EventArgs.Empty);
108    }
109
110    public event EventHandler Reset;
111    protected virtual void OnReset() {
112      EventHandler handler = Reset;
113      if (handler != null)
114        handler(this, EventArgs.Empty);
115    }
116    #endregion
117  }
118}
Note: See TracBrowser for help on using the repository browser.