Free cookie consent management tool by TermsFeed Policy Generator

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

intermediate version of graph visualization (ticket #867)

Location:
trunk/sources/HeuristicLab.Operators.Views.GraphVisualization/3.3/Model
Files:
6 edited

Legend:

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

    r2853 r2861  
    2727using System.Drawing;
    2828using Netron.Diagramming.Core;
     29using HeuristicLab.Collections;
    2930
    3031namespace HeuristicLab.Operators.Views.GraphVisualization {
     
    3435    Size Size { get; set; }
    3536
     37    void AddConnector(string connectorName);
     38    void RemoveConnector(string connectorName);
     39
     40    IEnumerable<KeyValuePair<string,IShapeInfo>> Connections {get;}
     41    INotifyObservableDictionaryItemsChanged<string, IShapeInfo> ObservableConnections { get; }
     42
     43    void AddConnection(string fromConnectorName, IShapeInfo toShapeInfo);
     44    void RemoveConnection(string fromConnectorName);
     45    void ChangeConnection(string fromConnector, IShapeInfo toShapeInfo);
     46
    3647    IShape CreateShape();
     48    void UpdateShape(IShape shape);
    3749  }
    3850}
  • 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}
  • trunk/sources/HeuristicLab.Operators.Views.GraphVisualization/3.3/Model/OperatorShape.cs

    r2853 r2861  
    3030  public class OperatorShape : ClassShape {
    3131
    32     private BidirectionalLookup<string, IConnector> additionalConnectors;
    3332    public OperatorShape()
    3433      : base() {
    35       this.additionalConnectors = new BidirectionalLookup<string,IConnector>();
     34      this.additionalConnectors = new List<IConnector>();
    3635    }
    3736
    38     private Connector predecessor;
    39     public Connector Predecessor {
     37    private List<IConnector> additionalConnectors;
     38    public IEnumerable<string> AdditionalConnectorNames {
     39      get { return this.additionalConnectors.Select(c => c.Name); }
     40    }
     41
     42    private IConnector predecessor;
     43    public IConnector Predecessor {
    4044      get { return this.predecessor; }
    4145    }
    4246
    43     private Connector successor;
    44     public Connector Successor {
     47    private IConnector successor;
     48    public IConnector Successor {
    4549      get { return this.successor; }
    4650    }
    4751
     52    private IConnector CreateConnector(string connectorName, Point location) {
     53      Connector connector = new Connector(location);
     54      connector.ConnectorStyle = ConnectorStyle.Square;
     55      connector.Parent = this;
     56      connector.Name = connectorName;
     57      return connector;
     58    }
     59
     60
    4861
    4962    public void AddConnector(string connectorName) {
    50       Connector connector = new Connector(new Point(Rectangle.Right, Rectangle.Bottom));
    51       connector.ConnectorStyle = ConnectorStyle.Square;
    52       connector.Parent = this;
    53       this.additionalConnectors.Add(connectorName, connector);
    54       Connectors.Add(connector);
     63      IConnector connector = this.CreateConnector(connectorName, this.BottomRightCorner);
     64
     65      this.additionalConnectors.Add(connector);
     66      this.Connectors.Add(connector);
     67      this.UpdateConnectorLocation();
    5568    }
    5669
    5770    public void RemoveConnector(string connectorName) {
    58       this.additionalConnectors.RemoveByFirst(connectorName);
    59       this.UpdateConnectorLocation();
     71      IConnector connector = this.additionalConnectors.Where(c => c.Name ==connectorName).FirstOrDefault();
     72      if (connector != null) {
     73        this.additionalConnectors.Remove(connector);
     74        this.Connectors.Remove(connector);
     75        this.UpdateConnectorLocation();
     76      }
    6077    }
    6178
    62     private void UpdateConnectorLocation() {
    63       //TODO set x position of connectors
     79    private void UpdateConnectorLocation() {
     80      int spacing = this.Rectangle.Width / this.additionalConnectors.Count + 1;
     81      int margin = spacing / 2;
     82      int posX = margin + this.Rectangle.X;
     83      int posY = this.additionalConnectors[0].Point.Y;
     84      for (int i = 0; i < this.additionalConnectors.Count; i++) {
     85        this.additionalConnectors[i].Point = new Point(posX, posY);
     86        posX += spacing;
     87      }
    6488    }
    6589
     
    7195      this.Connectors.Clear();
    7296
    73       predecessor = new Connector(new Point(Rectangle.Right, (int)(Rectangle.Top + Rectangle.Height / 2)));
    74       predecessor.ConnectorStyle = ConnectorStyle.Square;
    75       predecessor.Name = "Predecessor";
    76       predecessor.Parent = this;
     97      predecessor = this.CreateConnector("Predecessor", new Point(Rectangle.Left, Center.Y));
    7798      Connectors.Add(predecessor);
    7899
    79       successor = new Connector(new Point(Rectangle.Left, (int)(Rectangle.Top + Rectangle.Height / 2)));
    80       predecessor.ConnectorStyle = ConnectorStyle.Square;
    81       successor.Name = "Successor";
    82       successor.Parent = this;
     100      successor = this.CreateConnector("Successor",(new Point(Rectangle.Right, Center.Y)));
    83101      Connectors.Add(successor);
    84102      #endregion
  • trunk/sources/HeuristicLab.Operators.Views.GraphVisualization/3.3/Model/OperatorShapeInfo.cs

    r2853 r2861  
    2727using System.Drawing;
    2828using Netron.Diagramming.Core;
     29using System.Windows.Forms;
    2930
    3031namespace HeuristicLab.Operators.Views.GraphVisualization {
    3132  internal class OperatorShapeInfo : ShapeInfo {
    3233
    33     private HashSet<string> connectorNames;
     34    private List<string> connectorNames;
    3435    public OperatorShapeInfo()
    3536      : base(typeof(OperatorShape)) {
    36       this.connectorNames = new HashSet<string>();
     37      this.connectorNames = new List<string>();
    3738    }
    3839
     
    4344    }
    4445
    45     public void AddConnector(string connectorName) {
    46       if (this.connectorNames.Add(connectorName))
     46    public override void AddConnector(string connectorName) {
     47      if (!this.connectorNames.Contains(connectorName) && connectorName != "Successor") {
     48        this.connectorNames.Add(connectorName);
    4749        this.OnChanged();
     50      }
    4851    }
    4952
    50     public void RemoveConnector(string connectorName) {
    51       if (this.connectorNames.Remove(connectorName))
     53    public override void RemoveConnector(string connectorName) {
     54      if (this.connectorNames.Contains(connectorName)) {
     55        this.connectorNames.Remove(connectorName);
     56        if (this.connections.ContainsKey(connectorName))
     57          this.connections.Remove(connectorName);
    5258        this.OnChanged();
     59      }
    5360    }
    5461
     62    public override void AddConnection(string fromConnectorName, IShapeInfo toShapeInfo) {
     63      this.connections.Add(fromConnectorName, toShapeInfo);
     64    }
     65
     66    public override void RemoveConnection(string fromConnectorName) {
     67      if (this.connections.ContainsKey(fromConnectorName))
     68        this.connections.Remove(fromConnectorName);
     69    }
     70
     71    public override void ChangeConnection(string fromConnectorName, IShapeInfo toShapeInfo) {
     72      this.connections[fromConnectorName] = toShapeInfo;
     73    }
    5574
    5675    private string title;
     
    96115      return shape;
    97116    }
     117
     118    public override void UpdateShape(IShape shape) {
     119      base.UpdateShape(shape);
     120      OperatorShape operatorShape = shape as OperatorShape;
     121
     122      if (operatorShape != null) {
     123        operatorShape.Title = this.Title;
     124        operatorShape.SubTitle = this.Text;
     125        operatorShape.HeadColor = this.HeadColor;
     126
     127        int i = 0;
     128        int j = 0;
     129        //remove old connectors and skip correct connectors
     130        List<string> oldConnectorNames = operatorShape.AdditionalConnectorNames.ToList();
     131        while (i < this.connectorNames.Count && j < oldConnectorNames.Count) {
     132          if (this.connectorNames[i] != oldConnectorNames[j]) {
     133            operatorShape.RemoveConnector(oldConnectorNames[j]);
     134          } else
     135            i++;
     136          j++;
     137        }
     138        //remove old connectors
     139        for (; j < oldConnectorNames.Count; i++)
     140          operatorShape.RemoveConnector(oldConnectorNames[j]);
     141
     142        //add new connectors
     143        for (; i < this.connectorNames.Count; i++)
     144          operatorShape.AddConnector(this.connectorNames[i]);
     145      }
     146    }
    98147  }
    99148}
  • trunk/sources/HeuristicLab.Operators.Views.GraphVisualization/3.3/Model/ShapeInfo.cs

    r2853 r2861  
    2727using System.Drawing;
    2828using Netron.Diagramming.Core;
     29using HeuristicLab.Collections;
    2930
    3031namespace HeuristicLab.Operators.Views.GraphVisualization {
    3132  internal abstract class ShapeInfo : Item, IShapeInfo {
    32 
    33 
    3433    protected ShapeInfo(Type shapeType) {
    3534      if (!typeof(IShape).IsAssignableFrom(shapeType))
    3635        throw new ArgumentException("The passed shape type " + shapeType + " must be derived from IShape.");
    3736      this.shapeType = shapeType;
     37      this.connections = new ObservableDictionary<string, IShapeInfo>();
    3838    }
    3939
     
    6565    }
    6666
     67    protected ObservableDictionary<string, IShapeInfo> connections;
     68    public IEnumerable<KeyValuePair<string, IShapeInfo>> Connections {
     69      get { return this.connections; }
     70    }
     71    public INotifyObservableDictionaryItemsChanged<string, IShapeInfo> ObservableConnections {
     72      get { return this.connections; }
     73    }
     74
     75    public abstract void AddConnector(string connectorName);
     76    public abstract void RemoveConnector(string connectorName);
     77    public abstract void AddConnection(string fromConnectorName, IShapeInfo toShapeInfo);
     78    public abstract void RemoveConnection(string fromConnectorName);
     79    public abstract void ChangeConnection(string fromConnectorName, IShapeInfo toShapeInfo);
     80
     81
    6782    public virtual IShape CreateShape() {
    6883      IShape shape = (IShape)Activator.CreateInstance(this.shapeType);
     
    7287      return shape;
    7388    }
     89
     90    public virtual void UpdateShape(IShape shape) {
     91      shape.Location = this.Location;
     92      shape.Height = this.Size.Height;
     93      shape.Width = this.Size.Width;
     94    }
    7495  }
    7596}
  • trunk/sources/HeuristicLab.Operators.Views.GraphVisualization/3.3/Model/ShapeInfoFactory.cs

    r2853 r2861  
    2525using System.Text;
    2626using HeuristicLab.Core;
     27using Netron.Diagramming.Core;
     28using System.Drawing;
     29using System.Drawing.Drawing2D;
    2730
    2831namespace HeuristicLab.Operators.Views.GraphVisualization {
    29   public static class ShapeInfoFactory {
     32  public static class Factory {
     33    private static LinePenStyle connectionPenStyle;
     34
     35    static Factory() {
     36      connectionPenStyle = new LinePenStyle();
     37      connectionPenStyle.EndCap = LineCap.ArrowAnchor;
     38    }
     39
    3040    public static IShapeInfo CreateShapeInfo(IOperator op) {
    31       OperatorShapeInfo operatorShapeInfo = new OperatorShapeInfo();
     41      IEnumerable<string> paramterNames = op.Parameters.Where(p => p is IValueParameter<IOperator> && p.Name != "Successor").Select(p => p.Name);
     42      OperatorShapeInfo operatorShapeInfo = new OperatorShapeInfo(paramterNames);
    3243      operatorShapeInfo.Title = op.Name;
    3344      operatorShapeInfo.Text = op.GetType().ToString();
     
    3546      return operatorShapeInfo;
    3647    }
     48
     49    public static IConnection CreateConnection(IConnector from, IConnector to) {
     50      Connection connection = new Connection(from.Point, to.Point);
     51      connection.AllowMove = false;
     52      from.AttachConnector(connection.From);
     53      to.AttachConnector(connection.To);
     54      connection.PenStyle = connectionPenStyle;
     55      return connection;
     56    }
    3757  }
    3858}
Note: See TracChangeset for help on using the changeset viewer.