📄 resultscheck.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.testSystem;
import jmt.analytical.CommandLineSolver;
import jmt.common.exception.LoadException;
import jmt.common.xml.resources.XSDSchemaLoader;
import jmt.engine.simDispatcher.Dispatcher_jMVAschema;
import org.apache.log4j.Logger;
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
/**
* Solves the same model with mva and simulator, then checks
* whether the results are correct.
* The result of test are written on the log file of BatchTest class
*
* @author Stefano
* @version 4-mar-2005 11.02.23
*/
public class ResultsCheck {
public static final boolean DEBUG = false;
private String model_path;
private String jmva_model_path;
private String jsim_model_path;
private File model_file;
private File jmva_model_file;
private File jsim_model_file;
//retrieves the reference to the BatchTest logger
private Logger logger = Logger.getLogger(BatchTest.class);
//if true, more info are written to log (for example, precision, maxsamples, ecc..)
//if false, only basic info are written
private boolean extendedInfo;
//if true jsim will always use a automatic seed for simulation
private boolean automaticSeed = true;
//user defined seed for simulation
private long userDefinedSeed;
//number of simulation runs for this test
private int simulationRunsCounter = 1;
//if true jsim will delete the intermediate files used for resolution
private boolean deleteIntermediateSimFiles = false;
/**
* Creates a ResultCheck object.
* @param filePath the model to be tested
* @param extendedInfo if true, more test simulation info will be logged
*/
public ResultsCheck(String filePath, boolean extendedInfo) {
model_path = filePath;
model_file = new File(filePath);
this.extendedInfo = extendedInfo;
automaticSeed = true;
}
/**
* Creates a ResultCheck object.
* @param filePath the model to be tested
* @param extendedInfo if true, more test simulation info will be logged
* @param simSeed the simulationon seed to be used (use this feature only for test)
*/
public ResultsCheck(String filePath, boolean extendedInfo, long simSeed) {
model_path = filePath;
model_file = new File(filePath);
this.extendedInfo = extendedInfo;
automaticSeed = false;
userDefinedSeed = simSeed;
}
public void checkRes(int runs) {
simulationRunsCounter = 0;
for (int i = 1; i <= runs; i++) {
simulationRunsCounter++;
if (!checkRes()) {
logger.error("Simulation run number " + i + "failed for model file" + model_path);
}
}
}
/**
* Solves the model with jMVA and jSIM, then checks if all simulation
* confidence intervals contain the exact solution.
*
*/
public boolean checkRes() {
//verifies if the passed file exists
if (!model_file.exists()) {
//the passed file does not exist
logger.error("The model file " + model_path + "does not exist.");
return false;
}
//creates two copy of the same file, which will be passed to the risolution engines
jmva_model_path = model_file.getParent() + "\\jmva_" + model_file.getName();
jsim_model_path = model_file.getParent() + "\\jsim_" + model_file.getName();
//creates empty files
jmva_model_file = new File(jmva_model_path);
jsim_model_file = new File(jsim_model_path);
if (!copyFile(model_file, jmva_model_file, jsim_model_file)) {
//error during copy
logger.error("Cannot create copies of the model file " +
model_path);
};
if (!solve_jmva(jmva_model_file)) {
logger.error("The model " + jmva_model_path + " could not be solved using jMVA.");
return false;
} else {
logger.debug(jmva_model_path + ": jMVA solution has been completed.");
}
if (!solve_jsim(jsim_model_file)) {
logger.error("The model " + jsim_model_path + " could not be solved using jSIM.");
return false;
} else {
logger.debug(jsim_model_path + ": jSIM solution has been completed.");
}
//both solutions are available
//retrieves a DOM representation from xml files
Document jmva_document, jsim_document;
try {
jmva_document = load(new FileInputStream(jmva_model_file));
jsim_document = load(new FileInputStream(jsim_model_file));
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
createTestReport(jmva_document, jsim_document);
return true;
}
/**
* Creates two copies of the given input File.
* @param input the File to be copied
* @param output_file1 the first copy
* @param output_file2 the second copy
* @return true if the copy files have been successfully created, false otherwise
*/
private boolean copyFile(File input, File output_file1, File output_file2) {
FileInputStream inputStream = null;
FileOutputStream outputStream_file1 = null;
FileOutputStream outputStream_file2 = null;
int fileLenght = 0;
try {
inputStream = new FileInputStream(input.getAbsolutePath());
}
catch (IOException e) {
e.printStackTrace();
logger.error("Error while reading input file " + input.getAbsolutePath());
return false;
}
// try to open the new files, to be written with FileOutputStream.
try {
outputStream_file1 = new FileOutputStream(output_file1);
outputStream_file2 = new FileOutputStream(output_file2);
}
catch (IOException e) {
e.printStackTrace();
logger.error("Error while creating the copies of input file " + input.getAbsolutePath());
return false;
}
try {
//number of available bytes
fileLenght = inputStream.available(); // Mi informo sul num. bytes.
}
catch (IOException e) {
e.printStackTrace();
logger.error("Error while computing the length of input file " + input.getAbsolutePath());
return false;
}
// Reads the bytes from input stream and copies them into the two output streams
// using FileInputStream.read() and FileOutPutStream.write().
try {
int byte_read;
for(int i=0; i<fileLenght; i++) {
//reads one byte
byte_read = inputStream.read();
//copies one byte
outputStream_file1.write(byte_read);
outputStream_file2.write(byte_read);
}
}
catch (IOException e) {
e.printStackTrace();
logger.error("Error while copying input file " + input.getAbsolutePath());
return false;
}
// The files have been copied, now closes the streams...
try {
// closes InputStream
inputStream.close();
// closes OutputStream
outputStream_file1.close();
outputStream_file2.close();
}
catch (IOException e) {
e.printStackTrace();
logger.error("Error while closing the copies of input file " + input.getAbsolutePath());
return false;
}
// OK, file have been copied
return true;
}
/**
* Solves the given model (xml file) using the jmva solver
* @param model the xml File containing the model description
* @return true if the model has been correctly solved
*/
private boolean solve_jmva(File model) {
CommandLineSolver solver = new CommandLineSolver();
long start, stop, elapsed;
start = System.currentTimeMillis();
boolean success = solver.solve(model);
stop = System.currentTimeMillis();
elapsed = stop - start;
logger.debug("Model " + model.getAbsolutePath() + " solved in " + elapsed + " milliseconds.");
return success;
}
/**
* Solves the given model (xml file) using the jsim solver
* @param model the xml File containing the model description
* @return true if the model has been correctly solved
*/
private boolean solve_jsim(File model) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -