⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simulation.java

📁 一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!
💻 JAVA
📖 第 1 页 / 共 3 页
字号:

            //add measures
            SimMeasure ms;

            for (int i = 0; i < measures.size(); i++) {
                ms = (SimMeasure) measures.get(i);
                boolean verbose = ms.getMeasure().getVerbose();

                if (verbose) {
                    //if true, for each Measure sets the corresponding MeasureOutput
                    //a measure output can be a file (xml, txt, ...) which contains
                    //samples and final report of that measure

                    //MeasureOutput output = new SimpleMeasureOutput(ms.getMeasure(), false, ms.getMeasure().getName() + ".log");
                    MeasureOutput output = new CSVMeasureOutput(ms.getMeasure(), false);
                }

                network.addMeasure(ms.getMeasure());
            }


            //add all nodes sections
            for (int i = 0; i < nodes.size(); i++) {
                SimNode n = (SimNode) nodes.get(i);
                if (n.getInput() != null)
                    n.getNode().addSection(n.getInput());
                if (n.getService() != null)
                    n.getNode().addSection(n.getService());
                if (n.getOutput() != null)
                    n.getNode().addSection(n.getOutput());
            }


            //NEW
            //@author Stefano Omini

            //add blocking regions
            BlockingRegion br;
            for (int i = 0; i < regions.size(); i++) {
                br = (BlockingRegion) regions.get(i);

                String regionName = br.getName();
                String inputStationName = regionName + "_inputStation";

                InputSection is = new BlockingQueue(br);
                ServiceSection ss = new ServiceTunnel();
                OutputSection os = new BlockingRouter(br);

                SimNode inputStation = new SimNode(inputStationName, is, ss, os);

                //adds the input station of the blocking region
                nodes.add(inputStation);

                network.addNode(inputStation.getNode());

                //auto-connect the node, to avoid problems in job info lists refreshing
                //(otherwise a node with no connections presents problems)
                inputStation.getNode().connect(inputStation.getNode());

                NetNode inputSt = inputStation.getNode();
                if (inputStation.getInput() != null)
                    inputStation.getNode().addSection(inputStation.getInput());
                if (inputStation.getService() != null)
                    inputStation.getNode().addSection(inputStation.getService());
                if (inputStation.getOutput() != null)
                    inputStation.getNode().addSection(inputStation.getOutput());

                //sets the input station of the blocking region
                br.setInputStation(inputSt);

                //sets blocking region behaviour for inner nodes
                String[] regNodes = br.getRegionNodeNames();
                for (int j = 0; j < regNodes.length; j++) {
                    NetNode innerNode = br.getRegionNode(regNodes[j]);

                    //at the moment inner stations must have a Queue-type input section
                    //and a Router-type output section
                    //other not compliant sections will cause a NetException

                    //nodes which receive jobs from the outside must
                    //have redirecting queue behaviour turned on
                    NodeSection input = innerNode.getSection(NodeSection.INPUT);
                    if (input instanceof Queue) {
                        ((Queue) input).redirectionTurnON(br);
                    } else {
                        throw new NetException("Error in creating blocking region: " +
                                "inner station " + innerNode.getName() +
                                " has a not compliant input section.");
                    }

                    //nodes which sends jobs outside the region must have border router behaviour
                    NodeSection output = innerNode.getSection(NodeSection.OUTPUT);
                    if (output instanceof Router) {
                        ((Router) output).borderRouterTurnON(br);
                    } else {
                        throw new NetException("Error in creating blocking region: " +
                                "inner station " + innerNode.getName() +
                                " has a not compliant output section.");
                    }
                }
                if (MONITOR_BLOCKING_REGION) {
                    //adds measure about blocking region
                    Measure br_q_measure;
                    Measure br_resp_measure;
                    InverseMeasure br_thr_measure;

                    String className;

                    for (int c = 0; c < classes.length; c++) {
                        className = classes[c].getName();
                        //add queue length measure for this class
                        br_q_measure = new Measure(inputStationName+"_QL_"+className,
                                0.1, 0.1, 500000, false, null);
                        addMeasure(SimConstants.QUEUE_LENGTH, inputStationName,
                                br_q_measure, className);
                        //add response time measure for this class
                        br_resp_measure = new Measure(inputStationName+"_RESP_"+className,
                                0.1, 0.1, 500000, false, null);
                        addMeasure(SimConstants.RESPONSE_TIME, inputStationName,
                                br_resp_measure, className);
                        //add throughput measure for this class
                        br_thr_measure = new InverseMeasure(inputStationName+"_THR_"+className,
                                0.1, 0.1, 500000, false);
                        addMeasure(SimConstants.THROUGHPUT, inputStationName,
                                br_thr_measure, className);

                        network.addMeasure(br_q_measure);
                        network.addMeasure(br_resp_measure);
                        network.addMeasure(br_thr_measure);
                    }
                }

            }
            //end NEW

            //NEW
            //@author Stefano Omini
            //refresh nodes list, after input stations has been added
            netNodes = new NetNode[nodes.size()];
            for (int i = 0; i < nodes.size(); i++) {
                netNodes[i] = ((SimNode) nodes.get(i)).getNode();
            }
            //end NEW

            //intialize measures
            for (int i = 0; i < measures.size(); i++) {
                ms = (SimMeasure) measures.get(i);

                //NEW
                //@author Stefano Omini
                JobClass jClass;
                if (ms.getjClass() != "") {
                    //measure relative to a specified class
                    String[] jName = {ms.getjClass()};
                    int[] position = findClassPosition(jName);
                    jClass = classes[position[0]];
                } else {
                    //aggregated measure
                    jClass = null;
                }
                //end NEW

                // If measure is not global
                if (ms.getNodeName() != null && !ms.getNodeName().equals("")) {
                    //measures are computed by sampling specific
                    int nodePosition = findNodePosition(ms.getNodeName());

                    switch (ms.getMeasureType()) {
                        case SimConstants.QUEUE_LENGTH:
                            //measures the number of jobs in the global node (=mean number of jobs present in node joblist)
                            netNodes[nodePosition].analyze(SimConstants.LIST_NUMBER_OF_JOBS, jClass, ms.getMeasure());
                            break;
                        case SimConstants.UTILIZATION:
                            //measures the utilization of the service section (=mean number of jobs present in service section joblist)
                            netNodes[nodePosition].getSection(NodeSection.SERVICE).analyze(SimConstants.LIST_NUMBER_OF_JOBS, jClass, ms.getMeasure());
                            break;
                        case SimConstants.THROUGHPUT:
                            //measures the throughput of the service section (= throughput of service section joblist)
                            netNodes[nodePosition].getSection(NodeSection.SERVICE).analyze(SimConstants.LIST_THROUGHPUT, jClass, ms.getMeasure());
                            break;
                        case SimConstants.RESPONSE_TIME:
                            //measures the resp time for all visits
                            netNodes[nodePosition].analyze(SimConstants.LIST_RESPONSE_TIME, jClass, ms.getMeasure());
                            break;
                        case SimConstants.RESIDENCE_TIME:
                            //measures the response time on the global node (= mean residence time in node joblist)
                            netNodes[nodePosition].analyze(SimConstants.LIST_RESIDENCE_TIME, jClass, ms.getMeasure());
                            break;
                        case SimConstants.QUEUE_TIME:
                            //measures the queue time spent in the input setion (= mean residence time in input section joblist)
                            netNodes[nodePosition].getSection(NodeSection.INPUT).analyze(SimConstants.LIST_RESIDENCE_TIME, jClass, ms.getMeasure());
                            break;
                    }
                }
                // Global measures (new by Bertoli Marco)
                else {
                    switch (ms.getMeasureType()) {
                        case SimConstants.SYSTEM_RESPONSE_TIME:
                            network.getJobInfoList().analyzeResponseTime(jClass, ms.getMeasure());
                            break;
                        case SimConstants.SYSTEM_THROUGHPUT:
                            network.getJobInfoList().analyzeThroughput(jClass, ms.getMeasure());
                            break;
                        case SimConstants.SYSTEM_JOB_NUMBER:
                            network.getJobInfoList().analyzeJobNumber(jClass, ms.getMeasure());
                            break;
                    }
                }
            }

            //Preload
            //NEW
            //@author Stefano Omini
            if (preloadEnabled) {
                //preload enabled: uses the info loaded by SimLoader
                for (int s = 0; s < preload_stationNames.length; s++) {
                    //preload, in the specified station, the specified populations
                    preload_station(preload_stationNames[s],
                            preload_initialPopulations[s]);
                }
            }
            //end NEW




        } catch (jmt.common.exception.NetException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        initialized = true;

        logger.debug("Simulation has been initialized");
    }



    /**
     * Runs this simulation. If it hasn't been initialized, then throws an Exception.
     * Controls the timer for max simulation time (if it has been defined).
     * @throws Exception Simulaton not initialized
     */
    public void run() throws Exception {
        if (initialized) {

            //NEW
            //@author Stefano Omini
            if (isTimeLimited()) {
                //sets the timer task
                //a Timer must be created and run
                simTimer = new SimulationTimer(this, maxSimulationTime);
            }
            //end NEW

            //runs the simulation
            NetSystem.start();
            finished = true;

            //NEW
            //@author Stefano Omini
            if (isTimeLimited()) {
                //the simulation has finished before max simulation time has expired
                //cancels the timer
                simTimer.stopTimer();
            }
            //end NEW

            //NEW
            //@author Stefano Omini

            //simulation has finished
            //results are put into a xml file
            XMLSimulationOutput output = new XMLSimulationOutput(this);
            outputFile = output.writeAllMeasures();

            //end NEW

            NetSystem.terminate();
        } else {
            throw new NetException("Simulation has not been initialized");
        }
    }


    /**
     * Aborts simulation.
     * @throws jmt.common.exception.NetException
     */
    public void abort() throws jmt.common.exception.NetException {
        logger.info("Aborting simulation...");
        NetSystem.terminate();
    }

    /**
     * Returns output file handler
     * @return output file handler or null if a problem occurred
     */
    public File getOutputFile() {
        return outputFile;
    }



    //------------------end INITIALIZATION AND RUN------------------------------//





    //-------------------------SUPPORT METHODS---------------------------------//

    //check if exists a node with the given name
    private boolean isNode(String name) {
        for (int i = 0; i < nodes.size(); i++) {
            String netNode = ((SimNode) nodes.elementAt(i)).getNode().getName();
            if (netNode.equals(name))
                return true;
        }
        return false;
    }

    /**
     * Gets the measures contained in the measure buffer.
     * if possible change this method of obtaining intermediate
     * values of measures
     */
    public QueueMeasure getQueueMeasure() {
        return NetSystem.getQueueMeasure();
    }

    //finds the position of the classes from the giving names
    private int[] findClassPosition(String[] names) {
        int[] classPos = new int[names.length];
        for (int i = 0; i < names.length; i++) {
            String name = names[i];
            for (int j = 0; j < classes.length; j++) {
                JobClass aClass = classes[j];
                if (name.equals(aClass.getName()))
                    classPos[i] = j;
            }
        }
        return classPos;
    }

    //finds the node
    private int findNodePosition(String name) throws LoadException {
        for (int i = 0; i < nodes.size(); i++) {
            if (((SimNode) nodes.get(i)).getNode().getName().equals(name))
                return i;
        }
        throw new LoadException("node not found");
    }

    /**
     * Gets all the measures inserted so far.

⌨️ 快捷键说明

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