Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3386 was 3386, checked in by mkommend, 13 years ago

completly refactored the graph visualization (ticket #867)

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