Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Operators.Views.GraphVisualization/3.3/OperatorGraphVisualization/OperatorGraphVisualizationInfo.cs @ 11733

Last change on this file since 11733 was 6760, checked in by epitzer, 13 years ago

#1530 integrate changes from trunk

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