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

📄 xmlwriter.java

📁 一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                Object[] rangeKeys = strategy.getAllRanges();

                for (int j=0; j<ranges.length; j++) {
                    // Creates "from" parameter
                    XMLParameter from = new XMLParameter("from", Integer.class.getName(), null,
                            Integer.toString(strategy.getRangeFrom(rangeKeys[j])), true);
                    // Creates "distribution" parameter
                    XMLParameter[] distribution = DistributionWriter.getDistributionParameter(
                            strategy.getRangeDistribution(rangeKeys[j]));
                    // Creates "function" parameter (mean value of the distribution)
                    XMLParameter function = new XMLParameter("function", String.class.getName(), null,
                            strategy.getRangeDistributionMean(rangeKeys[j]), true);
                    ranges[j] = new XMLParameter("LDParameter", strategiesClasspathBase +
                        serviceStrategiesSuffix + "LDParameter", null,
                            new XMLParameter[]{from, distribution[0], distribution[1], function}, true);
                    // Sets array = false as it's not an array of equal elements
                    ranges[j].parameterArray = "false";
                }
                // Creates LDParameter array
                XMLParameter LDParameter = new XMLParameter("LDParameter", strategiesClasspathBase +
                        serviceStrategiesSuffix + "LDParameter", null, ranges, true);
                // Creates Service strategy
                distrParams[i] = new XMLParameter("LoadDependentStrategy", strategiesClasspathBase +
                        serviceStrategiesSuffix + "LoadDependentStrategy",
                        model.getClassName(currentClass), new XMLParameter[] {LDParameter}, true);
                distrParams[i].parameterArray = "false";
            }
        }
        XMLParameter globalDistr = new XMLParameter("ServiceStrategy",
                strategiesClasspathBase + "ServiceStrategy", null, distrParams, false);
        return globalDistr;
    }

    static protected void writeTunnelSection(Document doc, Node nodeNode,
                                             CommonModel model, Object stationKey){
        Element elem = doc.createElement(XML_E_STATION_SECTION);
        elem.setAttribute(XML_A_STATION_SECTION_CLASSNAME, CLASSNAME_TUNNEL);
        nodeNode.appendChild(elem);
    }

    static protected void writeDelaySection(Document doc, Node nodeNode,
                                            CommonModel model, Object stationKey){
        Element elem = doc.createElement(XML_E_STATION_SECTION);
        elem.setAttribute(XML_A_STATION_SECTION_CLASSNAME, CLASSNAME_DELAY);
        nodeNode.appendChild(elem);
        getServiceSection(model, stationKey).appendParameterElement(doc, elem);
    }

    static protected void writeRouterSection(Document doc, Node nodeNode,
                                             CommonModel model, Object stationKey){
        Element elem = doc.createElement(XML_E_STATION_SECTION);
        elem.setAttribute(XML_A_STATION_SECTION_CLASSNAME, CLASSNAME_ROUTER);
        nodeNode.appendChild(elem);
        //creating list of paramters for each single routing strategy
        Vector classes = model.getClassKeys();
        XMLParameter[] routingStrats = new XMLParameter[classes.size()];
        Object currentClass;
        for(int i=0; i<routingStrats.length; i++){
            currentClass = classes.get(i);
            routingStrats[i] = RoutingStrategyWriter.getRoutingStrategyParameter(
                    (RoutingStrategy)model.getRoutingStrategy(stationKey, currentClass),
                    model, currentClass, stationKey
            );
        }
        XMLParameter globalRouting = new XMLParameter("RoutingStrategy",
                strategiesClasspathBase + "RoutingStrategy", null, routingStrats, false
        );
        globalRouting.appendParameterElement(doc, elem);
    }


    //returns a list of keys for customer classes generated by a specific source station
    static protected Vector getClassesForSource(CommonModel model, Object stationKey){
        Vector keys = model.getClassKeys();
        Vector classes = new Vector();
        for(int i=0; i<keys.size(); i++){
            if(CLASS_TYPE_OPEN == model.getClassType(keys.get(i))
                    && model.getClassRefStation(keys.get(i)) == stationKey)
                classes.add(keys.get(i));
        }
        return classes;
    }

    //returns a list of keys for customer classes generated by a specific terminal station
    static protected Vector getClassesForTerminal(CommonModel model, Object stationKey){
        Vector keys = model.getClassKeys();
        Vector classes = new Vector();
        for(int i=0; i<keys.size(); i++){
            if(CLASS_TYPE_CLOSED == model.getClassType(keys.get(i))
                    && model.getClassRefStation(keys.get(i)) == stationKey)
                classes.add(keys.get(i));
        }
        return classes;
    }




