Commit ddcce4b3 authored by Stefan Schuhbaeck's avatar Stefan Schuhbaeck
Browse files

add elements of new entrypoint for automated simulation runs and tests

parent 9e34daec
......@@ -5,6 +5,7 @@ import org.vadere.simulator.projects.io.IOVadere;
import org.vadere.util.io.IOUtils;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
public class ScenarioFactory {
......@@ -31,4 +32,21 @@ public class ScenarioFactory {
return scenario;
}
public static Scenario createScenarioWithScenarioFilePath(
final Path scenarioPath) throws IOException {
String json = IOUtils.readTextFile(scenarioPath);
Scenario scenario = IOVadere.fromJson(json);
return scenario;
}
public static Scenario createScenarioWithScenarioJson(
final String scenarioJson) throws IOException {
Scenario scenario = IOVadere.fromJson(scenarioJson);
return scenario;
}
}
package org.vadere.simulator.control;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ScenarioExecutorService extends ThreadPoolExecutor {
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
return new ScenarioExecutorService(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
threadFactory);
}
public ScenarioExecutorService(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
}
public ScenarioExecutorService(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
}
public ScenarioExecutorService(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
}
public ScenarioExecutorService(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
}
@Override
protected void afterExecute(Runnable r, Throwable t) {
if (t != null) {
// todo Throwable von scenario run abolen
System.out.println("Error in execution found...");
t.printStackTrace();
} else {
System.out.println("Execution worked");
}
super.afterExecute(r, t);
}
}
package org.vadere.simulator.projects.migration;
import org.junit.Test;
import org.vadere.simulator.control.ScenarioExecutorService;
import org.vadere.simulator.entrypoints.ScenarioFactory;
import org.vadere.simulator.projects.Scenario;
import org.vadere.simulator.projects.ScenarioRun;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
public class TestSimulationRunner {
private final String outputPath = "target/TestRuns/output";
@Test
public void RunAllSimulations() {
// remove/clean dir from target folder and create new empty tree (used for output)
try {
Files.walk(Paths.get(outputPath).getParent())
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::deleteOnExit);
Files.createDirectories(Paths.get(outputPath));
} catch (IOException e) {
e.printStackTrace();
}
ThreadFactory threadFactory = new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
// System.out.println("creating pooled thread");
final Thread thread = new Thread(r);
//todo LogManager neuer Appender
// thread.setUncaughtExceptionHandler(exceptionHandler);
return thread;
}
};
ExecutorService threadPool = ScenarioExecutorService.newFixedThreadPool(4, threadFactory);
List<Path> scenarios = getScenarioFiles(Paths.get("../VadereModelTests/TestOSM"));
List<Path> subset = scenarios.subList(0, 3);
System.out.println(subset.size());
for (Path scenarioPath : subset) {
Scenario s = null;
try {
s = ScenarioFactory.createScenarioWithScenarioFilePath(scenarioPath);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("#####Start scenario: " + scenarioPath.getFileName());
threadPool.submit(new ScenarioRun(s, outputPath, (listener) -> System.out.println("done")));
}
threadPool.shutdown();
if (!threadPool.isTerminated()) {
try {
System.out.println("waiting 60 sec....");
threadPool.awaitTermination(60, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
threadPool.shutdownNow();
}
}
}
private List<Path> getScenarioFiles(Path vadereModelTest) {
LinkedList<Path> scenarioFiles = new LinkedList<>();
FileVisitor<Path> visitor = new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
if (dir.endsWith("output")) {
return FileVisitResult.SKIP_SUBTREE;
} else {
return FileVisitResult.CONTINUE;
}
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.getFileName().toString().endsWith("scenario")) {
scenarioFiles.add(file);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return null;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
if (Files.exists(dir.resolve(".git"))) {
return FileVisitResult.CONTINUE;
} else {
return FileVisitResult.SKIP_SUBTREE;
}
}
};
try {
Files.walkFileTree(vadereModelTest, visitor);
} catch (IOException e) {
e.printStackTrace();
}
return scenarioFiles;
}
}
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