source: trunk/CrypPlugins/WorkspaceManager/Execution/ExecutionEngine.cs @ 1591

Last change on this file since 1591 was 1591, checked in by kopal, 12 years ago

some small bugfixes

File size: 7.6 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;
21
22using WorkspaceManager.Model;
23using System.Threading;
24using System.Collections;
25using Cryptool.PluginBase;
26using System.Reflection;
27using Gears4Net;
28
29namespace WorkspaceManager.Execution
30{
31    /// <summary>
32    /// Engine to execute a model of the WorkspaceManager
33    /// </summary>
34    public class ExecutionEngine
35    {
36        private WorkspaceManager WorkspaceManagerEditor;
37        private Scheduler scheduler;
38     
39        /// <summary>
40        /// Creates a new ExecutionEngine
41        /// </summary>
42        /// <param name="workspaceManagerEditor"></param>
43        public ExecutionEngine(WorkspaceManager workspaceManagerEditor)
44        {
45            WorkspaceManagerEditor = workspaceManagerEditor;
46        }
47
48        /// <summary>
49        /// Is this ExecutionEngine running?
50        /// </summary>
51        public bool IsRunning
52        {
53            get;
54            private set;
55        }
56
57        /// <summary>
58        /// Execute the current Model
59        /// </summary>
60        /// <param name="workspaceModel"></param>
61        public void Execute(WorkspaceModel workspaceModel)
62        {
63            if (!IsRunning)
64            {
65                IsRunning = true;
66                scheduler = new WinFormsScheduler("Scheduler");
67                workspaceModel.resetStates();
68               
69                foreach (PluginModel pluginModel in workspaceModel.AllPluginModels)
70                {
71                    PluginProtocol pluginProtocol = new PluginProtocol(scheduler, pluginModel,this);
72                    pluginModel.PluginProtocol = pluginProtocol;
73                    scheduler.AddProtocol(pluginProtocol);
74                    pluginModel.checkExecutable(pluginProtocol);
75                    pluginProtocol.Start();
76                }
77            }
78        }     
79     
80        /// <summary>
81        /// Stop the execution
82        /// </summary>
83        public void Stop()
84        {
85            scheduler.Shutdown();
86            IsRunning = false;   
87        }
88
89        /// <summary>
90        /// Pause the execution
91        /// </summary>
92        public void Pause()
93        {
94            //not implemented yet
95        }
96
97        /// <summary>
98        /// Use the logger of the WorkspaceManagerEditor
99        /// </summary>
100        /// <param name="message"></param>
101        /// <param name="level"></param>
102        public void GuiLogMessage(string message, NotificationLevel level)
103        {           
104            WorkspaceManagerEditor.GuiLogMessage(message, level);
105        }           
106    }
107
108    /// <summary>
109    /// Message send to scheduler for a Plugin to trigger the PreExecution
110    /// </summary>
111    public class MessagePreExecution : MessageBase
112    {
113        public PluginModel PluginModel;
114    }
115
116    /// <summary>
117    /// Message send to scheduler for a Plugin to trigger the Execution
118    /// </summary>
119    public class MessageExecution : MessageBase
120    {
121        public PluginModel PluginModel;
122    }
123
124    /// <summary>
125    /// Message send to scheduler for a Plugin to trigger the PostExecution
126    /// </summary>
127    public class MessagePostExecution : MessageBase
128    {
129        public PluginModel PluginModel;
130    }
131
132     /// <summary>
133    /// A Protocol for a PluginModel
134    /// </summary>
135    public class PluginProtocol : ProtocolBase
136    {
137        private PluginModel pluginModel;
138        private ExecutionEngine executionEngine;
139
140        /// <summary>
141        /// Create a new protocol. Each protocol requires a scheduler which provides
142        /// a thread for execution.
143        /// </summary>
144        /// <param name="scheduler"></param>
145        public PluginProtocol(Scheduler scheduler, PluginModel pluginModel,ExecutionEngine executionEngine)
146            : base(scheduler)
147        {
148            this.pluginModel = pluginModel;
149            this.executionEngine = executionEngine;
150        }
151
152        /// <summary>
153        /// The main function of the protocol
154        /// </summary>
155        /// <param name="stateMachine"></param>
156        /// <returns></returns>
157        public override System.Collections.Generic.IEnumerator<ReceiverBase> Execute(AbstractStateMachine stateMachine)
158        {
159            yield return Receive<MessagePreExecution>(null, this.HandlePreExecute);
160            MessageExecution msg_exec = new MessageExecution();
161            msg_exec.PluginModel = this.pluginModel;
162            this.BroadcastMessageReliably(msg_exec);
163            yield return Receive<MessageExecution>(null, this.HandleExecute);
164            MessagePostExecution msg_post = new MessagePostExecution();
165            msg_post.PluginModel = this.pluginModel;
166            this.BroadcastMessageReliably(msg_post);
167            yield return Receive<MessagePostExecution>(null, this.HandlePostExecute);
168        }
169
170        /// <summary>
171        /// Handler function for a message.
172        /// This handler must not block, because it executes inside the thread of the scheduler.
173        /// </summary>
174        /// <param name="msg"></param>
175        private void HandlePreExecute(MessagePreExecution msg)
176        {
177            executionEngine.GuiLogMessage("HandlePreExecute for \"" + msg.PluginModel.Name + "\"", NotificationLevel.Debug);
178            msg.PluginModel.Plugin.PreExecution();
179        }
180
181        /// <summary>
182        /// Handler function for a message.
183        /// This handler must not block, because it executes inside the thread of the scheduler.
184        /// </summary>
185        /// <param name="msg"></param>
186        private void HandleExecute(MessageExecution msg)
187        {
188            executionEngine.GuiLogMessage("HandleExecute for \"" + msg.PluginModel.Name + "\"", NotificationLevel.Debug);
189            //Fill the plugins Inputs with data
190            foreach (ConnectorModel connectorModel in pluginModel.InputConnectors)
191            {
192                if (connectorModel.HasData)
193                {
194                    PropertyInfo propertyInfo = pluginModel.Plugin.GetType().GetProperty(connectorModel.PropertyName);
195                    propertyInfo.SetValue(pluginModel.Plugin, connectorModel.Data, null);
196                    connectorModel.Data = null;
197                    connectorModel.HasData = false;
198                    connectorModel.InputConnection.Active = false;
199                }
200            }
201            msg.PluginModel.Plugin.Execute();
202        }
203
204        /// <summary>
205        /// Handler function for a message.
206        /// This handler must not block, because it executes inside the thread of the scheduler.
207        /// </summary>
208        /// <param name="msg"></param>
209        private void HandlePostExecute(MessagePostExecution msg)
210        {
211            executionEngine.GuiLogMessage("HandlePostExecute for \"" + msg.PluginModel.Name + "\"", NotificationLevel.Debug);
212            msg.PluginModel.Plugin.PostExecution();
213        }
214    }
215}
Note: See TracBrowser for help on using the repository browser.