Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Operators.Views.GraphVisualization/3.3/OperatorGraphVisualization/OperatorGraphVisualizationInfo.cs @ 11642

Last change on this file since 11642 was 11185, checked in by pfleck, 10 years ago

#2208 merged trunk and updated version info

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