Exemple #1
0
 private static ACloudCompatible GetCloudComponent(WorkspaceModel workspaceModel)
 {
     try
     {
         var cloudModel =
             workspaceModel.GetAllPluginModels().First(pluginModel => pluginModel.Plugin is ACloudCompatible);
         return(cloudModel.Plugin as ACloudCompatible);
     }
     catch
     {
         return(null);
     }
 }
Exemple #2
0
        /// <summary>
        /// Called by the GUI-Updater Thread
        /// </summary>
        private void CheckGui()
        {
            try
            {
                while (true)
                {
                    if (Stopped)
                    {
                        return;
                    }

                    UpdateGuiElements();

                    double finishedPercentage = 0;
                    double count = 0;
                    foreach (PluginModel plugin in workspaceModel.GetAllPluginModels())
                    {
                        bool sIcontrolSlave = false;
                        foreach (ConnectorModel connector in plugin.GetInputConnectors())
                        {
                            if (connector.IControl && connector.InputConnections.Count == 1)
                            {
                                sIcontrolSlave = true;
                                break;
                            }
                        }
                        if (!sIcontrolSlave)
                        {
                            count++;
                            finishedPercentage += plugin.PercentageFinished;
                        }
                    }

                    ProgressChanged(finishedPercentage / count, 1.0);
                    Thread.Sleep(GuiUpdateInterval);
                }
            }
            catch (Exception ex)
            {
                GuiLogMessage(String.Format(Resources.ExecutionEngine_CheckGui_Exception_occured_during_update_of_GUI_of_Workspace___0__, ex.Message), NotificationLevel.Error);
            }
        }
        private static object[] GetObjectArray(WorkspaceModel model, string[] properties)
        {
            var res = new object[properties.Length];

            for (int i = 0; i < properties.Length; i++)
            {
                var el     = properties[i].Split('>');
                var plugin = model.GetAllPluginModels().First(x => x.GetName() == el[0]).Plugin;

                if (el[1].StartsWith("."))
                {
                    res[i] = plugin.Settings;
                }
                else
                {
                    res[i] = plugin;
                }
            }
            return(res);
        }
