Exemple #1
0
        private void StopModelButton_Click(object sender, EventArgs e)
        {
            int NumErrors = 0;

            foreach (DataGridViewRow Row in ModelsStateTable.SelectedRows)
            {
                string ModelName  = (string)Row.Cells[0].Value;
                int    ModelIndex = SimulationController.FindModelIndex(ModelName);

                try
                {
                    if (!SimulationController.ModelsProcesses[ModelIndex].HasExited)
                    {
                        SimulationController.ModelsProcesses[ModelIndex].Kill();
                    }
                }
                catch (Exception KillException)
                {
                    ToLogsTextBox("Process kill error: " + KillException.Message);
                    NumErrors++;
                }
            }

            if (NumErrors != 0)
            {
                MessageBox.Show(NumErrors.ToString() + " errors occured at attempt to stop model(s) simulation process", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            ModelsStateTable.ClearSelection();
        }
Exemple #2
0
        private void DeleteModelButton_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow Row in ModelsStateTable.SelectedRows)
            {
                string ModelName = (string)Row.Cells[0].Value;

                int ModelIndex = SimulationController.FindModelIndex(ModelName);
                ModelsResultsPages.TabPages.RemoveAt(ModelIndex);

                TabPage ModelPage = (TabPage)Pages.Controls.Find(ModelPageName + ModelName, true)[0];
                Pages.Controls.Remove(ModelPage);

                SimulationController.DeleteModel(ModelName);

                SimulationController.ModelsStates.RemoveAt(ModelIndex);
                ModelsStateTable.Rows.RemoveAt(ModelIndex);
            }

            if (SimulationController.Models.Count == 0)
            {
                DeleteModelButton.Enabled     = false;
                StartSimulationButton.Enabled = false;
            }

            ModelsStateTable.ClearSelection();
        }
