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

📄 commonmodel.java

📁 一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
        if(!oldType.equals(sd.type)){
            save = true;
            setDefaultsForStation(sd);
            addNewStationDetails(key);
            // If new type is not source or sink add to noSourceSink
            if (!type.equals(STATION_TYPE_SOURCE) && !type.equals(STATION_TYPE_SINK)) {
                if (!noSourceSinkKeyset.contains(key))
                    noSourceSinkKeyset.add(key);
            }
            else
                noSourceSinkKeyset.remove(key);

            // If station is in a blocking region removes it if its type is not valid
            if (sd.blockingRegion != null) {
                if (!canStationTypeBeBlocked(sd.type))
                    this.removeRegionStation(sd.blockingRegion, key);
            }
            else {
                if (canStationTypeBeBlocked(sd.type) && !blockableStations.contains(key))
                    blockableStations.add(key);
                else if (!canStationTypeBeBlocked(sd.type))
                    blockableStations.remove(key);
            }
        }
    }

    /*sets default values for station parameters. To be used when type of station has
    * been changed*/
    private void setDefaultsForStation(StationData sd){
        Integer  queueCapacity;
        queueCapacity = Defaults.getAsInteger("stationCapacity");
        Integer numberOfServers;
        numberOfServers = Defaults.getAsInteger("stationServers");

        sd.numOfServers = numberOfServers;
        sd.queueCapacity = queueCapacity;

        if(sd.type.equals(STATION_TYPE_DELAY)){
            /*Delay stations have infinite number of servers and no queue*/
            numberOfServers = new Integer(-1);
        }else if(sd.type.equals(STATION_TYPE_TERMINAL)||
                sd.type.equals(STATION_TYPE_ROUTER) ||
                sd.type.equals(STATION_TYPE_SOURCE)||
                sd.type.equals(STATION_TYPE_SINK) ||
                sd.type.equals(STATION_TYPE_JOIN)
                ){
            /*If this station has no queue and no service section, there's no need to
            * set any parameter value for these sections.*/
            numberOfServers = null;
        }
        else if (sd.type.equals(STATION_TYPE_FORK)) {
            sd.forkBlock = Defaults.getAsInteger("forkBlock");
            sd.numOfServers = Defaults.getAsInteger("forkJobsPerLink");
        }
    }

    /**Adds a new station to the model. Name and type must be specified.
     * @param name: name of the new station
     * @param type: string representing station type. It's value is contained in
     * <code>JSIMConstants</code> interface.
     * @return : key of search for this class*/
    public synchronized Object addStation(String name, String type) {
        Object key = new Long(++incrementalKey);
        //If this station has already been created, don't add search key to the list.
        if(!stationsKeyset.contains(key)){
            stationsKeyset.add(key);
            if (!type.equals(STATION_TYPE_SOURCE) && !type.equals(STATION_TYPE_SINK))
                noSourceSinkKeyset.add(key);
            // If this station can be blocked, adds it to blockable stations vector
            if(canStationTypeBeBlocked(type))
                blockableStations.add(key);
        }
        StationData newStation = new StationData(name, type, null, null);
        setDefaultsForStation(newStation);
        //add station to the correspondent hashmap
        stationDataHM.put(key, newStation);
        //setup connection defaults
        addNewStationConnections(key);
        //setup station details
        addNewStationDetails(key);
        save = true;
        //return key
        return key;
    }

    /**Deletes station given a search key.*/
    public synchronized void deleteStation(Object key) {
        if(stationsKeyset.contains(key)){
            // Removes this station from owner blocking region (if needed)
            StationData sd = (StationData) stationDataHM.get(key);
            if (sd.blockingRegion != null)
                removeRegionStation(sd.blockingRegion, key);
            stationsKeyset.remove(key);
            stationDataHM.remove(key);
            deleteStationConnections(key);
            deleteStationDetails(key);
            noSourceSinkKeyset.remove(key);
            blockableStations.remove(key);
            save = true;
        }

    }

    /**
     * Tells if a fork is blocking
     * <br>Author: Bertoli Marco
     * @param key search's key for fork
     * @return maximum number of jobs allowed in a fork-join
     * region (-1 is infinity)
     */
    public Integer getForkBlock(Object key) {
        StationData sd;
        if(stationDataHM.containsKey(key)){
            sd = (StationData) stationDataHM.get(key);
        }else return null;
        return sd.forkBlock;
    }

    /**
     * Sets if a fork is blocking
     * <br>Author: Bertoli Marco
     * @param key search's key for fork
     * @param value maximum number of jobs allowed in a fork-join
     * region (-1 is infinity)
     */
    public void setForkBlock(Object key, Integer value) {
        StationData sd;
        if(stationDataHM.containsKey(key)){
            sd = (StationData) stationDataHM.get(key);
        }else return;
        if (!sd.forkBlock.equals(value))
            save = true;
        sd.forkBlock = value;
    }

    /*------------------------------------------------------------------------------
     *---------------- Methods for setup of class-station parameters ---------------
     *------------------------------------------------------------------------------*/
    
    /**
     * Returns drop rule associated with given station queue section if capacity is finite
     * @param stationKey: search key for station.
     * @param classKey: search key for class.
     * @return FINITE_DROP || FINITE_BLOCK || FINITE_WAITING
     */
    public String getDropRule(Object stationKey, Object classKey) {
        StationClassData current =
            (StationClassData)stationDetailsBDM.get(classKey, stationKey);
        if(current!=null){
            return current.dropRule;
        }else return null;    
    }

    /**
     * Sets drop rule associated with given station queue section if capacity is finite
     * @param stationKey: search key for station.
     * @param classKey: search key for class.
     * @param rule FINITE_DROP || FINITE_BLOCK || FINITE_WAITING
     */
    public void setDropRule(Object stationKey, Object classKey, String rule) {
        StationClassData current =
            (StationClassData)stationDetailsBDM.get(classKey, stationKey);
        if(current!=null && rule != null){
            if (!current.dropRule.equals(rule))
                save = true;
            current.dropRule = rule;
        }    
    }

    public void setQueueStrategy(Object stationKey, Object classKey, String queueStrategy) {
        StationClassData current =
                (StationClassData)stationDetailsBDM.get(classKey, stationKey);
        if(current!=null){
            if (!current.queueStrategy.equals(queueStrategy))
                save = true;
            current.queueStrategy = queueStrategy;
        }
    }

    public String getQueueStrategy(Object stationKey, Object classKey) {
        StationClassData current =
                (StationClassData)stationDetailsBDM.get(classKey, stationKey);
        if(current!=null){
            return current.queueStrategy;
        }else return null;
    }

    public void setServiceTimeDistribution(Object stationKey, Object classKey, Object distribution) {
        StationClassData current =
                (StationClassData)stationDetailsBDM.get(classKey, stationKey);
        if(current!=null){
            if (!distribution.equals(current.serviceDistribution))
                save = true;
            current.serviceDistribution = distribution;
        }
    }

    public Object getServiceTimeDistribution(Object stationKey, Object classKey) {
        StationClassData current =
                (StationClassData)stationDetailsBDM.get(classKey, stationKey);
        if(current!=null){
            return current.serviceDistribution;
        }else return null;
    }

    public void setRoutingStrategy(Object stationKey, Object classKey, Object routingStrategy) {
        StationClassData current =
                (StationClassData)stationDetailsBDM.get(classKey, stationKey);
        if(current!=null){
            if (!routingStrategy.equals(current.routingStrategy))
                save = true;
            current.routingStrategy = routingStrategy;
        }
    }

    public Object getRoutingStrategy(Object stationKey, Object classKey) {
        StationClassData current =
                (StationClassData)stationDetailsBDM.get(classKey, stationKey);
        if(current!=null){
            return current.routingStrategy;
        }else return null;
    }

    /**
     * Francesco D'Aquino
     * Normalizes the routing probabilities for each station
     */
    public void manageProbabilities() {
        //get the vector of the stations
        Vector stations = getStationKeys();
        //get the vector of the classes
        Vector classes =  getClassKeys();
        //for each station ...
        for (int i=0;i<stations.size();i++) {
            //get the station at i from the station vector
            Object thisStation = stations.get(i);
            //if it isn't a sink...
            if (!getStationType(thisStation).equals(CommonConstants.STATION_TYPE_SINK)) {
                //for each class...
                for (int j=0;j<classes.size();j++) {
                    Object thisClass = classes.get(j);
                    //check if the routing strategy in thisStation is ProbabilityRouting
                    if (getRoutingStrategy(thisStation,thisClass) instanceof ProbabilityRouting) {
                        //if it is so, normalize routing probabilities
                        Vector outputKeys = getForwardConnections(thisStation);
                        ProbabilityRouting pr = (ProbabilityRouting)getRoutingStrategy(thisStation,thisClass);
                        Map values = pr.getValues();
                        normalizeProbabilities(values,outputKeys);
                    }
                }
            }
        }
    }

    // NB: cut from jmt.gui.common.xml.XMLWriter
    public void normalizeProbabilities(Map values, Vector outputKeys){
            Double[] probabilities = new Double[outputKeys.size()];
            Object[] keys = new Object[outputKeys.size()];
            outputKeys.toArray(keys);
            //extract all values from map in array form
            for(int i=0; i<keys.length; i++){
                probabilities[i] = (Double)values.get(keys[i]);
            }
            values.clear();
            //scan for null values and for total sum
            double totalSum = 0.0;
            int totalNonNull = 0;
            boolean allNull = true;
            for(int i=0; i<probabilities.length; i++){
                if(probabilities[i]!=null){
                    totalSum += probabilities[i].doubleValue();
                    totalNonNull++;
                    allNull = false;
                }
            }
            //modify non null values for their sum to match 1 and put null values to 1
            for(int i=0; i<probabilities.length; i++){
                if(probabilities[i]!=null || allNull){
                    if(totalSum==0){
                        probabilities[i] = new Double(1.0/(double)totalNonNull);
                    }else{
                        probabilities[i] = new Double(probabilities[i].doubleValue()/totalSum);
                    }
                }else{
                    probabilities[i] = new Double(0.0);
                }
                values.put(keys[i], probabilities[i]);
            }
        }

    /**
     * Used to set Defaults Class-Station parameters for a new Station.
     * This method is called when a station type is changed too.
     * @param sd StationData data structure for current station
     * @return created StationClassData object
     * Modified by Bertoli Marco
     */
    private StationClassData getDefaultDetailsForStationType(StationData sd){
        StationClassData defaultDetails = null;
        if(sd == null) return null;
        if(STATION_TYPE_DELAY.equals(sd.type)){
            defaultDetails = new StationClassData(Defaults.get("stationQueueStrategy"),
                    Defaults.getAsNewInstance("stationDelayServiceStrategy"),
                    Defaults.getAsNewInstance("stationRoutingStrategy"));
        }else if(STATION_TYPE_SERVER.equals(sd.type)){
            defaultDetails = new StationClassData(
                    Defaults.get("stationQueueStrategy"),
                    Defaults.getAsNewInstance("stationServiceStrategy"),
                    Defaults.getAsNewInstance("stationRoutingStrategy"));
        }else if(STATION_TYPE_SOURCE.equals(sd.type)){
            defaultDetails = new StationClassData(null, null,
                    Defaults.getAsNewInstance("stationRoutingStrategy"));
        }else if(STATION_TYPE_SINK.equals(sd.type)){
            defaultDetails = new StationClassData(null, null, null);
        }else if(STATION_TYPE_TERMINAL.equals(sd.type)){
            defaultDetails = new StationClassData(null, null,
                    Defaults.getAsNewInstance("stationRoutingStrategy"));
        }else if(STATION_TYPE_ROUTER.equals(sd.type)){
            defaultDetails = new StationClassData(Defaults.get("stationQueueStrategy"),
                    null, Defaults.getAsNewInstance("stationRoutingStrategy"));
        }else if(STATION_TYPE_FORK.equals(sd.type)){
            defaultDetails = new StationClassData(Defaults.get("stationQueueStrategy"),
                    null, null);
        }else if(STATION_TYPE_JOIN.equals(sd.type)){
            defaultDetails = new StationClassData(null,
                    null, Defaults.getAsNewInstance("stationRoutingStrategy"));
        }
        return defaultDetails;
    }

    /**Adds details for a new station. For each type of station proper parameter
     * values are set.*/
    protected void addNewStationDetails(Object stationKey){
        StationData sd = (StationData)stationDataHM.get(stationKey);
        Vector classKeys = getClassKeys();
        for(int i=0; i<classKeys.size(); i++){
            stationDetailsBDM.put(classKeys.get(i), stationKey, getDefaultDetailsForStationType(sd));
        }

⌨️ 快捷键说明

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