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

📄 jmodelmodel.java

📁 一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
         * Constructs a new serialized form of specified class
         * @param classkey key of search for specified class
         */
        public SerializedClass(Object classkey) {
            key = classkey;
            name = getClassName(classkey);
            color = getClassColor(classkey);
            type = getClassType(classkey);
            priority = getClassPriority(classkey);
            population = getClassPopulation(classkey);
            distribution = getClassDistribution(classkey);
            referenceStation = getClassRefStation(classkey);
        }
    }

    /**
     * Returns given name if a class with the same name does not exists or makes it unique
     * @param name class name
     * @return unique name
     */
    private String getUniqueClassName(String name) {
        TreeSet names = new TreeSet(); // Map of all unique names with their first users
        Vector keys = getClassKeys();
        // Finds all used names
        for (int i=0; i<keys.size(); i++)
            names.add(this.getClassName(keys.get(i)));

        // If name is new, returns it
        if (!names.contains(name))
            return name;

        int num;
        // If format is already '*_[number]' increment number
        char[] charname = name.toCharArray();
        int n = charname.length - 1;
        while (charname[n] >= '0' && charname[n] <= '9' && n > 0)
            n--;
        if (charname[n] == '_') {
            num = Integer.parseInt(name.substring(n+1));
            name = name.substring(0, n); // Removes suffix
        }
        // Otherwise uses number 1
        else
            num = 1;
        // Finds unique number
        while (names.contains(name + "_" + num))
            num++;
        return name + "_" + num;
    }
// ----------------------------------------------------------------------------------------------------

