Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Operators/3.3/MultiOperator.cs @ 15557

Last change on this file since 15557 was 15108, checked in by abeham, 7 years ago

#2775: merged 15084 to stable

File size: 6.6 KB
RevLine 
[2773]1#region License Information
2/* HeuristicLab
[14186]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2773]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
[9942]22using System;
23using System.Collections.Generic;
[15108]24using System.Linq;
[2773]25using HeuristicLab.Collections;
[3376]26using HeuristicLab.Common;
[2773]27using HeuristicLab.Core;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Operators {
32  /// <summary>
[3407]33  /// A base class for operators which apply arbitrary many other operators of a specific type.
[2773]34  /// </summary>
[3822]35  [Item("MultiOperator", "A base class for operators which apply arbitrary many other operators of a specific type.")]
[3017]36  [StorableClass]
[10507]37  public abstract class MultiOperator<T> : InstrumentedOperator, IMultiOperator<T> where T : class, IOperator {
[3407]38    private List<IValueParameter<T>> operatorParameters;
[9942]39    protected IEnumerable<IValueParameter<T>> OperatorParameters { get { return operatorParameters; } }
[2773]40
[3317]41    [Storable]
[3591]42    private IItemList<T> operators;
43    public IItemList<T> Operators {
[2773]44      get { return operators; }
[3591]45      protected set {
46        if (operators != value) {
47          if (value == null) throw new ArgumentException();
48          DeregisterOperatorsEvents();
49          operators = value;
50          RegisterOperatorsEvents();
[9216]51          UpdateOperatorParameters();
[3591]52        }
53      }
[2773]54    }
[15108]55   
56    IEnumerable<IOperator> IMultiOperator.Operators { get { return operators.AsEnumerable(); } }
[2773]57
[4722]58    [StorableConstructor]
59    protected MultiOperator(bool deserializing) : base(deserializing) { }
60    protected MultiOperator(MultiOperator<T> original, Cloner cloner)
61      : base(original, cloner) {
62      this.operators = cloner.Clone<IItemList<T>>(original.operators);
63      Initialize();
64    }
[3407]65    public MultiOperator()
[2773]66      : base() {
[3591]67      this.operators = new ItemList<T>();
[3211]68      Initialize();
[2773]69    }
[3591]70
[3211]71    [StorableHook(HookType.AfterDeserialization)]
[4722]72    private void AfterDeserialization() {
73      Initialize();
74    }
[5080]75
[15108]76    public virtual bool AddOperator(IOperator op) {
77      var tOp = op as T;
78      if (tOp == null) return false;
79      operators.Add(tOp);
80      return true;
81    }
82
83    public virtual bool RemoveOperator(IOperator op) {
84      var tOp = op as T;
85      if (tOp == null) return false;
86      return operators.Remove(tOp);
87    }
88
[3211]89    private void Initialize() {
[3317]90      if (operators != null) RegisterOperatorsEvents();
[3407]91      operatorParameters = new List<IValueParameter<T>>();
[3211]92      for (int i = 0; i < Operators.Count; i++) {
[3407]93        IValueParameter<T> opParam = (IValueParameter<T>)Parameters[i.ToString()];
[3211]94        operatorParameters.Add(opParam);
95        opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
96      }
97    }
98
[2773]99    private void UpdateOperatorParameters() {
[3407]100      foreach (IValueParameter<T> opParam in operatorParameters) {
[2773]101        opParam.ValueChanged -= new EventHandler(opParam_ValueChanged);
102        Parameters.Remove(opParam.Name);
103      }
[3211]104      operatorParameters.Clear();
[2773]105      for (int i = 0; i < Operators.Count; i++) {
[3729]106        IValueParameter<T> opParam = new OptionalValueParameter<T>(i.ToString(), string.Empty, Operators[i]);
[6051]107        opParam.Hidden = true;
[2773]108        opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
109        Parameters.Add(opParam);
[3211]110        operatorParameters.Add(opParam);
[2773]111      }
112    }
[3211]113
114    #region Events
115    private void RegisterOperatorsEvents() {
[3445]116      operators.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsAdded);
117      operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsRemoved);
118      operators.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsReplaced);
119      operators.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsMoved);
120      operators.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_CollectionReset);
[3211]121    }
122    private void DeregisterOperatorsEvents() {
[3445]123      operators.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsAdded);
124      operators.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsRemoved);
125      operators.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsReplaced);
126      operators.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsMoved);
127      operators.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_CollectionReset);
[3211]128    }
[3445]129    protected virtual void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
[2773]130      UpdateOperatorParameters();
131    }
[3445]132    protected virtual void Operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
[2773]133      UpdateOperatorParameters();
134    }
[3445]135    protected virtual void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
[3407]136      foreach (IndexedItem<T> item in e.Items)
[3211]137        operatorParameters[item.Index].Value = item.Value;
[2773]138    }
[3445]139    protected virtual void Operators_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
[3407]140      foreach (IndexedItem<T> item in e.Items)
[3211]141        operatorParameters[item.Index].Value = item.Value;
[2773]142    }
[3445]143    protected virtual void Operators_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
[2773]144      UpdateOperatorParameters();
145    }
146    private void opParam_ValueChanged(object sender, EventArgs e) {
[3407]147      IValueParameter<T> opParam = (IValueParameter<T>)sender;
[3729]148      if (opParam.Value == null)
149        operators.RemoveAt(operatorParameters.IndexOf(opParam));
150      else
151        operators[operatorParameters.IndexOf(opParam)] = opParam.Value;
[2773]152    }
[3211]153    #endregion
[2773]154  }
155}
Note: See TracBrowser for help on using the repository browser.