Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Operators.Views.GraphVisualization/3.3/OperatorGraphVisualization/OperatorGraphVisualizationInfo.cs @ 17078

Last change on this file since 17078 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

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