Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4070 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 10.3 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.Common;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Core {
29  /// <summary>
30  /// Represents a graph of operators.
31  /// </summary>
32  [Item("Operator Graph", "Represents a graph of operators.")]
33  [Creatable("Algorithm Design")]
34  [StorableClass]
35  public class OperatorGraph : Item {
36    [Storable]
37    private OperatorSet operators;
38    /// <summary>
39    /// Gets all operators of the current instance.
40    /// </summary>
41    public OperatorSet Operators {
42      get { return operators; }
43    }
44
45    [Storable]
46    private IOperator initialOperator;
47    /// <summary>
48    /// Gets or sets the initial operator (the starting one).
49    /// </summary>
50    /// <remarks>Calls <see cref="OnInitialOperatorChanged"/> in the setter.</remarks>
51    public IOperator InitialOperator {
52      get { return initialOperator; }
53      set {
54        if (initialOperator != value) {
55          if (value != null) Operators.Add(value);
56          initialOperator = value;
57          OnInitialOperatorChanged();
58        }
59      }
60    }
61
62    [Storable]
63    private IDeepCloneable visualizationInfo;
64    /// <summary>
65    /// Gets or sets the visualizationInfo.
66    /// </summary>
67    /// /// <remarks>The VisualizationInfo can only be set once and fires afterwards and InvalidOperationException</remarks>
68    public IDeepCloneable VisualizationInfo {
69      get { return visualizationInfo; }
70      set {
71        if (visualizationInfo != null)
72          throw new InvalidOperationException("The value of the property VisualizationInfo is already set and cannot be set again.");
73        visualizationInfo = value;
74      }
75    }
76
77    /// <summary>
78    /// Initializes a new instance of <see cref="OperatorGraph"/>.
79    /// </summary>
80    public OperatorGraph() {
81      operators = new OperatorSet();
82      initialOperator = null;
83      visualizationInfo = null;
84      Initialize();
85    }
86    [StorableConstructor]
87    protected OperatorGraph(bool deserializing) : base(deserializing) { }
88
89    //mkommend: IMPORTANT DO NOT REMOVE THIS PRIVATE EVENT
90    //needed to register OperatorGraph events in GraphVisualizationInfo
91    public event EventHandler DeserializationFinished;
92    private void OnOperatorGraphDeserializationFinished() {
93      EventHandler handler = DeserializationFinished;
94      if (handler != null)
95        handler(this, EventArgs.Empty);
96    }
97    [StorableHook(HookType.AfterDeserialization)]
98    private void Initialize() {
99      RegisterOperatorsEvents();
100      OnOperatorGraphDeserializationFinished();
101    }
102
103    /// <summary>
104    /// Clones the current instance (deep clone).
105    /// </summary>
106    /// <remarks>Deep clone through <see cref="cloner.Clone"/> method of helper class
107    /// <see cref="Auxiliary"/>.</remarks>
108    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
109    /// <returns>The cloned object as <see cref="OperatorGraph"/>.</returns>
110    public override IDeepCloneable Clone(Cloner cloner) {
111      OperatorGraph clone = (OperatorGraph)base.Clone(cloner);
112      clone.operators = (OperatorSet)cloner.Clone(operators);
113      clone.initialOperator = (IOperator)cloner.Clone(initialOperator);
114      clone.visualizationInfo = cloner.Clone(visualizationInfo);
115      clone.Initialize();
116      return clone;
117    }
118
119    /// <inheritdoc/>
120    public event EventHandler InitialOperatorChanged;
121    /// <summary>
122    /// Fires a new <c>InitialOperatorChanged</c> event.
123    /// </summary>
124    protected virtual void OnInitialOperatorChanged() {
125      if (InitialOperatorChanged != null)
126        InitialOperatorChanged(this, EventArgs.Empty);
127    }
128
129    #region Operators Events
130    private void AddOperator(IOperator op) {
131      RegisterOperatorEvents(op);
132      foreach (IParameter param in op.Parameters)
133        AddParameter(param);
134    }
135    private void RemoveOperator(IOperator op) {
136      foreach (IParameter param in op.Parameters)
137        RemoveParameter(param);
138      DeregisterOperatorEvents(op);
139
140      // remove edges to removed operator
141      IValueParameter[] opParams = (from o in Operators
142                                    from p in o.Parameters
143                                    where p is IValueParameter
144                                    where typeof(IOperator).IsAssignableFrom(((IValueParameter)p).DataType)
145                                    where (((IValueParameter)p).Value != null) && (((IValueParameter)p).Value == op)
146                                    select (IValueParameter)p).ToArray();
147      foreach (IValueParameter opParam in opParams)
148        opParam.Value = null;
149    }
150    private void AddParameter(IParameter param) {
151      IValueParameter valueParam = param as IValueParameter;
152      if ((valueParam != null) && (typeof(IOperator).IsAssignableFrom(valueParam.DataType))) {
153        RegisterOperatorParameterEvents(valueParam);
154        if (valueParam.Value != null) Operators.Add((IOperator)valueParam.Value);
155      }
156    }
157    private void RemoveParameter(IParameter param) {
158      IValueParameter valueParam = param as IValueParameter;
159      if ((valueParam != null) && (typeof(IOperator).IsAssignableFrom(valueParam.DataType))) {
160        DeregisterOperatorParameterEvents(valueParam);
161      }
162    }
163
164    private void RegisterOperatorsEvents() {
165      if (operators != null) {
166        operators.ItemsAdded += new CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsAdded);
167        operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsRemoved);
168        operators.CollectionReset += new CollectionItemsChangedEventHandler<IOperator>(Operators_CollectionReset);
169        foreach (IOperator op in operators) {
170          RegisterOperatorEvents(op);
171          var opParams = from p in op.Parameters
172                         where p is IValueParameter
173                         where typeof(IOperator).IsAssignableFrom(((IValueParameter)p).DataType)
174                         select (IValueParameter)p;
175          foreach (IValueParameter opParam in opParams)
176            RegisterOperatorParameterEvents(opParam);
177        }
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 opParam) {
193      opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
194    }
195    private void DeregisterOperatorParameterEvents(IValueParameter 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 opParam = (IValueParameter)sender;
237      if (opParam.Value != null) Operators.Add((IOperator)opParam.Value);
238    }
239    #endregion
240  }
241}
Note: See TracBrowser for help on using the repository browser.