Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/17/10 23:50:05 (14 years ago)
Author:
mkommend
Message:

completly refactored the graph visualization (ticket #867)

Location:
trunk/sources/HeuristicLab.Operators.Views.GraphVisualization/3.3/OperatorGraphVisualization
Files:
1 added
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Operators.Views.GraphVisualization/3.3/OperatorGraphVisualization/OperatorGraphVisualizationInfo.cs

    r3374 r3386  
    2929using HeuristicLab.Collections;
    3030using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     31using HeuristicLab.Common;
    3132
    3233namespace HeuristicLab.Operators.Views.GraphVisualization {
    3334  [StorableClass]
    34   public sealed class GraphVisualizationInfo : DeepCloneable {
     35  public sealed class OperatorGraphVisualizationInfo : GraphVisualizationInfo {
     36    [Storable]
    3537    private BidirectionalLookup<IOperator, IOperatorShapeInfo> operatorShapeInfoMapping;
    36     [Storable]
    37     private BidirectionalLookup<IOperator, IOperatorShapeInfo> OperatorShapeInfoMappingStore {
    38       get { return this.operatorShapeInfoMapping; }
    39       set {
    40         IOperator op;
    41         IOperatorShapeInfo shapeInfo;
    42         foreach (KeyValuePair<IOperator, IOperatorShapeInfo> pair in value.FirstEnumerable) {
    43           op = pair.Key;
    44           shapeInfo = pair.Value;
    45           shapeInfo.Icon = new Bitmap(op.ItemImage);
    46           this.RegisterOperatorEvents(op);
    47           this.operatorParameterCollectionMapping.Add(op, pair.Key.Parameters);
    48           this.operatorShapeInfoMapping.Add(op, shapeInfo);
    49           this.shapeInfos.Add(shapeInfo);
    50         }
    51 
    52         foreach (IOperator oper in value.FirstValues) {
    53           foreach (IParameter param in oper.Parameters) {
    54             this.parameterOperatorMapping.Add(param, oper);
    55             IValueParameter<IOperator> opParam = param as IValueParameter<IOperator>;
    56             if (opParam != null) {
    57               this.RegisterOperatorParameterEvents(opParam);
    58               shapeInfo = this.operatorShapeInfoMapping.GetByFirst(oper);
    59               if (opParam.Value != null) {
    60                 this.connections.Add(new KeyValuePair<IOperatorShapeInfo, string>(shapeInfo, param.Name), this.operatorShapeInfoMapping.GetByFirst(opParam.Value));
    61               }
    62             } else
    63               this.RegisterParameterEvents(param);
    64           }
    65         }
    66       }
    67     }
    68 
    6938    private BidirectionalLookup<IOperator, IObservableKeyedCollection<string, IParameter>> operatorParameterCollectionMapping;
    7039    private Dictionary<IParameter, IOperator> parameterOperatorMapping;
    7140
    72     private GraphVisualizationInfo() {
     41    private OperatorGraphVisualizationInfo()
     42      : base() {
    7343      this.operatorShapeInfoMapping = new BidirectionalLookup<IOperator, IOperatorShapeInfo>();
    7444      this.operatorParameterCollectionMapping = new BidirectionalLookup<IOperator, IObservableKeyedCollection<string, IParameter>>();
    7545      this.parameterOperatorMapping = new Dictionary<IParameter, IOperator>();
    76 
    77       this.shapeInfos = new ObservableSet<IOperatorShapeInfo>();
    78       this.connections = new ObservableDictionary<KeyValuePair<IOperatorShapeInfo, string>, IOperatorShapeInfo>();
    79     }
    80 
    81     public GraphVisualizationInfo(OperatorGraph operatorGraph)
     46    }
     47
     48    [StorableConstructor]
     49    private OperatorGraphVisualizationInfo(bool deserializing)
     50      : base() {
     51      this.operatorParameterCollectionMapping = new BidirectionalLookup<IOperator, IObservableKeyedCollection<string, IParameter>>();
     52      this.parameterOperatorMapping = new Dictionary<IParameter, IOperator>();
     53    }
     54
     55    public OperatorGraphVisualizationInfo(OperatorGraph operatorGraph)
    8256      : this() {
    83       this.OperatorGraph = operatorGraph;
     57      this.operatorGraph = operatorGraph;
     58      this.RegisterOperatorGraphEvents();
    8459
    8560      foreach (IOperator op in operatorGraph.Operators)
    86         if (!this.operatorShapeInfoMapping.ContainsFirst(op))
     61        if (!this.operatorShapeInfoMapping.ContainsFirst(op))  //could be added by referencing parameters
    8762          this.AddOperator(op);
    8863
     
    9065    }
    9166
     67    [StorableHook(HookType.AfterDeserialization)]
     68    private void DeserializationHook() {
     69      this.operatorGraph.DeserializationFinished += new EventHandler(operatorGraph_DeserializationFinished);
     70
     71      IOperator op;
     72      IOperatorShapeInfo shapeInfo;
     73      foreach (KeyValuePair<IOperator, IOperatorShapeInfo> pair in this.operatorShapeInfoMapping.FirstEnumerable) {
     74        op = pair.Key;
     75        shapeInfo = pair.Value;
     76        shapeInfo.Icon = new Bitmap(op.ItemImage);
     77        this.RegisterOperatorEvents(op);
     78        this.operatorParameterCollectionMapping.Add(op, op.Parameters);
     79      }
     80
     81      foreach (IOperator oper in this.operatorShapeInfoMapping.FirstValues) {
     82        foreach (IParameter param in oper.Parameters) {
     83          this.parameterOperatorMapping.Add(param, oper);
     84          IValueParameter<IOperator> opParam = param as IValueParameter<IOperator>;
     85          if (opParam != null)
     86            this.RegisterOperatorParameterEvents(opParam);
     87          else
     88            this.RegisterParameterEvents(param);
     89        }
     90      }
     91    }
     92
     93    private void operatorGraph_DeserializationFinished(object sender, EventArgs e) {
     94      this.RegisterOperatorGraphEvents();
     95      if (this.operatorGraph.InitialOperator != null) {
     96        IOperatorShapeInfo newInitialShapeInfo = this.operatorShapeInfoMapping.GetByFirst(this.operatorGraph.InitialOperator);
     97        if (newInitialShapeInfo != null) {
     98          oldInitialShapeColor = newInitialShapeInfo.Color;
     99          newInitialShapeInfo.Color = Color.LightGreen;
     100        }
     101        oldInitialShape = this.InitialShape;
     102        this.OnInitialShapeChanged();
     103      }
     104      this.operatorGraph.DeserializationFinished -= new EventHandler(operatorGraph_DeserializationFinished);
     105    }
     106
    92107    public override IDeepCloneable Clone(Cloner cloner) {
    93       GraphVisualizationInfo clone = new GraphVisualizationInfo();
    94       cloner.RegisterClonedObject(this, clone);
     108      OperatorGraphVisualizationInfo clone = (OperatorGraphVisualizationInfo)base.Clone(cloner);
    95109      clone.operatorGraph = (OperatorGraph)cloner.Clone(this.operatorGraph);
     110      clone.RegisterOperatorGraphEvents();
    96111      clone.oldInitialShape = (IOperatorShapeInfo)cloner.Clone(this.oldInitialShape);
    97112      clone.oldInitialShapeColor = this.oldInitialShapeColor;
     
    105120        clone.operatorParameterCollectionMapping.Add(op, pair.Key.Parameters);
    106121        clone.operatorShapeInfoMapping.Add(op, shapeInfo);
    107         clone.shapeInfos.Add(shapeInfo);
    108122      }
    109123
     
    112126          clone.parameterOperatorMapping.Add(param, oper);
    113127          IValueParameter<IOperator> opParam = param as IValueParameter<IOperator>;
    114           if (opParam != null) {
     128          if (opParam != null)
    115129            clone.RegisterOperatorParameterEvents(opParam);
    116             shapeInfo = clone.operatorShapeInfoMapping.GetByFirst(oper);
    117             if (opParam.Value != null) {
    118               clone.connections.Add(new KeyValuePair<IOperatorShapeInfo, string>(shapeInfo, param.Name), clone.operatorShapeInfoMapping.GetByFirst(opParam.Value));
    119             }
    120           } else
     130          else
    121131            clone.RegisterParameterEvents(param);
    122132        }
    123133      }
    124 
     134      if (this.operatorGraph.InitialOperator != null) {
     135        IOperatorShapeInfo newInitialShapeInfo = this.operatorShapeInfoMapping.GetByFirst(this.operatorGraph.InitialOperator);
     136        if (newInitialShapeInfo != null) {
     137          oldInitialShapeColor = newInitialShapeInfo.Color;
     138          newInitialShapeInfo.Color = Color.LightGreen;
     139        }
     140        oldInitialShape = this.InitialShape;
     141        this.OnInitialShapeChanged();
     142      }
    125143      return clone;
    126144    }
    127145
    128     public event EventHandler InitialShapeChanged;
     146    internal IOperator GetOperatorForShapeInfo(IOperatorShapeInfo shapeInfo) {
     147      return this.operatorShapeInfoMapping.GetBySecond(shapeInfo);
     148    }
     149
    129150    private void operatorGraph_InitialOperatorChanged(object sender, EventArgs e) {
    130151      this.UpdateInitialShape();
     
    143164
    144165      oldInitialShape = this.InitialShape;
    145       if (this.InitialShapeChanged != null)
    146         this.InitialShapeChanged(this, new EventArgs());
    147     }
    148 
    149     [Storable]
    150     private IOperatorShapeInfo oldInitialShape;
    151     [Storable]
     166      this.OnInitialShapeChanged();
     167    }
     168
     169    private IShapeInfo oldInitialShape;
    152170    private Color oldInitialShapeColor;
    153     public IOperatorShapeInfo InitialShape {
     171    public override IShapeInfo InitialShape {
    154172      get {
    155173        IOperator op = this.operatorGraph.InitialOperator;
     
    161179        if (value == null)
    162180          this.OperatorGraph.InitialOperator = null;
    163         else
    164           this.OperatorGraph.InitialOperator = this.operatorShapeInfoMapping.GetBySecond(value);
    165       }
    166     }
    167 
    168 
     181        else {
     182          IOperatorShapeInfo shapeInfo = (IOperatorShapeInfo)value;
     183          this.OperatorGraph.InitialOperator = this.operatorShapeInfoMapping.GetBySecond(shapeInfo);
     184        }
     185      }
     186    }
     187
     188    [Storable]
    169189    private OperatorGraph operatorGraph;
    170     [Storable]
    171190    public OperatorGraph OperatorGraph {
    172191      get { return this.operatorGraph; }
    173       private set {
    174         if (this.operatorGraph != null || value == null)
    175           throw new InvalidOperationException("Could not set OperatorGraph");
    176 
    177         this.operatorGraph = value;
    178         this.operatorGraph.InitialOperatorChanged += new EventHandler(operatorGraph_InitialOperatorChanged);
    179         this.operatorGraph.Operators.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsAdded);
    180         this.operatorGraph.Operators.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsRemoved);
    181         this.operatorGraph.Operators.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IOperator>(Operators_CollectionReset);
    182       }
    183     }
    184 
    185     private ObservableSet<IOperatorShapeInfo> shapeInfos;
    186     public INotifyObservableCollectionItemsChanged<IOperatorShapeInfo> ObserveableShapeInfos {
    187       get { return this.shapeInfos; }
    188     }
    189     public IEnumerable<IOperatorShapeInfo> OperatorShapeInfos {
    190       get { return this.shapeInfos; }
    191     }
    192     public IOperator GetOperatorForShapeInfo(IOperatorShapeInfo shapeInfo) {
    193       return this.operatorShapeInfoMapping.GetBySecond(shapeInfo);
    194     }
    195 
    196     private ObservableDictionary<KeyValuePair<IOperatorShapeInfo, string>, IOperatorShapeInfo> connections;
    197     public INotifyObservableDictionaryItemsChanged<KeyValuePair<IOperatorShapeInfo, string>, IOperatorShapeInfo> ObservableConnections {
    198       get { return this.connections; }
    199     }
    200     public IEnumerable<KeyValuePair<KeyValuePair<IOperatorShapeInfo, string>, IOperatorShapeInfo>> Connections {
    201       get { return this.connections; }
     192    }
     193
     194    private void RegisterOperatorGraphEvents() {
     195      this.operatorGraph.InitialOperatorChanged += new EventHandler(operatorGraph_InitialOperatorChanged);
     196      this.operatorGraph.Operators.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsAdded);
     197      this.operatorGraph.Operators.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsRemoved);
     198      this.operatorGraph.Operators.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IOperator>(Operators_CollectionReset);
     199    }
     200
     201    private void DeregisterOperatorGraphEvents() {
     202      this.operatorGraph.InitialOperatorChanged -= new EventHandler(operatorGraph_InitialOperatorChanged);
     203      this.operatorGraph.Operators.ItemsAdded -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsAdded);
     204      this.operatorGraph.Operators.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsRemoved);
     205      this.operatorGraph.Operators.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IOperator>(Operators_CollectionReset);
    202206    }
    203207
     
    215219    }
    216220
    217     internal void RemoveShapeInfo(IOperatorShapeInfo shapeInfo) {
     221    public override void RemoveShapeInfo(IShapeInfo shapeInfo) {
     222      IOperatorShapeInfo opShapeInfo = (IOperatorShapeInfo)shapeInfo;
     223      if (this.operatorShapeInfoMapping.ContainsSecond(opShapeInfo)) {
     224        IOperator op = this.operatorShapeInfoMapping.GetBySecond(opShapeInfo);
     225        this.operatorGraph.Operators.Remove(op);
     226      }
     227    }
     228
     229    public override void RemoveConnectionInfo(IConnectionInfo connectionInfo) {
     230      IOperatorShapeInfo shapeInfo = (IOperatorShapeInfo)connectionInfo.From;
    218231      IOperator op = this.operatorShapeInfoMapping.GetBySecond(shapeInfo);
    219       this.operatorGraph.Operators.Remove(op);
    220     }
    221 
    222     internal void AddConnection(IOperatorShapeInfo shapeInfoFrom, string connectorName, IOperatorShapeInfo shapeInfoTo) {
    223       IOperator opFrom = this.operatorShapeInfoMapping.GetBySecond(shapeInfoFrom);
    224       IOperator opTo = this.operatorShapeInfoMapping.GetBySecond(shapeInfoTo);
    225 
    226       IValueParameter<IOperator> param = (IValueParameter<IOperator>)opFrom.Parameters[connectorName];
    227       param.Value = opTo;
    228     }
    229 
    230     internal void ChangeConnection(IOperatorShapeInfo shapeInfoFrom, string connectorName, IOperatorShapeInfo shapeInfoTo) {
    231       IOperator opFrom = this.operatorShapeInfoMapping.GetBySecond(shapeInfoFrom);
    232       IOperator opTo = this.operatorShapeInfoMapping.GetBySecond(shapeInfoTo);
    233 
    234       IValueParameter<IOperator> param = (IValueParameter<IOperator>)opFrom.Parameters[connectorName];
    235       param.Value = opTo;
    236     }
    237 
    238     internal void RemoveConnection(IOperatorShapeInfo shapeInfoFrom, string connectorName) {
    239       IOperator opFrom = this.operatorShapeInfoMapping.GetBySecond(shapeInfoFrom);
    240       IValueParameter<IOperator> param = (IValueParameter<IOperator>)opFrom.Parameters[connectorName];
     232      IValueParameter<IOperator> param = (IValueParameter<IOperator>)op.Parameters[connectionInfo.ConnectorFrom];
    241233      param.Value = null;
    242234    }
     
    247239      if (!this.operatorShapeInfoMapping.ContainsFirst(op)) {
    248240        this.RegisterOperatorEvents(op);
    249         IOperatorShapeInfo shapeInfo = Factory.CreateOperatorShapeInfo(op);
     241        IOperatorShapeInfo shapeInfo = OperatorShapeInfoFactory.CreateOperatorShapeInfo(op);
    250242        this.operatorParameterCollectionMapping.Add(op, op.Parameters);
    251243        this.operatorShapeInfoMapping.Add(op, shapeInfo);
     
    255247      }
    256248    }
    257 
    258249    private void RemoveOperator(IOperator op) {
    259250      this.DeregisterOperatorEvents(op);
     
    333324      if (opParam != null) {
    334325        this.RegisterOperatorParameterEvents(opParam);
    335         IOperatorShapeInfo shapeInfo = this.operatorShapeInfoMapping.GetByFirst(op);
    336         shapeInfo.AddConnector(param.Name);
     326        IOperatorShapeInfo shapeInfoFrom = this.operatorShapeInfoMapping.GetByFirst(op);
     327        shapeInfoFrom.AddConnector(param.Name);
    337328
    338329        if (opParam.Value != null) {
    339330          if (!this.operatorShapeInfoMapping.ContainsFirst(opParam.Value))
    340331            this.AddOperator(opParam.Value);
    341           this.connections.Add(new KeyValuePair<IOperatorShapeInfo, string>(shapeInfo, param.Name), this.operatorShapeInfoMapping.GetByFirst(opParam.Value));
     332          IOperatorShapeInfo shapeInfoTo = this.operatorShapeInfoMapping.GetByFirst(opParam.Value);
     333          this.connectionInfos.Add(new ConnectionInfo(shapeInfoFrom, param.Name, shapeInfoTo, OperatorShapeInfoFactory.PredecessorConnector));
    342334        }
    343335      } else
     
    350342        this.DeregisterOperatorParameterEvents(opParam);
    351343        IOperatorShapeInfo shapeInfo = this.operatorShapeInfoMapping.GetByFirst(op);
    352         this.connections.Remove(new KeyValuePair<IOperatorShapeInfo, string>(shapeInfo, param.Name));
     344        this.connectionInfos.RemoveWhere(c => c.From == shapeInfo && c.ConnectorFrom == param.Name);
     345        this.connectionInfos.RemoveWhere(c => c.To == shapeInfo && c.ConnectorTo == param.Name);
    353346        shapeInfo.RemoveConnector(param.Name);
    354347      } else
     
    360353    private void opParam_ValueChanged(object sender, EventArgs e) {
    361354      IValueParameter<IOperator> opParam = (IValueParameter<IOperator>)sender;
    362       if (opParam != null) {
    363         IOperator op = this.parameterOperatorMapping[opParam];
    364         IOperatorShapeInfo shapeInfo = this.operatorShapeInfoMapping.GetByFirst(op);
    365         KeyValuePair<IOperatorShapeInfo, string> connectionFrom = new KeyValuePair<IOperatorShapeInfo, string>(shapeInfo, opParam.Name);
    366 
    367         if (opParam.Value == null)
    368           this.connections.Remove(connectionFrom);
    369         else {
    370           if (!this.operatorShapeInfoMapping.ContainsFirst(opParam.Value))
    371             this.AddOperator(opParam.Value);
    372           this.connections[connectionFrom] = this.operatorShapeInfoMapping.GetByFirst(opParam.Value);
    373         }
     355      IOperator op = this.parameterOperatorMapping[opParam];
     356      IOperatorShapeInfo shapeInfoFrom = this.operatorShapeInfoMapping.GetByFirst(op);
     357      KeyValuePair<IOperatorShapeInfo, string> connectionFrom = new KeyValuePair<IOperatorShapeInfo, string>(shapeInfoFrom, opParam.Name);
     358
     359      this.connectionInfos.RemoveWhere(c => c.From == shapeInfoFrom && c.ConnectorFrom == opParam.Name);
     360      if (opParam.Value != null) {
     361        if (!this.operatorShapeInfoMapping.ContainsFirst(opParam.Value))
     362          this.AddOperator(opParam.Value);
     363        IShapeInfo shapeInfoTo = this.operatorShapeInfoMapping.GetByFirst(opParam.Value);
     364        this.AddConnectionInfo(new ConnectionInfo(shapeInfoFrom, opParam.Name, shapeInfoTo, OperatorShapeInfoFactory.PredecessorConnector));
    374365      }
    375366    }
Note: See TracChangeset for help on using the changeset viewer.