Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/24/10 17:58:03 (15 years ago)
Author:
mkommend
Message:

intermediate version of graph visualization (ticket #867)

File:
1 edited

Legend:

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

    r2853 r2861  
    3030
    3131namespace HeuristicLab.Operators.Views.GraphVisualization {
    32   public sealed class OperatorGraphVisualizationInfo : Item {
     32  public sealed class OperatorGraphVisualizationInfo : DeepCloneable {
    3333    private BidirectionalLookup<IOperator, IShapeInfo> shapeInfoMapping;
    34 
    35     public OperatorGraphVisualizationInfo(OperatorGraph operatorGraph) {
     34    private BidirectionalLookup<IOperator, IObservableKeyedCollection<string, IParameter>> operatorParameterCollectionMapping;
     35    private Dictionary<IValueParameter<IOperator>, IOperator> parameterOperatorMapping;
     36
     37    private OperatorGraphVisualizationInfo() {
     38      this.shapeInfoMapping = new BidirectionalLookup<IOperator, IShapeInfo>();
     39      this.operatorParameterCollectionMapping = new BidirectionalLookup<IOperator, IObservableKeyedCollection<string, IParameter>>();
     40      this.parameterOperatorMapping = new Dictionary<IValueParameter<IOperator>, IOperator>();
     41
     42      this.shapeInfos = new ObservableSet<IShapeInfo>();
     43    }
     44
     45    public OperatorGraphVisualizationInfo(OperatorGraph operatorGraph)
     46      : this() {
    3647      this.operatorGraph = operatorGraph;
    37       this.shapeInfoMapping = new BidirectionalLookup<IOperator, IShapeInfo>();
    38       this.shapeInfos = new ObservableSet<IShapeInfo>();
    39 
     48      this.operatorGraph.InitialOperatorChanged += new EventHandler(operatorGraph_InitialOperatorChanged);
    4049      foreach (IOperator op in operatorGraph.Operators)
    4150        this.AddOperator(op);
    42       this.initialOperator = operatorGraph.InitialOperator;
    4351
    4452      operatorGraph.Operators.ItemsAdded += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsAdded);
     
    4755    }
    4856
     57    public event EventHandler InitialShapeChanged;
     58    private void operatorGraph_InitialOperatorChanged(object sender, EventArgs e) {
     59      if (this.InitialShapeChanged != null)
     60        this.InitialShapeChanged(this, new EventArgs());
     61    }
     62
     63    public IShapeInfo InitialShape {
     64      get {
     65        IOperator op = this.operatorGraph.InitialOperator;
     66        if (op == null)
     67          return null;
     68        return this.shapeInfoMapping.GetByFirst(op);
     69      }
     70    }
     71
    4972    private OperatorGraph operatorGraph;
    5073    public OperatorGraph OperatorGraph {
     
    5679      get { return this.shapeInfos; }
    5780    }
    58 
    5981    public IEnumerable<IShapeInfo> ShapeInfos {
    6082      get { return this.shapeInfos; }
    6183    }
    6284
    63     private IOperator initialOperator;
    64     public IOperator InitialOperator {
    65       get { return this.initialOperator; }
    66       set {
    67         if (this.initialOperator != value) {
    68           if (!this.shapeInfoMapping.ContainsFirst(value))
    69             throw new ArgumentException("Could not set initial operator in graph visualization information, because the operator " +
    70               value.ToString() + " is not contained in the operator set.");
    71           this.initialOperator = value;
    72           this.OnChanged();
    73         }
    74       }
    75     }
    76 
    7785    internal void AddShapeInfo(IOperator op, IShapeInfo shapeInfo) {
    7886      this.RegisterOperatorEvents(op);
     87      this.operatorParameterCollectionMapping.Add(op, op.Parameters);
    7988      this.shapeInfoMapping.Add(op, shapeInfo);
    8089      this.shapeInfos.Add(shapeInfo);
     
    9099    #region operator events
    91100    private void AddOperator(IOperator op) {
    92       if (!shapeInfoMapping.ContainsFirst(op)) {
    93         this.RegisterOperatorEvents(op);
    94 
    95         IShapeInfo shapeInfo = ShapeInfoFactory.CreateShapeInfo(op);
    96         this.shapeInfoMapping.Add(op, shapeInfo);
    97         this.shapeInfos.Add(shapeInfo);
    98       }
     101      if (shapeInfoMapping.ContainsFirst(op))
     102        return;
     103
     104      this.RegisterOperatorEvents(op);
     105      IShapeInfo shapeInfo = Factory.CreateShapeInfo(op);
     106      this.operatorParameterCollectionMapping.Add(op, op.Parameters);
     107      this.shapeInfoMapping.Add(op, shapeInfo);
     108      foreach (IParameter param in op.Parameters)
     109        this.AddParameter(op, param);
     110
     111      this.shapeInfos.Add(shapeInfo);
    99112    }
    100113
    101114    private void RemoveOperator(IOperator op) {
    102115      this.DeregisterOperatorEvents(op);
     116      foreach (IParameter param in op.Parameters)
     117        this.RemoveParameter(op, param);
    103118
    104119      IShapeInfo shapeInfo = this.shapeInfoMapping.GetByFirst(op);
     120      this.operatorParameterCollectionMapping.RemoveByFirst(op);
    105121      this.shapeInfoMapping.RemoveByFirst(op);
    106122      this.shapeInfos.Remove(shapeInfo);
     
    139155
    140156    #region parameter events
    141     private void AddParameter(IParameter param) {
     157    private void AddParameter(IOperator op, IParameter param) {
    142158      IValueParameter<IOperator> opParam = param as IValueParameter<IOperator>;
    143159      if (opParam != null) {
    144         RegisterOperatorParameterEvents(opParam);
    145         //TODO add connector
    146       }
    147     }
    148     private void RemoveParameter(IParameter param) {
     160        this.RegisterOperatorParameterEvents(opParam);
     161        this.parameterOperatorMapping.Add(opParam, op);
     162        IShapeInfo shapeInfo = this.shapeInfoMapping.GetByFirst(op);
     163        shapeInfo.AddConnector(param.Name);
     164
     165        if (opParam.Value != null) {
     166          if (!this.shapeInfoMapping.ContainsFirst(opParam.Value))
     167            this.AddOperator(opParam.Value);
     168          shapeInfo.AddConnection(param.Name, this.shapeInfoMapping.GetByFirst(opParam.Value));
     169        }
     170      }
     171    }
     172    private void RemoveParameter(IOperator op, IParameter param) {
    149173      IValueParameter<IOperator> opParam = param as IValueParameter<IOperator>;
    150174      if (opParam != null) {
    151         DeregisterOperatorParameterEvents(opParam);
    152         //TODO remove connector
     175        this.DeregisterOperatorParameterEvents(opParam);
     176        this.parameterOperatorMapping.Remove(opParam);
     177        IShapeInfo shapeInfo = this.shapeInfoMapping.GetByFirst(op);
     178        shapeInfo.RemoveConnector(param.Name);
    153179      }
    154180    }
     
    156182    private void opParam_ValueChanged(object sender, EventArgs e) {
    157183      IValueParameter<IOperator> opParam = (IValueParameter<IOperator>)sender;
    158       //TODO update connections
     184      if (opParam != null) {
     185        IOperator op = this.parameterOperatorMapping[opParam];
     186        IShapeInfo shapeInfo = this.shapeInfoMapping.GetByFirst(op);
     187
     188        if (opParam.Value == null)
     189          shapeInfo.RemoveConnection(opParam.Name);
     190        else
     191          shapeInfo.ChangeConnection(opParam.Name, this.shapeInfoMapping.GetByFirst(opParam.Value));
     192      }
    159193    }
    160194
    161195    private void Parameters_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
    162       foreach (IParameter param in e.Items)
    163         AddParameter(param);
     196      IObservableKeyedCollection<string, IParameter> parameterCollection = sender as IObservableKeyedCollection<string, IParameter>;
     197      IOperator op = this.operatorParameterCollectionMapping.GetBySecond(parameterCollection);
     198      foreach (IParameter param in e.Items)
     199        AddParameter(op, param);
    164200    }
    165201    private void Parameters_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
    166       foreach (IParameter param in e.Items)
    167         RemoveParameter(param);
     202      IObservableKeyedCollection<string, IParameter> parameterCollection = sender as IObservableKeyedCollection<string, IParameter>;
     203      IOperator op = this.operatorParameterCollectionMapping.GetBySecond(parameterCollection);
     204      foreach (IParameter param in e.Items)
     205        RemoveParameter(op, param);
    168206    }
    169207    private void Parameters_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
     208      IObservableKeyedCollection<string, IParameter> parameterCollection = sender as IObservableKeyedCollection<string, IParameter>;
     209      IOperator op = this.operatorParameterCollectionMapping.GetBySecond(parameterCollection);
    170210      foreach (IParameter param in e.OldItems)
    171         RemoveParameter(param);
    172       foreach (IParameter param in e.Items)
    173         AddParameter(param);
     211        RemoveParameter(op, param);
     212      foreach (IParameter param in e.Items)
     213        AddParameter(op, param);
    174214    }
    175215    private void Parameters_CollectionReset(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
     216      IObservableKeyedCollection<string, IParameter> parameterCollection = sender as IObservableKeyedCollection<string, IParameter>;
     217      IOperator op = this.operatorParameterCollectionMapping.GetBySecond(parameterCollection);
    176218      foreach (IParameter param in e.OldItems)
    177         RemoveParameter(param);
    178       foreach (IParameter param in e.Items)
    179         AddParameter(param);
    180     }
     219        RemoveParameter(op, param);
     220      foreach (IParameter param in e.Items)
     221        AddParameter(op, param);
     222    }
     223
    181224
    182225    private void RegisterOperatorParameterEvents(IValueParameter<IOperator> opParam) {
     
    187230    }
    188231    #endregion
    189 
    190 
    191 
    192 
    193 
    194 
    195 
    196 
    197     private static IConnection CreateConnection(IShape from, IShape to) {
    198       IConnector parentConnector = from.Connectors.Where(c => c.Name == "Bottom connector").First();
    199       IConnector operatorConnector = to.Connectors.Where(c => c.Name == "Top connector").First();
    200 
    201       IConnection connection = new Connection(parentConnector.Point, operatorConnector.Point);
    202 
    203       parentConnector.AttachConnector(connection.From);
    204       operatorConnector.AttachConnector(connection.To);
    205 
    206       return connection;
    207     }
    208232  }
    209233}
Note: See TracChangeset for help on using the changeset viewer.