Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RegressionBenchmarks/HeuristicLab.Core/3.3/OperatorGraph.cs @ 7192

Last change on this file since 7192 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 10.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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  [StorableClass]
34  public class OperatorGraph : Item, IStorableContent {
35    public string Filename { get; set; }
36
37    [Storable]
38    private OperatorSet operators;
39    /// <summary>
40    /// Gets all operators of the current instance.
41    /// </summary>
42    public OperatorSet Operators {
43      get { return operators; }
44    }
45
46    [Storable]
47    private IOperator initialOperator;
48    /// <summary>
49    /// Gets or sets the initial operator (the starting one).
50    /// </summary>
51    /// <remarks>Calls <see cref="OnInitialOperatorChanged"/> in the setter.</remarks>
52    public IOperator InitialOperator {
53      get { return initialOperator; }
54      set {
55        if (initialOperator != value) {
56          if (value != null) Operators.Add(value);
57          initialOperator = value;
58          OnInitialOperatorChanged();
59        }
60      }
61    }
62
63    [Storable]
64    private IDeepCloneable visualizationInfo;
65    /// <summary>
66    /// Gets or sets the visualizationInfo.
67    /// </summary>
68    /// /// <remarks>The VisualizationInfo can only be set once and fires afterwards and InvalidOperationException</remarks>
69    public IDeepCloneable VisualizationInfo {
70      get { return visualizationInfo; }
71      set {
72        if (visualizationInfo != null)
73          throw new InvalidOperationException("The value of the property VisualizationInfo is already set and cannot be set again.");
74        visualizationInfo = value;
75      }
76    }
77
78    [StorableConstructor]
79    protected OperatorGraph(bool deserializing) : base(deserializing) { }
80    protected OperatorGraph(OperatorGraph original, Cloner cloner)
81      : base(original, cloner) {
82      operators = cloner.Clone(original.operators);
83      initialOperator = cloner.Clone(original.initialOperator);
84      visualizationInfo = cloner.Clone(original.visualizationInfo);
85      Initialize();
86    }
87    /// <summary>
88    /// Initializes a new instance of <see cref="OperatorGraph"/>.
89    /// </summary>
90    public OperatorGraph() {
91      operators = new OperatorSet();
92      initialOperator = null;
93      visualizationInfo = null;
94      Initialize();
95    }
96
97    //mkommend: IMPORTANT DO NOT REMOVE THIS EVENT
98    //needed to register OperatorGraph events in GraphVisualizationInfo
99    public event EventHandler DeserializationFinished;
100    private void OnOperatorGraphDeserializationFinished() {
101      EventHandler handler = DeserializationFinished;
102      if (handler != null) handler(this, EventArgs.Empty);
103    }
104
105    [StorableHook(HookType.AfterDeserialization)]
106    private void AfterDeserialization() {
107      Initialize();
108      OnOperatorGraphDeserializationFinished();
109    }
110    private void Initialize() {
111      RegisterOperatorsEvents();
112    }
113
114    /// <summary>
115    /// Clones the current instance (deep clone).
116    /// </summary>
117    /// <remarks>Deep clone through <see cref="cloner.Clone"/> method of helper class
118    /// <see cref="Auxiliary"/>.</remarks>
119    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
120    /// <returns>The cloned object as <see cref="OperatorGraph"/>.</returns>
121    public override IDeepCloneable Clone(Cloner cloner) {
122      return new OperatorGraph(this, cloner);
123    }
124
125    /// <inheritdoc/>
126    public event EventHandler InitialOperatorChanged;
127    /// <summary>
128    /// Fires a new <c>InitialOperatorChanged</c> event.
129    /// </summary>
130    protected virtual void OnInitialOperatorChanged() {
131      var handler = InitialOperatorChanged;
132      if (handler != null) handler(this, EventArgs.Empty);
133    }
134
135    #region Operators Events
136    private void AddOperator(IOperator op) {
137      RegisterOperatorEvents(op);
138      foreach (IParameter param in op.Parameters)
139        AddParameter(param);
140    }
141    private void RemoveOperator(IOperator op) {
142      foreach (IParameter param in op.Parameters)
143        RemoveParameter(param);
144      DeregisterOperatorEvents(op);
145
146      // remove edges to removed operator
147      IValueParameter[] opParams = (from o in Operators
148                                    from p in o.Parameters
149                                    where p is IValueParameter
150                                    where typeof(IOperator).IsAssignableFrom(((IValueParameter)p).DataType)
151                                    where (((IValueParameter)p).Value != null) && (((IValueParameter)p).Value == op)
152                                    select (IValueParameter)p).ToArray();
153      foreach (IValueParameter opParam in opParams)
154        opParam.Value = null;
155    }
156    private void AddParameter(IParameter param) {
157      IValueParameter valueParam = param as IValueParameter;
158      if ((valueParam != null) && (typeof(IOperator).IsAssignableFrom(valueParam.DataType))) {
159        RegisterOperatorParameterEvents(valueParam);
160        if (valueParam.Value != null) Operators.Add((IOperator)valueParam.Value);
161      }
162    }
163    private void RemoveParameter(IParameter param) {
164      IValueParameter valueParam = param as IValueParameter;
165      if ((valueParam != null) && (typeof(IOperator).IsAssignableFrom(valueParam.DataType))) {
166        DeregisterOperatorParameterEvents(valueParam);
167      }
168    }
169
170    private void RegisterOperatorsEvents() {
171      if (operators != null) {
172        operators.ItemsAdded += new CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsAdded);
173        operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsRemoved);
174        operators.CollectionReset += new CollectionItemsChangedEventHandler<IOperator>(Operators_CollectionReset);
175        foreach (IOperator op in operators) {
176          RegisterOperatorEvents(op);
177          var opParams = from p in op.Parameters
178                         where p is IValueParameter
179                         where typeof(IOperator).IsAssignableFrom(((IValueParameter)p).DataType)
180                         select (IValueParameter)p;
181          foreach (IValueParameter opParam in opParams)
182            RegisterOperatorParameterEvents(opParam);
183        }
184      }
185    }
186    private void RegisterOperatorEvents(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 DeregisterOperatorEvents(IOperator op) {
193      op.Parameters.ItemsAdded -= new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsAdded);
194      op.Parameters.ItemsRemoved -= new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsRemoved);
195      op.Parameters.ItemsReplaced -= new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsReplaced);
196      op.Parameters.CollectionReset -= new CollectionItemsChangedEventHandler<IParameter>(Parameters_CollectionReset);
197    }
198    private void RegisterOperatorParameterEvents(IValueParameter opParam) {
199      opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
200    }
201    private void DeregisterOperatorParameterEvents(IValueParameter opParam) {
202      opParam.ValueChanged -= new EventHandler(opParam_ValueChanged);
203    }
204
205    private void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IOperator> e) {
206      foreach (IOperator op in e.Items)
207        AddOperator(op);
208    }
209    private void Operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IOperator> e) {
210      foreach (IOperator op in e.Items)
211        RemoveOperator(op);
212      if (!Operators.Contains(InitialOperator)) InitialOperator = null;
213    }
214    private void Operators_CollectionReset(object sender, CollectionItemsChangedEventArgs<IOperator> e) {
215      foreach (IOperator op in e.OldItems)
216        RemoveOperator(op);
217      foreach (IOperator op in e.Items)
218        AddOperator(op);
219      if (!Operators.Contains(InitialOperator)) InitialOperator = null;
220    }
221    private void Parameters_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
222      foreach (IParameter param in e.Items)
223        AddParameter(param);
224    }
225    private void Parameters_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
226      foreach (IParameter param in e.Items)
227        RemoveParameter(param);
228    }
229    private void Parameters_ItemsReplaced(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 Parameters_CollectionReset(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
236      foreach (IParameter param in e.OldItems)
237        RemoveParameter(param);
238      foreach (IParameter param in e.Items)
239        AddParameter(param);
240    }
241    private void opParam_ValueChanged(object sender, EventArgs e) {
242      IValueParameter opParam = (IValueParameter)sender;
243      if (opParam.Value != null) Operators.Add((IOperator)opParam.Value);
244    }
245    #endregion
246  }
247}
Note: See TracBrowser for help on using the repository browser.