dispatcher_jsimschema.java

来自「一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!」· Java 代码 · 共 531 行 · 第 1/2 页

JAVA
531
字号
     * @return the percentage of progress (= number of computed confidence intervals /
     * total number of required confidence intervals)
     */
    public double checkSimProgress() {
        if (!simStarted) {
            //not started yet
            return 0.0;
        }
        if (simFinished) {
            //already finished
            return 1.0;
        }
        return progressTime;
    }



    /**
     * Pauses the simulation, refreshes temp measures and then restarts simulation
     */
    public synchronized void refreshTempMeasures() {

        //simulation not started yet
        if (!simStarted)
            return;

        //pauses the computation
        if (pauseSim()) {
            // Aborts measures (if any)
            while (!measuresToAbort.isEmpty()) {
                int index = ((Integer)measuresToAbort.remove(0)).intValue();
                tempMeasures[index].abort();
            }

            //at the first execution, measures must be retrieved
            if (tempMeasures == null) {
                if (net == null) {
                    return;
                } else {
                    //gets the measures defined for this queue network
                    LinkedList measures = net.getMeasures();

                    //creates the array of temp measures
                    tempMeasures = new TempMeasure[measures.size()];

                    for (int m = 0; m < measures.size(); m++) {
                        tempMeasures[m] = new TempMeasure((Measure) measures.get(m));
                        tempMeasures[m].refreshMeasure();
                    }
                }
            } else {
                //temp measures have already been retrieved
                //only refresh temp values
                for (int m = 0; m < tempMeasures.length; m++) {
                    tempMeasures[m].refreshMeasure();
                }
            }
            // Updates simulation progress time
            try {
                progressTime = NetSystem.checkProgress(net);
            } catch (NetException e) {
                progressTime = 0.0;
            }

            //restart computation
            restartSim();

        } else if (simFinished) { // Gets last value for each measure
            for (int m = 0; m < tempMeasures.length; m++) {
                tempMeasures[m].refreshMeasure();
            }
        }
    }


    /**
     * gets the temp measures
     * @return the temp measures
     */
    public TempMeasure[] getTempMeasures() {
        return tempMeasures;
    }



    public synchronized boolean abortAllMeasures() {
        if (!simStarted) {
            return false;
        }

        if (pauseSim()) {
            refreshTempMeasures();

            logger.debug("All remaining measures will be aborted");
            for (int m = 0; m < tempMeasures.length; m++) {
                abortMeasure(m);
            }

            return true;

        } else {
            return false;
        }
    }

    public synchronized boolean killSimulation() {
        //simulation not started yet
        if (!simStarted)
            return false;
        try {
            NetSystem.terminate();
        }
        catch (NetException nex) {
            nex.printStackTrace();
        }
        return true;
    }


    public synchronized boolean abortMeasure(int index) {
        boolean success = false;

        if (pauseSim()) {
            refreshTempMeasures();

            if ((0 <= index) && (index <= tempMeasures.length)) {
                success = tempMeasures[index].abort();
            } else {
                success = false;
            }
            restartSim();
        }

        return success;
    }

    /**
     * Aborts a measure at next measure refresh... Used to avoid to stop simulation too frequently
     * Author: Bertoli Marco
     */
    public synchronized void abortMeasureAtRefresh(int measureIndex) {
        measuresToAbort.add(new Integer(measureIndex));
    }


    /**
     * Prints on System.out the temp values of measures that haven't finished yet
     */
    public void printTempMeasures() {

        if (pauseSim()) {
            if (tempMeasures != null) {
                for (int m = 0; m < tempMeasures.length; m++) {
                    TempMeasure temp = tempMeasures[m];
                    if (!temp.isFinished()) {
                        System.out.println(temp.getName() + ": " +
                                Double.toString(temp.getTempMean()) + " " +
                                temp.getNsamples());
                    }
                }
            }
            restartSim();
        }

    }



    /**
     * Pauses the simulation
     * @return true if the operation was successful, false otherwise (for example
     * simulation not started or already finished)
     */
    public synchronized boolean pauseSim() {
        simPaused = true;
        return NetSystem.pause();
    }

    /**
     * Resumes the simulation
     * @return true if the operation was successful, false otherwise (for example
     * simulation not started or already finished)
     */
    public synchronized boolean restartSim() {
        simPaused = false;
        return NetSystem.restartFromPause();
    }


    //Francesco D'Aquino
    public Simulation getSimulation() {
        return sim;
    }


    //------------------------TEST-----------------------------------//


    public static void testSimulationTime() {

        int N = 5;
        long[] duration = new long[N];
        long tot = 0;


        for (int i = 0; i < N; i++) {

            String path = "D:\\JMTtest\\prova" + i + ".xml";

            Dispatcher_jSIMschema disp = new Dispatcher_jSIMschema(path);
            long start, stop, elapsed;

            start = System.currentTimeMillis();

            if (disp.solveHandlingExceptions()) {
                stop = System.currentTimeMillis();
                elapsed = stop - start;
                //System.out.println("Duration execution "+i+": ");
                System.out.println(Long.toString(elapsed));

                duration[i] = elapsed;
                tot += elapsed;

            } else {
                System.out.println("Error!!");
            }
        }

        long mean = tot / N;
        System.out.println("Mean: ");
        System.out.println(Long.toString(mean));

    }


    public static void testSolution() {
        //Dispatcher disp = new Dispatcher("D:\\test_2open.xml");
        Dispatcher_jSIMschema disp = new Dispatcher_jSIMschema("D:\\JMTtest\\solverstep0.xml");
        //Dispatcher disp = new Dispatcher("D:\\JMTtest\\solverstep10.xml");
        //Dispatcher disp = new Dispatcher("D:\\JMTtest\\prova.xml");
        //Dispatcher disp = new Dispatcher("D:\\JMTtest\\test_base.xml");

        //Dispatcher disp = new Dispatcher("D:\\JMTtest\\model_with_blocking.xml");
        //Dispatcher disp = new Dispatcher("D:\\JMTtest\\model_with_blocking_2.xml");
        //Dispatcher disp = new Dispatcher("D:\\JMTtest\\model_with_blocking_open_drop.xml");
        //Dispatcher disp = new Dispatcher("D:\\JMTtest\\preload.xml");
        disp.setSimulationSeed(23000);


        long start, stop, elapsed;
        start = System.currentTimeMillis();

        if (disp.solveHandlingExceptions()) {
            stop = System.currentTimeMillis();
            elapsed = stop - start;
            System.out.println("OK - Simulation time: ");
            System.out.println(Long.toString(elapsed));
        } else {
            System.out.println("Error!!");
        }

    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?