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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Collections;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Parameters;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace 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 | public abstract class MultipleCallsOperator : SingleSuccessorOperator {
|
---|
35 | protected IValueParameter<IOperator>[] OperatorParameters {
|
---|
36 | get {
|
---|
37 | return (from p in Parameters
|
---|
38 | where p is IValueParameter<IOperator>
|
---|
39 | where Operators.Contains(((IValueParameter<IOperator>)p).Value)
|
---|
40 | orderby p.Name ascending
|
---|
41 | select (IValueParameter<IOperator>)p).ToArray();
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | private OperatorList operators;
|
---|
46 | [Storable]
|
---|
47 | public OperatorList Operators {
|
---|
48 | get { return operators; }
|
---|
49 | private set {
|
---|
50 | operators = value;
|
---|
51 | operators.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(operators_ItemsAdded);
|
---|
52 | operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(operators_ItemsRemoved);
|
---|
53 | operators.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(operators_ItemsReplaced);
|
---|
54 | operators.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(operators_ItemsMoved);
|
---|
55 | operators.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(operators_CollectionReset);
|
---|
56 | var opParams = OperatorParameters;
|
---|
57 | foreach (IValueParameter<IOperator> opParam in opParams)
|
---|
58 | opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | public MultipleCallsOperator()
|
---|
63 | : base() {
|
---|
64 | Operators = new OperatorList();
|
---|
65 | }
|
---|
66 |
|
---|
67 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
68 | MultipleCallsOperator clone = (MultipleCallsOperator)base.Clone(cloner);
|
---|
69 | clone.Operators = (OperatorList)cloner.Clone(operators);
|
---|
70 | return clone;
|
---|
71 | }
|
---|
72 |
|
---|
73 | private void UpdateOperatorParameters() {
|
---|
74 | var opParams = OperatorParameters;
|
---|
75 | foreach (IValueParameter<IOperator> opParam in opParams) {
|
---|
76 | opParam.ValueChanged -= new EventHandler(opParam_ValueChanged);
|
---|
77 | Parameters.Remove(opParam.Name);
|
---|
78 | }
|
---|
79 | for (int i = 0; i < Operators.Count; i++) {
|
---|
80 | IValueParameter<IOperator> opParam = new OperatorParameter(i.ToString(), string.Empty, Operators[i]);
|
---|
81 | opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
|
---|
82 | Parameters.Add(opParam);
|
---|
83 | }
|
---|
84 | }
|
---|
85 | private void operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOperator>> e) {
|
---|
86 | UpdateOperatorParameters();
|
---|
87 | }
|
---|
88 | private void operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOperator>> e) {
|
---|
89 | UpdateOperatorParameters();
|
---|
90 | }
|
---|
91 | private void operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOperator>> e) {
|
---|
92 | foreach (IndexedItem<IOperator> item in e.Items)
|
---|
93 | ((IValueParameter<IOperator>)Parameters[item.Index.ToString()]).Value = item.Value;
|
---|
94 | }
|
---|
95 | private void operators_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOperator>> e) {
|
---|
96 | foreach (IndexedItem<IOperator> item in e.Items)
|
---|
97 | ((IValueParameter<IOperator>)Parameters[item.Index.ToString()]).Value = item.Value;
|
---|
98 | }
|
---|
99 | private void operators_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOperator>> e) {
|
---|
100 | UpdateOperatorParameters();
|
---|
101 | }
|
---|
102 | private void opParam_ValueChanged(object sender, EventArgs e) {
|
---|
103 | IValueParameter<IOperator> opParam = (IValueParameter<IOperator>)sender;
|
---|
104 | int index = int.Parse(opParam.Name);
|
---|
105 | operators[index] = opParam.Value;
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|