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

📄 xmlwriter.java

📁 一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!
💻 JAVA
📖 第 1 页 / 共 4 页
字号:

/*-----------------------------------------------------------------------------------
------------------------ Inner classes for more ease of use -------------------------
------------------------------------------------------------------------------------*/
    protected static class XMLParameter{

        public boolean isSubParameter = false;

        public String parameterName;
        public String parameterClasspath;
        public String parameterRefClass;
        public String parameterValue;
        public String parameterArray;
        public XMLParameter[] parameters;

        public XMLParameter(String name,
                            String classpath,
                            String refClass,
                            String value,
                            boolean isSubParameter){
            this(name, classpath, refClass, value, null, isSubParameter);
            parameterArray="false";
        }

        public XMLParameter(String name,
                            String classpath,
                            String refClass,
                            XMLParameter[] parameters,
                            boolean isSubParameter){
            this(name, classpath, refClass, null, parameters, isSubParameter);
            parameterArray = "true";
        }

        private XMLParameter(String name,
                             String classpath,
                             String refClass,
                             String value,
                             XMLParameter[] parameters,
                             boolean isSubParameter){
            parameterName = name;
            parameterClasspath = classpath;
            parameterRefClass = refClass;
            parameterValue = value;
            this.parameters = parameters;
            this.isSubParameter = isSubParameter;
            if(parameters!=null){
                if(parameters.length>1) parameterArray = "false";
                else parameterArray = "true";
            }else{
                parameterArray = "false";
            }
        }

        public void appendParameterElement(Document doc, Element scope){
            //creating inner element containing queue length
            Element parameter = doc.createElement(
                    isSubParameter ? XML_E_SUBPARAMETER : XML_E_PARAMETER
            );
            if(parameterClasspath!=null){
                parameter.setAttribute(XML_A_PARAMETER_CLASSPATH, parameterClasspath);
            }
            if(parameterName!=null){
                parameter.setAttribute(XML_A_PARAMETER_NAME, parameterName);
            }
            if(parameterArray!=null && "true".equals(parameterArray)){
                parameter.setAttribute(XML_A_PARAMETER_ARRAY, parameterArray);
            }
            //adding element refclass for this parameter
            if(parameterRefClass!=null){
                Element refclass = doc.createElement(XML_E_PARAMETER_REFCLASS);
                refclass.appendChild(doc.createTextNode(parameterRefClass));
                scope.appendChild(refclass);
            }
            //adding element value of parameter
            if(parameterValue!=null){
                Element value = doc.createElement(XML_E_PARAMETER_VALUE);
                value.appendChild(doc.createTextNode(parameterValue));
                parameter.appendChild(value);
            }
            if(parameters!=null){
                for(int i=0; i<parameters.length; i++){
                    if(parameters[i]!=null){
                        parameters[i].appendParameterElement(doc, parameter);
                    }
                }
            }
            scope.appendChild(parameter);
        }
    }


    /**This class provides a simple method to obtain XMLparameter representation
     * of a distribution object. Creation of a distribution parameter is a bit awkward, so
     * I'll explain it as best as I can as it follows.
     * generally a distribution is associated to a service time strategy, either it is
     * an interarrival distribution for open classes job generation, or a proper service
     * time distribution for a certain station. As a result, distribution parameter is
     * inserted in a ServiceTimeStrategy parameter which is the one userclass is
     * associated to. Inside this parameter node should be inserted 2 subParameter nodes:
     * <br>-One for distribution description(containing distribution classpath and name)
     * <br>- One containing all of the distribution constructor parameters.
     * <br>The first one has null value, the second contains a list of parameters which, as
     * they are different from each other, they are not considered as array. Then, the
     * node which contains them has no value for array attribute.*/
    protected static class DistributionWriter{

        /*returns a distribution in XMLParameter format, to allow nesting it in
        other parameters. */
        static XMLParameter getDistributionParameter(Distribution distr,
                                                     CommonModel model,
                                                     Object classKey){
            XMLParameter[] distribution = getDistributionParameter(distr);
            XMLParameter returnValue = new XMLParameter(
                    "ServiceTimeStrategy",
                    strategiesClasspathBase + serviceStrategiesSuffix + "ServiceTimeStrategy",
                    model.getClassName(classKey),
                    new XMLParameter[]{distribution[0], distribution[1]}, true
            );
            /*although this parameter contains several others, array attribute must be set
            to "false", as their type are not neccessarily equal*/
            returnValue.parameterArray = "false";
            return returnValue;
        }

        /**
         * Returns a Distribution in XMLParameter format without refclass. This is used to write
         * load dependent service section distributions
         * @param distr distribution to be written
         * @return the two object to rapresent a distribution: distribution and its parameter object
         * Author: Bertoli Marco
         */
        static XMLParameter[] getDistributionParameter(Distribution distr) {
            XMLParameter[] distrXMLPars = new XMLParameter[distr.getNumberOfParameters()];
            Distribution.Parameter distrPar;
            Object valueObj;
            for(int i=0; i<distrXMLPars.length; i++){
                distrPar = distr.getParameter(i);
                valueObj = distrPar.getValue();
                if(valueObj != null){
                    String value = valueObj.toString();
                    distrXMLPars[i] = new XMLParameter(
                            distrPar.getName(), distrPar.getValueClass().getName(),
                            null, value, true
                    );
                }
            }
            XMLParameter[] ret = new XMLParameter[2];
            ret[0] = new XMLParameter(distr.getName(),
                    distr.getClassPath(), (String)null, (String)null, true
            );
            ret[1] = new XMLParameter("distrPar",
                    distr.getParameterClassPath(), null, distrXMLPars, true
            );
            ret[1].parameterArray = "false";
            return ret;
        }
    }


    /**This class creates an xml parameter node given a jmt.gui.common.RoutingStrategy
     * object.*/
    protected static class RoutingStrategyWriter{

        static XMLParameter getRoutingStrategyParameter(RoutingStrategy routingStrat,
                                                        CommonModel model,
                                                        Object classKey,
                                                        Object stationKey){
            //parameter containing array of empirical entries
            XMLParameter probRoutingPar = null;
            if(routingStrat.getValues() != null && routingStrat instanceof ProbabilityRouting){
                Vector outputs = model.getForwardConnections(stationKey);
                Map values = routingStrat.getValues();
                model.normalizeProbabilities(values, outputs);
                XMLParameter[] empiricalEntries = new XMLParameter[outputs.size()];
                for(int i=0; i<empiricalEntries.length; i++){
                    XMLParameter stationDest = new XMLParameter(
                            "stationName", String.class.getName(), null,
                            model.getStationName(outputs.get(i)), true
                    );
                    String prob = values.get(outputs.get(i)).toString();
                    XMLParameter routProb = new XMLParameter(
                            "probability", Double.class.getName(), null,
                            prob, true
                    );
                    empiricalEntries[i] = new XMLParameter(
                            "EmpiricalEntry", EmpiricalEntry.class.getName(), null,
                            new XMLParameter[]{stationDest, routProb}, true
                    );
                    empiricalEntries[i].parameterArray = "false";
                }
                probRoutingPar = new XMLParameter(
                        "EmpiricalEntryArray", EmpiricalEntry.class.getName(), null,
                        empiricalEntries, true
                );
            }
            //creating parameter for empirical strategy: must be null if routing is empirical
            XMLParameter[] innerRoutingPar =probRoutingPar!=null?
                    new XMLParameter[]{probRoutingPar} : null;
            XMLParameter routingStrategy = new XMLParameter(routingStrat.getName(),
                    routingStrat.getClassPath(), model.getClassName(classKey),
                    innerRoutingPar, true
            );
            routingStrategy.parameterArray = "false";
            return routingStrategy;
        }

        /*private static 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]);
            }
        }*/
    }
}

⌨️ 快捷键说明

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