Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Operators.Views.GraphVisualization/3.3/OperatorGraphVisualization/OperatorGraphVisualizationInfo.cs @ 9333

Last change on this file since 9333 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

File size: 21.5 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.Drawing;
25using System.Linq;
26using HeuristicLab.Collections;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Operators.Views.GraphVisualization {
32  [StorableClass]
33  public sealed class OperatorGraphVisualizationInfo : GraphVisualizationInfo {
34    [Storable]
35    private BidirectionalLookup<IOperator, IOperatorShapeInfo> operatorShapeInfoMapping;
36    private BidirectionalLookup<IOperator, IKeyedItemCollection<string, IParameter>> operatorParameterCollectionMapping;
37    private Dictionary<IParameter, IOperator> parameterOperatorMapping;
38
39    private OperatorGraphVisualizationInfo()
40      : base() {
41      this.operatorShapeInfoMapping = new BidirectionalLookup<IOperator, IOperatorShapeInfo>();
42      this.operatorParameterCollectionMapping = new BidirectionalLookup<IOperator, IKeyedItemCollection<string, IParameter>>();
43      this.parameterOperatorMapping = new Dictionary<IParameter, IOperator>();
44    }
45
46    [StorableConstructor]
47    private OperatorGraphVisualizationInfo(bool deserializing)
48      : base(deserializing) {
49      this.operatorParameterCollectionMapping = new BidirectionalLookup<IOperator, IKeyedItemCollection<string, IParameter>>();
50      this.parameterOperatorMapping = new Dictionary<IParameter, IOperator>();
51    }
52    private OperatorGraphVisualizationInfo(OperatorGraphVisualizationInfo original, Cloner cloner)
53      : base(original, cloner) {
54      operatorShapeInfoMapping = new BidirectionalLookup<IOperator, IOperatorShapeInfo>();
55      operatorParameterCollectionMapping = new BidirectionalLookup<IOperator, IKeyedItemCollection<string, IParameter>>();
56      parameterOperatorMapping = new Dictionary<IParameter, IOperator>();
57
58      operatorGraph = cloner.Clone(original.operatorGraph);
59      RegisterOperatorGraphEvents();
60      oldInitialShape = cloner.Clone(original.oldInitialShape);
61      oldInitialShapeColor = original.oldInitialShapeColor;
62
63      foreach (KeyValuePair<IOperator, IOperatorShapeInfo> pair in original.operatorShapeInfoMapping.FirstEnumerable) {
64        IOperator op = cloner.Clone(pair.Key);
65        IOperatorShapeInfo shapeInfo = cloner.Clone(pair.Value);
66        RegisterOperatorEvents(op);
67        operatorParameterCollectionMapping.Add(op, pair.Key.Parameters);
68        operatorShapeInfoMapping.Add(op, shapeInfo);
69      }
70
71      foreach (IOperator oper in operatorShapeInfoMapping.FirstValues) {
72        foreach (IParameter param in oper.Parameters) {
73          parameterOperatorMapping.Add(param, oper);
74          IValueParameter opParam = param as IValueParameter;
75          if (opParam != null && typeof(IOperator).IsAssignableFrom(param.DataType))
76            RegisterOperatorParameterEvents(opParam);
77          else
78            RegisterParameterEvents(param);
79        }
80      }
81      if (original.operatorGraph.InitialOperator != null) {
82        IOperatorShapeInfo newInitialShapeInfo = original.operatorShapeInfoMapping.GetByFirst(original.operatorGraph.InitialOperator);
83        if (newInitialShapeInfo != null) {
84          oldInitialShapeColor = newInitialShapeInfo.Color;
85          newInitialShapeInfo.Color = Color.LightGreen;
86        }
87        oldInitialShape = InitialShape;
88        OnInitialShapeChanged();
89      }
90    }
91    public override IDeepCloneable Clone(Cloner cloner) {
92      return new OperatorGraphVisualizationInfo(this, cloner);
93    }
94
95    public OperatorGraphVisualizationInfo(OperatorGraph operatorGraph)
96      : this() {
97      this.operatorGraph = operatorGraph;
98      this.RegisterOperatorGraphEvents();
99
100      foreach (IOperator op in operatorGraph.Operators)
101        if (!this.operatorShapeInfoMapping.ContainsFirst(op))  //could be added by referencing parameters
102          this.AddOperator(op);
103
104      this.UpdateInitialShape();
105    }
106
107    [StorableHook(HookType.AfterDeserialization)]
108    private void AfterDeserialization() {
109      this.operatorGraph.DeserializationFinished += new EventHandler(operatorGraph_DeserializationFinished);
110
111      IOperator op;
112      IOperatorShapeInfo shapeInfo;
113      foreach (KeyValuePair<IOperator, IOperatorShapeInfo> pair in this.operatorShapeInfoMapping.FirstEnumerable) {
114        op = pair.Key;
115        shapeInfo = pair.Value;
116        shapeInfo.Icon = new Bitmap(op.ItemImage);
117        this.RegisterOperatorEvents(op);
118        this.operatorParameterCollectionMapping.Add(op, op.Parameters);
119      }
120
121      foreach (IOperator oper in this.operatorShapeInfoMapping.FirstValues) {
122        foreach (IParameter param in oper.Parameters) {
123          IValueParameter opParam = param as IValueParameter;
124          this.parameterOperatorMapping.Add(param, oper);
125          if (opParam != null && typeof(IOperator).IsAssignableFrom(param.DataType))
126            this.RegisterOperatorParameterEvents(opParam);
127          else
128            this.RegisterParameterEvents(param);
129        }
130      }
131    }
132
133    private void operatorGraph_DeserializationFinished(object sender, EventArgs e) {
134      this.RegisterOperatorGraphEvents();
135      if (this.operatorGraph.InitialOperator != null) {
136        IOperatorShapeInfo newInitialShapeInfo = this.operatorShapeInfoMapping.GetByFirst(this.operatorGraph.InitialOperator);
137        if (newInitialShapeInfo != null) {
138          oldInitialShapeColor = newInitialShapeInfo.Color;
139          newInitialShapeInfo.Color = Color.LightGreen;
140        }
141        oldInitialShape = this.InitialShape;
142        this.OnInitialShapeChanged();
143      }
144      this.operatorGraph.DeserializationFinished -= new EventHandler(operatorGraph_DeserializationFinished);
145    }
146
147    internal IOperator GetOperatorForShapeInfo(IOperatorShapeInfo shapeInfo) {
148      return this.operatorShapeInfoMapping.GetBySecond(shapeInfo);
149    }
150
151    private void operatorGraph_InitialOperatorChanged(object sender, EventArgs e) {
152      this.UpdateInitialShape();
153    }
154
155    private void UpdateInitialShape() {
156      IOperatorShapeInfo old = this.oldInitialShape as OperatorShapeInfo;
157      if (old != null)
158        old.Color = oldInitialShapeColor;
159
160      OperatorShapeInfo newInitialShapeInfo = this.InitialShape as OperatorShapeInfo;
161      if (newInitialShapeInfo != null) {
162        oldInitialShapeColor = newInitialShapeInfo.Color;
163        newInitialShapeInfo.Color = Color.LightGreen;
164      }
165
166      oldInitialShape = this.InitialShape;
167      this.OnInitialShapeChanged();
168    }
169
170    private IShapeInfo oldInitialShape;
171    private Color oldInitialShapeColor;
172    public override IShapeInfo InitialShape {
173      get {
174        IOperator op = this.operatorGraph.InitialOperator;
175        if (op == null)
176          return null;
177        return this.operatorShapeInfoMapping.GetByFirst(op);
178      }
179      set {
180        if (value == null)
181          this.OperatorGraph.InitialOperator = null;
182        else {
183          IOperatorShapeInfo shapeInfo = (IOperatorShapeInfo)value;
184          this.OperatorGraph.InitialOperator = this.operatorShapeInfoMapping.GetBySecond(shapeInfo);
185        }
186      }
187    }
188
189    [Storable]
190    private OperatorGraph operatorGraph;
191    public OperatorGraph OperatorGraph {
192      get { return this.operatorGraph; }
193    }
194
195    private void RegisterOperatorGraphEvents() {
196      this.operatorGraph.InitialOperatorChanged += new EventHandler(operatorGraph_InitialOperatorChanged);
197      this.operatorGraph.Operators.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsAdded);
198      this.operatorGraph.Operators.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsRemoved);
199      this.operatorGraph.Operators.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IOperator>(Operators_CollectionReset);
200    }
201
202    private void DeregisterOperatorGraphEvents() {
203      this.operatorGraph.InitialOperatorChanged -= new EventHandler(operatorGraph_InitialOperatorChanged);
204      this.operatorGraph.Operators.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsAdded);
205      this.operatorGraph.Operators.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsRemoved);
206      this.operatorGraph.Operators.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IOperator>(Operators_CollectionReset);
207    }
208
209    #region methods to manipulate operatorgraph by the shape info
210    internal void AddShapeInfo(IOperator op, IOperatorShapeInfo shapeInfo) {
211      this.RegisterOperatorEvents(op);
212      this.operatorParameterCollectionMapping.Add(op, op.Parameters);
213      this.operatorShapeInfoMapping.Add(op, shapeInfo);
214      this.shapeInfos.Add(shapeInfo);
215
216      foreach (IParameter param in op.Parameters)
217        this.AddParameter(op, param);
218
219      this.operatorGraph.Operators.Add(op);
220    }
221
222    public override void RemoveShapeInfo(IShapeInfo shapeInfo) {
223      IOperatorShapeInfo opShapeInfo = (IOperatorShapeInfo)shapeInfo;
224      if (this.operatorShapeInfoMapping.ContainsSecond(opShapeInfo)) {
225        IOperator op = this.operatorShapeInfoMapping.GetBySecond(opShapeInfo);
226        this.operatorGraph.Operators.Remove(op);
227      }
228    }
229
230    public override void RemoveConnectionInfo(IConnectionInfo connectionInfo) {
231      IOperatorShapeInfo shapeInfo = (IOperatorShapeInfo)connectionInfo.From;
232      IOperator op = this.operatorShapeInfoMapping.GetBySecond(shapeInfo);
233      IValueParameter param = (IValueParameter)op.Parameters[connectionInfo.ConnectorFrom];
234      param.Value = null;
235    }
236
237    public override void AddConnectionInfo(IConnectionInfo connectionInfo) {
238      IOperatorShapeInfo shapeInfo = (IOperatorShapeInfo)connectionInfo.From;
239      IOperator op = this.operatorShapeInfoMapping.GetBySecond(shapeInfo);
240      IOperatorShapeInfo shapeInfoTo = (IOperatorShapeInfo)connectionInfo.To;
241      IOperator opTo = this.operatorShapeInfoMapping.GetBySecond(shapeInfoTo);
242      IValueParameter param = (IValueParameter)op.Parameters.Where(p => p.Name == connectionInfo.ConnectorFrom).SingleOrDefault();
243      if (param != null)
244        param.Value = opTo;
245    }
246    #endregion
247
248    #region operator events
249    private void AddOperator(IOperator op) {
250      if (!this.operatorShapeInfoMapping.ContainsFirst(op)) {
251        this.RegisterOperatorEvents(op);
252        IOperatorShapeInfo shapeInfo = OperatorShapeInfoFactory.CreateOperatorShapeInfo(op);
253        this.operatorParameterCollectionMapping.Add(op, op.Parameters);
254        this.operatorShapeInfoMapping.Add(op, shapeInfo);
255        this.shapeInfos.Add(shapeInfo);
256        foreach (IParameter param in op.Parameters)
257          this.AddParameter(op, param);
258      }
259    }
260    private void RemoveOperator(IOperator op) {
261      this.DeregisterOperatorEvents(op);
262      foreach (IParameter param in op.Parameters)
263        this.RemoveParameter(op, param);
264
265      IOperatorShapeInfo shapeInfo = this.operatorShapeInfoMapping.GetByFirst(op);
266      this.operatorParameterCollectionMapping.RemoveByFirst(op);
267      this.operatorShapeInfoMapping.RemoveByFirst(op);
268      this.shapeInfos.Remove(shapeInfo);
269    }
270
271    private void OperatorBreakpointChanged(object sender, EventArgs e) {
272      IOperator op = (IOperator)sender;
273      IOperatorShapeInfo operatorShapeInfo = this.operatorShapeInfoMapping.GetByFirst(op);
274      if (op.Breakpoint) {
275        operatorShapeInfo.LineColor = Color.Red;
276        operatorShapeInfo.LineWidth = 2;
277      } else {
278        operatorShapeInfo.LineColor = Color.Black;
279        operatorShapeInfo.LineWidth = 1;
280      }
281    }
282
283    private void OperatorItemImageChanged(object sender, EventArgs e) {
284      IOperator op = (IOperator)sender;
285      IOperatorShapeInfo operatorShapeInfo = this.operatorShapeInfoMapping.GetByFirst(op);
286      operatorShapeInfo.Icon = new Bitmap(op.ItemImage);
287    }
288
289    private void OperatorNameChanged(object sender, EventArgs e) {
290      IOperator op = (IOperator)sender;
291      IOperatorShapeInfo operatorShapeInfo = this.operatorShapeInfoMapping.GetByFirst(op);
292      operatorShapeInfo.Title = op.Name;
293    }
294
295    private void Operators_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IOperator> e) {
296      foreach (IOperator op in e.Items)
297        this.AddOperator(op);
298    }
299    private void Operators_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IOperator> e) {
300      foreach (IOperator op in e.Items)
301        this.RemoveOperator(op);
302    }
303    private void Operators_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IOperator> e) {
304      foreach (IOperator op in e.OldItems)
305        this.RemoveOperator(op);
306      foreach (IOperator op in e.Items)
307        this.AddOperator(op);
308    }
309
310    private void RegisterOperatorEvents(IOperator op) {
311      op.Parameters.ItemsAdded += new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsAdded);
312      op.Parameters.ItemsRemoved += new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsRemoved);
313      op.Parameters.ItemsReplaced += new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsReplaced);
314      op.Parameters.CollectionReset += new CollectionItemsChangedEventHandler<IParameter>(Parameters_CollectionReset);
315      op.NameChanged += new EventHandler(OperatorNameChanged);
316      op.ItemImageChanged += new EventHandler(OperatorItemImageChanged);
317      op.BreakpointChanged += new EventHandler(OperatorBreakpointChanged);
318    }
319
320    private void DeregisterOperatorEvents(IOperator op) {
321      op.Parameters.ItemsAdded -= new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsAdded);
322      op.Parameters.ItemsRemoved -= new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsRemoved);
323      op.Parameters.ItemsReplaced -= new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsReplaced);
324      op.Parameters.CollectionReset -= new CollectionItemsChangedEventHandler<IParameter>(Parameters_CollectionReset);
325      op.NameChanged -= new EventHandler(OperatorNameChanged);
326      op.ItemImageChanged -= new EventHandler(OperatorItemImageChanged);
327      op.BreakpointChanged -= new EventHandler(OperatorBreakpointChanged);
328    }
329    #endregion
330
331    #region parameter events
332    private void AddParameter(IOperator op, IParameter param) {
333      this.parameterOperatorMapping.Add(param, op);
334      IValueParameter opParam = param as IValueParameter;
335      if (opParam != null && typeof(IOperator).IsAssignableFrom(param.DataType)) {
336        this.RegisterOperatorParameterEvents(opParam);
337        IOperatorShapeInfo shapeInfoFrom = this.operatorShapeInfoMapping.GetByFirst(op);
338        shapeInfoFrom.AddConnector(param.Name);
339
340        if (opParam.Value != null) {
341          if (!this.operatorShapeInfoMapping.ContainsFirst((IOperator)opParam.Value))
342            this.AddOperator((IOperator)opParam.Value);
343          IOperatorShapeInfo shapeInfoTo = this.operatorShapeInfoMapping.GetByFirst((IOperator)opParam.Value);
344          this.connectionInfos.Add(new ConnectionInfo(shapeInfoFrom, param.Name, shapeInfoTo, OperatorShapeInfoFactory.PredecessorConnector));
345        }
346      } else
347        this.RegisterParameterEvents(param);
348    }
349
350    private void RemoveParameter(IOperator op, IParameter param) {
351      IValueParameter opParam = param as IValueParameter;
352      if (opParam != null && typeof(IOperator).IsAssignableFrom(param.DataType)) {
353        this.DeregisterOperatorParameterEvents(opParam);
354        IOperatorShapeInfo shapeInfo = this.operatorShapeInfoMapping.GetByFirst(op);
355        this.connectionInfos.RemoveWhere(c => c.From == shapeInfo && c.ConnectorFrom == param.Name);
356        this.connectionInfos.RemoveWhere(c => c.To == shapeInfo && c.ConnectorTo == param.Name);
357        shapeInfo.RemoveConnector(param.Name);
358      } else
359        this.DeregisterParameterEvents(param);
360
361      this.parameterOperatorMapping.Remove(param);
362    }
363
364    private void opParam_ValueChanged(object sender, EventArgs e) {
365      IValueParameter opParam = (IValueParameter)sender;
366      if (this.parameterOperatorMapping.ContainsKey(opParam)) {
367        IOperator op = this.parameterOperatorMapping[opParam];
368        IOperatorShapeInfo shapeInfoFrom = this.operatorShapeInfoMapping.GetByFirst(op);
369        KeyValuePair<IOperatorShapeInfo, string> connectionFrom = new KeyValuePair<IOperatorShapeInfo, string>(shapeInfoFrom, opParam.Name);
370
371        this.connectionInfos.RemoveWhere(c => c.From == shapeInfoFrom && c.ConnectorFrom == opParam.Name);
372        if (opParam.Value != null) {
373          if (!this.operatorShapeInfoMapping.ContainsFirst((IOperator)opParam.Value))
374            this.AddOperator((IOperator)opParam.Value);
375          IOperatorShapeInfo shapeInfoTo = this.operatorShapeInfoMapping.GetByFirst((IOperator)opParam.Value);
376          base.AddConnectionInfo(new ConnectionInfo(shapeInfoFrom, opParam.Name, shapeInfoTo, OperatorShapeInfoFactory.PredecessorConnector));
377        }
378      }
379    }
380
381    private void Parameters_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
382      IKeyedItemCollection<string, IParameter> parameterCollection = sender as IKeyedItemCollection<string, IParameter>;
383      IOperator op = this.operatorParameterCollectionMapping.GetBySecond(parameterCollection);
384      foreach (IParameter param in e.Items)
385        AddParameter(op, param);
386      this.UpdateParameterLabels(op);
387    }
388    private void Parameters_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
389      IKeyedItemCollection<string, IParameter> parameterCollection = sender as IKeyedItemCollection<string, IParameter>;
390      IOperator op = this.operatorParameterCollectionMapping.GetBySecond(parameterCollection);
391      foreach (IParameter param in e.Items)
392        RemoveParameter(op, param);
393      this.UpdateParameterLabels(op);
394    }
395    private void Parameters_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
396      IKeyedItemCollection<string, IParameter> parameterCollection = sender as IKeyedItemCollection<string, IParameter>;
397      IOperator op = this.operatorParameterCollectionMapping.GetBySecond(parameterCollection);
398      foreach (IParameter param in e.OldItems)
399        RemoveParameter(op, param);
400      foreach (IParameter param in e.Items)
401        AddParameter(op, param);
402      this.UpdateParameterLabels(op);
403    }
404    private void Parameters_CollectionReset(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
405      IKeyedItemCollection<string, IParameter> parameterCollection = sender as IKeyedItemCollection<string, IParameter>;
406      IOperator op = this.operatorParameterCollectionMapping.GetBySecond(parameterCollection);
407      foreach (IParameter param in e.OldItems)
408        RemoveParameter(op, param);
409      foreach (IParameter param in e.Items)
410        AddParameter(op, param);
411      this.UpdateParameterLabels(op);
412    }
413
414    private void RegisterOperatorParameterEvents(IValueParameter opParam) {
415      opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
416    }
417    private void DeregisterOperatorParameterEvents(IValueParameter opParam) {
418      opParam.ValueChanged -= new EventHandler(opParam_ValueChanged);
419    }
420    private void RegisterParameterEvents(IParameter param) {
421      param.ToStringChanged += new EventHandler(param_ToStringChanged);
422      param.NameChanged += new EventHandler(param_NameChanged);
423    }
424    private void DeregisterParameterEvents(IParameter param) {
425      param.ToStringChanged -= new EventHandler(param_ToStringChanged);
426      param.NameChanged -= new EventHandler(param_NameChanged);
427    }
428
429    private void param_NameChanged(object sender, EventArgs e) {
430      IParameter param = (IParameter)sender;
431      IOperator op = this.parameterOperatorMapping[param];
432      this.UpdateParameterLabels(op);
433    }
434    private void param_ToStringChanged(object sender, EventArgs e) {
435      IParameter param = (IParameter)sender;
436      IOperator op = this.parameterOperatorMapping[param];
437      this.UpdateParameterLabels(op);
438    }
439
440    private void UpdateParameterLabels(IOperator op) {
441      IEnumerable<IParameter> parameters = op.Parameters.Where(p => !(p is IValueParameter && typeof(IOperator).IsAssignableFrom(p.DataType)));
442      IOperatorShapeInfo operatorShapeInfo = this.operatorShapeInfoMapping.GetByFirst(op);
443      if (parameters.Count() > 0)
444        operatorShapeInfo.UpdateLabels(parameters.Select(p => p.ToString()));
445      else
446        operatorShapeInfo.UpdateLabels(new List<string>());
447    }
448    #endregion
449  }
450}
Note: See TracBrowser for help on using the repository browser.