Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Operators/3.3/MultipleCallsOperator.cs @ 3181

Last change on this file since 3181 was 3017, checked in by epitzer, 15 years ago

Merge StorableClassType.Empty into StorableClassType.MarkedOnly and make it the default if not specified (#548)

File size: 4.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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.Linq;
24using HeuristicLab.Collections;
25using HeuristicLab.Core;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Operators {
30  /// <summary>
31  /// A base class for operators which apply multiple user-defined operators.
32  /// </summary>
33  [Item("MultipleCallsOperator", "A base class for operators which apply multiple user-defined operators.")]
34  [StorableClass]
35  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    }
45
46    private OperatorList operators;
47    [Storable]
48    public OperatorList Operators {
49      get { return operators; }
50      private set {
51        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);
60      }
61    }
62
63    public MultipleCallsOperator()
64      : base() {
65      Operators = new OperatorList();
66    }
67
68    public override IDeepCloneable Clone(Cloner cloner) {
69      MultipleCallsOperator clone = (MultipleCallsOperator)base.Clone(cloner);
70      clone.Operators = (OperatorList)cloner.Clone(operators);
71      return clone;
72    }
73
74    private void UpdateOperatorParameters() {
75      var opParams = OperatorParameters;
76      foreach (IValueParameter<IOperator> opParam in opParams) {
77        opParam.ValueChanged -= new EventHandler(opParam_ValueChanged);
78        Parameters.Remove(opParam.Name);
79      }
80      for (int i = 0; i < Operators.Count; i++) {
81        IValueParameter<IOperator> opParam = new OperatorParameter(i.ToString(), string.Empty, Operators[i]);
82        opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
83        Parameters.Add(opParam);
84      }
85    }
86    private void operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOperator>> e) {
87      UpdateOperatorParameters();
88    }
89    private void operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOperator>> e) {
90      UpdateOperatorParameters();
91    }
92    private void operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOperator>> e) {
93      foreach (IndexedItem<IOperator> item in e.Items)
94        ((IValueParameter<IOperator>)Parameters[item.Index.ToString()]).Value = item.Value;
95    }
96    private void operators_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOperator>> e) {
97      foreach (IndexedItem<IOperator> item in e.Items)
98        ((IValueParameter<IOperator>)Parameters[item.Index.ToString()]).Value = item.Value;
99    }
100    private void operators_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOperator>> e) {
101      UpdateOperatorParameters();
102    }
103    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;
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.