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

📄 loaddependentstrategy.java

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

import jmt.common.exception.IncorrectDistributionParameterException;
import jmt.common.exception.NetException;
import jmt.engine.NetStrategies.ServiceStrategy;
import jmt.engine.QueueNet.NodeSection;
import jmt.engine.math.DirectCircularList;
import jmt.engine.random.Distribution;
import jmt.engine.random.Parameter;
import org.cheffo.jeplite.JEP;
import org.cheffo.jeplite.ParseException;

import java.util.Arrays;


/**
 * <p>Title: Load Dependent Service Time Strategy</p>
 * <p>Description: This is a load dependent implementation of <code>ServiceStrategy</code>,
 * useful to model macro-portion of network into a single station and to model local
 * area networks.</p>
 * 
 * @author Bertoli Marco
 *         Date: 6-ott-2005
 *         Time: 16.37.36
 */
public class LoadDependentStrategy extends ServiceStrategy {
    private static final String VARIABLE = "n";
    private static final int CACHESIZE = 1024;
    // Used to speedup lookup
    private byte queueSection = NodeSection.INPUT;
    private int residentJobs = NodeSection.PROPERTY_ID_RESIDENT_JOBS;

    private LDParameter[] parameters;
    // Used to parse mean function
    private static JEP parser;
    // Used to cache mean values. This structure has O(1) access time.
    DirectCircularList cache;

    /**
     * Creates a new Load Dependent Service Time Strategy
     * @param parameters an array with all parameters in LDParameter format
     */
    public LoadDependentStrategy(LDParameter[] parameters) {
        Arrays.sort(parameters);
        this.parameters = parameters;
        cache = new DirectCircularList(CACHESIZE);
        if (parser == null) {
            parser = new JEP();
            parser.addStandardFunctions();
            parser.addStandardConstants();
        }
    }

    /**
     * Returns service time that current job will wait inside current station
     * @param CallingSection reference to calling service section
     * @return time to wait into this service section
     */
    public double wait(NodeSection CallingSection) throws jmt.common.exception.NetException {
        // Gets number of jobs in the station as the sum of job in queue and job under service
        try {
            // Number of jobs into service section
            int jobs = CallingSection.getIntSectionProperty(residentJobs);
            // Number of jobs into input section
            jobs += CallingSection.getOwnerNode().getSection(queueSection).
                    getIntSectionProperty(residentJobs);

            // Search in cache for corresponding item
            MeanCache item = (MeanCache) cache.get(jobs);
            if (item == null) {
                // Item is not in cache, so retrives right LDParameter and perform parsing of function
                int index = Arrays.binarySearch(parameters, new Integer(jobs));
                if (index < 0) // Calculates index of previous element
                    index = - index - 2;
                LDParameter parameter = parameters[index];
                item = new MeanCache(parameter, jobs);
                cache.set(jobs, item);
            }
            // At this point item is retrived from cache or parsed.
            if (item.meanValid)
                item.parameter.setMean(item.mean); // Note: this is needed as parameter is shared amongs all items of the same LDParameter

            return item.distribution.nextRand(item.parameter);

        } catch (NetException e) {
            throw new NetException("Error in LoadDependentStrategy: Cannot get number of jobs into current station");
        } catch (IncorrectDistributionParameterException ex) {
            throw new NetException(ex.getMessage());
        }
    }

    /**
     * Helper method used to evaluate specified function to return a distribution's mean value
     * @param function to be evaluated
     * @param n current value of queue length to be used to evaluate specified function
     * @return evaluated function
     */
    private double evaluateFunction(String function, int n) throws IncorrectDistributionParameterException {
        parser.addVariable(VARIABLE, n);
        parser.parseExpression(function);
        if (!parser.hasError())
            try {
                return parser.getValue();
            } catch (ParseException e) {
                // Do nothing as we throe later...
            }
        throw new IncorrectDistributionParameterException(
                "Error: invalid function to be parsed for load dependent service section --> "
                + parser.getErrorInfo());
    }

    /**
     * Inner class used to cache mean values and distributions to avoid parsing a function
     * at each call of this strategy
     */
    private class MeanCache {
        /** mean value */
        public double mean;
        /** tells if mean field is valid */
        public boolean meanValid;
        /** distribution used to evaluate service times */
        public Distribution distribution;
        /** parameter of distribution used to evaluate service times */
        public Parameter parameter;

        /**
         * Creates a new MeanCache object by parsing mean value in given LDParameter
         * @param ldp LDParameter of right range
         * @param n current queue length value
         * @throws IncorrectDistributionParameterException if function cannot be parsed correctly
         */
        public MeanCache(LDParameter ldp, int n) throws IncorrectDistributionParameterException {
            distribution = ldp.getDistribution();
            parameter = ldp.getDistrParameter();
            if (ldp.getFunction() == null)
                meanValid = false;
            else {
                mean = evaluateFunction(ldp.getFunction(), n);
                meanValid = true;
            }
        }
    }
}

⌨️ 快捷键说明

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