source: trunk/sources/HeuristicLab.Operators.Views.GraphVisualization/3.3/OperatorGraphVisualizationView.cs @ 2801

Last change on this file since 2801 was 2801, checked in by mkommend, 10 years ago

added first version of OperatorGraphVisualization (ticket #867)

File size: 5.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Linq;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.MainForm;
31using HeuristicLab.Core;
32using HeuristicLab.Core.Views;
33using Netron.Diagramming.Core;
34
35namespace HeuristicLab.Operators.Views.GraphVisualization {
36  [Content(typeof(IOperator), false)]
37  public partial class OperatorGraphVisualizationView : ItemView {
38    private Dictionary<IOperator, IShape> operatorShapeMapping;
39    private Dictionary<KeyValuePair<IOperator, IOperator>, IConnection> connectionMapping;
40    /// <summary>
41    /// Initializes a new instance of <see cref="OperatorGraphVisualizationView"/> with caption "Operator Graph".
42    /// </summary>
43    public OperatorGraphVisualizationView() {
44      InitializeComponent();
45      Caption = "Operator Graph";
46      this.operatorShapeMapping = new Dictionary<IOperator, IShape>();
47      this.connectionMapping = new Dictionary<KeyValuePair<IOperator, IOperator>, IConnection>();
48    }
49
50    /// <summary>
51    /// Initializes a new instance of <see cref="OperatorGraphVisualizationView"/>
52    /// with the given <paramref name="operatorGraph"/>.
53    /// </summary>
54    /// <remarks>Calls <see cref="OperatorGraphView()"/>.</remarks>
55    /// <param name="operatorGraph">The operator graph to represent visually.</param>
56    public OperatorGraphVisualizationView(IOperator content)
57      : this() {
58      this.Content = content;
59    }
60
61
62    /// <summary>
63    /// Gets or sets the operator graph to represent visually.
64    /// </summary>
65    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
66    /// No own data storage present.</remarks>
67    public new IOperator Content {
68      get { return (IOperator)base.Content; }
69      set { base.Content = value; }
70    }
71
72    private IModel GraphModel {
73      get { return this.graphVisualization.Controller.Model; }
74    }
75
76    protected override void OnContentChanged() {
77      base.OnContentChanged();
78      this.GraphModel.Shapes.Clear();
79      this.operatorShapeMapping.Clear();
80      this.connectionMapping.Clear();
81
82      this.CreateGraph(null, this.Content);
83      foreach (IShape shape in this.operatorShapeMapping.Values)
84        this.GraphModel.AddShape(shape);
85
86      foreach (IConnection connection in this.connectionMapping.Values)
87        this.GraphModel.AddConnection(connection);
88
89      this.graphVisualization.Controller.View.Invalidate();
90      this.graphVisualization.Controller.Model.LayoutRoot = this.operatorShapeMapping[this.Content];
91      this.graphVisualization.Controller.RunActivity("Standard TreeLayout");
92    }
93
94    private void opParam_ValueChanged(object sender, EventArgs e) {
95      if (InvokeRequired)
96        Invoke(new EventHandler(opParam_ValueChanged), sender, e);
97      else {
98        //IValueParameter<IOperator> opParam = (IValueParameter<IOperator>)sender;
99        //foreach (TreeNode node in opParamNodeTable[opParam].ToArray())
100        //  //remove nodes
101        //foreach (TreeNode node in opParamNodeTable[opParam]) {
102        //  //add nodes
103        //}
104      }
105    } 
106
107    private void CreateGraph(IOperator parent, IOperator op) {
108      if (op == null)
109        return;
110
111      IShape shape;
112      if (!this.operatorShapeMapping.ContainsKey(op)) {
113        shape = CreateOperatorShape(op);
114        this.operatorShapeMapping[op] = shape;
115
116        foreach (IParameter param in op.Parameters) {
117          IValueParameter<IOperator> opParam = param as IValueParameter<IOperator>;
118          if (opParam != null) {
119            opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
120            this.CreateGraph(op, opParam.Value);
121          }
122        }
123      }
124
125      if (parent != null)
126        ConnectShapes(parent, op);
127    }
128
129    private void RemoveGraph(IOperator op) {
130      if(op == null)
131        return;
132      IShape shape = this.operatorShapeMapping[op];
133      this.GraphModel.RemoveShape(shape);
134    }
135
136    private IConnection ConnectShapes(IOperator parent, IOperator op) {
137      IShape operatorShape = this.operatorShapeMapping[op];
138      IShape parentShape = this.operatorShapeMapping[parent];
139      IConnector operatorConnector = parentShape.Connectors.Where(c => c.Name == "Bottom connector").First();
140      IConnector parentConnector = operatorShape.Connectors.Where(c => c.Name == "Top connector").First();
141
142      IConnection connection = new Connection(parentConnector.Point, operatorConnector.Point);
143      parentConnector.AttachConnector(connection.From);
144      operatorConnector.AttachConnector(connection.To);
145
146      this.connectionMapping[new KeyValuePair<IOperator, IOperator>(parent, op)] = connection;
147      return connection;
148    }
149
150    private IShape CreateOperatorShape(IOperator op) {
151      ClassShape shape = new ClassShape();
152      shape.Name = op.Name;
153      shape.Text = op.Name;
154      shape.Title = op.Name;
155      shape.SubTitle = op.GetType().ToString();
156
157      return shape;
158    }
159  }
160}
Note: See TracBrowser for help on using the repository browser.