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

📄 distribution.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.common.distributions;
import jmt.gui.common.serviceStrategies.ServiceStrategy;

import javax.swing.*;
import java.lang.reflect.Field;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
import java.util.Vector;

/**
 * <p>Title: Distribution</p>
 * <p>Description: This abstract class provides a generic pattern used to specify a
 * distribution.</p>
 * 
 * @author Bertoli Marco
 *         Date: 25-giu-2005
 *         Time: 14.47.12
 */
public abstract class Distribution implements ServiceStrategy {
    protected static Distribution[] all = null; // Used to store all distributions
    protected static Distribution[] allWithMean = null; // Used to store all distributions with Mean value adjustable
    protected String classpath;
    protected String name;
    protected String parameterClasspath;
    protected Parameter[] parameters;
    protected String description;
    protected ImageIcon image;
    protected ValueChecker checker;
    protected boolean hasMean, hasC; // Used to provide input parameters with the tuple (mean, C)
    protected double c, mean;

    /**
     * Constructs a new Distribution object. Initialize all internal objects calling abstract
     * methods setParameterNames(), setParameterClasses(), setParameterValues().
     * @param name Name of the distribution
     * @param classpath engine classpath for this distribution
     * @param parameterClasspath ngine's classpath for this distribution's parameters
     * @param description description of this distribution
     */
    public Distribution (String name, String classpath, String parameterClasspath, String description) {
        this.classpath = classpath;
        this.name = name;
        this.parameterClasspath = parameterClasspath;
        this.description = description;
        this.parameters = setParameters();
        this.image = setImage();
        updateCM();
    }

    /**
     * Gets engine's classpath for this distribution
     * @return classpath
     */
    public String getClassPath() {
        return classpath;
    }

    /**
     * Get engine's classpath for this distribution's parameters (This is needed as each
     * distribution in the engine has a Distribution object and a Parameter object which
     * are distinct classes)
     * @return parameter's classpath
     */
    public String getParameterClassPath() {
        return parameterClasspath;
    }

    /**
     * Gets this distribution's name
     * @return distribution's name
     */
    public String getName() {
        return name;
    }

    /**
     * Get's this distribution description
     * @return distribution's description
     */
    public String getDescription() {
        return description;
    }

    /**
     * Returns number of parameters for this distribution
     * @return number of parameters
     */
    public int getNumberOfParameters() {
        return parameters.length;
    }

    /**
     * Get a parameter data structure of this distribution, given its index
     * @param num index of requested parameter
     * @return requested parameter
     * @throws java.lang.ArrayIndexOutOfBoundsException if num is not inside bounds
     */
    public Parameter getParameter(int num) {
        return parameters[num];
    }

    /**
     * Gets a parameter data structure of this distribution, given its name
     * @param name name of parameter to be retrived
     * @return requested parameter or null if a parameter with specified name does not exist
     */
    public Parameter getParameter(String name) {
        for (int i=0; i<parameters.length; i++)
            if (parameters[i].getName().equals(name))
                return parameters[i];
        return null;
    }

    /**
     * Gets explicative image of this distribution used, together with description, to help the
     * user to understand meaning of parameters.
     * @return explicative image
     */
    public ImageIcon getImage() {
        return image;
    }

    /**
     * Checks if all parameters' values are correct for this distribution type
     * @return true if no check has to be performed or parameters are correct, false otherwise.
     */
    public boolean checkValue() {
        if (checker != null)
            return checker.checkValue(this);
        else
            return true;
    }

    /**
     * Returns precondition that parameters' values must satisfy for this distribution to be valid
     * Every Distribution that provides a checker must provide a valid precondition message too.
     * @return Message describing distribution's preconditions
     */
    public String getPrecondition() {
        return "none";
    }

    /**
     * Returns a deep copy of this Distribution
     * @return a clone of this distribution
     */
    public Object clone() {
        // Gets subtype of this class to instantiate new object through reflection
        Class instance = this.getClass();
        Distribution tmp = null;
        try {
            tmp = (Distribution) instance.newInstance();
            // Newly created instance will have default parameters. Now will clone parameters
            // of this and sets them as parameters of new instance.
            Parameter[] parameters = new Parameter[this.parameters.length];
            for (int i=0; i<this.parameters.length; i++)
                parameters[i] = (Parameter) this.parameters[i].clone();
            tmp.parameters = parameters;
            tmp.mean = mean;
            tmp.c = c;
        } catch (IllegalAccessException e) {
            System.err.println("Error: Cannot clone Distribution object: cannot access to correct class");
            e.printStackTrace();
        } catch (InstantiationException e) {
            System.err.println("Error: Cannot clone Distribution object: instantiation problem during reflection");
            e.printStackTrace();
        }
        return tmp;
    }

    /**
     * Returns if this distribution can be initialized providing variation coefficient C
     * @return true iff this distribution can be initialized using variation coefficient C
     */
    public boolean hasC() {
        return hasC;
    }

    /**
     * Returns if this distribution can be initialized providing its mean value
     * @return true iff this distribution can be initialized using its mean value
     */
    public boolean hasMean() {
        return hasMean;
    }

    /**
     * Returns distribution variation coefficient C only if <code>hasC()</code> is true
     * @return variation coefficient C
     */
    public double getC() {
        return c;
    }

    /**
     * Returns distribution mean only if <code>hasMean()</code> is true
     * @return distribution Mean
     */
    public double getMean() {
        return mean;
    }

    /**
     * Sets a value checker for this entire distribution (checks intra-parameters preconditions)
     * @param checker checker to be set to this distribution
     */
    protected void setValueChecker(ValueChecker checker) {
        this.checker = checker;
    }

    /**
     * Helper method used to formats given number into string according to default rules.
     * @param d bouble to be converted
     * @return string representation of given number
     */
    protected String FormatNumber(double d) {
        DecimalFormat nf = new DecimalFormat();
        // Puts '.' as decimal separator.
        nf.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.ENGLISH));
        String ret;
        // If module of number is greater than 1e3 or lesser than 1e-3 uses exponential notation
        if (Math.abs(d) >= 1e-3 && Math.abs(d) <= 1e3 || d == 0) {
            nf.applyPattern("#.###");
            ret = nf.format(d);
            if (ret.length() > 7)
                ret = ret.substring(0, 6);
        }
        else{
            nf.applyPattern("0.00E00");
            ret = nf.format(d);
        }
        return ret;
    }

    /**
     * Return an array with an istance of every allowed Distribution. Uses internal
     * caching to search for distributions only the first time that this method is called.
     * @return an array with an istance of every allowed Distribution
     */
    public static Distribution[] findAll() {
        if (all != null)
            return all;
        String path = "jmt.gui.common.distributions.";



⌨️ 快捷键说明

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