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

📄 simulation.java

📁 一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/**    
  * Copyright (C) 2006, Laboratorio di Valutazione delle Prestazioni - Politecnico di Milano

  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.

  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.

  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
  
package jmt.engine.simEngine;

import jmt.common.exception.LoadException;
import jmt.common.exception.NetException;
import jmt.engine.NodeSections.*;
import jmt.engine.QueueNet.*;
import jmt.engine.dataAnalysis.*;
import jmt.engine.dataAnalysis.measureOutputs.CSVMeasureOutput;
import jmt.engine.log.JSimLogger;
import jmt.engine.random.engine.RandomEngine;
import org.w3c.dom.Document;

import java.io.IOException;
import java.io.File;
import java.util.Vector;


/**
 * This class creates a new Simulation. It provides an easy way to initialize
 * the jmt engine.
 *
 * @author Federico Granata, Stefano Omini, Bertoli Marco
 */
public class Simulation {
    //TODO Enables this by XML
    /** If true will add blocking region measures if blocking regions are defined */
    public boolean MONITOR_BLOCKING_REGION = false;
    /** Output file handler */
    File outputFile;

    //---------------------- SIMULATION COMPONENTS --------------------------//

    //name of the simulation
    private String name;
    // queueing network associated to this simulation
    private QueueNetwork network;

    //classes of the system
    private JobClass[] classes = null;
    //service centers of the system
    private Vector nodes = new Vector();
    //connections
    private Vector connections = new Vector();
    //measures requested by the simulation
    private Vector measures = new Vector();
    //blocking regions
    private Vector regions = new Vector();

    //used to run the simulation only if the queue network has been initialized
    private boolean initialized = false;

    //NEW
    //@author Stefano Omini
    //logger of the simulation
    private JSimLogger logger = JSimLogger.getLogger(JSimLogger.STD_LOGGER);
    //private JSimLogger logger = JSimLogger.getRootLogger();
    //end NEW

    // Global JobInfoList - Bertoli Marco
    private GlobalJobInfoList globalInfoList;

    //---------------------- end SIMULATION COMPONENTS --------------------------//


    //---------------------- SIMULATION TIMER --------------------------//

    //NEW
    //@author Stefano Omini

    //true if an upper bound has been set to the simulation time
    private boolean timeLimited = false;
    //the max simulation time
    private long maxSimulationTime = 0;
    //the timer which controls max simulation time
    SimulationTimer simTimer = null;
    //true if the simulation has finished
    private boolean finished = false;

    //end NEW

    //---------------------- end SIMULATION TIMER --------------------------//


    //---------------------- SIMULATION SEED --------------------------//

    //NEW
    //@author Stefano Omini

    //seed of the simulation
    private long seed = -1;
    //reference to the RandomEngine used by the simulation
    private RandomEngine randomEng = null;

    //end NEW

    //---------------------- end SIMULATION SEED --------------------------//


    //NEW
    //@author Stefano Omini
    private SimParameters simParameters = null;
    //end NEW


    //---------------------- XML FILES PATH --------------------------//
    //NEW
    //@author Stefano Omini

    //path of the xml file containing the mva model description
    //this attribute has been setted only if a SimLoader has been used to
    //create this Simulation object
    String xmlModelDefPath;

    //path of the xml file containing the simulation model description
    //this attribute has been setted only if a SimLoader has been used to
    //create this Simulation object
    String xmlSimModelDefPath;

    //end NEW

    //---------------------- end XML FILES PATH --------------------------//


    //---------------------- PRELOAD ------------------------------------//

    //if true preload jobs
    private boolean preloadEnabled = false;
    //the names of the stations to be preloaded
    private String[] preload_stationNames;
    //the matrix of initial populations
    private int[][] preload_initialPopulations;

    //---------------------- end PRELOAD -------------------------------//




    //-------------------------CONSTRUCTORS-------------------------------//

    //NEW
    //@author Stefano Omini

    /**
     * Creates a new Simulation object.
     * @param seed simulation seed (if -1, an automatic seed will be generated)
     * @param simName Name of simulation
     * @param maxSimulationTime The max duration (in milliseconds) of the simulation
     * @throws IOException
     *
     */
    public Simulation(long seed, String simName, long maxSimulationTime) throws IOException {

        //name of the simulation
        name = simName;

        //Initializes NetSystem
        NetSystem.initialize();

        //sets the maximum duration of the simulation run
        this.timeLimited = true;
        this.maxSimulationTime = maxSimulationTime;

        //sets seed (if -1 --> automatic seed)
        if (seed != -1) {
            //sets the seed
            setRandomEngineSeed(seed);
        }
    }



