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

Last change on this file since 2932 was 2932, checked in by swagner, 12 years ago

Operator architecture refactoring (#95)

  • replaced Changed in IItem by ToStringChanged
File size: 10.1 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    [Storable]
70    private IDeepCloneable visualizationInfo;
71    /// <summary>
72    /// Gets or sets the visualizationInfo.
73    /// </summary>
74    /// /// <remarks>The VisualizationInfo can only be set once and fires afterwards and InvalidOperationException</remarks>
75    public IDeepCloneable VisualizationInfo {
76      get { return visualizationInfo; }
77      set {
78        if (visualizationInfo != null)
79          throw new InvalidOperationException("The value of the property VisualizationInfo is already set and cannot be set again.");
80        visualizationInfo = value;
81      }
82    }
83
84    /// <summary>
85    /// Initializes a new instance of <see cref="OperatorGraph"/>.
86    /// </summary>
87    public OperatorGraph() {
88      Operators = new OperatorSet();
89      initialOperator = null;
90      visualizationInfo = null;
91    }
92
93    /// <summary>
94    /// Clones the current instance (deep clone).
95    /// </summary>
96    /// <remarks>Deep clone through <see cref="cloner.Clone"/> method of helper class
97    /// <see cref="Auxiliary"/>.</remarks>
98    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
99    /// <returns>The cloned object as <see cref="OperatorGraph"/>.</returns>
100    public override IDeepCloneable Clone(Cloner cloner) {
101      OperatorGraph clone = new OperatorGraph();
102      cloner.RegisterClonedObject(this, clone);
103      clone.Operators = (OperatorSet)cloner.Clone(operators);
104      clone.initialOperator = (IOperator)cloner.Clone(initialOperator);
105      return clone;
106    }
107
108    /// <inheritdoc/>
109    public event EventHandler InitialOperatorChanged;
110    /// <summary>
111    /// Fires a new <c>InitialOperatorChanged</c> event.
112    /// </summary>
113    protected virtual void OnInitialOperatorChanged() {
114      if (InitialOperatorChanged != null)
115        InitialOperatorChanged(this, EventArgs.Empty);
116    }
117
118    #region Operators Events
119    private void AddOperator(IOperator op) {
120      RegisterOperatorEvents(op);
121      foreach (IParameter param in op.Parameters)
122        AddParameter(param);
123    }
124    private void RemoveOperator(IOperator op) {
125      foreach (IParameter param in op.Parameters)
126        RemoveParameter(param);
127      DeregisterOperatorEvents(op);
128
129      // remove edges to removed operator
130      var opParams = from o in Operators
131                     from p in o.Parameters
132                     where p is IValueParameter<IOperator>
133                     where (((IValueParameter<IOperator>)p).Value != null) && (((IValueParameter<IOperator>)p).Value == op)
134                     select (IValueParameter<IOperator>)p;
135      foreach (IValueParameter<IOperator> opParam in opParams)
136        opParam.Value = null;
137    }
138    private void AddParameter(IParameter param) {
139      IValueParameter<IOperator> opParam = param as IValueParameter<IOperator>;
140      if (opParam != null) {
141        RegisterOperatorParameterEvents(opParam);
142        if (opParam.Value != null) Operators.Add(opParam.Value);
143      }
144    }
145    private void RemoveParameter(IParameter param) {
146      IValueParameter<IOperator> opParam = param as IValueParameter<IOperator>;
147      if (opParam != null) {
148        DeregisterOperatorParameterEvents(opParam);
149      }
150    }
151
152    private void RegisterOperatorsEvents() {
153      if (operators != null) {
154        operators.ItemsAdded += new CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsAdded);
155        operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsRemoved);
156        operators.CollectionReset += new CollectionItemsChangedEventHandler<IOperator>(Operators_CollectionReset);
157        foreach (IOperator op in operators) {
158          RegisterOperatorEvents(op);
159          foreach (IParameter param in op.Parameters) {
160            IValueParameter<IOperator> opParam = param as IValueParameter<IOperator>;
161            if (opParam != null) RegisterOperatorParameterEvents(opParam);
162          }
163        }
164      }
165    }
166    private void DeregisterOperatorsEvents() {
167      if (operators != null) {
168        foreach (IOperator op in operators) {
169          foreach (IParameter param in op.Parameters) {
170            IValueParameter<IOperator> opParam = param as IValueParameter<IOperator>;
171            if (opParam != null) DeregisterOperatorParameterEvents(opParam);
172          }
173          DeregisterOperatorEvents(op);
174        }
175        operators.ItemsAdded -= new CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsAdded);
176        operators.ItemsRemoved -= new CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsRemoved);
177        operators.CollectionReset -= new CollectionItemsChangedEventHandler<IOperator>(Operators_CollectionReset);
178      }
179    }
180    private void RegisterOperatorEvents(IOperator op) {
181      op.Parameters.ItemsAdded += new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsAdded);
182      op.Parameters.ItemsRemoved += new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsRemoved);
183      op.Parameters.ItemsReplaced += new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsReplaced);
184      op.Parameters.CollectionReset += new CollectionItemsChangedEventHandler<IParameter>(Parameters_CollectionReset);
185    }
186    private void DeregisterOperatorEvents(IOperator op) {
187      op.Parameters.ItemsAdded -= new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsAdded);
188      op.Parameters.ItemsRemoved -= new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsRemoved);
189      op.Parameters.ItemsReplaced -= new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsReplaced);
190      op.Parameters.CollectionReset -= new CollectionItemsChangedEventHandler<IParameter>(Parameters_CollectionReset);
191    }
192    private void RegisterOperatorParameterEvents(IValueParameter<IOperator> opParam) {
193      opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
194    }
195    private void DeregisterOperatorParameterEvents(IValueParameter<IOperator> opParam) {
196      opParam.ValueChanged -= new EventHandler(opParam_ValueChanged);
197    }
198
199    private void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IOperator> e) {
200      foreach (IOperator op in e.Items)
201        AddOperator(op);
202    }
203    private void Operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IOperator> e) {
204      foreach (IOperator op in e.Items)
205        RemoveOperator(op);
206      if (!Operators.Contains(InitialOperator)) InitialOperator = null;
207    }
208    private void Operators_CollectionReset(object sender, CollectionItemsChangedEventArgs<IOperator> e) {
209      foreach (IOperator op in e.OldItems)
210        RemoveOperator(op);
211      foreach (IOperator op in e.Items)
212        AddOperator(op);
213      if (!Operators.Contains(InitialOperator)) InitialOperator = null;
214    }
215    private void Parameters_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
216      foreach (IParameter param in e.Items)
217        AddParameter(param);
218    }
219    private void Parameters_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
220      foreach (IParameter param in e.Items)
221        RemoveParameter(param);
222    }
223    private void Parameters_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
224      foreach (IParameter param in e.OldItems)
225        RemoveParameter(param);
226      foreach (IParameter param in e.Items)
227        AddParameter(param);
228    }
229    private void Parameters_CollectionReset(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
230      foreach (IParameter param in e.OldItems)
231        RemoveParameter(param);
232      foreach (IParameter param in e.Items)
233        AddParameter(param);
234    }
235    private void opParam_ValueChanged(object sender, EventArgs e) {
236      IValueParameter<IOperator> opParam = (IValueParameter<IOperator>)sender;
237      if (opParam.Value != null) Operators.Add(opParam.Value);
238    }
239    #endregion
240  }
241}
Note: See TracBrowser for help on using the repository browser.