Commit 05835852 authored by Simon Trapp's avatar Simon Trapp

Relative path support for distributions

Bugfix: Replay Distribution now uses replay_filename.
Removed Input Field for Distributions -> filenames in p1.
All distributions with filenames now support relative paths to the .qpe
file. OS specific separators are automatically fixed.
parent 271fa3bc
......@@ -73,7 +73,6 @@ import de.tud.cs.qpe.editors.net.QueueEditorPage;
import de.tud.cs.qpe.model.DocumentManager;
import de.tud.cs.qpe.model.NetHelper;
import de.tud.cs.qpe.utils.CellValidators;
import de.tud.cs.qpe.utils.FileCellEditor;
import de.tud.cs.qpe.utils.XmlAttributeEditingSupport;
import de.tud.cs.qpe.utils.XmlAttributeLabelProvider;
import de.tud.cs.qpe.utils.XmlEnumerationAttributeEditingSupport;
......@@ -253,61 +252,6 @@ public class QueueingPlacePropertyComposite extends PlacePropertyComposite {
col = colorTable.createColumn("p3", 40);
colorTable.setColumnLabelProvider(col, new DistributionFunctionParameterLabelProvider(2));
colorTable.setColumnEditingSupport(col, new DistributionFunctionParameterEditingSupport(col.getViewer(), 2));
col = colorTable.createColumn("Input File", 80);
colorTable.setColumnLabelProvider(col, new CellLabelProvider() {
@Override
public void update(ViewerCell cell) {
Element colorRef = (Element)cell.getElement();
String curDistFkt = colorRef.attributeValue(
"distribution-function", "Exponential");
if("Empirical".equals(curDistFkt)) {
cell.setText(colorRef.attributeValue("pdf_filename", ""));
} else if ("Replay".equals(curDistFkt)) {
cell.setText(colorRef.attributeValue("replay_filename", ""));
} else {
cell.setText("");
}
}
});
colorTable.setColumnEditingSupport(col, new XmlAttributeEditingSupport(col.getViewer(), "pdf_filename") {
@Override
protected CellEditor createCellEditor(Composite parent) {
return new FileCellEditor(parent);
}
@Override
protected boolean canEdit(Object element) {
Element colorRef = (Element)element;
String curDistFkt = colorRef.attributeValue(
"distribution-function", "Exponential");
return ("Empirical".equals(curDistFkt) || "Replay".equals(curDistFkt));
}
@Override
protected void setValue(Object element, Object value) {
updateInternalAttribute(element);
super.setValue(element, value);
}
@Override
protected Object getValue(Object element) {
updateInternalAttribute(element);
return ((Element) element).attributeValue(attribute, "");
}
private void updateInternalAttribute(Object element) {
Element colorRef = (Element) element;
String curDistFkt = colorRef.attributeValue("distribution-function", "Exponential");
if ("Empirical".equals(curDistFkt))
this.attribute = "pdf_filename";
else if ("Replay".equals(curDistFkt))
this.attribute = "replay_filename";
}
});
}
protected void updatePropertyFields() {
......@@ -417,7 +361,7 @@ public class QueueingPlacePropertyComposite extends PlacePropertyComposite {
"StudentT", "Uniform", "VonMises", "Empirical", "Deterministic", "Replay", "MARS", "WEKA" };
protected final int numParams[] = { 2, 3, 3, 1, 2, 2, 1, 1, 1, 2, 1, 2,
1, 2, 1, 0, 1, 1 };
1, 3, 1, 1, 1, 1 };
protected final String paramNames[][] = { { "alpha", "beta", null },
{ "mean", "gamma", "cut" }, { "mean", "gamma", "cut" },
......@@ -426,14 +370,17 @@ public class QueueingPlacePropertyComposite extends PlacePropertyComposite {
{ "tau", null, null }, { "p", null, null },
{ "mean", "stdDev", null }, { "freedom", null, null },
{ "min", "max", null }, { "freedom", null, null },
{ "scale", "offset", null }, { "c", null, null }, {}, { "marsFile" }, { "wekaFile" } };
{ "pdf_filename", "scale", "offset" }, { "c", null, null }, { "replay_filename" }, { "marsFile" },
{ "wekaFile" } };
protected final String defaultValues[][] = { { "1", "1", null },
{ "1", "1", "1" }, { "1", "1", "1" }, { "1", null, null },
{ "1", "1", null }, { "1", "1", null }, { "1", null, null },
{ "1", null, null }, { "1", null, null }, { "1", "1", null },
{ "1", null, null }, { "1", "1", null }, { "1", null, null },
{ "1", "0", null }, { "1", null, null }, {}, { null }, { null } };
{ "pdf_filename", "1", "0" }, { "1", null, null }, { "replay_filename" },
{ "absolute_model_path_here" },
{ "absolute_model_path_here" } };
public String[] getItems() {
return items;
......
......@@ -128,6 +128,8 @@ public class SimQPNController {
private Element XMLDescription;
private static boolean simulationCurrentlyRunning;
private static String qpeFilepath;
private AggregateStats[] aggregateStats;
/**
......@@ -144,7 +146,7 @@ public class SimQPNController {
* @return
* @throws SimQPNException
*/
public static SimQPNController createSimQPNController(Element XMLDescription,
public static SimQPNController createSimQPNController(String qpeFilename, Element XMLDescription,
String configurationName, String logConfigFilename, Date randomSeed)
throws SimQPNException {
XMLValidator.validateInputNet(XMLDescription);
......@@ -152,17 +154,17 @@ public class SimQPNController {
SimQPNController sim = new SimQPNController();
if(randomSeed == null){
Date currentTime = new Date(); // User current time as random seed
sim.initialize(XMLDescription, configurationName, logConfigFilename, currentTime);
sim.initialize(qpeFilename, XMLDescription, configurationName, logConfigFilename, currentTime);
}else{
sim.initialize(XMLDescription, configurationName, logConfigFilename, randomSeed);
sim.initialize(qpeFilename, XMLDescription, configurationName, logConfigFilename, randomSeed);
}
return sim;
}
public static SimQPNController createSimQPNController(Element XMLDescription,
public static SimQPNController createSimQPNController(String qpeFilename, Element XMLDescription,
String configurationName, String logConfigFilename)
throws SimQPNException {
return createSimQPNController(XMLDescription, configurationName, logConfigFilename, null);
return createSimQPNController(qpeFilename, XMLDescription, configurationName, logConfigFilename, null);
}
/**
......@@ -182,13 +184,16 @@ public class SimQPNController {
* @return
* @exception
*/
private void initialize(Element XMLDescription, String configurationName, String logConfigFilename, Date date)
private void initialize(String qpeFilename, Element XMLDescription, String configurationName,
String logConfigFilename, Date date)
throws SimQPNException {
LogUtil.initializeLogging(XMLDescription, configurationName, logConfigFilename);
// NOTE: Random needs to be initialized before starting the model definition
RandomNumberGenerator.initialize(date);
this.qpeFilepath = qpeFilename;
this.configuration = ConfigurationLoader.loadConfiguration(
XMLDescription, configurationName);
......@@ -265,6 +270,10 @@ public class SimQPNController {
SimQPNController.simulationCurrentlyRunning = simRunning;
}
public static String getQPEFilePath() {
return qpeFilepath;
}
public Element getXMLDescription() {
return XMLDescription;
}
......
......@@ -93,17 +93,19 @@ public class SimQPN implements IApplication {
BasicConfigurator.configure();
LogManager.getRootLogger().setLevel((Level) Level.FATAL);
Document inputDoc = loadXMLFile(inputFilename);
runSimulatorOnDocument(inputDoc, configuration, outputFilename, logConfigFilename, progress, null);
runSimulatorOnDocument(inputFilename, inputDoc, configuration, outputFilename, logConfigFilename, progress,
null);
return runtime;
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
private static void runSimulatorOnDocument(Document netDocument,
private static void runSimulatorOnDocument(String qpeFilename, Document netDocument,
String configurationName, String outputFilename, String logConfigFilename, SimulatorProgress progress, Date date) throws SimQPNException {
Element net = netDocument.getRootElement();
SimQPNController sim = SimQPNController.createSimQPNController(net, configurationName, logConfigFilename, date);
SimQPNController sim = SimQPNController.createSimQPNController(qpeFilename, net, configurationName,
logConfigFilename, date);
long tic = System.currentTimeMillis();
sim.execute(configurationName, outputFilename, progress);
long toc = System.currentTimeMillis();
......@@ -139,6 +141,7 @@ public class SimQPN implements IApplication {
BasicConfigurator.configure();
Document netDocument = null;
String documentFilepath = null;
String configuration = null;
String outputFilename = null;
String logConfigFilename = null;
......@@ -162,6 +165,7 @@ public class SimQPN implements IApplication {
logConfigFilename = args[++x];
} else {
netDocument = loadXMLFile(args[x]);
documentFilepath = args[x];
}
}
......@@ -206,7 +210,7 @@ public class SimQPN implements IApplication {
System.out.println("Running configuration \"" + configuration + "\"");
System.out.println();
try {
runSimulatorOnDocument(netDocument, configuration,
runSimulatorOnDocument(documentFilepath, netDocument, configuration,
outputFilename, logConfigFilename, new ConsoleSimulatorProgress(), date);
} catch (SimQPNException e) {
e.printStackTrace();
......
......@@ -56,9 +56,9 @@ public class ContinuousEmpiricalCreator extends DistributionCreator {
@Override
protected void loadParams() throws SimQPNException {
pdf = this.loadDoublesFromFile("probabilitiesFile");
pdffilename = this.loadStringParam("probabilitiesFile");
pdffilename = getAbsoluteFilepath(this.loadStringParam("probabilitiesFile"));
values = this.loadDoublesFromFile("valuesFile");
valuesfilename = this.loadStringParam("valuesFile");
valuesfilename = getAbsoluteFilepath(this.loadStringParam("valuesFile"));
if (pdf.length != values.length) {
throw new InputMismatchException("The length of the distribution and its corresponding values must match.");
}
......
......@@ -56,9 +56,9 @@ public class DeterministicConcurrencyCreator extends DistributionCreator {
@Override
protected void loadParams() throws SimQPNException {
concurrencies = this.loadDoublesFromFile("concurrenciesFile");
concurrenciesFilename = this.loadStringParam("concurrenciesFile");
concurrenciesFilename = getAbsoluteFilepath(this.loadStringParam("concurrenciesFile"));
responseTimes = this.loadDoublesFromFile("responsetimesFile");
responseTimesFilename = this.loadStringParam("responsetimesFile");
responseTimesFilename = getAbsoluteFilepath(this.loadStringParam("responsetimesFile"));
}
@Override
......
......@@ -57,9 +57,9 @@ public class DiscreteEmpiricalCreator extends DistributionCreator {
@Override
protected void loadParams() throws SimQPNException {
pdf = this.loadDoublesFromFile("probabilitiesFile");
pdffilename = this.loadStringParam("probabilitiesFile");
pdffilename = getAbsoluteFilepath(this.loadStringParam("probabilitiesFile"));
values = this.loadDoublesFromFile("valuesFile");
valuesfilename = this.loadStringParam("valuesFile");
valuesfilename = getAbsoluteFilepath(this.loadStringParam("valuesFile"));
if (pdf.length != values.length) {
throw new InputMismatchException("The length of the distribution and its corresponding values must match.");
}
......
......@@ -45,13 +45,15 @@ import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.dom4j.Element;
import de.tud.cs.simqpn.kernel.loading.distributions.AbstractDistribution;
import de.tud.cs.simqpn.kernel.SimQPNController;
import de.tud.cs.simqpn.kernel.SimQPNException;
public abstract class DistributionCreator {
......@@ -122,7 +124,7 @@ public abstract class DistributionCreator {
protected double[] loadDoublesFromFile(String paramName) throws SimQPNException {
String doublesFilename = loadStringParam(paramName);
String doublesFilename = getAbsoluteFilepath(loadStringParam(paramName));
File doublesFile = new File(doublesFilename);
if (!doublesFile.exists()) {
throw new SimQPNException(
......@@ -185,4 +187,29 @@ public abstract class DistributionCreator {
}
}
}
/**
* returns the absolute filepath of a filename. If path is relative, use
* relation to .qpe file.
**/
protected String getAbsoluteFilepath(String filename) {
filename = separatorsToSystem(filename);
if (!Paths.get(filename).isAbsolute()) {
Path qpePath = Paths.get(SimQPNController.getQPEFilePath());
filename = qpePath.getParent().toString() + File.separatorChar + filename;
}
return filename;
}
private String separatorsToSystem(String res) {
if (res == null)
return null;
if (File.separatorChar == '\\') {
// From Windows to Linux/Mac
return res.replace('/', File.separatorChar);
} else {
// From Linux/Mac to Windows
return res.replace('\\', File.separatorChar);
}
}
}
......@@ -55,7 +55,7 @@ public class EmpiricalCreator extends DistributionCreator {
@Override
protected void loadParams() throws SimQPNException {
pdf = this.loadDoublesFromFile("pdf_filename");
pdfFilename = this.loadStringParam("pdf_filename");
pdfFilename = getAbsoluteFilepath(this.loadStringParam("pdf_filename"));
scale = this.loadDoubleParam("scale");
offset = this.loadDoubleParam("offset");
}
......
......@@ -62,7 +62,7 @@ public class MARSCreator extends DistributionCreator {
@Override
protected void loadParams() throws SimQPNException {
marsFilename = this.loadStringParam("marsFile");
marsFilename = getAbsoluteFilepath(this.loadStringParam("marsFile"));
loadMARSModelFromFile(marsFilename);
}
......
......@@ -41,7 +41,6 @@
*/
package de.tud.cs.simqpn.kernel.loading.distributions;
import de.tud.cs.simqpn.kernel.loading.distributions.AbstractDistribution;
import de.tud.cs.simqpn.kernel.SimQPNException;
import de.tud.cs.simqpn.kernel.util.LogUtil;
......@@ -53,7 +52,7 @@ public class PeriodicalCreator extends DistributionCreator {
@Override
protected void loadParams() throws SimQPNException {
pdf = this.loadDoublesFromFile("pdf_filename");
pdfFilename = this.loadStringParam("pdf_filename");
pdfFilename = getAbsoluteFilepath(this.loadStringParam("pdf_filename"));
}
@Override
......
......@@ -41,7 +41,6 @@
*/
package de.tud.cs.simqpn.kernel.loading.distributions;
import de.tud.cs.simqpn.kernel.loading.distributions.AbstractDistribution;
import de.tud.cs.simqpn.kernel.SimQPNException;
public class ReplayCreator extends DistributionCreator {
......@@ -51,8 +50,8 @@ public class ReplayCreator extends DistributionCreator {
@Override
protected void loadParams() throws SimQPNException {
replayValues = this.loadDoublesFromFile("pdf_filename");
replayFilename = this.loadStringParam("pdf_filename");
replayValues = this.loadDoublesFromFile("replay_filename");
replayFilename = getAbsoluteFilepath(this.loadStringParam("replay_filename"));
}
@Override
......
......@@ -55,7 +55,7 @@ public class WEKACreator extends DistributionCreator {
@Override
protected void loadParams() throws SimQPNException {
wekaFilename = this.loadStringParam("wekaFile");
wekaFilename = getAbsoluteFilepath(this.loadStringParam("wekaFile"));
try {
FileInputStream fis = new FileInputStream(wekaFilename);
ObjectInputStream ois = new ObjectInputStream(fis);
......
......@@ -113,7 +113,7 @@ public class StartSimulatorAction extends AbstractHandler {
if (result == WizardDialog.OK) {
String configuration = wizard.getActiveConfiguration();
try {
Simulation simulation = new Simulation(net, shell,
Simulation simulation = new Simulation(qpeInput.getPath().toOSString(), net, shell,
configuration);
ProgressMonitorDialog progress = new ProgressMonitorDialog(
......@@ -193,6 +193,7 @@ public class StartSimulatorAction extends AbstractHandler {
protected Element net;
protected Shell shell;
private String configuration;
private String qpeFilepath;
private IProgressMonitor monitor;
private int totalWork;
private int worked;
......@@ -204,10 +205,11 @@ public class StartSimulatorAction extends AbstractHandler {
private long remainingTime; // (in seconds)
private String failedPlace = null;
public Simulation(Element net, Shell shell, String configuration) {
public Simulation(String qpeFilepath, Element net, Shell shell, String configuration) {
this.net = net;
this.shell = shell;
this.configuration = configuration;
this.qpeFilepath = qpeFilepath;
}
class EditorOpener implements Runnable {
......@@ -249,7 +251,8 @@ public class StartSimulatorAction extends AbstractHandler {
if(date == null){
date = new Date(); //set random date
}
SimQPNController sim = SimQPNController.createSimQPNController(net, configuration, null, date);
SimQPNController sim = SimQPNController.createSimQPNController(qpeFilepath, net, configuration, null,
date);
File resultFile = sim.execute(configuration, null, this);
net = sim.getXMLDescription();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment