Commit 22e88e60 authored by Christian Meyer's avatar Christian Meyer
Browse files

identifier is made part of output path

    can be given as argument to launcher

OutputPathGenerator.java
    added parameter identifier to createPaths method
DefaultOutputPathGenerator.java
    identifier is made part of outer path
    updated test
LauncherArgs.java
    added identifier as option
Launcher.java
    identifier is passed to output path generator
    updated test
SimulationLooper, DefaultSimulationLooper.java
    output paths are passed as Iterator
    updated test
parent 99c14e95
......@@ -186,7 +186,8 @@ public class Launcher {
protected final void process(LauncherArgs args, ZmtSimState simState, SimParams defaultParams) {
super.process(args, simState, defaultParams);
simState.setParams(loadSimParams(args, context.paramsLoader, defaultParams));
process(args, simState);
process(args, simState, context.outputPathGenerator.createPaths(simState.getClass(), args.getMode(),
getWorkingDirectory(), args.getIdentifier()).iterator());
}
/**
......@@ -195,8 +196,10 @@ public class Launcher {
* @param args
* @param simState
* simulation object with parameters set
* @param pathIterator
* the iterator of output paths
*/
protected abstract void process(LauncherArgs args, ZmtSimState simState);
protected abstract void process(LauncherArgs args, ZmtSimState simState, Iterator<Path> pathIterator);
/**
* Obtains parameter class for {@code simClass} and loads it from path
......@@ -226,32 +229,16 @@ public class Launcher {
}
/**
* Creates the output path for non-batch runs.
*
* @author mey
*
*/
private abstract class NonBatchProcessor extends LoadParamsProcessor {
@Override
protected void process(LauncherArgs args, ZmtSimState simState) {
Path outputPath = context.outputPathGenerator
.createPaths(simState.getClass(), args.getMode(), getWorkingDirectory()).iterator().next();
simState.setOutputPath(outputPath);
}
}
/**
* Runs simulation for a single time.
*
* @author mey
*
*/
private class SingleProcessor extends NonBatchProcessor {
private class SingleProcessor extends LoadParamsProcessor {
@Override
protected void process(LauncherArgs args, ZmtSimState simState) {
protected void process(LauncherArgs args, ZmtSimState simState, Iterator<Path> pathIterator) {
simState.setOutputPath(pathIterator.next());
context.simulationLooper.loop(simState, args.getSimTime(), args.getPrintStatusInterval());
}
}
......@@ -262,9 +249,11 @@ public class Launcher {
* @author mey
*
*/
private class GuiProcessor extends NonBatchProcessor {
private class GuiProcessor extends LoadParamsProcessor {
@Override
protected void process(LauncherArgs args, final ZmtSimState simState) {
protected void process(LauncherArgs args, ZmtSimState simState, Iterator<Path> pathIterator) {
simState.setOutputPath(pathIterator.next());
Class<? extends ZmtGUIState> guiStateClass;
try {
guiStateClass = context.classLocator.findGuiStateClass(args.getSimName());
......@@ -273,14 +262,12 @@ public class Launcher {
}
final ZmtGUIState guiState = createGuiState(guiStateClass, simState);
final Iterator<Path> outputPathsIterator = context.outputPathGenerator
.createPaths(simState.getClass(), args.getMode(), getWorkingDirectory()).iterator();
guiState.addListener(new GuiListener() {
@Override
public void finished(ZmtSimState state) {
state.setOutputPath(outputPathsIterator.next());
state.setOutputPath(pathIterator.next());
}
});
......@@ -318,7 +305,7 @@ public class Launcher {
*/
private class BatchProcessor extends LoadParamsProcessor {
@Override
protected void process(LauncherArgs args, ZmtSimState simState) {
protected void process(LauncherArgs args, ZmtSimState simState, Iterator<Path> pathIterator) {
AutoParams autoParams;
try {
autoParams = context.paramsLoader.loadAutoParams(args.getAutoParamsPath());
......@@ -332,12 +319,10 @@ public class Launcher {
// apply combinations: use params loaded in base as default
Iterable<AppliedCombination> appliedCombinations = context.combinationApplier
.applyCombinations(combinations, simState.getParams());
Iterable<Path> outputPaths = context.outputPathGenerator.createPaths(simState.getClass(), args.getMode(),
getWorkingDirectory());
// run a simulation for every parameter object
context.simulationLooper.loop(simState.getClass(), appliedCombinations, combinations.size(),
args.getMaxThreads(), autoParams.getSimTime(), args.getPrintStatusInterval(),
args.isCombinationInFolderNames(), outputPaths);
args.isCombinationInFolderNames(), pathIterator);
}
}
......
......@@ -41,6 +41,8 @@ public class LauncherArgs {
private int maxThreads = 0;
@Option(name = "-t", aliases = "--print-status-interval", usage = "Prints a log message about simulation status in given step intervals. '0' to disable.\n(SINGLE and BATCH mode only)")
private int printStatusInterval = DEFAULT_PRINT_STATUS_INTERVAL;
@Option(name = "-id", aliases = "--identifier", usage = "Identifier which is added to output path names.")
private String identifier = "";
@Option(name = "-es", aliases = "--export-sim-params", help = true, usage = "Exports default simulation parameters.")
private File exportSimParamsFile;
@Option(name = "-ea", aliases = "--export-auto-params", help = true, usage = "Exports automation parameters that include values from all automatable default parameters. Use this option as a starting point to create automation parameters for your BATCH runs.")
......@@ -92,6 +94,10 @@ public class LauncherArgs {
return printStatusInterval;
}
public String getIdentifier() {
return identifier;
}
public Path getExportSimParamsPath() {
return exportSimParamsFile == null ? null : exportSimParamsFile.toPath();
}
......
......@@ -37,8 +37,9 @@ class DefaultOutputPathGenerator implements OutputPathGenerator {
* {@link Mode#BATCH}.
*/
@Override
public Iterable<Path> createPaths(Class<? extends SimState> simClass, final Mode mode, Path directory) {
final Path outerPath = generateOuterPath(simClass, mode, directory);
public Iterable<Path> createPaths(Class<? extends SimState> simClass, Mode mode, Path directory,
String identifier) {
final Path outerPath = generateOuterPath(simClass, mode, directory, identifier);
return new Iterable<Path>() {
......@@ -59,11 +60,14 @@ class DefaultOutputPathGenerator implements OutputPathGenerator {
* the launch mode
* @param directory
* the parent directory for the created directories
* @param identifier
* the identifier which will be part of the generated path
* @return the outer output path
*/
private static Path generateOuterPath(Class<? extends SimState> simClass, Mode mode, Path directory) {
private static Path generateOuterPath(Class<? extends SimState> simClass, Mode mode, Path directory,
String identifier) {
String directoryName = Output.generateFileName(processForFileName(simClass), CLASSIFIER_OUTER,
mode.toString().toLowerCase());
mode.toString().toLowerCase(), identifier);
if (mode == Mode.BATCH) {
// find next batch index
......
......@@ -51,9 +51,8 @@ class DefaultSimulationLooper implements SimulationLooper {
@Override
public void loop(Class<? extends ZmtSimState> simClass, Iterable<AppliedCombination> appliedCombinations,
int combinationsCount, int maxThreads, double simTime, int printStatusInterval,
boolean combinationInFolderNames, Iterable<Path> outputPaths) {
boolean combinationInFolderNames, Iterator<Path> pathIterator) {
SimRunContext context = new SimRunContext(simClass, simTime, combinationsCount, printStatusInterval);
Iterator<Path> outputPathsIterator = outputPaths.iterator();
int jobNum = 0;
int availableProcessors = Runtime.getRuntime().availableProcessors();
......@@ -64,7 +63,7 @@ class DefaultSimulationLooper implements SimulationLooper {
ExecutorService executor = new BlockingExecutor(nThreads);
long startTime = System.currentTimeMillis();
for (AppliedCombination appliedCombination : appliedCombinations) {
Path outputPath = outputPathsIterator.next();
Path outputPath = pathIterator.next();
if (combinationInFolderNames) {
// replace run_XXXXX with combination's string representation
try {
......
......@@ -16,7 +16,9 @@ public interface OutputPathGenerator extends LauncherStrategy {
* the launch mode
* @param directory
* the parent directory for the created directories
* @param identifier
* an identifier which will be part of the created directory
* @return iterable to directories
*/
Iterable<Path> createPaths(Class<? extends SimState> simClass, Mode mode, Path directory);
Iterable<Path> createPaths(Class<? extends SimState> simClass, Mode mode, Path directory, String identifier);
}
package de.zmt.launcher.strategies;
import java.nio.file.Path;
import java.util.Iterator;
import de.zmt.launcher.strategies.CombinationApplier.AppliedCombination;
import sim.engine.ZmtSimState;
......@@ -39,12 +40,12 @@ public interface SimulationLooper extends LauncherStrategy {
* to disable)
* @param combinationInFolderNames
* use combination to generate inner folder names
* @param outputPaths
* an iterable of output paths that is iterated for every new
* @param pathIterator
* an {@link Iterator} of output paths that is used for every new
* simulation run
*/
void loop(Class<? extends ZmtSimState> simClass, Iterable<AppliedCombination> appliedCombinations,
int combinationsCount, int maxThreads, double simTime, int printStatusInterval,
boolean combinationInFolderNames, Iterable<Path> outputPaths);
boolean combinationInFolderNames, Iterator<Path> pathIterator);
}
package de.zmt.launcher;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import javax.xml.bind.JAXBException;
......@@ -147,7 +150,7 @@ public class LauncherTest {
@Override
public void loop(Class<? extends ZmtSimState> simClass, Iterable<AppliedCombination> simParamsObjects,
int combinationsCount, int maxThreads, double simTime, int printStatusInterval,
boolean combinationInFolderNames, Iterable<Path> outputPaths) {
boolean combinationInFolderNames, Iterator<Path> pathIterator) {
fail("Wrong method called.");
}
......@@ -273,8 +276,19 @@ public class LauncherTest {
private int index;
@Override
public Iterable<Path> createPaths(Class<? extends SimState> simClass, Mode mode, Path directory) {
return Collections.singleton(Paths.get(String.valueOf(index++)));
public Iterable<Path> createPaths(Class<? extends SimState> simClass, Mode mode, Path directory, String identifier) {
return () -> new Iterator<Path>() {
@Override
public boolean hasNext() {
return true;
}
@Override
public Path next() {
return directory.resolve(identifier + index++);
}
};
}
}
......@@ -315,7 +329,7 @@ public class LauncherTest {
@Override
public void loop(Class<? extends ZmtSimState> simClass, Iterable<AppliedCombination> appliedCombinations,
int combinationsCount, int maxThreads, double simTime, int printStatusInterval,
boolean combinationInFolderNames, Iterable<Path> outputPaths) {
boolean combinationInFolderNames, Iterator<Path> pathIterator) {
assertEquals(TestSimState.class, simClass);
assertEquals(APPLIED_COMBINATIONS, appliedCombinations);
assertEquals(CMD_LINE_MAX_THREADS, maxThreads);
......
......@@ -22,6 +22,7 @@ import de.zmt.launcher.LauncherTest.TestSimState;
public class DefaultOutputPathGeneratorTest {
private static final OutputPathGenerator OUTPUT_PATH_GENERATOR = new DefaultOutputPathGenerator();
private static final int ITERATIONS = 6;
private static final String PATH_IDENTIFIER = "test-identifier";
@Rule
public final TemporaryFolder folder = new TemporaryFolder();
......@@ -50,7 +51,7 @@ public class DefaultOutputPathGeneratorTest {
private void createPathsOnNonEmpty(Mode mode) throws IOException {
// create a folder that should be skipped later
OUTPUT_PATH_GENERATOR.createPaths(TestSimState.class, mode, tempDir).iterator().next();
OUTPUT_PATH_GENERATOR.createPaths(TestSimState.class, mode, tempDir, PATH_IDENTIFIER).iterator().next();
// create another file / folder which should not matter
folder.newFile("file");
......@@ -61,7 +62,8 @@ public class DefaultOutputPathGeneratorTest {
private void createAndVerify(int firstIndex, Mode mode) {
Set<Path> uniquePaths = new HashSet<>();
Iterator<Path> iterator = OUTPUT_PATH_GENERATOR.createPaths(TestSimState.class, mode, tempDir).iterator();
Iterator<Path> iterator = OUTPUT_PATH_GENERATOR.createPaths(TestSimState.class, mode, tempDir, PATH_IDENTIFIER)
.iterator();
for (int i = 0; i < ITERATIONS; i++) {
Path outputPath = iterator.next();
......@@ -76,6 +78,7 @@ public class DefaultOutputPathGeneratorTest {
} else {
assertThat(innerName, endsWith(Integer.toString(i + firstIndex)));
}
assertThat(outputPath.toString(), containsString(PATH_IDENTIFIER));
// adding a duplicate would return false
assertTrue(uniquePaths.add(outputPath));
......
......@@ -74,7 +74,7 @@ public class DefaultSimulationLooperTest {
CountDownTestSimState.doneSignal.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
CountDownTestSimState.doneSignal = new CountDownLatch(RUN_COUNT);
SIMULATION_LOOPER.loop(CountDownTestSimState.class, APPLIED_COMBINATIONS, APPLIED_COMBINATIONS.size(),
MAX_THREADS, SIM_TIME, PRINT_STATUS_INTERVAL, combinationInFolderNames, outputPaths);
MAX_THREADS, SIM_TIME, PRINT_STATUS_INTERVAL, combinationInFolderNames, outputPaths.iterator());
waitUntilSimsFinished();
Iterator<AppliedCombination> iterator = APPLIED_COMBINATIONS.iterator();
......@@ -95,7 +95,8 @@ public class DefaultSimulationLooperTest {
CountDownTestSimState.doneSignal.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
CountDownTestSimState.doneSignal = new CountDownLatch(RUN_COUNT);
SIMULATION_LOOPER.loop(ThreadLocalSingletonTestSimState.class, APPLIED_COMBINATIONS,
APPLIED_COMBINATIONS.size(), MAX_THREADS, SIM_TIME, PRINT_STATUS_INTERVAL, false, outputPaths);
APPLIED_COMBINATIONS.size(), MAX_THREADS, SIM_TIME, PRINT_STATUS_INTERVAL, false,
outputPaths.iterator());
waitUntilSimsFinished();
}
......
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