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

📄 dispatcher_jsimschema.java

📁 一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**    
  * 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.simDispatcher;

import jmt.common.exception.LoadException;
import jmt.common.exception.NetException;
import jmt.engine.QueueNet.NetSystem;
import jmt.engine.QueueNet.QueueNetwork;
import jmt.engine.dataAnalysis.Measure;
import jmt.engine.dataAnalysis.TempMeasure;
import jmt.engine.log.JSimLogger;
import jmt.engine.simEngine.SimLoader;
import jmt.engine.simEngine.Simulation;

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

/**
 * Receives the absolute path of a xml file which describes the model using the
 * SIMmodeldefinition.xsd schema.
 * The simulation results, at the end, are converted and
 * are inserted in the original file.
 *
 * @author Stefano Omini
 * Modified by Bertoli Marco
 *
 * Modified by Francesco D'Aquino
 */
public class Dispatcher_jSIMschema {


    //xml files containing model definition, simmodel definition and sim results
    File simModelDefinition;
    File simResults;

    //path of the xml files
    String simModelDefinitionPath;
    String simResultsPath;

    //true if the simulation results have been already obtained, false otherwise
    boolean resultsObtained = false;

    //if true, the simulation seed
    private boolean automaticSeed = true;
    //if true, the simulation seed
    private long simulationSeed;

    private JSimLogger logger = JSimLogger.getLogger(JSimLogger.STD_LOGGER);

    //used to compute the progress of the simulation
    private boolean simStarted = false;
    private boolean simFinished = false;
    private boolean simPaused = false;
    private QueueNetwork net;


    //used to store temp measures info
    private TempMeasure[] tempMeasures = null;

    // used to store progress time - Bertoli Marco
    private double progressTime;
    private Vector measuresToAbort = new Vector(); // Vector with measures to abort at refresh

    private long maxDuration = -1;

    private Simulation sim;



    /**
     * This constructor receives the absolute path of the xml file containing
     * the model to be solved.
     * The model must be described using the SIMmodeldefinition.xsd schema
     *
     * @param absolutePath absolute path of the xml file containing the
     * model to be solved
     */
    public Dispatcher_jSIMschema(String absolutePath) {
        simModelDefinitionPath = absolutePath;
        simModelDefinition = new File(absolutePath);
    }


    /**
     * This constructor receives the absolute path of the xml file containing
     * the model to be solved.
     * The model must be described using the SIMmodeldefinition.xsd schema
     *
     * @param model the xml file containing the
     * model to be solved
     */
    public Dispatcher_jSIMschema(File model) {
        simModelDefinition = model;
        simModelDefinitionPath = model.getAbsolutePath();
    }




    /**
     * By invoking this method, simulation will generate a random simulation seed
     */
    public void automaticSimulationSeed() {
        automaticSeed = true;
    }

    /**
     * Specifies the seed to be used in the simulation.
     * @param seed the simulation seed
     */
    public void setSimulationSeed(long seed) {
        simulationSeed = seed;
        automaticSeed = false;
    }

    /**
     * Sets simulation max duration time
     * @param durationMillis maximum time in milliseconds
     */
    public void setSimulationMaxDuration(long durationMillis) {
        maxDuration = durationMillis;
    }




    public boolean solveModel() throws IOException, LoadException, NetException, Exception {


        /*********************SIM DEFINITION MODEL*********************/

        // does model file exist??
        if (!simModelDefinition.exists()) {
            //the passed file does not exist
            logger.error("The sim model file " + simModelDefinitionPath + " does not exist...");
            return false;
        } else {
            logger.debug("Sim model definition path: " + simModelDefinitionPath);
        }


        /*********************SIMULATION LOADING AND RUNNING*********************/


        //now prepare simulation
        //try {

            SimLoader simLoader = new SimLoader(simModelDefinitionPath);
            sim = simLoader.getSim();
            // Sets simulation max duration (if specified)
            if (maxDuration > 0)
                sim.setMaxSimulationTime(maxDuration);

            //sets in the Simulation object the path of the
            //xml model definition
            sim.setXmlSimModelDefPath(simModelDefinitionPath);

            if (!automaticSeed) {
                //if automaticSeed == false, then a user defined seed is present
                sim.setRandomEngineSeed(simulationSeed);
                sim.initialize();

                logger.debug("jSIM correctly initialized with simulation seed = " + simulationSeed);
            } else {
                sim.initialize();
                logger.debug("jSIM correctly initialized");
            }

            //find QueueNetwork reference
            net = sim.getNetwork();

            long start, stop, elapsed;

            simStarted = true;
            //Start time
            start = System.currentTimeMillis();



            //run simulation
            sim.run();


            // /stop time
            stop = System.currentTimeMillis();
            elapsed = (stop - start) / 1000;


            simPaused = false;
            simFinished = true;

            logger.info("Model " + simModelDefinitionPath + " solved by jSIM in " + Double.toString(elapsed) + " seconds");
/*
        } catch (IOException e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return false;
        } catch (LoadException e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            return false;
        }   */

        return true;
    }

    public boolean solveHandlingExceptions() {
        try {
            return solveModel();
        }
        catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex.getMessage());
            return false;
        }
    }

    /**
     * Tells if simulation was started
     * @return true iff simulation was started
     */
    public boolean isStarted() {
        return simStarted;
    }

    /**
     * Tells if simulation was finished
     * @return true iff simulation was finished
     */
    public boolean isFinished() {
        return simFinished;
    }

    /**
     * Tells if simulation was paused
     * @return true iff simulation was paused
     */
    public boolean isPaused() {
        return simPaused;
    }

    /**
     * allows to know the state of simulation. Value is calculated at every
     * <code>refreshTempMeasures()</code> to avoid pausing simulation two times.

⌨️ 快捷键说明

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