[2740] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2008 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.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using System.Xml;
|
---|
| 27 | using HeuristicLab.Collections;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Operators {
|
---|
| 33 | /// <summary>
|
---|
| 34 | /// A base class for operators which have multiple successors.
|
---|
| 35 | /// </summary>
|
---|
| 36 | [Item("MultipleSuccessorsOperator", "A base class for operators which have multiple successors.")]
|
---|
| 37 | [Creatable("Test")]
|
---|
| 38 | [EmptyStorableClass]
|
---|
| 39 | public abstract class MultipleSuccessorsOperator : Operator {
|
---|
[2756] | 40 | protected IValueParameter<IOperator>[] SuccessorParameters {
|
---|
[2740] | 41 | get {
|
---|
| 42 | return (from p in Parameters
|
---|
[2756] | 43 | where p is IValueParameter<IOperator>
|
---|
[2740] | 44 | orderby p.Name ascending
|
---|
[2756] | 45 | select (IValueParameter<IOperator>)p).ToArray();
|
---|
[2740] | 46 | }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | private OperatorList successors;
|
---|
| 50 | public OperatorList Successors {
|
---|
| 51 | get {
|
---|
| 52 | if (successors == null) {
|
---|
| 53 | successors = new OperatorList();
|
---|
| 54 | var opParams = SuccessorParameters;
|
---|
[2756] | 55 | foreach (IValueParameter<IOperator> opParam in opParams) {
|
---|
[2740] | 56 | opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
|
---|
| 57 | successors.Add(opParam.Value);
|
---|
| 58 | }
|
---|
| 59 | successors.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(successors_ItemsAdded);
|
---|
| 60 | successors.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(successors_ItemsRemoved);
|
---|
| 61 | successors.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(successors_ItemsReplaced);
|
---|
| 62 | successors.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(successors_ItemsMoved);
|
---|
| 63 | successors.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(successors_CollectionReset);
|
---|
| 64 | }
|
---|
| 65 | return successors;
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | public MultipleSuccessorsOperator()
|
---|
| 70 | : base() {
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | private void UpdateOperatorParameters() {
|
---|
| 74 | var opParams = SuccessorParameters;
|
---|
[2756] | 75 | foreach (IValueParameter<IOperator> opParam in opParams) {
|
---|
[2740] | 76 | opParam.ValueChanged -= new EventHandler(opParam_ValueChanged);
|
---|
| 77 | Parameters.Remove(opParam.Name);
|
---|
| 78 | }
|
---|
| 79 | for (int i = 0; i < Successors.Count; i++) {
|
---|
[2756] | 80 | IValueParameter<IOperator> opParam = new OperatorParameter(i.ToString(), string.Empty, Successors[i]);
|
---|
[2740] | 81 | opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
|
---|
| 82 | Parameters.Add(opParam);
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | private void successors_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOperator>> e) {
|
---|
| 87 | UpdateOperatorParameters();
|
---|
| 88 | }
|
---|
| 89 | private void successors_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOperator>> e) {
|
---|
| 90 | UpdateOperatorParameters();
|
---|
| 91 | }
|
---|
| 92 | private void successors_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOperator>> e) {
|
---|
| 93 | foreach (IndexedItem<IOperator> item in e.Items)
|
---|
[2756] | 94 | ((IValueParameter<IOperator>)Parameters[item.Index.ToString()]).Value = item.Value;
|
---|
[2740] | 95 | }
|
---|
| 96 | private void successors_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOperator>> e) {
|
---|
| 97 | foreach (IndexedItem<IOperator> item in e.Items)
|
---|
[2756] | 98 | ((IValueParameter<IOperator>)Parameters[item.Index.ToString()]).Value = item.Value;
|
---|
[2740] | 99 | }
|
---|
| 100 | private void successors_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOperator>> e) {
|
---|
| 101 | UpdateOperatorParameters();
|
---|
| 102 | }
|
---|
| 103 | private void opParam_ValueChanged(object sender, EventArgs e) {
|
---|
[2756] | 104 | IValueParameter<IOperator> opParam = (IValueParameter<IOperator>)sender;
|
---|
[2740] | 105 | int index = int.Parse(opParam.Name);
|
---|
| 106 | successors[index] = opParam.Value;
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 | }
|
---|