Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8365 was 8004, checked in by mkommend, 13 years ago

#1864: Corrected deserialization of the operator collection.

File size: 6.0 KB
RevLine 
[4323]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[4323]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;
[7579]28using HeuristicLab.Data;
[6938]29using HeuristicLab.Parameters;
[4323]30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
[4564]32namespace HeuristicLab.Optimization {
[4596]33  [Item("Problem", "Represents the base class for a problem.")]
[4323]34  [StorableClass]
[5809]35  public abstract class Problem : ParameterizedNamedItem, IProblem {
[8004]36    private const string OperatorsParameterName = "Operators";
[7999]37    public IFixedValueParameter<ItemCollection<IItem>> OperatorsParameter {
38      get { return (IFixedValueParameter<ItemCollection<IItem>>)Parameters[OperatorsParameterName]; }
[6938]39    }
40
[7201]41    public static new Image StaticItemImage {
[5287]42      get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
[4323]43    }
44
45    [StorableConstructor]
[7431]46    protected Problem(bool deserializing) : base(deserializing) { }
[5809]47    protected Problem(Problem original, Cloner cloner)
[4722]48      : base(original, cloner) {
49      RegisterEventHandlers();
50    }
51
[4323]52    protected Problem()
53      : base() {
[7999]54      Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>(), false));
[6938]55      OperatorsParameter.Hidden = true;
[4563]56      RegisterEventHandlers();
[4323]57    }
58
59    [StorableHook(HookType.AfterDeserialization)]
60    private void AfterDeserialization() {
[7999]61      // BackwardsCompatibility3.3
62      #region Backwards compatible code, remove with 3.4
[8004]63      IParameter operatorsParam;
64      if (Parameters.TryGetValue(OperatorsParameterName, out operatorsParam)) {
65        var operators = operatorsParam.ActualValue as OperatorCollection;
66        if (operators != null) {
67          Parameters.Remove(OperatorsParameterName);
68          Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>(operators), false));
69          OperatorsParameter.Hidden = true;
70        }
[7999]71      }
72      #endregion
73
[4563]74      RegisterEventHandlers();
[4323]75    }
76
[4563]77    private void RegisterEventHandlers() {
[7999]78      Operators.ItemsAdded += new CollectionItemsChangedEventHandler<IItem>(Operators_Changed);
79      Operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IItem>(Operators_Changed);
80      Operators.CollectionReset += new CollectionItemsChangedEventHandler<IItem>(Operators_Changed);
[4323]81    }
82
83    #region properties
[7431]84    // BackwardsCompatibility3.3
85    #region Backwards compatible code, remove with 3.4
[7999]86    [Storable(Name = "Operators", AllowOneWay = true)]
[4323]87    private IEnumerable<IOperator> StorableOperators {
[7431]88      set {
[8004]89        IParameter operatorsParam;
90        if (Parameters.TryGetValue(OperatorsParameterName, out operatorsParam)) {
91          var items = operatorsParam.ActualValue as ItemCollection<IItem>;
92          if (items == null) Parameters.Remove(operatorsParam);
93        }
94
95        //necessary to convert old experiments files where no parameter was used for saving the operators
[7431]96        if (!Parameters.ContainsKey(OperatorsParameterName)) {
[8004]97          Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>(), false));
[7431]98          OperatorsParameter.Hidden = true;
99        }
[8004]100        if (value != null) Operators.AddRange(value);
[7431]101      }
[4323]102    }
[7431]103    #endregion
[7999]104    protected ItemCollection<IItem> Operators {
[7431]105      get {
106        // BackwardsCompatibility3.3
107        #region Backwards compatible code, remove with 3.4
108        if (!Parameters.ContainsKey(OperatorsParameterName)) {
[7999]109          Parameters.Add(new FixedValueParameter<ItemCollection<IItem>>(OperatorsParameterName, "The operators and items that the problem provides to the algorithms.", new ItemCollection<IItem>(), false));
[7431]110          OperatorsParameter.Hidden = true;
111        }
112        #endregion
113        return OperatorsParameter.Value;
114      }
[4323]115    }
[7999]116    IEnumerable<IItem> IProblem.Operators { get { return Operators; } }
[4323]117    #endregion
118
[7706]119    protected override IEnumerable<KeyValuePair<string, IItem>> GetCollectedValues(IValueParameter param) {
120      var children = base.GetCollectedValues(param);
[7579]121      foreach (var child in children) {
122        if (child.Value is IOperator)
123          yield return new KeyValuePair<string, IItem>(child.Key, new StringValue(((IOperator)child.Value).Name));
124        else yield return child;
125      }
126    }
127
[4323]128    #region events
129    private void Operators_Changed(object sender, EventArgs e) {
130      OnOperatorsChanged();
131    }
132    public event EventHandler OperatorsChanged;
133    protected virtual void OnOperatorsChanged() {
134      EventHandler handler = OperatorsChanged;
135      if (handler != null)
136        handler(this, EventArgs.Empty);
137    }
138
139    public event EventHandler Reset;
140    protected virtual void OnReset() {
141      EventHandler handler = Reset;
142      if (handler != null)
143        handler(this, EventArgs.Empty);
144    }
145    #endregion
146  }
147}
Note: See TracBrowser for help on using the repository browser.