Changeset 3211
- Timestamp:
- 03/25/10 02:38:33 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Operators/3.3/MultipleCallsOperator.cs
r3017 r3211 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.Linq; 24 25 using HeuristicLab.Collections; … … 34 35 [StorableClass] 35 36 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; 45 38 46 39 private OperatorList operators; … … 49 42 get { return operators; } 50 43 private set { 44 if (operators != null) DeregisterOperatorsEvents(); 51 45 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(); 60 47 } 61 48 } … … 64 51 : base() { 65 52 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 } 66 64 } 67 65 … … 69 67 MultipleCallsOperator clone = (MultipleCallsOperator)base.Clone(cloner); 70 68 clone.Operators = (OperatorList)cloner.Clone(operators); 69 clone.Initialize(); 71 70 return clone; 72 71 } 73 72 74 73 private void UpdateOperatorParameters() { 75 var opParams = OperatorParameters; 76 foreach (IValueParameter<IOperator> opParam in opParams) { 74 foreach (OperatorParameter opParam in operatorParameters) { 77 75 opParam.ValueChanged -= new EventHandler(opParam_ValueChanged); 78 76 Parameters.Remove(opParam.Name); 79 77 } 78 operatorParameters.Clear(); 80 79 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]); 82 81 opParam.ValueChanged += new EventHandler(opParam_ValueChanged); 83 82 Parameters.Add(opParam); 83 operatorParameters.Add(opParam); 84 84 } 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); 85 101 } 86 102 private void operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOperator>> e) { … … 92 108 private void operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOperator>> e) { 93 109 foreach (IndexedItem<IOperator> item in e.Items) 94 ((IValueParameter<IOperator>)Parameters[item.Index.ToString()]).Value = item.Value;110 operatorParameters[item.Index].Value = item.Value; 95 111 } 96 112 private void operators_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOperator>> e) { 97 113 foreach (IndexedItem<IOperator> item in e.Items) 98 ((IValueParameter<IOperator>)Parameters[item.Index.ToString()]).Value = item.Value;114 operatorParameters[item.Index].Value = item.Value; 99 115 } 100 116 private void operators_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOperator>> e) { … … 102 118 } 103 119 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; 107 122 } 123 #endregion 108 124 } 109 125 }
Note: See TracChangeset
for help on using the changeset viewer.