/*-----------------------------------------------------------------------------------
*----------------------- Methods for construction of measures -----------------------
*-----------------------------------------------------------------------------------*/
    static protected void writeMeasures(Document doc, Node simNode, CommonModel model){
        Vector v = model.getMeasureKeys();
        for(int i=0; i<v.size(); i++){
            Element elem = doc.createElement(XML_E_MEASURE);
            Object key = v.get(i);
            String station = model.getStationName(model.getMeasureStation(key)),
                    userClass = model.getClassName(model.getMeasureClass(key)),
                    type = model.getMeasureType(v.get(i));
            elem.setAttribute(XML_A_MEASURE_TYPE, type);
            String name = "";

            if (station != null) {
                elem.setAttribute(XML_A_MEASURE_STATION, station);
                name += station + "_";
            }
            else
                elem.setAttribute(XML_A_MEASURE_STATION, "");

            if (userClass != null) {
                elem.setAttribute(XML_A_MEASURE_CLASS, userClass);
                name += userClass + "_";
            }
            else
                elem.setAttribute(XML_A_MEASURE_CLASS, "");

            name += type;

            // Inverts alpha and keeps only 10 decimal cifres
            double alpha = 1 - model.getMeasureAlpha(key).doubleValue();
            alpha = Math.rint(alpha * 1e10) / 1e10;
            elem.setAttribute(XML_A_MEASURE_ALPHA, Double.toString(alpha));
            elem.setAttribute(XML_A_MEASURE_PRECISION, model.getMeasurePrecision(key).toString());
            elem.setAttribute(XML_A_MEASURE_NAME, name);
            elem.setAttribute(XML_A_MEASURE_VERBOSE, "false");
            simNode.appendChild(elem);
        }
    }

/*-----------------------------------------------------------------------------------
*--------------------- Methods for construction of connections ----------------------
*-----------------------------------------------------------------------------------*/
    static protected void writeConnections(Document doc, Node simNode, CommonModel model){
        Vector stations = model.getStationKeys();
        String[] stationNames = new String[stations.size()];
        for(int i=0; i<stationNames.length; i++){
            stationNames[i] = model.getStationName(stations.get(i));
        }
        for(int i=0; i<stationNames.length; i++){
            for(int j=0; j<stationNames.length; j++){
                if(model.areConnected(stations.get(i), stations.get(j))){
                    Element elem = doc.createElement(XML_E_CONNECTION);
                    elem.setAttribute(XML_A_CONNECTION_SOURCE, stationNames[i]);
                    elem.setAttribute(XML_A_CONNECTION_TARGET, stationNames[j]);
                    simNode.appendChild(elem);
                }
            }
        }
    }

