source: trunk/CrypPlugins/WorkspaceManager/Model/WorkspaceModel.cs @ 1700

Last change on this file since 1700 was 1700, checked in by kopal, 12 years ago
  • WorkspaceManager now allows multiple Connections on InputConnectors
  • Created own Gears4Net Scheduler which implements flow control in WorkspaceManagers ExecutionEngine to support loops
  • some small bugfixes
File size: 11.2 KB
Line 
1/*                             
2   Copyright 2010 Nils Kopal, Viktor M.
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15*/
16
17using System;
18using System.Collections.Generic;
19using System.Linq;
20using System.Text;
21using System.Windows;
22using Cryptool.PluginBase;
23using System.Runtime.Serialization.Formatters.Soap;
24
25namespace WorkspaceManager.Model
26{
27    /// <summary>
28    /// Class to represent our Workspace
29    /// </summary>
30    [Serializable]
31    public class WorkspaceModel 
32    {
33        [NonSerialized]
34        private WorkspaceManager workspaceManagerEditor;
35
36        /// <summary>
37        /// The surrounding WorkspaceManagerEditor
38        /// </summary>
39        public WorkspaceManager WorkspaceManagerEditor { 
40            get{
41                return workspaceManagerEditor;
42            }
43            set{ 
44                workspaceManagerEditor = value;
45            }
46        }
47
48        /// <summary>
49        /// All PluginModels of our Workspace Model
50        /// </summary>
51        public List<PluginModel> AllPluginModels;
52
53        /// <summary>
54        /// All Connector Models of our Workspace Model
55        /// </summary>
56        public List<ConnectorModel> AllConnectorModels;
57
58        /// <summary>
59        /// All ConnectionModels of our Workspace Model
60        /// </summary>
61        public List<ConnectionModel> AllConnectionModels;
62
63        /// <summary>
64        /// Creates a new Workspace Model
65        /// </summary>
66        public WorkspaceModel()
67        { 
68            this.AllPluginModels = new List<PluginModel>();
69            this.AllConnectionModels = new List<ConnectionModel>();
70            this.AllConnectorModels = new List<ConnectorModel>();           
71        }
72
73        /// <summary>
74        /// Creates a new PluginModel belonging to this WorkspaceModel
75        /// </summary>
76        /// <param name="x"></param>
77        /// <param name="y"></param>
78        /// <param name="width"></param>
79        /// <param name="height"></param>
80        /// <param name="pluginType"></param>
81        /// <returns></returns>
82        public PluginModel newPluginModel(Point position, double width, double height, Type pluginType)
83        {
84            PluginModel pluginModel = new PluginModel();
85            pluginModel.WorkspaceModel = this;           
86            pluginModel.Startable = pluginType.GetPluginInfoAttribute().Startable;
87            pluginModel.Position = position;
88            pluginModel.Width = width;
89            pluginModel.Height = height;
90            pluginModel.PluginType = pluginType;           
91            pluginModel.Name = pluginType.Name;
92            pluginModel.generateConnectors();
93            pluginModel.Plugin.OnGuiLogNotificationOccured += this.WorkspaceManagerEditor.GuiLogNotificationOccured;
94            pluginModel.Plugin.OnPluginProgressChanged += pluginModel.PluginProgressChanged;
95            pluginModel.Plugin.OnPluginStatusChanged += pluginModel.PluginStatusChanged;
96            this.AllPluginModels.Add(pluginModel);
97            this.WorkspaceManagerEditor.HasChanges = true;
98            return pluginModel;
99        }
100
101        /// <summary>
102        /// Creates a new PluginModel belonging to this WorkspaceModel
103        /// Position and Dimension are (x,y,width,height) = (0,0,0,0)
104        /// </summary>
105        /// <param name="pluginType"></param>
106        /// <returns></returns>
107        public PluginModel newPluginModel(Type pluginType)
108        {
109            return newPluginModel(new Point(0, 0), 0, 0, pluginType);
110        }       
111
112        /// <summary>
113        /// Creates a new Connection starting at "from"-Connector going to "to"-Connector with
114        /// the given connectionType
115        /// </summary>
116        /// <param name="from"></param>
117        /// <param name="to"></param>
118        /// <param name="connectionType"></param>
119        /// <returns></returns>
120        public ConnectionModel newConnectionModel(ConnectorModel from, ConnectorModel to, Type connectionType)
121        {
122            ConnectionModel connectionModel = new ConnectionModel();
123            connectionModel.WorkspaceModel = this;
124            connectionModel.From = from;
125            connectionModel.To = to;
126            from.OutputConnections.Add(connectionModel);
127            to.InputConnections.Add(connectionModel);
128            connectionModel.ConnectionType = connectionType;
129            this.AllConnectionModels.Add(connectionModel);
130            this.WorkspaceManagerEditor.HasChanges = true;
131            return connectionModel;
132        }
133
134        /// <summary>
135        /// Deletes the pluginModel and all of its Connectors and the connected Connections
136        /// from our WorkspaceModel
137        /// </summary>
138        /// <param name="pluginModel"></param>
139        /// <returns></returns>
140        public bool deletePluginModel(PluginModel pluginModel)
141        {
142            //we can only delete PluginModels which are part of our WorkspaceModel
143            if (this.AllPluginModels.Contains(pluginModel))
144            {
145                // remove all InputConnectors belonging to this pluginModel from our WorkspaceModel
146                foreach (ConnectorModel inputConnector in new List<ConnectorModel>(pluginModel.InputConnectors))
147                {
148                    deleteConnectorModel(inputConnector);
149                }
150
151                // remove all OutputConnectors belonging to this pluginModel from our WorkspaceModel
152                foreach (ConnectorModel outputConnector in new List<ConnectorModel>(pluginModel.OutputConnectors))
153                {
154                    deleteConnectorModel(outputConnector);
155                }
156                pluginModel.Plugin.Dispose();
157                pluginModel.onDelete();
158                this.WorkspaceManagerEditor.HasChanges = true;
159                return this.AllPluginModels.Remove(pluginModel);
160            }           
161            return false;
162        }
163
164        /// <summary>
165        /// Deletes the connectorModel and the connected Connections
166        /// from our WorkspaceModel
167        /// </summary>
168        /// <param name="connectorModel"></param>
169        /// <returns></returns>
170        private bool deleteConnectorModel(ConnectorModel connectorModel)
171        {
172            //we can only delete ConnectorModels which are part of our WorkspaceModel
173            if(this.AllConnectorModels.Contains(connectorModel)){
174
175                //remove all input ConnectionModels belonging to this Connector from our WorkspaceModel
176                foreach (ConnectionModel connectionModel in new List<ConnectionModel>(connectorModel.InputConnections))
177                {
178                    deleteConnectionModel(connectionModel);
179                }
180
181                //remove all output ConnectionModels belonging to this Connector from our WorkspaceModel
182                foreach (ConnectionModel outputConnection in new List<ConnectionModel>(connectorModel.OutputConnections))
183                {
184                    deleteConnectionModel(outputConnection);
185                }
186                               
187                connectorModel.onDelete();
188                this.WorkspaceManagerEditor.HasChanges = true;
189                return this.AllConnectorModels.Remove(connectorModel);
190            }
191            return false;
192        }
193
194        /// <summary>
195        /// Removes the connectionModel from our Workspace Model and removes it from all Connectors
196        /// </summary>
197        /// <param name="connectionModel"></param>
198        /// <returns></returns>
199        public bool deleteConnectionModel(ConnectionModel connectionModel)
200        {
201            if (connectionModel == null)
202                return false;
203
204            connectionModel.To.InputConnections.Remove(connectionModel);
205            connectionModel.From.OutputConnections.Remove(connectionModel);           
206            connectionModel.onDelete();
207            this.WorkspaceManagerEditor.HasChanges = true;
208            return this.AllConnectionModels.Remove(connectionModel);
209        }
210
211        /// <summary>
212        /// Sets all Connections and Connectors to state nonActive/noData
213        /// </summary>
214        public void resetStates()
215        {
216            foreach (PluginModel pluginModel in this.AllPluginModels)
217            {
218                pluginModel.State = PluginModelState.Normal;
219            }
220            foreach (ConnectionModel connection in this.AllConnectionModels)
221            {
222                connection.Active = false;
223            }
224            foreach (ConnectorModel connector in this.AllConnectorModels)
225            {
226                connector.HasData = false;
227                connector.Data = null;
228            }
229        }
230
231        /// <summary>
232        /// Reconnects a Connection with an other Connector
233        /// </summary>
234        /// <param name="connectionModel"></param>
235        /// <param name="connectorModel"></param>
236        /// <returns></returns>
237        public bool reconnectConnection(ConnectionModel connectionModel, ConnectorModel connectorModel)
238        {
239            if (connectionModel.To != null)
240            {
241                connectionModel.To.InputConnections.Remove(connectionModel);
242            }
243            connectionModel.To = connectorModel;
244            connectorModel.InputConnections.Add(connectionModel);
245            this.WorkspaceManagerEditor.HasChanges = true;
246            return true;
247        }
248
249        /// <summary>
250        /// Checks wether a Connector and a Connector are compatible to be connected
251        /// They are compatible if their types are equal or the base type of the Connector
252        /// is equal to the type of the other Connector
253        /// </summary>
254        /// <param name="connectorModelA"></param>
255        /// <param name="connectorModelB"></param>
256        /// <returns></returns>
257        public static bool compatibleConnectors(ConnectorModel connectorModelA, ConnectorModel connectorModelB)
258        {
259            if (!connectorModelA.Outgoing || connectorModelB.Outgoing)
260            {
261                return false;
262            }
263           
264            if (connectorModelA.ConnectorType.Equals(connectorModelB.ConnectorType)
265                || connectorModelA.ConnectorType.FullName == "System.Object"
266                || connectorModelB.ConnectorType.FullName == "System.Object"
267                || connectorModelA.ConnectorType.IsSubclassOf(connectorModelB.ConnectorType)
268                || connectorModelA.ConnectorType.GetInterfaces().Contains(connectorModelB.ConnectorType))             
269            {
270                return true;
271            }
272            else
273            {
274                return false;
275            }
276        }
277    }
278}
Note: See TracBrowser for help on using the repository browser.