コード例 #1
0
        private void btn_Stop_Sim_Click(object sender, EventArgs e)
        {
            if (isSimRunning)
            {
                if (!(e is EventArgsMessage))
                    if (!(MessageBox.Show("Do you really want to stop this simulation?",
                        "STOP SIM?", MessageBoxButtons.YesNo) == DialogResult.Yes))
                        return;

                run = int.MaxValue - 1000;

                timerDraw.Tick -= new EventHandler(runVisualizer);
                enableConfiguration();
                enableConfigurationMultirun();
                lock (reporter)
                    reporter = null;
                lock (_tableIO)
                {
                    foreach (StreamWriter sw in _tableIO.Values)
                        sw.Close();
                    _tableIO = new Dictionary<string, StreamWriter>();
                }
                if (btnToggleGraphics.Text == "Start Graphics")
                    btnToggleGraphics_Click(sender, e);
                isSimRunning = false;
                firstResize = false;
                staticQueue.Clear();
                gfxBackBuffer.Clear(fieldColor);
                gfxStaticBuffer.Clear(Color.Transparent);
                gfxForeBuffer.Clear(Color.Transparent);
                if (e is EventArgsMessage)
                {
                    EventArgsMessage args = (EventArgsMessage)e;
                    this.label_Info.Text = args.Message;
                }
                else
                    this.label_Info.Text = "Simulation Stopped!";

                this.btn_Pause_Sim.Image = Image.FromStream(this.GetType().Assembly.GetManifestResourceStream("MNS_Visualizer._icons.play.png"));
                if ((string)btn_Pause_Sim.Tag == "pause")
                    btn_Pause_Sim.Click -= new EventHandler(btn_Pause_Sim_Click);
                else if ((string)btn_Pause_Sim.Tag == "play")
                    btn_Pause_Sim.Click -= new EventHandler(btn_Play_Sim_Click);
                else if ((string)btn_Pause_Sim.Tag == "run")
                    btn_Pause_Sim.Click -= new EventHandler(btn_Run_Sim_Click);
                btn_Pause_Sim.Click += new EventHandler(btn_Run_Sim_Click);
            }
        }
