Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/25/10 02:38:33 (15 years ago)
Author:
swagner
Message:

Fixed parameter update in MultipleCallsOperator (#950).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Operators/3.3/MultipleCallsOperator.cs

    r3017 r3211  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Linq;
    2425using HeuristicLab.Collections;
     
    3435  [StorableClass]
    3536  public abstract class MultipleCallsOperator : SingleSuccessorOperator {
    36     protected IValueParameter<IOperator>[] OperatorParameters {
    37       get {
    38         return (from p in Parameters
    39                 where p is IValueParameter<IOperator>
    40                 where Operators.Contains(((IValueParameter<IOperator>)p).Value)
    41                 orderby p.Name ascending
    42                 select (IValueParameter<IOperator>)p).ToArray();
    43       }
    44     }
     37    private List<OperatorParameter> operatorParameters;
    4538
    4639    private OperatorList operators;
     
    4942      get { return operators; }
    5043      private set {
     44        if (operators != null) DeregisterOperatorsEvents();
    5145        operators = value;
    52         operators.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(operators_ItemsAdded);
    53         operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(operators_ItemsRemoved);
    54         operators.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(operators_ItemsReplaced);
    55         operators.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(operators_ItemsMoved);
    56         operators.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(operators_CollectionReset);
    57         var opParams = OperatorParameters;
    58         foreach (IValueParameter<IOperator> opParam in opParams)
    59           opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
     46        if (operators != null) RegisterOperatorsEvents();
    6047      }
    6148    }
     
    6451      : base() {
    6552      Operators = new OperatorList();
     53      Initialize();
     54    }
     55
     56    [StorableHook(HookType.AfterDeserialization)]
     57    private void Initialize() {
     58      operatorParameters = new List<OperatorParameter>();
     59      for (int i = 0; i < Operators.Count; i++) {
     60        OperatorParameter opParam = (OperatorParameter)Parameters[i.ToString()];
     61        operatorParameters.Add(opParam);
     62        opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
     63      }
    6664    }
    6765
     
    6967      MultipleCallsOperator clone = (MultipleCallsOperator)base.Clone(cloner);
    7068      clone.Operators = (OperatorList)cloner.Clone(operators);
     69      clone.Initialize();
    7170      return clone;
    7271    }
    7372
    7473    private void UpdateOperatorParameters() {
    75       var opParams = OperatorParameters;
    76       foreach (IValueParameter<IOperator> opParam in opParams) {
     74      foreach (OperatorParameter opParam in operatorParameters) {
    7775        opParam.ValueChanged -= new EventHandler(opParam_ValueChanged);
    7876        Parameters.Remove(opParam.Name);
    7977      }
     78      operatorParameters.Clear();
    8079      for (int i = 0; i < Operators.Count; i++) {
    81         IValueParameter<IOperator> opParam = new OperatorParameter(i.ToString(), string.Empty, Operators[i]);
     80        OperatorParameter opParam = new OperatorParameter(i.ToString(), string.Empty, Operators[i]);
    8281        opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
    8382        Parameters.Add(opParam);
     83        operatorParameters.Add(opParam);
    8484      }
     85    }
     86
     87    #region Events
     88    private void RegisterOperatorsEvents() {
     89      operators.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(operators_ItemsAdded);
     90      operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(operators_ItemsRemoved);
     91      operators.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(operators_ItemsReplaced);
     92      operators.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(operators_ItemsMoved);
     93      operators.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(operators_CollectionReset);
     94    }
     95    private void DeregisterOperatorsEvents() {
     96      operators.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(operators_ItemsAdded);
     97      operators.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(operators_ItemsRemoved);
     98      operators.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(operators_ItemsReplaced);
     99      operators.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(operators_ItemsMoved);
     100      operators.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(operators_CollectionReset);
    85101    }
    86102    private void operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOperator>> e) {
     
    92108    private void operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOperator>> e) {
    93109      foreach (IndexedItem<IOperator> item in e.Items)
    94         ((IValueParameter<IOperator>)Parameters[item.Index.ToString()]).Value = item.Value;
     110        operatorParameters[item.Index].Value = item.Value;
    95111    }
    96112    private void operators_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOperator>> e) {
    97113      foreach (IndexedItem<IOperator> item in e.Items)
    98         ((IValueParameter<IOperator>)Parameters[item.Index.ToString()]).Value = item.Value;
     114        operatorParameters[item.Index].Value = item.Value;
    99115    }
    100116    private void operators_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOperator>> e) {
     
    102118    }
    103119    private void opParam_ValueChanged(object sender, EventArgs e) {
    104       IValueParameter<IOperator> opParam = (IValueParameter<IOperator>)sender;
    105       int index = int.Parse(opParam.Name);
    106       operators[index] = opParam.Value;
     120      OperatorParameter opParam = (OperatorParameter)sender;
     121      operators[operatorParameters.IndexOf(opParam)] = opParam.Value;
    107122    }
     123    #endregion
    108124  }
    109125}
Note: See TracChangeset for help on using the changeset viewer.