Exemple #4
0
        /// <summary>
        /// Starts the execution of the defined workspace
        /// 1) Parses the commandline parameters
        /// 2) Creates CT2 model and execution engine
        /// 3) Starts execution
        /// 4) Gives data as defined by user to the model
        /// 5) Retrieves results for output and outputs these
        /// 6) [terminates]
        /// </summary>
        /// <param name="args"></param>
        public void Start(string[] args)
        {
            _startTime = DateTime.Now;

            //Step 0: Set locale to English
            var cultureInfo = new CultureInfo("en-us", false);

            CultureInfo.CurrentCulture              = cultureInfo;
            CultureInfo.CurrentUICulture            = cultureInfo;
            CultureInfo.DefaultThreadCurrentCulture = cultureInfo;

            //Step 1: Check, if Help needed
            if (ArgsHelper.GetShowHelp(args))
            {
                Environment.Exit(0);
            }

            //Step 2: Get cwm_file to open
            string cwm_file = ArgsHelper.GetCWMFileName(args);

            if (cwm_file == null)
            {
                Console.WriteLine("Please specify a cwm file using -cwm=filename");
                Environment.Exit(-1);
            }
            if (!File.Exists(cwm_file))
            {
                Console.WriteLine("Specified cwm file \"{0}\" does not exist", cwm_file);
                Environment.Exit(-2);
            }

            //Step 3: Get additional parameters
            _verbose = ArgsHelper.CheckVerboseMode(args);
            try
            {
                _timeout = ArgsHelper.GetTimeout(args);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Environment.Exit(-2);
            }
            try
            {
                _loglevel = ArgsHelper.GetLoglevel(args);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Environment.Exit(-2);
            }
            try
            {
                _terminationType = ArgsHelper.GetTerminationType(args);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Environment.Exit(-2);
            }
            try
            {
                _jsonoutput = ArgsHelper.CheckJsonOutput(args);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Environment.Exit(-2);
            }

            //Step 4: Check, if discover mode was selected
            if (ArgsHelper.CheckDiscoverMode(args))
            {
                DiscoverCWMFile(cwm_file);
                Environment.Exit(0);
            }

            //Step 5: Get input parameters
            List <Parameter> inputParameters = null;

            try
            {
                inputParameters = ArgsHelper.GetInputParameters(args);
                if (_verbose)
                {
                    foreach (var param in inputParameters)
                    {
                        Console.WriteLine("Input parameter given: " + param);
                    }
                }
            }
            catch (InvalidParameterException ipex)
            {
                Console.WriteLine(ipex.Message);
                Environment.Exit(-3);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception occured while parsing parameters: {0}", ex.Message);
                Environment.Exit(-3);
            }

            //Step 6: Get output parameters
            List <Parameter> outputParameters = null;

            try
            {
                outputParameters = ArgsHelper.GetOutputParameters(args);
                if (_verbose)
                {
                    foreach (var param in inputParameters)
                    {
                        Console.WriteLine("Output parameter given: " + param);
                    }
                }
            }
            catch (InvalidParameterException ipex)
            {
                Console.WriteLine(ipex.Message);
                Environment.Exit(-3);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception occured while parsing parameters: {0}", ex.Message);
                Environment.Exit(-3);
            }

            //Step 7: Update application domain. This allows loading additional .net assemblies
            try
            {
                UpdateAppDomain();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception occured while updating AppDomain: {0}", ex.Message);
                Environment.Exit(-4);
            }

            //Step 8: Load cwm file and create model
            try
            {
                ModelPersistance modelPersistance = new ModelPersistance();
                _workspaceModel = modelPersistance.loadModel(cwm_file, true);

                foreach (var pluginModel in _workspaceModel.GetAllPluginModels())
                {
                    pluginModel.Plugin.OnGuiLogNotificationOccured += OnGuiLogNotificationOccured;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception occured while loading model from cwm file: {0}", ex.Message);
                Environment.Exit(-5);
            }

            //Step 9: Set input parameters
            foreach (var param in inputParameters)
            {
                string name  = param.Name;
                bool   found = false;
                foreach (var component in _workspaceModel.GetAllPluginModels())
                {
                    //we also memorize here the name of each plugin
                    if (!_pluginNames.ContainsKey(component.Plugin))
                    {
                        _pluginNames.Add(component.Plugin, component.GetName());
                    }

                    if (component.GetName().ToLower().Equals(param.Name.ToLower()))
                    {
                        if (component.PluginType.FullName.Equals("CrypTool.TextInput.TextInput"))
                        {
                            var settings     = component.Plugin.Settings;
                            var textProperty = settings.GetType().GetProperty("Text");

                            if (param.ParameterType == ParameterType.Text || param.ParameterType == ParameterType.Number)
                            {
                                textProperty.SetValue(settings, param.Value);
                            }
                            else if (param.ParameterType == ParameterType.File)
                            {
                                try
                                {
                                    if (!File.Exists(param.Value))
                                    {
                                        Console.WriteLine("Input file does not exist: {0}", param.Value);
                                        Environment.Exit(-7);
                                    }
                                    var value = File.ReadAllText(param.Value);
                                    textProperty.SetValue(settings, value);
                                }
                                catch (Exception ex)
                                {
                                    Console.WriteLine("Exception occured while reading file {0}: {0}", param.Value, ex.Message);
                                    Environment.Exit(-7);
                                }
                            }
                            //we need to call initialize to get the new text to the ui of the TextInput component
                            //otherwise, it will output the value retrieved by deserialization
                            component.Plugin.Initialize();
                            found = true;
                        }
                    }
                }
                if (!found)
                {
                    Console.WriteLine("Component for setting input parameter not found: {0}", param);
                    Environment.Exit(-7);
                }
            }

            //Step 10: Set output parameters
            foreach (var param in outputParameters)
            {
                string name  = param.Name;
                bool   found = false;
                foreach (var component in _workspaceModel.GetAllPluginModels())
                {
                    if (component.GetName().ToLower().Equals(param.Name.ToLower()))
                    {
                        if (component.PluginType.FullName.Equals("TextOutput.TextOutput"))
                        {
                            component.Plugin.PropertyChanged += Plugin_PropertyChanged;
                            found = true;
                        }
                    }
                }
                if (!found)
                {
                    Console.WriteLine("TextOutput for setting output parameter not found: {0}", param);
                    Environment.Exit(-7);
                }
            }

            //Step 11: add OnPluginProgressChanged handlers
            foreach (var plugin in _workspaceModel.GetAllPluginModels())
            {
                plugin.Plugin.OnPluginProgressChanged += OnPluginProgressChanged;
            }

            //Step 12: Create execution engine
            try
            {
                _engine = new ExecutionEngine(null);
                _engine.OnGuiLogNotificationOccured += OnGuiLogNotificationOccured;
                _engine.Execute(_workspaceModel, false);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception occured while executing model: {0}", ex.Message);
                Environment.Exit(-7);
            }

            //Step 13: Start execution in a dedicated thread
            DateTime endTime = DateTime.Now.AddSeconds(_timeout);
            Thread   t       = new Thread(() =>
            {
                CultureInfo.CurrentCulture = new CultureInfo("en-Us", false);
                while (_engine.IsRunning())
                {
                    Thread.Sleep(100);
                    if (_engine.IsRunning() && _timeout < int.MaxValue && DateTime.Now >= endTime)
                    {
                        Console.WriteLine("Timeout ({0} seconds) reached. Kill process hard now", _timeout);
                        Environment.Exit(-8);
                    }
                }
                if (_verbose)
                {
                    Console.WriteLine("Execution engine stopped. Terminate now");
                    Console.WriteLine("Total execution took: {0}", DateTime.Now - _startTime);
                }
                Environment.Exit(0);
            });

            t.Start();
        }
Exemple #5
0
        private void DiscoverWorkspaceModel(string cwm_file)
        {
            if (_jsonoutput)
            {
                Console.Write("{\"components\":[");
            }
            else
            {
                Console.WriteLine("Discovery of cwm_file \"{0}\"", cwm_file);
                Console.WriteLine();
            }
            int counter         = 0;
            var allPluginModels = _workspaceModel.GetAllPluginModels();

            foreach (var pluginModel in allPluginModels)
            {
                counter++;
                if (!_jsonoutput)
                {
                    Console.WriteLine("\"{0}\" (\"{1}\")", pluginModel.GetName(), pluginModel.Plugin.GetType().FullName);
                }

                var inputs             = pluginModel.GetInputConnectors();
                var outputs            = pluginModel.GetOutputConnectors();
                var settings           = pluginModel.Plugin.Settings;
                var taskPaneAttributes = settings.GetSettingsProperties(pluginModel.Plugin);

                if (_jsonoutput)
                {
                    Console.Write("{0}", JsonHelper.GetPluginDiscoveryString(pluginModel, inputs, outputs, taskPaneAttributes));
                    if (counter < allPluginModels.Count)
                    {
                        Console.Write(",");
                    }
                    continue;
                }
                if (inputs.Count > 0)
                {
                    Console.WriteLine("- Input connectors:");
                    foreach (var input in inputs)
                    {
                        Console.WriteLine("-- \"{0}\" (\"{1}\")", input.GetName(), input.ConnectorType.FullName);
                    }
                }
                if (outputs.Count > 0)
                {
                    Console.WriteLine("- Output connectors:");
                    foreach (var output in outputs)
                    {
                        Console.WriteLine("-- \"{0}\" (\"{1}\")", output.GetName(), output.ConnectorType.FullName);
                    }
                }
                if (taskPaneAttributes != null && taskPaneAttributes.Length > 0)
                {
                    Console.WriteLine("- Settings:");
                    foreach (var taskPaneAttribute in taskPaneAttributes)
                    {
                        Console.WriteLine("-- \"{0}\" (\"{1}\")", taskPaneAttribute.PropertyName, taskPaneAttribute.PropertyInfo.PropertyType.FullName);
                    }
                }
                Console.WriteLine();
            }
            if (_jsonoutput)
            {
                Console.Write("]}");
            }
        }