コード例 #2
0
        private void btn_RunSim_Click(object sender, EventArgs e)
        {
            isSimPaused = false;
            if (isSimRunning)
                return;
            disableConfiguration();

            label_Info.Text = "Configuring Simulation.";
            label_Info.Refresh();
            System.Threading.Thread.Sleep(250);
            // ********** VERIFY CONFIGURATION **********
            // This could be moved to click/change events
            // and the Begin button disabled until config
            // is validated.
            bool valid = true;

            if (selected_IApplicationEventGenerator.ForeColor == Color.Red)
                valid = false;
            if (selected_IDeployer.ForeColor == Color.Red)
                valid = false;
            if (selected_ILocation.ForeColor == Color.Red)
                valid = false;
            if (selected_INodeFactory.ForeColor == Color.Red)
                valid = false;
            if (selected_IPhysicalProcessor.ForeColor == Color.Red)
                valid = false;
            if (selected_IRandomizerFactory.ForeColor == Color.Red)
                valid = false;
            if (selected_GraphicsRunning.ForeColor == Color.Red)
                valid = false;

            string filename = text_IOFolder.Text + "\\"
                    + text_IOFileTag.Text + "_SimInfo.txt";

            if (filename.IndexOfAny(System.IO.Path.GetInvalidPathChars()) > -1)
                valid = false;

            if (!valid)
            {
                enableConfiguration();
                label_Info.Text = "ERR: Fix Highlighted Red/Yellow Options.";
                return;
            }

            // To prevent SW from getting stuck. Seems to happen when restarting many visualizations.
            sw = new StopWatch.StopWatch();
            sw.Reset();
            resetTimeTick = 0;

            // Set up modules & settings
            Type INodeFactoryType = (Type)this.cb_INode.SelectedItem;
            Type IPhysicalProcessorType = (Type)this.cb_IPhysicalProcessor.SelectedItem;
            Type IDeployerType = (Type)this.cb_IDeployer.SelectedItem;
            Type IApplicationEventGeneratorType = (Type)this.cb_IApplicationEventGenerator.SelectedItem;
            Type IRandomizerFactoryType = (Type)this.cb_IRandomizerFactory.SelectedItem;
            Type ILocationType = (Type)this.cb_ILocation.SelectedItem;
            double x1 = double.Parse(text_FieldX1.Text);
            double y1 = double.Parse(text_FieldY1.Text);
            double x2 = double.Parse(text_FieldX2.Text);
            double y2 = double.Parse(text_FieldY2.Text);

            double timeScale = double.Parse(TimeScale.Text);
            if (!cb_InvertScale.Checked)
                timeScale = 1 / timeScale;                          // Sim seconds per real second
            secPerTick = timeScale / double.Parse(FPS.Text);        // Sim seconds per frame

            bool multirun = false;
            int numruns = 1;
            if (cb_Multirun.Checked && (tb_Multirun.BackColor == Color.White))
            {
                multirun = true;
                numruns = int.Parse(tb_Multirun.Text);
                disableConfigurationMultirun();
            }

            outputFile = null;

            // write to Run Info File
            string indent = "     ";
            StreamWriter simInfoFile = new StreamWriter(filename, false);
            simInfoFile.WriteLine("Simulation Tag: " + text_IOFileTag.Text);
            simInfoFile.Write("Run Flags: ");
            if (cb_AppSetsSink.Checked)
                simInfoFile.Write("[Application Sets Sink Node] ");
            if (cb_randomSink.Checked)
                simInfoFile.Write("[Randomized Sink Node] ");
            if (cb_GraphicsOff.Checked)
                simInfoFile.Write("[Graphics Off] ");
            else
                simInfoFile.Write("[Graphics On] ");
            if (cb_Multirun.Checked)
                simInfoFile.Write("[Multirun with " + tb_Multirun.Text + " Runs]");
            simInfoFile.Write("\n");

            simInfoFile.WriteLine("ILocation: " + ILocationType.ToString());
            simInfoFile.WriteLine(indent + "Initial Corner: ("
                + text_FieldX1.Text + ", " + text_FieldY1.Text + ")");
            simInfoFile.WriteLine(indent + "Final Corner:   ("
                + text_FieldX2.Text + ", " + text_FieldY2.Text + ")");

            // Initial randomizer factory
            IRandomizerFactory randomFactoryMultirun
                        = (IRandomizerFactory)Activator.CreateInstance(IRandomizerFactoryType);
            randomFactoryMultirun.PanelObjs = setPanelObjValues(PanelObjs_IRandomizerFactory);
            randomFactoryMultirun.Initialize();

            simInfoFile.WriteLine("IRandomizerFactory: " + IRandomizerFactoryType.ToString());
            foreach (PanelObj pObj in PanelObjs_IRandomizerFactory)
            {
                pObj.UpdateInfo();
                simInfoFile.WriteLine(indent + pObj.name + ": Text = " + pObj.text
                    + "; Value = " + pObj.value);
            }

            simInfoFile.WriteLine("IDeployer: " + IDeployerType.ToString());
            foreach (PanelObj pObj in PanelObjs_IDeployer)
            {
                pObj.UpdateInfo();
                simInfoFile.WriteLine(indent + pObj.name + ": Text = " + pObj.text
                    + "; Value = " + pObj.value);
            }

            simInfoFile.WriteLine("IPhysicalProcessor: " + IPhysicalProcessorType.ToString());
            foreach (PanelObj pObj in PanelObjs_IPhysProc)
            {
                pObj.UpdateInfo();
                simInfoFile.WriteLine(indent + pObj.name + ": Text = " + pObj.text
                    + "; Value = " + pObj.value);
            }

            simInfoFile.WriteLine("INodeFactory: " + INodeFactoryType.ToString());
            foreach (PanelObj pObj in PanelObjs_INode)
            {
                pObj.UpdateInfo();
                simInfoFile.WriteLine(indent + pObj.name + ": Text = " + pObj.text
                    + "; Value = " + pObj.value);
            }

            simInfoFile.WriteLine("IApplicationEventGenerator: " + IApplicationEventGeneratorType.ToString());
            foreach (PanelObj pObj in PanelObjs_IApplicationEventGenerator)
            {
                pObj.UpdateInfo();
                simInfoFile.WriteLine(indent + pObj.name + ": Text = " + pObj.text
                    + "; Value = " + pObj.value);
            }

            simInfoFile.WriteLine();

            // Start running simulation
            label_Info.Text = "Simulation Running";
            label_Info.Refresh();
            System.Threading.Thread.Sleep(250);

            for (run = 0; run < numruns; run++)
            {
                // SIM START
                isSimRunning = true;
                reporter = new Reporter(secPerTick);
                reporter.EnableGraphics = !cb_GraphicsOff.Checked;

                IRandomizerFactory randomFactory = (IRandomizerFactory)Activator.CreateInstance(IRandomizerFactoryType);
                randomFactory.PanelObjs = setPanelObjValues(PanelObjs_IRandomizerFactory);
                if (run == 0)
                    randomFactory.SetSeed(randomFactoryMultirun.InitialSeed);
                else
                    randomFactory.SetSeed(randomFactoryMultirun.CreateRandomizer().Next());

                simInfoFile.WriteLine("Run #" + run + ": Randomizer Seed Info");
                simInfoFile.WriteLine(indent + "Random Seed: " + randomFactory.InitialSeed);

                Nodes nodes = new Nodes(randomFactory.CreateRandomizer());

                EventManager eventMgr = new EventManager();

                IPhysicalProcessor physProc
                    = (IPhysicalProcessor)Activator.CreateInstance(IPhysicalProcessorType);
                physProc.PanelObjs = setPanelObjValues(PanelObjs_IPhysProc);

                ReporterIWF repIWF = new ReporterIWF(secPerTick, physProc.TransmissionSpeed,
                    physProc.PropagationSpeed);
                repIWF.Attach(reporter);
                physProc.RepIWF = repIWF;

                INodeFactory nodeFactory = (INodeFactory)Activator.CreateInstance(INodeFactoryType);
                nodeFactory.PanelObjs = setPanelObjValues(PanelObjs_INode);
                nodeFactory.Initialize(eventMgr, physProc, randomFactory, reporter);

                XYDoubleLocation[] field = new XYDoubleLocation[2];
                field[0] = new XYDoubleLocation(x1, y1);
                field[1] = new XYDoubleLocation(x2, y2);

                IDeployer deployer = (IDeployer)Activator.CreateInstance(IDeployerType);
                deployer.PanelObjs = setPanelObjValues(PanelObjs_IDeployer);
                deployer.Initialize(nodes, nodeFactory, field, randomFactory);

                //SimulationCompleteEvent finalEvent = new SimulationCompleteEvent();
                //finalEvent.Time = 0 /*hours*/   * 60 * 60
                //                + 10 /*minutes*/ * 60
                //                + 0 /*seconds*/;
                //eventMgr.AddEvent(finalEvent);

                deployer.Deploy();

                // Physical Processor needs to be initialized after the nodes are deployed.
                physProc.Initialize(nodes, eventMgr);
                repIWF.MaxBitDistance = physProc.MaximumRange;

                IApplicationEventGenerator eventGen = (IApplicationEventGenerator)
                        Activator.CreateInstance(IApplicationEventGeneratorType);
                eventGen.PanelObjs = setPanelObjValues(PanelObjs_IApplicationEventGenerator);
                eventGen.Attach(reporter);
                eventGen.Initialize(eventMgr, nodes, randomFactory.CreateRandomizer(),
                    field);

                if (cb_AppSetsSink.Checked)
                {
                    if (cb_randomSink.Checked)
                    {
                        eventGen.GenerateEvent();
                        nodes.SetRandomSinkNode();
                    }
                    else
                    {
                        INode[] furthestPair = nodes.FindFurthestNodes();
                        eventGen.GenerateEvent(furthestPair[0].Location);
                        furthestPair[1].IsSink = true;
                    }
                }
                else
                    eventGen.GenerateEvent();

                nodes.InitializeNodes();

                tab_INode.SelectedTab = tab_Statistics;

                backgroundWorker_RunSim.RunWorkerAsync(eventMgr);
                while (backgroundWorker_RunSim.IsBusy)
                    Application.DoEvents();

                if (cb_GraphicsOff.Checked)
                {
                    label_Info.Text = "Run " + (run + 1).ToString() + " of " + numruns.ToString()
                        + " is running.";
                    label_Info.Refresh();

                    runSimStruct runSimArgs;
                    runSimArgs.outputFile = outputFile;
                    runSimArgs.run = run;

                    backgroundWorker_RunSim.RunWorkerAsync(runSimArgs);
                    while (backgroundWorker_RunSim.IsBusy)
                        Application.DoEvents();
                }
                else
                {
                    label_Info.Text = "Visualizer Running";
                    label_Info.Refresh();
                    currTimeTick = 0;
                    timerDraw.Tick += new EventHandler(runVisualizer);
                    timerDraw.Start();
                }
            }

            if (cb_GraphicsOff.Checked)
            {// Press "stop"
                EventArgsMessage args = new EventArgsMessage("Simulation Complete.");
                btn_Stop_Sim_Click(sender, args);
                enableConfigurationMultirun();
            }

            simInfoFile.Close();
        }