#region License Information /* HeuristicLab * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using HeuristicLab.Collections; using HeuristicLab.Common; using HeuristicLab.Core; using HEAL.Fossil; namespace HeuristicLab.Operators { /// /// An operator which collects the actual values of parameters. /// [Item("ValuesCollector", "An operator which collects the actual values of parameters.")] [StorableType("83F958A5-AE91-44C9-B329-BC9A36DC4E40")] public abstract class ValuesCollector : SingleSuccessorOperator, IOperator { [Storable] private ParameterCollection collectedValues; public ParameterCollection CollectedValues { get { return collectedValues; } } [StorableConstructor] protected ValuesCollector(StorableConstructorFlag _) : base(_) { } protected ValuesCollector(ValuesCollector original, Cloner cloner) : base(original, cloner) { this.collectedValues = cloner.Clone(original.collectedValues); Initialize(); } public ValuesCollector() : base() { collectedValues = new ParameterCollection(); Initialize(); } [StorableHook(HookType.AfterDeserialization)] private void AfterDeserialization() { Initialize(); } private void Initialize() { collectedValues.ItemsAdded += new CollectionItemsChangedEventHandler(collectedValues_ItemsAdded); collectedValues.ItemsRemoved += new CollectionItemsChangedEventHandler(collectedValues_ItemsRemoved); collectedValues.CollectionReset += new CollectionItemsChangedEventHandler(collectedValues_CollectionReset); } private void collectedValues_ItemsAdded(object sender, CollectionItemsChangedEventArgs e) { Parameters.AddRange(e.Items); } private void collectedValues_ItemsRemoved(object sender, CollectionItemsChangedEventArgs e) { Parameters.RemoveRange(e.Items); } #region NOTE // NOTE: The ItemsReplaced event does not have to be handled here as it is only fired when the name (i.e. key) of a parameter // changes. As the same parameter is also contained in the Parameters collection of the operator, the Parameters collection // will react on this name change on its own. #endregion private void collectedValues_CollectionReset(object sender, CollectionItemsChangedEventArgs e) { Parameters.RemoveRange(e.OldItems); Parameters.AddRange(e.Items); } } }