/*-----------------------------------------------------------------------------------
--------------------- Methods for construction of preload data --- Bertoli Marco ----
------------------------------------------------------------------------------------*/
    static protected void writePreload(Document doc, Node simNode, CommonModel model){
        // Finds if and where preloading is needed
        Vector stations = model.getStationKeys();
        Vector classes = model.getClassKeys();
        // A map containing all stations that need preloading
        Vector p_stations = new Vector();
        for (int stat=0; stat<stations.size(); stat++) {
            Object key = stations.get(stat);
            Vector p_class = new Vector();
            for (int i=0; i<classes.size(); i++) {
                Object classKey = classes.get(i);
                Integer jobs = model.getPreloadedJobs(key, classKey);
                if (jobs.intValue() > 0) {
                    Element elem = doc.createElement(XML_E_CLASSPOPULATION);
                    elem.setAttribute(XML_A_CLASSPOPULATION_NAME, model.getClassName(classKey));
                    elem.setAttribute(XML_A_CLASSPOPULATION_POPULATION, jobs.toString());
                    p_class.add(elem);
                }
            }
            // If any preload is provided for this station, creates its element and adds it to p_stations
            if (!p_class.isEmpty()) {
                Element elem = doc.createElement(XML_E_STATIONPOPULATIONS);
                elem.setAttribute(XML_A_PRELOADSTATION_NAME, model.getStationName(key));
                while(!p_class.isEmpty())
                    elem.appendChild((Element)p_class.remove(0));
                p_stations.add(elem);
            }
        }
        // If p_stations is not empty, creates a preload section for stations in p_stations
        if (!p_stations.isEmpty()) {
            Element preload = doc.createElement(XML_E_PRELOAD);
            while (!p_stations.isEmpty())
                preload.appendChild((Element)p_stations.remove(0));
            simNode.appendChild(preload);
        }
    }

/*-----------------------------------------------------------------------------------
------------------- Methods for construction of blocking regions --------------------
--------------------------------- Bertoli Marco ------------------------------------*/
    static protected void writeBlockingRegions(Document doc, Node simNode, CommonModel model) {
        Vector regions = model.getRegionKeys();
        for (int reg =0; reg<regions.size(); reg++) {
            Object key = regions.get(reg);
            Element region = doc.createElement(XML_E_REGION);
            // Set name attribute
            region.setAttribute(XML_A_REGION_NAME, model.getRegionName(key));
            // Set type attribute (optional)
            region.setAttribute(XML_A_REGION_TYPE, model.getRegionType(key));
            // Adds nodes (stations) to current region
            Iterator stations = model.getBlockingRegionStations(key).iterator();
            while(stations.hasNext()) {
                Element node = doc.createElement(XML_E_REGIONNODE);
                node.setAttribute(XML_A_REGIONNODE_NAME, model.getStationName(stations.next()));
                region.appendChild(node);
            }
            // Add class constraints
            Iterator classes = model.getClassKeys().iterator();
            while (classes.hasNext()) {
                Object classKey = classes.next();
                Element classConstraint = doc.createElement(XML_E_CLASSCONSTRAINT);
                classConstraint.setAttribute(XML_A_CLASSCONSTRAINT_CLASS, model.getClassName(classKey));
                classConstraint.setAttribute(XML_A_CLASSCONSTRAINT_MAXJOBS, model.getRegionClassCustomerConstraint(key, classKey).toString());
                region.appendChild(classConstraint);
            }
            // Add global constraint
            Element globalConstraint = doc.createElement(XML_E_GLOBALCONSTRAINT);
            globalConstraint.setAttribute(XML_A_GLOBALCONSTRAINT_MAXJOBS, model.getRegionCustomerConstraint(key).toString());
            region.appendChild(globalConstraint);
            // Add drop rules
            classes = model.getClassKeys().iterator();
            while (classes.hasNext()) {
                Object classKey = classes.next();
                Element drop = doc.createElement(XML_E_DROPRULES);
                drop.setAttribute(XML_A_DROPRULES_CLASS, model.getClassName(classKey));
                drop.setAttribute(XML_A_DROPRULES_DROP, model.getRegionClassDropRule(key,classKey).toString());
                region.appendChild(drop);
            }
            simNode.appendChild(region);
        }
    }

⌨️ 快捷键说明

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