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

📄 jmodelmodel.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.gui.jmodel.definitions;

import jmt.gui.common.Defaults;
import jmt.gui.common.definitions.CommonModel;
import jmt.gui.common.routingStrategies.RoutingStrategy;
import jmt.gui.common.serviceStrategies.ServiceStrategy;
import jmt.gui.jmodel.JMODELConstants;

import java.awt.*;
import java.awt.geom.Point2D;
import java.util.HashMap;
import java.util.TreeSet;
import java.util.Vector;

/**
 * <p>Title: JMODEL-Model </p>
 * <p>Description: Main data structure used to store model definition data,
 * to save and load model and to compile xml files for the simulator and
 * as a bridge to connect JMODEL with JSIM and JMVA</p>
 *
 * @author Bertoli Marco
 * Date: 2-giu-2005
 * Time: 10.27.54
 *
 *
 * Modified by Francesco D'Aquino 11/11/2005
 */
//todo MMM JMVA bridge, undo/redo

public class JMODELModel extends CommonModel
        implements JmodelClassDefinition, JmodelStationDefinition, JMODELConstants {

    // ----- Variables -------------------------------------------------------------
    protected HashMap objectNumber = new HashMap(); // Used to generate progressive unique default names
    protected HashMap classColor = new HashMap(); // Used to store classes color
    protected HashMap stationPositions = new HashMap(); // Used to store station positions
    protected Color[] defaultColor = new Color[]
        {Color.blue, Color.red, Color.green, Color.magenta, Color.orange, Color.cyan, Color.yellow
        }; // Defaults color prompted when inserting new class (next ones are generated random)

    //Francesco D'Aquino
    protected boolean animationEnabled = Defaults.getAsBoolean("isWithAnimation").booleanValue();
    // end Francesco D'Aquino

    // ----- Methods----------------------------------------------------------------
    /**
     * Default constructor, creates a new instance of <code>JMODELModel</code>
     */
    public JMODELModel() {
        super();
    }

// ----- Class Definition Methods ---------------------------------------------------------------------
    /**
     * Creates a new Class with default parameters and default generated name
     * @return search key for newly created class
     */
    public Object addClass() {
        Long num;
        if (!objectNumber.containsKey(Defaults.get("className"))) {
            // This if first station of that type
            num = new Long(0L);
        }
        else {
            // Uses next number
            num = (Long) objectNumber.get(Defaults.get("className"));
            num = new Long(num.longValue()+1);
        }
        objectNumber.put(Defaults.get("className"), num);
        return addClass(getUniqueClassName(Defaults.get("className")+num.toString()),
                Defaults.getAsInteger("classType").intValue(),
                Defaults.getAsInteger("classPriority").intValue(),
                Defaults.getAsInteger("classPopulation"),
                Defaults.getAsNewInstance("classDistribution"));
    }

    /**
     * Delete spefified class
     * @param key search key for class to be deleted
     * Overrides default one to add "Color"
     */
    public void deleteClass(Object key) {
        super.deleteClass(key);
        classColor.remove(key);
    }

    /**
     * Adds a class and sets all class parameters at once.
     * @param name name of the class to be added
     * @param type type of class
     * @param priority value of priority field
     * @param population population of this class
     * @param distribution initial distribution type
     * @return search key identifying the new class.
     * Overrides default one to add "Color"
     */
    public Object addClass(String name, int type, int priority, Integer population, Object distribution) {
        Object tmp = super.addClass(getUniqueClassName(name), type, priority, population, distribution);
        this.setClassColor(tmp, getNewColor());
        return tmp;
    }

    /**
     * Sets name of the class linked to the specific key
     * @param name name to be set to selected class
     * @param key search's key for selected class
     */
    public void setClassName(String name, Object key){
        super.setClassName(getUniqueClassName(name), key);
    }

    /**
     * Returns a class parameter, given the class key of search, and the parameter name
     * specified by proper code.
     * @param key of search for class
     * @param parameterName name of parameter to be returned
     * @return selected parameter's value
     * Overrides default one to add "Color"
     */
    public Object getClassParameter(Object key, int parameterName) {
        if (parameterName == CLASS_COLOR)
            return getClassColor(key);
        else if (parameterName == REFERENCE_SOURCE_NAME)
            return getClassRefStation(key);
        else
            return super.getClassParameter(key, parameterName);
    }

    /**
     * Sets a class parameter, given the class key of search, and the parameter name
     * specified by proper code.
     * @param key key of search for class
     * @param parameterName name of parameter to be changed
     * @param value selected parameter should be setted to this value
     * Overrides default one to add "Color"
     */
    public void setClassParameter(Object key, int parameterName, Object value) {
        if (parameterName == CLASS_COLOR)
            setClassColor(key, (Color) value);
        else if (parameterName == REFERENCE_SOURCE_NAME)
            setClassRefStation(key, value);
        else
            super.setClassParameter(key, parameterName, value);
    }

    /**
     * Sets type of the class linked to the specific key. Type of class is represented by
     * an int number whose value is contained in <code>JMODELConstants</code>.
     * */
    public synchronized void setClassType(int type, Object key){
        // If a class type changes, resets its reference station
        int old = getClassType(key);
        super.setClassType(type, key);
        // If type has changed sets ref station to first found source if class is open
        if (old != type) {
            setClassRefStation(key, null);
            if (type == CLASS_TYPE_OPEN) {
                Vector stations = this.getStationKeys();
                for (int i=0; i<stations.size(); i++)
                    if (this.getStationType(stations.get(i)).equals(STATION_TYPE_SOURCE)) {
                        setClassRefStation(key, stations.get(i));
                        break;
                    }
            }
        }
    }

    /**
     * Sets the color of a class
     * @param key search key for class
     * @param color Color that should be associated with given class
     */
    public void setClassColor(Object key, Color color) {
        classColor.put(key, color);
    }

    /**
     * Returns a class color
     * @param key search key for class
     * @return Color associated with given class, or null if class does not exist
     */
    public Color getClassColor(Object key) {
        return (Color) classColor.get(key);
    }

    /**
     * Generates a color for a new class. First enumerates all elements of defaultColor array
     * then generates new colors at random.
     * @return Newly generated color
     */
    public Color getNewColor() {
        Long num;
        if (!objectNumber.containsKey(COLOR_NAME)) {
            // This if first station of that type
            num = new Long(0L);
        }
        else {
            // Uses next number
            num = (Long) objectNumber.get(COLOR_NAME);
            num = new Long(num.longValue()+1);
        }
        objectNumber.put(COLOR_NAME, num);
        if (num.longValue() < defaultColor.length)
            return defaultColor[num.intValue()];
        else
            return new Color((int)Math.floor(Math.random() * 256),
                    (int)Math.floor(Math.random() * 256),
                    (int)Math.floor(Math.random() * 256));
    }

    //Francesco D'Aquino ---------------------------
    /**
     * Return true if queue animation is enabled
     *
     * @return true if the animation is enabled
     */
    public boolean isAnimationEnabled() {
        return this.animationEnabled;
    }

    /**
     * Enable or disable queue animation
     *
     * @param isEnabled - set it to true to enable queue animation
     */
    public void setAnimationEnabled(boolean isEnabled) {
        animationEnabled = isEnabled;
    }
    // end Francesco D'Aquino ----------------------------

    /**
     * Returns a serialized object for a given class, used to support cut/paste,
     * undo/redo operations
     * @param classKey Search's key for station
     * @return Serialized class object
     */
    public Object serializeClass(Object classKey) {
        return new SerializedClass(classKey);
    }

    /**
     * Deserializes given class
     * @param serializedForm class's Serialized form got from <code>serializeClass</code>
     * method.
     * @return search's key for inserted class
     */
    public Object deserializeClass(Object serializedForm) {
        SerializedClass sc = (SerializedClass) serializedForm;
        Object newkey = addClass(sc.name, sc.type,  sc.priority, sc.population, sc.distribution);
        setClassColor(newkey, sc.color);
        setClassRefStation(newkey, sc.referenceStation);
        return newkey;
    }

    /**
     * Object returned when asking for serializedClass
     */
    public class SerializedClass {
        // Attributes of this class
        public Object key;
        public String name;
        public Color color;
        public int type;
        public int priority;
        public Integer population;
        public Object distribution;
        public Object referenceStation;

        /**

⌨️ 快捷键说明

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