// ----- Station Definition Methods -------------------------------------------------------------------
    /**
     * Adds a new station to the model. Only type is required as a default name will be set.
     * @param type Constant string from JSIMConstants interface.
     * @return key of search for newly created station
     */
    public Object addStation(String type) {
        Long num;
        if (!objectNumber.containsKey(type)) {
            // This if first station of that type
            num = new Long(0L);
        }
        else {
            // Uses next number
            num = (Long) objectNumber.get(type);
            num = new Long(num.longValue()+1);
        }
        objectNumber.put(type, num);
        return super.addStation(getUniqueStationName(type + num), type);
    }

    /**
     * Sets type of the station, given the search key.
     * @param key: search key for station to be modified
     * @param name: name of station.
     */
    public void setStationName(String name, Object key) {
        // Changes station name only if needed
        if (!name.equals(getStationName(key)))
            super.setStationName(getUniqueStationName(name), key);
    }

    /**
     * Deletes station given a search key
     * @param key search key for station to be deleted
     */
    public synchronized void deleteStation(Object key) {
        super.deleteStation(key);
        stationPositions.remove(key);
    }

    /**
     * Sets position for a target station into jgraph window. This method is used to
     * implement save routine
     * @param key key of search for target station
     * @param position position where target station is placed
     */
    public void setStationPosition(Object key, Point2D position) {
        stationPositions.put(key, position);
    }

    /**
     * Returns stored position for a given station into jgraph window
     * (Note that this is stored position, not original position. This method
     * is used to implement load routine)
     * @param key key of search for target station
     * @return position where target station has to be placed
     */
    public Point2D getStationPosition(Object key) {
        return (Point2D) stationPositions.get(key);
    }

    /**
     * Gets a preview of name that will be assigned automatically to new created object of
     * specified type. This method is used to calculate dimensions of caption to be displayed
     * under component on jgraph window
     * @param type Constant string from JMODELConstants interface.
     * @return name assigned to next station of 'type' type.
     */
    public String previewStationName(String type) {
        if (!objectNumber.containsKey(type))
            return getUniqueStationName(type+"0");
        else
            return getUniqueStationName(type+((Long) objectNumber.get(type)).toString());
    }

    /**
     * Returns a serialized form of a given station to support cut/copy, undo/redo operations
     * @param key Search's key for station
     * @return serialized station or null if station key does not exists
     */
    public Object serializeStation(Object key) {
        return new SerializedStation(key);
    }

    /**
     * Inserts a serialized station into data structure, used to support paste operation.
     * Note that Empirical routing strategy can be inconsistent with station's current connections.
     * (will be resolved in <code>JmtClipboard</code>
     * @param serializedForm station's Serialized form got from <code>serializeStation</code>
     * method.
     * @return search's key for inserted station
     */
    public Object deserializeStation(Object serializedForm) {
        SerializedStation ss = (SerializedStation)((SerializedStation) serializedForm).clone();
        ss.name = getUniqueStationName(ss.name);
        Object key = this.addStation(ss.name, ss.type);
        setStationQueueCapacity(ss.queueCapacity, key);
        setStationNumberOfServers(ss.numOfServers, key);
        // Now sets class-dependant parameters ONLY if stored class exists (so has not been deleted
        // in the meantime)
        Object[] classkeyset = ss.serviceStrategy.keySet().toArray();
        Vector currentclasses = getClassKeys();
        for (int i=0; i<classkeyset.length; i++)
            if (currentclasses.contains(classkeyset[i])) {
                setQueueStrategy(key, classkeyset[i], (String)ss.queueStrategy.get(classkeyset[i]));
                setServiceTimeDistribution(key, classkeyset[i], ss.serviceStrategy.get(classkeyset[i]));
                setRoutingStrategy(key, classkeyset[i], ss.routingStrategy.get(classkeyset[i]));
            }
        return key;
    }

    /**
     * Object returned when asking for serializedStation
     */
    public class SerializedStation {
        // Attributes of this station only
        protected Object key;
        public String name;
        public String type;
        public Integer numOfServers;
        public Integer queueCapacity;
        // Attributes of the tuple (class, this station)
        public HashMap queueStrategy = new HashMap();
        public HashMap serviceStrategy = new HashMap();
        public HashMap routingStrategy = new HashMap();

        public SerializedStation(){

        }
        public SerializedStation(Object key) {
            this.key = key;
            name = getStationName(key);
            type = getStationType(key);
            queueCapacity = getStationQueueCapacity(key);
            numOfServers = getStationNumberOfServers(key);
            // Now sets all attributes of the (station/class) touple
            Vector classes = getClassKeys();
            Object currclass;
            for (int i=0; i< classes.size(); i++) {
                currclass = classes.get(i);
                queueStrategy.put(currclass, getQueueStrategy(key, currclass));
                serviceStrategy.put(currclass,
                        ((ServiceStrategy)getServiceTimeDistribution(key, currclass)).clone());
                routingStrategy.put(currclass,
                        ((RoutingStrategy)getRoutingStrategy(key, currclass)).clone());
            }
        }

        /**
         * Returns a deep copy of this object
         * @return A deep copy of this object
         */
        public Object clone() {
            SerializedStation ss = new SerializedStation();
            ss.key = key;
            ss.name = name;
            ss.type = type;
            ss.numOfServers = numOfServers;
            ss.queueCapacity = queueCapacity;
            Object[] classes = serviceStrategy.keySet().toArray();
            // QueueStrategy can share values as they are strings (immutable). On the other hand,
            // as Distribution and RoutingStrategy are mutable, they have to be cloned explicitly
            ss.queueStrategy = new HashMap(queueStrategy);
            for (int i=0; i<classes.length; i++) {
                ss.serviceStrategy.put(classes[i],
                        ((ServiceStrategy)serviceStrategy.get(classes[i])).clone());
                ss.routingStrategy.put(classes[i],
                        ((RoutingStrategy)routingStrategy.get(classes[i])).clone());
            }
            return ss;
        }
    }

    /**
     * Returns given name if a station with the same name does not exists or makes it unique
     * @param name station name
     * @return unique name
     */
    private String getUniqueStationName(String name) {
        TreeSet names = new TreeSet(); // Map of all unique names with their first users
        Vector keys = getStationKeys();
        // Finds all used names
        for (int i=0; i<keys.size(); i++)
            names.add(this.getStationName(keys.get(i)));

        // If name is new, returns it
        if (!names.contains(name))
            return name;

        int num;
        // If format is already '*_[number]' increment number
        char[] charname = name.toCharArray();
        int n = charname.length - 1;
        while (charname[n] >= '0' && charname[n] <= '9' && n > 0)
            n--;
        if (charname[n] == '_') {
            num = Integer.parseInt(name.substring(n+1));
            name = name.substring(0, n); // Removes suffix
        }
        // Otherwise uses number 1
        else
            num = 1;
        // Finds unique number
        while (names.contains(name + "_" + num))
            num++;
        return name + "_" + num;
    }
// ----------------------------------------------------------------------------------------------------
// ----- Blocking Region Definition Methods -----------------------------------------------------------

    /**
     * Adds a new blocking region with unique name to current model
     * @return search's key for created blocking region
     */
    public Object addBlockingRegion() {
        long num = 0L;
        String baseName = Defaults.get("blockingRegionName");

        // Gets all blocking region names to find an unique name
        TreeSet names = new TreeSet();
        Vector keys = getRegionKeys();
        for (int i=0; i<keys.size(); i++)
            names.add(getRegionName(keys.get(i)));

        String name = baseName + num;
        while (names.contains(name))
            name = baseName + (++num);

        return addBlockingRegion(name,
                Defaults.get("blockingRegionType"));
    }
// ----------------------------------------------------------------------------------------------------
}

⌨️ 快捷键说明

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