Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/OperatorGraph.cs @ 2810

Last change on this file since 2810 was 2796, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

  • worked on operators, parameters and problems
File size: 9.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.Collections.Generic;
24using System.Text;
25using System.Xml;
26using System.Linq;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Common;
29using HeuristicLab.Collections;
30
31namespace HeuristicLab.Core {
32  /// <summary>
33  /// Represents a graph of operators.
34  /// </summary>
35  [Item("OperatorGraph", "Represents a graph of operators.")]
36  [Creatable("Algorithms")]
37  public class OperatorGraph : Item {
38    private OperatorSet operators;
39    /// <summary>
40    /// Gets all operators of the current instance.
41    /// </summary>
42    [Storable]
43    public OperatorSet Operators {
44      get { return operators; }
45      private set {
46        DeregisterOperatorsEvents();
47        operators = value;
48        RegisterOperatorsEvents();
49      }
50    }
51
52    [Storable]
53    private IOperator initialOperator;
54    /// <summary>
55    /// Gets or sets the initial operator (the starting one).
56    /// </summary>
57    /// <remarks>Calls <see cref="OnInitialOperatorChanged"/> in the setter.</remarks>
58    public IOperator InitialOperator {
59      get { return initialOperator; }
60      set {
61        if (initialOperator != value) {
62          if (value != null) Operators.Add(value);
63          initialOperator = value;
64          OnInitialOperatorChanged();
65        }
66      }
67    }
68
69    /// <summary>
70    /// Initializes a new instance of <see cref="OperatorGraph"/>.
71    /// </summary>
72    public OperatorGraph() {
73      Operators = new OperatorSet();
74      initialOperator = null;
75    }
76
77    /// <summary>
78    /// Clones the current instance (deep clone).
79    /// </summary>
80    /// <remarks>Deep clone through <see cref="cloner.Clone"/> method of helper class
81    /// <see cref="Auxiliary"/>.</remarks>
82    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
83    /// <returns>The cloned object as <see cref="OperatorGraph"/>.</returns>
84    public override IDeepCloneable Clone(Cloner cloner) {
85      OperatorGraph clone = new OperatorGraph();
86      cloner.RegisterClonedObject(this, clone);
87      clone.Operators = (OperatorSet)cloner.Clone(operators);
88      clone.initialOperator = (IOperator)cloner.Clone(initialOperator);
89      return clone;
90    }
91
92    /// <inheritdoc/>
93    public event EventHandler InitialOperatorChanged;
94    /// <summary>
95    /// Fires a new <c>InitialOperatorChanged</c> event.
96    /// </summary>
97    protected virtual void OnInitialOperatorChanged() {
98      if (InitialOperatorChanged != null)
99        InitialOperatorChanged(this, EventArgs.Empty);
100      OnChanged();
101    }
102
103    #region Operators Events
104    private void AddOperator(IOperator op) {
105      RegisterOperatorEvents(op);
106      foreach (IParameter param in op.Parameters)
107        AddParameter(param);
108    }
109    private void RemoveOperator(IOperator op) {
110      foreach (IParameter param in op.Parameters)
111        RemoveParameter(param);
112      DeregisterOperatorEvents(op);
113
114      // remove edges to removed operator
115      var opParams = from o in Operators
116                     from p in o.Parameters
117                     where p is IValueParameter<IOperator>
118                     where (((IValueParameter<IOperator>)p).Value != null) && (((IValueParameter<IOperator>)p).Value == op)
119                     select (IValueParameter<IOperator>)p;
120      foreach (IValueParameter<IOperator> opParam in opParams)
121        opParam.Value = null;
122    }
123    private void AddParameter(IParameter param) {
124      IValueParameter<IOperator> opParam = param as IValueParameter<IOperator>;
125      if (opParam != null) {
126        RegisterOperatorParameterEvents(opParam);
127        if (opParam.Value != null) Operators.Add(opParam.Value);
128      }
129    }
130    private void RemoveParameter(IParameter param) {
131      IValueParameter<IOperator> opParam = param as IValueParameter<IOperator>;
132      if (opParam != null) {
133        DeregisterOperatorParameterEvents(opParam);
134      }
135    }
136
137    private void RegisterOperatorsEvents() {
138      if (operators != null) {
139        operators.Changed += new ChangedEventHandler(Operators_Changed);
140        operators.ItemsAdded += new CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsAdded);
141        operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsRemoved);
142        operators.CollectionReset += new CollectionItemsChangedEventHandler<IOperator>(Operators_CollectionReset);
143        foreach (IOperator op in operators) {
144          RegisterOperatorEvents(op);
145          foreach (IParameter param in op.Parameters) {
146            IValueParameter<IOperator> opParam = param as IValueParameter<IOperator>;
147            if (opParam != null) RegisterOperatorParameterEvents(opParam);
148          }
149        }
150      }
151    }
152    private void DeregisterOperatorsEvents() {
153      if (operators != null) {
154        foreach (IOperator op in operators) {
155          foreach (IParameter param in op.Parameters) {
156            IValueParameter<IOperator> opParam = param as IValueParameter<IOperator>;
157            if (opParam != null) DeregisterOperatorParameterEvents(opParam);
158          }
159          DeregisterOperatorEvents(op);
160        }
161        operators.Changed -= new ChangedEventHandler(Operators_Changed);
162        operators.ItemsAdded -= new CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsAdded);
163        operators.ItemsRemoved -= new CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsRemoved);
164        operators.CollectionReset -= new CollectionItemsChangedEventHandler<IOperator>(Operators_CollectionReset);
165      }
166    }
167    private void RegisterOperatorEvents(IOperator op) {
168      op.Parameters.ItemsAdded += new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsAdded);
169      op.Parameters.ItemsRemoved += new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsRemoved);
170      op.Parameters.ItemsReplaced += new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsReplaced);
171      op.Parameters.CollectionReset += new CollectionItemsChangedEventHandler<IParameter>(Parameters_CollectionReset);
172    }
173    private void DeregisterOperatorEvents(IOperator op) {
174      op.Parameters.ItemsAdded -= new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsAdded);
175      op.Parameters.ItemsRemoved -= new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsRemoved);
176      op.Parameters.ItemsReplaced -= new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsReplaced);
177      op.Parameters.CollectionReset -= new CollectionItemsChangedEventHandler<IParameter>(Parameters_CollectionReset);
178    }
179    private void RegisterOperatorParameterEvents(IValueParameter<IOperator> opParam) {
180      opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
181    }
182    private void DeregisterOperatorParameterEvents(IValueParameter<IOperator> opParam) {
183      opParam.ValueChanged -= new EventHandler(opParam_ValueChanged);
184    }
185
186    private void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IOperator> e) {
187      foreach (IOperator op in e.Items)
188        AddOperator(op);
189    }
190    private void Operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IOperator> e) {
191      foreach (IOperator op in e.Items)
192        RemoveOperator(op);
193      if (!Operators.Contains(InitialOperator)) InitialOperator = null;
194    }
195    private void Operators_CollectionReset(object sender, CollectionItemsChangedEventArgs<IOperator> e) {
196      foreach (IOperator op in e.OldItems)
197        RemoveOperator(op);
198      foreach (IOperator op in e.Items)
199        AddOperator(op);
200      if (!Operators.Contains(InitialOperator)) InitialOperator = null;
201    }
202    private void Parameters_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
203      foreach (IParameter param in e.Items)
204        AddParameter(param);
205    }
206    private void Parameters_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
207      foreach (IParameter param in e.Items)
208        RemoveParameter(param);
209    }
210    private void Parameters_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
211      foreach (IParameter param in e.OldItems)
212        RemoveParameter(param);
213      foreach (IParameter param in e.Items)
214        AddParameter(param);
215    }
216    private void Parameters_CollectionReset(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
217      foreach (IParameter param in e.OldItems)
218        RemoveParameter(param);
219      foreach (IParameter param in e.Items)
220        AddParameter(param);
221    }
222    private void opParam_ValueChanged(object sender, EventArgs e) {
223      IValueParameter<IOperator> opParam = (IValueParameter<IOperator>)sender;
224      if (opParam.Value != null) Operators.Add(opParam.Value);
225    }
226    private void Operators_Changed(object sender, ChangedEventArgs e) {
227      OnChanged(e);
228    }
229    #endregion
230  }
231}
Note: See TracBrowser for help on using the repository browser.