📄 dispatcher_jmvaschema.java
字号:
*/
public TempMeasure[] getTempMeasures() {
return tempMeasures;
}
public boolean abortAllMeasures() {
if (!simStarted) {
return false;
}
if (simFinished)
return true;
if (pauseSim()) {
refreshTempMeasures();
logger.debug("All remaining measures will be aborted");
for (int m = 0; m < tempMeasures.length; m++) {
abortMeasure(m);
}
return true;
} else {
return false;
}
}
public boolean abortMeasure(int index) {
boolean success = false;
if (pauseSim()) {
refreshTempMeasures();
if ((0 <= index) && (index <= tempMeasures.length)) {
success = tempMeasures[index].abort();
} else {
success = false;
}
restartSim();
}
return success;
}
/**
* Prints on System.out the temp values of measures that haven't finished yet
*/
public void printTempMeasures() {
if (pauseSim()) {
if (tempMeasures != null) {
for (int m = 0; m < tempMeasures.length; m++) {
TempMeasure temp = tempMeasures[m];
if (temp.isFinished()) {
continue;
} else {
System.out.println(temp.getName() + ": " +
Double.toString(temp.getTempMean()) + " " +
temp.getNsamples());
}
}
}
restartSim();
}
return;
}
/**
* Pauses the simulation
* @return true if the operation was successful, false otherwise (for example
* simulation not started or already finished)
*/
private boolean pauseSim() {
return NetSystem.pause();
}
/**
* Resumes the simulation
* @return true if the operation was successful, false otherwise (for example
* simulation not started or already finished)
*/
private boolean restartSim() {
return NetSystem.restartFromPause();
}
//------------------------TEST-----------------------------------//
public static void testSimulationTime() {
int N = 5;
long[] duration = new long[N];
long tot = 0;
for (int i = 0; i < N; i++) {
String path = "D:\\JMTtest\\prova" + i + ".xml";
Dispatcher_jMVAschema disp = new Dispatcher_jMVAschema(path);
long start, stop, elapsed;
start = System.currentTimeMillis();
if (disp.solveModel()) {
stop = System.currentTimeMillis();
elapsed = stop - start;
//System.out.println("Duration execution "+i+": ");
System.out.println(Long.toString(elapsed));
duration[i] = elapsed;
tot += elapsed;
} else {
System.out.println("Error!!");
}
}
long mean = tot / N;
System.out.println("Mean: ");
System.out.println(Long.toString(mean));
}
public static void testSolution() {
//Dispatcher disp = new Dispatcher("D:\\test_2open.xml");
Dispatcher_jMVAschema disp = new Dispatcher_jMVAschema("D:\\JMTtest\\solverstep0.xml");
//Dispatcher disp = new Dispatcher("D:\\JMTtest\\solverstep10.xml");
//Dispatcher disp = new Dispatcher("D:\\JMTtest\\prova.xml");
//Dispatcher disp = new Dispatcher("D:\\JMTtest\\test_base.xml");
//Dispatcher disp = new Dispatcher("D:\\JMTtest\\model_with_blocking.xml");
//Dispatcher disp = new Dispatcher("D:\\JMTtest\\model_with_blocking_2.xml");
//Dispatcher disp = new Dispatcher("D:\\JMTtest\\model_with_blocking_open_drop.xml");
//Dispatcher disp = new Dispatcher("D:\\JMTtest\\preload.xml");
disp.setSimulationSeed(23000);
long start, stop, elapsed;
start = System.currentTimeMillis();
if (disp.solveModel()) {
stop = System.currentTimeMillis();
elapsed = stop - start;
//System.out.println("OK - Simulation time: ");
//System.out.println(Long.toString(elapsed));
} else {
System.out.println("Error!!");
}
}
private boolean copyFile(File input, File output_file) {
FileInputStream inputStream = null;
FileOutputStream outputStream_file1 = 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_file);
}
catch (IOException e) {
e.printStackTrace();
logger.error("Error while creating the copy of input file " + input.getAbsolutePath());
return false;
}
try {
//number of available bytes
fileLenght = inputStream.available();
}
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 output stream
// 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);
}
}
catch (IOException e) {
e.printStackTrace();
logger.error("Error while copying input file " + input.getAbsolutePath());
return false;
}
// The file has been copied, now closes the stream...
try {
// closes InputStream
inputStream.close();
// closes OutputStream
outputStream_file1.close();
}
catch (IOException e) {
e.printStackTrace();
logger.error("Error while closing the copy of input file " + input.getAbsolutePath());
return false;
}
// OK, file has been copied
return true;
}
/**
* A system is said to have sufficient capacity to process a given load
* <tt>lambda</tt> if no service center is saturated as a result of the combined loads
* of all the open classes.
* <br>
* WARNING: This method should be called before solving the system.
* @return true if sufficient capacity exists for the given workload, false otherwise
*/
public boolean checkProcessingCapacity(){
Dispatcher_Exact disp_exact = new Dispatcher_Exact(modelDefinition);
return disp_exact.hasSufficientProcessingCapacity();
}
/**
* Sets if parser must validate
* @param validate true if parser must validate, false otherwise
*/
public void setValidate(boolean validate) {
this.validate = validate;
}
/**
* Removes all null measures from output model. A null measure is a measure from a class
* with 0 service demand at a station. Removed measures are stored to be later restored.
* @param input input JMVA model
* @param output output JSIM model
*/
protected void removeNullMeasures(ExactModel input, CommonModel output) {
// Finds a mapping between indices in ExactModel data structure and in CommonModel one
Object[] classKeys = new Object[input.getClasses()];
Object[] stationKeys = new Object[input.getStations()];
for (int i=0; i<input.getClasses(); i++)
classKeys[i] = output.getClassByName(input.getClassNames()[i]);
for (int i=0; i<input.getStations(); i++)
stationKeys[i] = output.getStationByName(input.getStationNames()[i]);
// Search for null elements and remove them
for (int i=0; i<input.getStations();i++)
for (int j=0; j<input.getClasses(); j++)
if (input.getVisits()[i][j] == 0 || (input.getServiceTimes()[i][j][0] == 0 && input.getStationTypes()[i] != ExactModel.STATION_LD)) {
// Removes all measures from station i for class j
Iterator it = output.getMeasureKeys().iterator();
while(it.hasNext()) {
Object key = it.next();
if (output.getMeasureClass(key) == classKeys[j] && output.getMeasureStation(key) == stationKeys[i])
it.remove();
}
}
}
/**
* Adds all removed null measures to output document
* @param input input exactmodel
* @param output output document
*/
protected void addNullMeasuresCorrectThroughputs(ExactModel input, Document output) {
Element solutions = (Element) output.getElementsByTagName("solutions").item(0);
// Search for null elements adds them
for (int i=0; i<input.getStations();i++) {
// for all stations
NodeList stations = solutions.getElementsByTagName("stationresults");
Element station = null;
for (int k=0; k<stations.getLength(); k++) {
station = (Element) stations.item(k);
if (station.getAttribute("station").equals(input.getStationNames()[i]))
break;
}
for (int j=0; j<input.getClasses(); j++) {
// for all classes
NodeList classes = station.getElementsByTagName("classresults");
Element customerclass = null;
for (int k=0; k<stations.getLength(); k++) {
customerclass = (Element) classes.item(k);
if (customerclass.getAttribute("customerclass").equals(input.getClassNames()[j]))
break;
}
if (input.getVisits()[i][j] == 0 || (input.getServiceTimes()[i][j][0] == 0 && input.getStationTypes()[i] != ExactModel.STATION_LD)) {
// Create a null measure for each measure type
for (int k=0; k < ExactModel.INDICES_TYPES.length; k++) {
Element measure = output.createElement("measure");
measure.setAttribute("measureType", ExactModel.INDICES_TYPES[k]);
measure.setAttribute("meanValue", "0.0");
measure.setAttribute("successful","true");
measure.setAttribute("lowerLimit", "0.0");
measure.setAttribute("upperLimit", "0.0");
customerclass.appendChild(measure);
}
}
else if (transform[i][j] != 1) {
// Correct throughputs
NodeList measures = customerclass.getElementsByTagName("measure");
for (int k=0; k<measures.getLength(); k++) {
Element measure = (Element)measures.item(k);
if (measure.getAttribute("measureType").equalsIgnoreCase("Throughput")) {
double val = Double.parseDouble(measure.getAttribute("meanValue"));
val *= transform[i][j];
measure.setAttribute("meanValue", Double.toString(val));
logger.debug("Scaled throughput by "+transform[i][j]+" factor");
break;
}
}
}
}
}
}
public static void main(String[] args) {
final Dispatcher_jMVAschema disp = new Dispatcher_jMVAschema("c:\\test\\test.xml");
disp.setValidate(false);
//disp.getTransformedModel("c:\\test\\test.xml", "c:\\test\\test1.xml");
new Thread() {
public void run() {
try {
sleep(60*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
disp.abortAllMeasures();
}
}.start();
disp.solveModelNewTransform();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -