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

📄 xmlsimulationoutput.java

📁 一个用于排队系统仿真的开源软件,有非常形象的图象仿真过程!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        		
        		for (int c = 0; c < classNumber; c++) {
        			
        			className = classes[c].getName();
        			//logger.debug("Computing region U for class " + className);
        			
        			classWeight = reg.getClassWeights()[c];                		
        			classTotalQueue=0.0;
        			boolean success = true;
        			
        			if (classWeight==0.0){
        				regUtilization = 0.0;
        			} else {                			
        				for (int s=0; s<stations.length; s++){
        					double queue = findQueueMeasure(className, stations[s]);
        					if (queue == -1){
        						success = false;
        						break;
        					}
        					//logger.debug("Queue at station " + stations[s] + ": " + Double.toString(queue));
        					classTotalQueue += queue;                			
        				}
        				
        				if (success){
        					//logger.debug("Total queue: " + Double.toString(classTotalQueue));
            				//logger.debug("Weight: " + Double.toString(classWeight));
            				regUtilization = (classTotalQueue*classWeight) / maxCapacity ;
            				logger.debug("Region Utilization for " + reg.getName() + "-" + className + ": " + Double.toString(regUtilization));	
        				} else {
        					//regUtilization = -1;
        					regUtilization = Double.NEGATIVE_INFINITY;
        					logger.debug("Region Utilization for " + reg.getName() + "-" + className + " not computed");
        				}
        			}
        			
        			Element el = doc.createElement("measure");
        			root.appendChild(el);

        			String inputStation = reg.getInputStation().getName();
        			el.setAttribute("station", inputStation);
        			el.setAttribute("class", className);
        			el.setAttribute("meanValue", Double.toString(regUtilization));
        			el.setAttribute("successful", Boolean.toString(success));
        			el.setAttribute("measureType", "Region Utilization");
        		}                    
        		
        	}

        }

    }
    /**
     * Writes the output of the drop measures, when blocking regions have been defined.
     */
    public void writeMeasures_queueDroppedJobs() {

        NodeList nodeList = sim.getNetwork().getNodes();
        NetNode[] nodes = new NetNode[nodeList.size()];

        for (int n = 0; n < nodeList.size(); n++) {
            nodes[n] = nodeList.get(n);
        }

        JobClass[] classes = sim.getClasses();
        int classNumber = classes.length;

        if (nodes != null) {
            //at least one region has been defined

            for (int n = 0; n < nodes.length; n++) {
                try {
                    //retrieves the blocking queue of the input station
                    NodeSection ns = nodes[n].getSection(NodeSection.INPUT);

                    if (ns instanceof Queue) {

                        if (((Queue) ns).hasInfiniteQueue()) {
                            //infinite queue, no dropped jobs

                            //skip this station
                            continue;
                        }

                        //For each class count dropped jobs
                        for (int c = 0; c < classNumber; c++) {

                            Element el = doc.createElement("measure");
                            root.appendChild(el);

                            String node = nodes[n].getName();
                            String className = classes[c].getName();

                            el.setAttribute("station", node);
                            el.setAttribute("class", className);

                            //after list auto-refresh has been disabled in class Queue, drop jobs are
                            //no longer included in NodeSection arrived jobs (while they are still included
                            //in NetNode arrived jobs)

                            //arrived jobs = jobs arrived and put into queue
                            //dropped jobs = jobs arrived when the queue was full
                            //
                            // then
                            //
                            // total jobs = arrived jobs + dropped jobs

                            int notDroppedJobs = ns.getIntSectionProperty(NodeSection.PROPERTY_ID_ARRIVED_JOBS);
                            int droppedJobs = ((Queue) ns).getDroppedJobPerClass(c);

                            double drop_percentage = (double) droppedJobs / (notDroppedJobs + droppedJobs);

                            el.setAttribute("meanValue", Double.toString(drop_percentage));

                            //System.out.println("Loss probability: " + Double.toString(drop_percentage));
                            //System.out.println(Double.toString(drop_percentage));


                            //always true: it is not a confidence interval but an exact value
                            boolean success = true;
                            el.setAttribute("successful", Boolean.toString(success));
                            el.setAttribute("measureType", "Dropped jobs");

                            //number of analyzed and discarded samples: in this case their meaning is
                            //received jobs and dropped jobs
                            el.setAttribute("analyzedSamples", Integer.toString(droppedJobs + notDroppedJobs));
                            el.setAttribute("discardedSamples", Integer.toString(droppedJobs));


                            /*
                            //analyzer confidence requirements
                            el.setAttribute("maxSamples", Integer.toString(0));
                            el.setAttribute("precision", Double.toString(0));
                            el.setAttribute("alfa", Double.toString(0));
                            */
                            logger.debug("Dropped jobs percentage for " + node + "-" + className + ": " + drop_percentage);

                        }
                    }

                } catch (NetException ne) {
                    ne.printStackTrace();
                    return;
                }
            }

        }

    }







    public File writeAllMeasures() {
        for (int i = 0; i < measureList.length; i++) {
            //writes all the measures in a Document
            writeMeasure(measureList[i]);
        }

        writeMeasures_regionDroppedJobs();
        writeMeasures_regionUtilization();

        writeMeasures_queueDroppedJobs();


        /////////////////
        //Output the XML
        try {
            //set up a transformer
            TransformerFactory transfac = TransformerFactory.newInstance();
            Transformer trans = transfac.newTransformer();
            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
            trans.setOutputProperty(OutputKeys.INDENT, "yes");

            //we want to save the xml results in the same directory of the model definition file
            String parent = null;
            String xmlResultsName = null;

            if (mvaModelDefinition != null) {
                //mvaModelDefinition defined
                if (mvaModelDefinition.isAbsolute()) {
                    //the passed filename is absolute
                    parent = mvaModelDefinition.getParent();
                    if (parent.endsWith(SEPARATOR)) {
                        xmlResultsName = parent + "res_sim_" + mvaModelDefinition.getName();
                    } else {
                        xmlResultsName = parent + SEPARATOR + "res_sim_" + mvaModelDefinition.getName();
                    }


                } else {
                    //the passed filename is not absolute
                    xmlResultsName = SEPARATOR + "res_sim_" + mvaModelDefinition.getName();
                }
            } else {
                //mvaModelDefinition not defined

                //use sim model path
                String simModelPath = sim.getXmlSimModelDefPath();
                File simModelFile;

                if (simModelPath != null) {
                    //sim model file exist
                    simModelFile = new File(simModelPath);
                    xmlResultsName = simModelFile.getParent() + SEPARATOR + "res_sim_" + sim.getName() + ".xml";

                } else {
                    //get the user dir
                    String curDir = System.getProperty("user.dir");
                    String name;
                    if (sim.getName() == null) {
                        name = "";
                    } else {
                        name = "_" + sim.getName();
                    }
                    xmlResultsName = curDir + SEPARATOR + "res_sim_JMT" + name + ".xml";
                }


            }

            //creates the results file
            resultsFile = new File(xmlResultsName);
            if (DEBUG) {
                System.out.println(resultsFile.getAbsolutePath());
            }

            DOMSource source = new DOMSource(doc);

            // Prepare the output file
            File temp = File.createTempFile("~jmt_sim_output", ".xml", resultsFile.getParentFile());
            StreamResult result = new StreamResult(temp);

            // Write the DOM document to the file
            trans.transform(source, result);

            // commit
            if (resultsFile.exists()) {
                resultsFile.delete();
            }

            temp.renameTo(resultsFile);

            // Check because somethime rename fails...
            if (resultsFile.exists())
                return resultsFile;
            else
                return temp;

        } catch (javax.xml.transform.TransformerConfigurationException exc) {
            exc.printStackTrace();
        } catch (javax.xml.transform.TransformerException exc) {
            exc.printStackTrace();
        } catch (java.io.IOException exc) {
            exc.printStackTrace();
        } catch (NullPointerException exc) {
            exc.printStackTrace();
        }
        return null;
    }

    private double findQueueMeasure(String className, String stationName){
    	
    	try {
    	
    	org.w3c.dom.NodeList measureList = root.getElementsByTagName("measure");
    	int measureNumber = measureList.getLength();
    	Element currentMeasure;
    	
    	for (int m=0; m<measureNumber; m++){
    		currentMeasure = (Element) measureList.item(m);
    		
    		if (!currentMeasure.getAttribute("measureType").equalsIgnoreCase("Queue Length")){
    			continue;
    		}
    		
    		if (!currentMeasure.getAttribute("station").equalsIgnoreCase(stationName)){
    			continue;
    		}
    		
    		if (!currentMeasure.getAttribute("class").equalsIgnoreCase(className)){
    			continue;
    		}
    		
    		String success = currentMeasure.getAttribute("successful");
    		
    		if (success.equalsIgnoreCase("true")){
    			String measureValue = currentMeasure.getAttribute("meanValue");
        		logger.debug("Q measure found for " + stationName + "-" + className + ": " + measureValue);
    			return Double.parseDouble(measureValue);
    		} else {
        		logger.debug("Q measure found for " + stationName + "-" + className + ": NOT SUCCESSFUL");
    			return -1;
    		}
    		
    		
    	}
    	
    	//else measure not found
    	logger.debug("Q measure not found for " + stationName + "-" + className);
    	return -1;
    	
    	} catch(Exception e){
    		e.printStackTrace();
			logger.debug("Q measure not found for " + stationName + "-" + className);
    		return -1;
    	}
    	
    }





}

⌨️ 快捷键说明

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