    //NEW
    //@author Stefano Omini
    /**
     * Creates a new Simulation object.
     * @param seed simulation seed (if -1, an automatic seed will be generated)
     * @param simName Name of simulation
     * @throws IOException
     */
    public Simulation(long seed, String simName) throws IOException {
        //name of the simulation
        name = simName;

        //Initializes NetSystem
        NetSystem.initialize();

        //sets seed (if -1 --> automatic seed)
        if (seed != -1) {
            //sets the seed
            setRandomEngineSeed(seed);
        }
    }

    //end NEW


    //-------------------------end CONSTRUCTORS-------------------------------//




    //--------------METHODS TO ADD SIMULATION COMPONENTS----------------------//

    // These methods are used while class SimLoader loads the model from an xml file:
    // all the simulation components are at first put into vectors,
    // then the method initialize() uses these vectors to setup the Simulation object.


    /**
     * Adds all Job Classes to the simulation.	 *
     * @param classes the job classes of simulation
     */
    public void addClasses(JobClass[] classes) {
        this.classes = classes;
    }

    /**
     * Adds a node to the simulation model.
     * @param name name of the node
     * @param inSec input section of the node
     * @param serSec service section of the node
     * @param outSec output section of the node
     */
    public void addNode(String name, InputSection inSec, ServiceSection serSec,
                        OutputSection outSec) {
        nodes.add(new SimNode(name, inSec, serSec, outSec));
    }

    /**
     * Connects two nodes of the simulation: if a node hasn't been inserted in the model,
     * then return a LoadException.
     * @param start the sourcce node
     * @param end the target node
     * @throws LoadException one of the nodes hasn't been inserted in te model
     */
    public void addConnection(String start, String end) throws LoadException {
        if (isNode(start) && isNode(end))
            connections.add(new Connection(start, end));
        else
            throw new LoadException("Trying to connect nodes that haven't been inserted yet.");
    }


    /**
     * Adds to the system a new measure to be computed.
     * @param measureType type of measure (see the constants specified in this class).
     * @param nodeName name of the node to be measured.
     * @param measure
     * @param jClass
     * @throws LoadException
     */
    public void addMeasure(int measureType, String nodeName, Measure measure, String jClass) throws LoadException {

        //NEW
        //@author Stefano Omini
        //sets all the parameters shared by all Measure object
        // (i.e. number and size of batches, ...) 
        if (simParameters != null) {
            measure.setSimParameters(simParameters);
        }
        //end NEW

        measures.add(new SimMeasure(measureType, nodeName, measure, jClass));
    }


    //NEW
    //@author Stefano Omini

    /**
     * Creates a blocking region.
     *
     * @param name The name of the blocking region
     * @param maxCapacity the max capacity of the region
     * @param maxCapacityPerClass the max capacity for each class
     * @param classWeights the weight of each class (tokens per job request)
     * @param drop for each class, specifies if jobs should be dropped or not when the region is blocked
     * @param stations the array of the names of the stations contained in the region
     *
     * @throws LoadException if some errors occur during the region creation
     */
    public void addRegion(String name, double maxCapacity, double[] maxCapacityPerClass,
                          double[] classWeights, boolean[] drop, String[] stations) throws LoadException {
        if (true)
        //TODO: andrebbero aggiunti controlli (nome non univoco, jobs<0, ecc...)
            regions.add(new BlockingRegion(name, maxCapacity, maxCapacityPerClass, classWeights, drop, this, stations));
        else
            throw new LoadException("Exception while creating a blocking region.");
    }

    //end NEW


    //--------------end METHODS TO ADD SIMULATION COMPONENTS---------------//


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


    /**
     * Intilalizes all the components of this Simulation.
     * This method assures that all the operations are
     * executed in the correct order.
     *
     */
    public void initialize() throws NetException {

        // creates network
        network = new QueueNetwork("jSIM simulation: " + name);
        // adds network to NetSystem
        NetSystem.addNetwork(network);


        //add all job classes to QueueNetwork
        for (int i = 0; i < classes.length; i++) {
            network.addJobClass(classes[i]);
        }

        try {
            //creates all nodes
            NetNode[] netNodes = new NetNode[nodes.size()];
            for (int i = 0; i < nodes.size(); i++) {
                netNodes[i] = ((SimNode) nodes.get(i)).getNode();
            }

            //add connections
            for (int i = 0; i < connections.size(); i++) {
                int nodePosition1 = findNodePosition(((Connection) connections.get(i)).getStart());
                int nodePosition2 = findNodePosition(((Connection) connections.get(i)).getEnd());
                netNodes[nodePosition1].connect(netNodes[nodePosition2]);
            }

            //add all nodes to QueueNetwork
            for (int i = 0; i < nodes.size(); i++) {
                SimNode simNode = ((SimNode) nodes.get(i));
                if (simNode.isReference())
                //reference nodes are the nodes (random source, terminal, ..)
                //which create jobs: these nodes must receive the start event
                    network.addReferenceNode(simNode.getNode());
                else
                    network.addNode(simNode.getNode());
            }

⌨️ 快捷键说明

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