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

📄 commonmodel.java

📁 一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
        ClassData cd;
        if(classDataHM.containsKey(key)){
            cd = (ClassData)classDataHM.get(key);
        }else return null;
        switch(parameterName){
            case CLASS_NAME:{
                return cd.name;
            }case CLASS_TYPE:{
                return new Integer(cd.type);
            }case CLASS_PRIORITY:{
                return new Integer(cd.priority);
            }case CLASS_POPULATION:{
                return cd.population;
            }case CLASS_DISTRIBUTION:{
                return cd.distribution;
            }default:{
                return null;
            }
        }
    }

    /**Sets a class parameter, given the class key of search, and the parameter name
     * specified by proper code. User is responsible for passing correct type for
     * parameters, e.g. name must be a String, type, priority and population must be an
     * integer. Distribution must be an object. If type for a field is not correct, a
     * <code> ClassCastException</code> will be raised.*/
    public synchronized void setClassParameter(Object key, int parameterName, Object value){
        ClassData cd;
        if(classDataHM.containsKey(key)){
            cd = (ClassData)classDataHM.get(key);
        }else return;
        switch(parameterName){
            case CLASS_NAME:{
                cd.name = (String)value;
                break;
            }case CLASS_TYPE:{
                cd.type = ((Integer)value).intValue();
                break;
            }case CLASS_PRIORITY:{
                cd.priority = ((Integer)value).intValue();
                break;
            }case CLASS_POPULATION:{
                cd.population = (Integer) value;
                break;
            }case CLASS_DISTRIBUTION:{
                cd.distribution = value;
                break;
            }
        }
        save = true;
    }

    /**Sets all class parameters at once given the class' search key. If search key does
     * not exist, no class will be added.*/
    public synchronized void setClassParameters(Object key,
                                                String name,
                                                int type,
                                                int priority,
                                                Integer population,
                                                Object distribution){
        if(!classesKeyset.contains(key)){
            return;
        }
        if(type == CLASS_TYPE_CLOSED){
            distribution = null;
        }else if(type == CLASS_TYPE_OPEN){
            population = null;
        }
        classDataHM.put(key, new ClassData(name, type, priority, population, distribution));
        save = true;
    }


    /**Adds a class and sets all class parameters at once, return the new search key
     * identifying the new class.*/
    public synchronized Object addClass(String name,
                                        int type,
                                        int priority,
                                        Integer population,
                                        Object distribution){
        //generating a new key
        incrementalKey++;
        Object key = new Long(incrementalKey);
        classesKeyset.add(key);

        if(type == CLASS_TYPE_CLOSED){
            distribution = null;
        }else if(type == CLASS_TYPE_OPEN){
            population = null;
        }
        //add class to model
        classDataHM.put(key, new ClassData(name, type, priority, population, distribution));
        //add per-class station details
        addStationDetailsForClass(key);
        //add per-class blocking region details
        addBlockingDetailsForClass(key);
        save = true;
        return key;
    }

    /**Deletes class given its search key. */
    public synchronized void deleteClass(Object key){
        if(classesKeyset.contains(key)){
            classesKeyset.remove(key);
            classDataHM.remove(key);
            deleteStationDetailsForClass(key);
            deleteBlockingDetailsForClass(key);
            save = true;
        }
    }

    /**
     * Sets the reference station for a given class
     * @param classKey search key for class
     * @param stationKey search key for referenced station
     */
    public void setClassRefStation(Object classKey, Object stationKey){
        ClassData cd = (ClassData)classDataHM.get(classKey);
        if(cd == null) return;
        Object oldRef = cd.refSource;
        cd.refSource = stationsKeyset.contains(stationKey) ? stationKey : null;
        if (oldRef != stationKey)
            save = true;
    }

    /**
     * Returns the reference station for a given class
     * @param classKey search key for class
     * @return search key for referenced station or null if it was not specified yet
     */
    public Object getClassRefStation(Object classKey){
        ClassData cd = (ClassData)classDataHM.get(classKey);
        if(cd == null) return null;
        //if previously saved refSource has been deleted, delete it to mantain coherence
        if(!stationsKeyset.contains(cd.refSource)){
            cd.refSource=null;
        }
        return cd.refSource;
    }



    /*------------------------------------------------------------------------------
    *----------------------  methods for station definition  -----------------------
    *-------------------------------------------------------------------------------*/

    /**
     * This method returns the key set of sources
     *
     * Author: Francesco D'Aquino
     */
    public Vector getStationKeysSource() {
        Vector stations = this.getStationKeys();
        Vector sources = new Vector(0,1);
        for (int i=0;i<stations.size();i++) {
            Object thisStation = stations.get(i);
            if (this.getStationType(thisStation).equals(CommonConstants.STATION_TYPE_SOURCE))
            sources.add(thisStation);
        }
        return sources;
    }

    /**
     * This method returns the key set of sources
     *
     * @return an array containing the entire set of server keys
     *
     * Author: Francesco D'Aquino
     */
    public Vector getStationKeysServer() {
        Vector stations = this.getStationKeys();
        Vector servers = new Vector(0,1);
        for (int i=0;i<stations.size();i++) {
            Object thisStation = stations.get(i);
            if (this.getStationType(thisStation).equals(CommonConstants.STATION_TYPE_SERVER))
            servers.add(thisStation);
        }
        return servers;
    }

    /**
     * This method returns the key set of delays
     *
     * @return an array containing the entire set of delay keys
     *
     * Author: Francesco D'Aquino
     */
    public Vector getStationKeysDelay() {
        Vector stations = this.getStationKeys();
        Vector delays = new Vector(0,1);
        for (int i=0;i<stations.size();i++) {
            Object thisStation = stations.get(i);
            if (this.getStationType(thisStation).equals(CommonConstants.STATION_TYPE_DELAY))
            delays.add(thisStation);
        }
        return delays;
    }

    /**Returns entire key set for search of stations. Objects contained  in returned vector
     * can be directly passed to methods for parameters retrieval.*/
    public Vector getStationKeys() {
        return stationsKeyset;
    }

    /**
     * This method returns all station keys except source and sink ones.
     */
    public Vector getStationKeysNoSourceSink() {
        return noSourceSinkKeyset;
    }

    /** Returns name of the station in <code>String</code> representation, given the
     * search key.
     * @param key: search key for station to be modified
     * @return : name of station.*/
    public synchronized String getStationName(Object key) {
        StationData sd;
        if(stationDataHM.containsKey(key)){
            sd = (StationData) stationDataHM.get(key);
        }else return null;
        return sd.name;
    }

    /** Sets type of the station, given the search key.
     * @param key: search key for station to be modified
     * @param name: name of station.*/
    public synchronized void setStationName(String name, Object key) {
        StationData sd;
        if(stationDataHM.containsKey(key)){
            sd = (StationData) stationDataHM.get(key);
        }else return;
        if (sd.name != name)
            save = true;
        sd.name = name;
    }

    /** Returns queue capacity of the station given the search key.*/
    public synchronized Integer getStationQueueCapacity(Object key) {
        StationData sd;
        if(stationDataHM.containsKey(key)){
            sd = (StationData) stationDataHM.get(key);
        }else return null;
        return sd.queueCapacity;
    }

    /** Sets queue capacity of the station, given the search key.*/
    public synchronized void setStationQueueCapacity(Integer queueCapacity, Object key) {
        StationData sd;
        if(stationDataHM.containsKey(key)){
            sd = (StationData) stationDataHM.get(key);
        }else return;
        if (!sd.queueCapacity.equals(queueCapacity))
            save = true;
        sd.queueCapacity = queueCapacity;
    }

    /** Returns number of servers or number of forked job for the station given the search key.*/
    public synchronized Integer getStationNumberOfServers(Object key) {
        StationData sd;
        if(stationDataHM.containsKey(key)){
            sd = (StationData) stationDataHM.get(key);
        }else return null;
        return sd.numOfServers;
    }

    /** Sets number of servers or number of forked job for the station, given the search key.*/
    public synchronized void setStationNumberOfServers(Integer numberOfServers, Object key) {
        StationData sd;
        if(stationDataHM.containsKey(key)){
            sd = (StationData) stationDataHM.get(key);
        }else return;
        if (!sd.numOfServers.equals(numberOfServers))
            save = true;
        sd.numOfServers = numberOfServers;
    }

    /** Returns parameter for the station given the search key and parameter code.*/
    public synchronized Object getStationParameter(Object key, int parameterCode) {
        StationData sd;
        if(stationDataHM.containsKey(key)){
            sd = (StationData) stationDataHM.get(key);
        }else return null;
        switch(parameterCode){
            case STATION_NAME:{
                return sd.name;
            }case STATION_TYPE:{
                return sd.type;
            }case STATION_QUEUE_CAPACITY:{
                return sd.queueCapacity;
            }case STATION_NUMBER_OF_SERVERS:{
                return sd.numOfServers;
            }default:{return null;}
        }
    }

    /** Sets parameter for the station, given the search key and parameter code.*/
    public synchronized void setStationParameter(Object value, Object key, int parameterCode) {
        switch(parameterCode){
            case STATION_NAME:{
                setStationName((String)value, key);
                break;
            }case STATION_TYPE:{
                setStationType((String)value, key);
                break;
            }case STATION_QUEUE_CAPACITY:{
                setStationQueueCapacity((Integer)value, key);
                break;
            }case STATION_NUMBER_OF_SERVERS:{
                setStationNumberOfServers((Integer)value, key);
                break;
            }
        }
        save = true;
    }

    /** Returns type of the station in <code>String</code> representation, given the
     * search key.
     * @param key: search key for station to be modified
     * @return : type of station.*/
    public synchronized String getStationType(Object key) {
        StationData sd;
        if(stationDataHM.containsKey(key)){
            sd = (StationData) stationDataHM.get(key);
        }else return null;
        return sd.type;
    }

    /** Sets name of the station, given the search key.
     * @param key: search key for station to be modified
     * @param type: type of station.*/
    public synchronized void setStationType(String type, Object key) {
        StationData sd;
        if(stationDataHM.containsKey(key)){
            sd = (StationData) stationDataHM.get(key);
        }else return;
        String oldType = sd.type;
        sd.type = type;
        //If type has changed, all of the parameters must be reset to defaults

⌨️ 快捷键说明

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