Exemple #3
0
        private void ExportResultsButton_Click(object sender, EventArgs e)
        {
            if (SimulationController.SimulationState[3] != State.Finished)
            {
                MessageBox.Show("It is possible to export results only after the end of a simulation", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (ModelsStateTable.SelectedRows.Count == 0)
            {
                foreach (Model ConnectedModel in SimulationController.Models)
                {
                    ResultsToCsv(ConnectedModel);

                    int   ModelIndex = SimulationController.FindModelIndex(ConnectedModel.GetName());
                    Chart Graph      = (Chart)ModelsResultsPages.TabPages[ModelIndex].Controls[0].Controls[1];
                    Graph.SaveImage(ConnectedModel.GetResultsDirectoryPath() + "\\" + ResultGraphFilename, System.Drawing.Imaging.ImageFormat.Png);
                }
            }
            else
            {
                foreach (DataGridViewRow Row in ModelsStateTable.SelectedRows)
                {
                    string ModelName      = (string)Row.Cells[0].Value;
                    Model  ConnectedModel = SimulationController.FindModel(ModelName);
                    ResultsToCsv(ConnectedModel);

                    int   ModelIndex = SimulationController.FindModelIndex(ConnectedModel.GetName());
                    Chart Graph      = (Chart)ModelsResultsPages.TabPages[ModelIndex].Controls[0].Controls[1];
                    Graph.SaveImage(ConnectedModel.GetResultsDirectoryPath() + "\\" + ResultGraphFilename, System.Drawing.Imaging.ImageFormat.Png);
                }
            }

            if (CompareButton.Enabled == false)
            {
                string ComparisonFolder = Directory.GetCurrentDirectory() + "\\" + SimulationFolderName + "\\" + SimulationController.SimulationName + "\\" + ComparisonFolderName;
                Directory.CreateDirectory(ComparisonFolder);
                Chart CompareGraph = (Chart)ModelsResultsPages.TabPages[ModelsResultsPages.TabPages.Count - 1].Controls[0].Controls[0];
                CompareGraph.SaveImage(ComparisonFolder + "\\" + ResultGraphFilename, System.Drawing.Imaging.ImageFormat.Png);
            }
        }
Exemple #4
0
        private void ResultsToCsv(Model ConnectedModel)
        {
            int          ModelIndex = SimulationController.FindModelIndex(ConnectedModel.GetName());
            DataGridView Table      = (DataGridView)ModelsResultsPages.TabPages[ModelIndex].Controls[0].Controls[0];

            string[] Result = new string[Table.RowCount + 1];

            string HeaderString = "";

            for (int ColIndex = 0; ColIndex < Table.ColumnCount; ColIndex++)
            {
                HeaderString += Table.Columns[ColIndex].HeaderText;
                if (ColIndex != Table.ColumnCount - 1)
                {
                    HeaderString += ",";
                }
            }
            Result[0] = HeaderString;

            for (int RowIndex = 0; RowIndex < Table.RowCount; RowIndex++)
            {
                string RowString = "";
                for (int ColIndex = 0; ColIndex < Table.ColumnCount; ColIndex++)
                {
                    RowString += (string)Table.Rows[RowIndex].Cells[ColIndex].Value;
                    if (ColIndex != Table.ColumnCount - 1)
                    {
                        RowString += ",";
                    }
                }

                Result[RowIndex + 1] = RowString;
            }

            File.WriteAllLines(ConnectedModel.GetResultsDirectoryPath() + "\\" + ResultFileName, Result);
        }
Exemple #5
0
        public void ModelThreadTask(object ThreadObject)
        {
            string ModelName   = (string)ThreadObject;
            int    ModelIndex  = SimulationController.FindModelIndex(ModelName);
            Model  ThreadModel = SimulationController.FindModel(ModelName);

            ToLogsTextBox("Model " + ModelName + " thread started");

            // PREPARATION BEGIN
            ToLogsTextBox("Model " + ModelName + " preparation for simulation started");

            SimulationController.ModelsStates[ModelIndex][1] = State.Running;
            Invoke(new Action(() => { UpdateSimulationState(); }));

            Directory.CreateDirectory(ThreadModel.GetResultsDirectoryPath());

            string PreparationResult = "";

            try
            {
                if (ThreadModel.GetType() == ModelsTypes.UOCNS)
                {
                    UOCNS Uocns = (UOCNS)ThreadModel;
                    Uocns.PrepareForSimulation();
                }
                else if (ThreadModel.GetType() == ModelsTypes.Booksim)
                {
                    Booksim BookSim = (Booksim)ThreadModel;
                    BookSim.PrepareForSimulation();
                }
                else if (ThreadModel.GetType() == ModelsTypes.Newxim)
                {
                    Newxim NewXim = (Newxim)ThreadModel;
                    NewXim.PrepareForSimulation();
                }

                PreparationResult = State.Completed;
            }
            catch (Exception PreparationException)
            {
                PreparationResult = State.Error;
                ToLogsTextBox("Model " + ModelName + " preparation for simulation " + PreparationResult.ToLower() + ": " + PreparationException.Message + PreparationException.StackTrace);
            }

            if (PreparationResult == State.Error)
            {
                SimulationController.ModelsStates[ModelIndex][1] = State.Error;
                SimulationController.ModelsStates[ModelIndex][2] = State.Stopped;
                SimulationController.ModelsStates[ModelIndex][3] = State.Stopped;
                Invoke(new Action(() => { UpdateSimulationState(); }));
                ToLogsTextBox("Model " + ModelName + " thread finished");
            }
            else
            {
                ToLogsTextBox("Model " + ModelName + " preparation for simulation " + PreparationResult.ToLower());
                // PREPARATION END

                // SIMULATION BEGIN
                ToLogsTextBox("Model " + ModelName + " simulation started");

                SimulationController.ModelsStates[ModelIndex][1] = State.Completed;
                SimulationController.ModelsStates[ModelIndex][2] = State.Running;
                Invoke(new Action(() => { UpdateSimulationState(); }));

                int  ProcessExitCode = 0;
                bool ExceptionThrown = false;
                try
                {
                    if (ThreadModel.GetType() == ModelsTypes.UOCNS)
                    {
                        UOCNS Uocns = (UOCNS)ThreadModel;
                        SimulationController.ModelsProcesses[ModelIndex].StartInfo.FileName  = Uocns.GetExecutableFilePath();
                        SimulationController.ModelsProcesses[ModelIndex].StartInfo.Arguments = Uocns.GetConfigFilePath() + " " + Uocns.GetResultsDirectoryPath();

                        SimulationController.ModelsProcesses[ModelIndex].StartInfo.CreateNoWindow = true;

                        // SIMULATION PROCESS BEGIN
                        SimulationController.ModelsProcesses[ModelIndex].Start();
                        ToLogsTextBox("Model " + ModelName + " simulation process started");

                        SimulationController.ModelsProcesses[ModelIndex].WaitForExit();
                        ProcessExitCode = SimulationController.ModelsProcesses[ModelIndex].ExitCode;
                        ToLogsTextBox("Model " + ModelName + " simulation process finished with exit code " + ProcessExitCode.ToString());
                        // SIMULATION PROCESS END

                        SimulationController.ModelsProcesses[ModelIndex].Close();
                    }
                    else if (ThreadModel.GetType() == ModelsTypes.Booksim)
                    {
                        Booksim BookSim = (Booksim)ThreadModel;

                        SimulationController.ModelsProcesses[ModelIndex].StartInfo.FileName               = BookSim.GetExecutableFilePath();
                        SimulationController.ModelsProcesses[ModelIndex].StartInfo.Arguments              = BookSim.GetConfigFilePath();
                        SimulationController.ModelsProcesses[ModelIndex].StartInfo.CreateNoWindow         = true;
                        SimulationController.ModelsProcesses[ModelIndex].StartInfo.UseShellExecute        = false;
                        SimulationController.ModelsProcesses[ModelIndex].StartInfo.RedirectStandardOutput = true;

                        ToLogsTextBox("Model " + ModelName + " simulation process started");

                        int    IterationsAmount   = int.Parse(BookSim.GetIterationsAmount());
                        string StartInjectionRate = BookSim.GetInjectionRate();
                        for (int IterationNumber = 0; IterationNumber < IterationsAmount; IterationNumber++)
                        {
                            string IterationInjectionRate = Optimization.ConstantStep(IterationNumber, StartInjectionRate);
                            ((Booksim)SimulationController.Models[ModelIndex]).SetInjectionRate(IterationInjectionRate);
                            ((Booksim)SimulationController.Models[ModelIndex]).PrepareForSimulation();

                            SimulationController.ModelsProcesses[ModelIndex].Start();
                            string IterationResult = SimulationController.ModelsProcesses[ModelIndex].StandardOutput.ReadToEnd();

                            SimulationController.ModelsProcesses[ModelIndex].WaitForExit();
                            ProcessExitCode  = SimulationController.ModelsProcesses[ModelIndex].ExitCode;
                            IterationResult += "\r\nExit code: " + ProcessExitCode.ToString() + "\r\n";

                            if (IterationNumber == 0)
                            {
                                File.WriteAllText(BookSim.GetResultsDirectoryPath() + "\\" + Booksim.DefaultResultFileName, IterationResult);
                            }
                            else
                            {
                                File.AppendAllText(BookSim.GetResultsDirectoryPath() + "\\" + Booksim.DefaultResultFileName, IterationResult);
                            }
                        }

                        ((Booksim)SimulationController.Models[ModelIndex]).SetInjectionRate(StartInjectionRate);

                        ToLogsTextBox("Model " + ModelName + " simulation process finished with exit code " + ProcessExitCode.ToString());

                        SimulationController.ModelsProcesses[ModelIndex].Close();
                    }
                    else if (ThreadModel.GetType() == ModelsTypes.Newxim)
                    {
                        Newxim NewXim = (Newxim)ThreadModel;
                        SimulationController.ModelsProcesses[ModelIndex].StartInfo.FileName               = NewXim.GetExecutableFilePath();
                        SimulationController.ModelsProcesses[ModelIndex].StartInfo.Arguments              = "-config " + NewXim.GetConfigFilePath();
                        SimulationController.ModelsProcesses[ModelIndex].StartInfo.UseShellExecute        = false;
                        SimulationController.ModelsProcesses[ModelIndex].StartInfo.RedirectStandardOutput = true;
                        SimulationController.ModelsProcesses[ModelIndex].StartInfo.CreateNoWindow         = true;

                        // SIMULATION PROCESS BEGIN
                        ToLogsTextBox("Model " + ModelName + " simulation process started");

                        int    IterationsAmount   = int.Parse(NewXim.GetIterationsAmount());
                        string StartInjectionRate = NewXim.GetPacketInjectionRate();
                        for (int IterationNumber = 0; IterationNumber < IterationsAmount; IterationNumber++)
                        {
                            string IterationInjectionRate = Optimization.ConstantStep(IterationNumber, StartInjectionRate);
                            ((Newxim)SimulationController.Models[ModelIndex]).SetPacketInjectionRate(IterationInjectionRate);
                            ((Newxim)SimulationController.Models[ModelIndex]).PrepareForSimulation();

                            SimulationController.ModelsProcesses[ModelIndex].Start();
                            string IterationResult = SimulationController.ModelsProcesses[ModelIndex].StandardOutput.ReadToEnd();

                            SimulationController.ModelsProcesses[ModelIndex].WaitForExit();
                            ProcessExitCode  = SimulationController.ModelsProcesses[ModelIndex].ExitCode;
                            IterationResult += "\r\nExit code: " + ProcessExitCode.ToString() + "\r\n";

                            if (IterationNumber == 0)
                            {
                                File.WriteAllText(NewXim.GetResultsDirectoryPath() + "\\" + Newxim.DefaultResultFileName, IterationResult);
                            }
                            else
                            {
                                File.AppendAllText(NewXim.GetResultsDirectoryPath() + "\\" + Newxim.DefaultResultFileName, IterationResult);
                            }
                        }

                        ((Newxim)SimulationController.Models[ModelIndex]).SetPacketInjectionRate(StartInjectionRate);

                        ToLogsTextBox("Model " + ModelName + " simulation process finished with exit code " + ProcessExitCode.ToString());
                        // SIMULATION PROCESS END

                        SimulationController.ModelsProcesses[ModelIndex].Close();
                    }
                }
                catch (Exception SimulationException)
                {
                    ExceptionThrown = true;
                    ToLogsTextBox("Model " + ModelName + " simulation finished with error: " + SimulationException.Message + SimulationException.StackTrace);
                }

                if (ExceptionThrown || (ThreadModel.GetType() == ModelsTypes.UOCNS && ProcessExitCode != 0) || (ThreadModel.GetType() == ModelsTypes.Booksim && ProcessExitCode != 255) || (ThreadModel.GetType() == ModelsTypes.Newxim && ProcessExitCode != 0))
                {
                    SimulationController.ModelsStates[ModelIndex][2] = State.Error;
                    SimulationController.ModelsStates[ModelIndex][3] = State.Stopped;
                    Invoke(new Action(() => { UpdateSimulationState(); }));
                    if (!ExceptionThrown)
                    {
                        ToLogsTextBox("Model " + ModelName + " simulation finished");
                    }
                    ToLogsTextBox("Model " + ModelName + " thread finished");
                }
                else
                {
                    ToLogsTextBox("Model " + ModelName + " simulation finished");
                    // SIMULATION END

                    // COLLECTING RESULTS BEGIN
                    ToLogsTextBox("Model " + ModelName + " collecting results started");

                    SimulationController.ModelsStates[ModelIndex][2] = State.Completed;
                    SimulationController.ModelsStates[ModelIndex][3] = State.Running;
                    Invoke(new Action(() => { UpdateSimulationState(); }));

                    string CollectingResultsResult = "";
                    try
                    {
                        if (ThreadModel.GetType() == ModelsTypes.UOCNS)
                        {
                            UOCNS Uocns = (UOCNS)ThreadModel;
                            string[,] Result = Uocns.CollectSimulationResults();
                            Invoke(new Action(() => { FillResultsTables(ModelIndex, Result); }));
                        }
                        else if (ThreadModel.GetType() == ModelsTypes.Booksim)
                        {
                            Booksim BookSim = (Booksim)ThreadModel;
                            string[,] Result = BookSim.CollectSimulationResults();
                            Invoke(new Action(() => { FillResultsTables(ModelIndex, Result); }));
                        }
                        else if (ThreadModel.GetType() == ModelsTypes.Newxim)
                        {
                            Newxim NewXim = (Newxim)ThreadModel;
                            string[,] Result = NewXim.CollectSimulationResults();
                            Invoke(new Action(() => { FillResultsTables(ModelIndex, Result); }));
                        }

                        CollectingResultsResult = State.Completed;
                    }
                    catch (Exception CollectingResultsException)
                    {
                        CollectingResultsResult = State.Error;
                        ToLogsTextBox("Model " + ModelName + " collecting results " + CollectingResultsResult.ToLower() + ": " + CollectingResultsException.Message + CollectingResultsException.StackTrace);
                    }

                    if (CollectingResultsResult == State.Error)
                    {
                        SimulationController.ModelsStates[ModelIndex][3] = State.Error;
                    }
                    else
                    {
                        SimulationController.ModelsStates[ModelIndex][3] = State.Completed;
                        ToLogsTextBox("Model " + ModelName + " collecting results " + CollectingResultsResult.ToLower());
                    }

                    Invoke(new Action(() => { UpdateSimulationState(); }));
                    // COLLECTING RESULTS END

                    ToLogsTextBox("Model " + ModelName + " thread finished");
                } // if SIMULATION COMPLETED
            }     // if PREPARATION COMPLETED
        }         // Task