📄 xmlsimulationoutput.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.dataAnalysis;
import jmt.common.exception.NetException;
import jmt.engine.NodeSections.BlockingQueue;
import jmt.engine.NodeSections.Queue;
import jmt.engine.QueueNet.*;
import jmt.engine.log.JSimLogger;
import jmt.engine.simEngine.Simulation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
/**
* Saves all the measure outputs in a xml file.
*
* @author Stefano, Bertoli Marco
* @version 13-dic-2004 17.34.35
* Modified by Bertoli Marco 01-jan-2006 --> BugFixed for linux
* 8-mar-2006 Added support for global measures
*/
public class XMLSimulationOutput extends SimulationOutput {
private static final String SEPARATOR = System.getProperty("file.separator");
final static boolean DEBUG = false;
private JSimLogger logger = JSimLogger.getLogger();
//the root element of results xml
Element root;
//the Document object corresponding to the results xml
Document doc;
//the File containing the simulation results
File resultsFile;
//the File containing the original model definition
File mvaModelDefinition;
//the File containing the original model definition
File simModelDefinition;
public XMLSimulationOutput(Simulation simulation) {
super(simulation);
try {
/////////////////////////////
//Creating an empty XML Document
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
doc = docBuilder.newDocument();
////////////////////////
//Creating the XML tree
//create the root element and add it to the document
root = doc.createElement("solutions");
doc.appendChild(root);
//sets the attribute of the root
if (sim.getName() != null)
root.setAttribute("modelName", sim.getName());
else
root.setAttribute("modelName", "Unknown Model Name");
root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
//sets the xsd schema of the results xml file
root.setAttribute("xsi:noNamespaceSchemaLocation", "SIMmodeloutput.xsd");
//these results have been obtained through a simulation
root.setAttribute("solutionMethod", "simulation");
//in the results file, we need the name of the xml file containing
//the model definition
String name = sim.getXmlModelDefPath();
if (name != null) {
mvaModelDefinition = new File(name);
}
//only file name is passed, not the absolute path
//in fact definition and results will be put in the same directory
//TODO: forse va messo assoluto...
//root.setAttribute("modelDefinitionPath", simModelDefinition.getName());
if (name != null)
root.setAttribute("modelDefinitionPath", name);
else
root.setAttribute("modelDefinitionPath", ".");
} catch (Exception e) {
System.out.println(e);
}
}
/**
* Writes the output of the specified measure.
*/
public void writeMeasure(Measure measure) {
Element elem = doc.createElement("measure");
root.appendChild(elem);
DynamicDataAnalyzer analyzer = measure.getAnalyzer();
// Checks null values to avoid problems under linux
if (measure.getNodeName() == null || measure.getNodeName().equals(""))
// aggregate measure
elem.setAttribute("station", "");
else
// class measure
elem.setAttribute("station", measure.getNodeName());
// Checks null values to avoid problems under linux
if (measure.getJobClassName() == null || measure.getJobClassName().equals("")) {
//aggregate measure
elem.setAttribute("class", "");
} else {
//class measure
elem.setAttribute("class", measure.getJobClassName());
}
//finds and sets measure type
int type = measure.getMeasureType();
String typeName = "";
switch (type) {
case SimConstants.QUEUE_LENGTH:
typeName = "Queue Length";
break;
case SimConstants.QUEUE_TIME:
typeName = "Queue Time";
break;
case SimConstants.UTILIZATION:
typeName = "Utilization";
break;
case SimConstants.THROUGHPUT:
typeName = "Throughput";
break;
case SimConstants.RESIDENCE_TIME:
typeName = "Residence Time";
break;
case SimConstants.RESPONSE_TIME:
typeName = "Response Time";
break;
case SimConstants.SYSTEM_RESPONSE_TIME:
typeName = "System Response Time";
break;
case SimConstants.SYSTEM_THROUGHPUT:
typeName = "System Throughput";
break;
case SimConstants.SYSTEM_JOB_NUMBER:
typeName = "Customer Number";
break;
}
elem.setAttribute("measureType", typeName);
//analyzer confidence requirements
elem.setAttribute("maxSamples", Integer.toString(analyzer.getMaxData()));
elem.setAttribute("precision", Double.toString(analyzer.getPrecision()));
elem.setAttribute("alfa", Double.toString(analyzer.getAlfa()));
//analyzer has been succesful?
boolean success = analyzer.getSuccess();
elem.setAttribute("successful", Boolean.toString(success));
//number of analyzed and discarded samples
elem.setAttribute("analyzedSamples", Integer.toString(measure.getAnalyzedSamples()));
elem.setAttribute("discardedSamples", Integer.toString(measure.getDiscardedSamples()));
//this is the extimated mean, but it may be wrong
elem.setAttribute("meanValue", Double.toString(measure.getExtimatedMeanValue()));
elem.setAttribute("upperLimit", Double.toString(measure.getUpperLimit()));
elem.setAttribute("lowerLimit", Double.toString(measure.getLowerLimit()));
}
/**
* Writes the output of the drop measures, when blocking regions have been defined.
*/
public void writeMeasures_regionDroppedJobs() {
BlockingRegion[] regions = sim.getAllRegions();
JobClass[] classes = sim.getClasses();
int classNumber = classes.length;
if (regions != null) {
//at least one region has been defined
for (int r = 0; r < regions.length; r++) {
try {
//retrieves the blocking queue of the input station
NodeSection ns = regions[r].getInputStation().getSection(NodeSection.INPUT);
if (ns instanceof BlockingQueue) {
//For each class count dropped jobs
for (int c = 0; c < classNumber; c++) {
Element el = doc.createElement("measure");
root.appendChild(el);
String inputStation = regions[r].getInputStation().getName();
String className = classes[c].getName();
el.setAttribute("station", inputStation);
el.setAttribute("class", className);
int arrivedJobs = ns.getIntSectionProperty(NodeSection.PROPERTY_ID_ARRIVED_JOBS);
int droppedJobs = ((BlockingQueue) ns).getDroppedJobPerClass(c);
double drop_percentage = (double) droppedJobs / arrivedJobs;
//System.out.println(drop_percentage);
el.setAttribute("meanValue", 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(arrivedJobs));
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 region " + regions[r].getName() + "-" + className + ": " + drop_percentage);
}
}
} catch (NetException ne) {
ne.printStackTrace();
return;
}
}
}
}
/**
* Writes the output of the drop measures, when blocking regions have been defined.
*/
public void writeMeasures_regionUtilization() {
BlockingRegion[] regions = sim.getAllRegions();
JobClass[] classes = sim.getClasses();
int classNumber = classes.length;
if (regions != null) {
//at least one region has been defined
for (int r = 0; r < regions.length; r++) {
BlockingRegion reg = regions[r];
if (!reg.hasGlobalConstraint()){
continue;
}
//max capacity
double maxCapacity = reg.getMaxCapacity();
String[] stations = reg.getRegionNodeNames();
double classTotalQueue, regUtilization;
double classWeight;
String className;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -