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

remove ScenarioRun, allow DeserializationFeature.ACCEPT_FLOAT_AS_INT

parent 29bdf720
...@@ -71,7 +71,7 @@ public class ScenarioRun implements Runnable { ...@@ -71,7 +71,7 @@ public class ScenarioRun implements Runnable {
public void run() { public void run() {
try { try {
//add Scenario Name to Log4j Mapped Diagnostic Context to filter log by ScenarioRun //add Scenario Name to Log4j Mapped Diagnostic Context to filter log by ScenarioRun
MDC.put("scenario.Name", outputPath.getFileName().toString()); // MDC.put("scenario.Name", outputPath.getFileName().toString());
/** /**
* To make sure that no other Thread changes the scenarioStore object during the initialization of a scenario run * To make sure that no other Thread changes the scenarioStore object during the initialization of a scenario run
...@@ -111,7 +111,7 @@ public class ScenarioRun implements Runnable { ...@@ -111,7 +111,7 @@ public class ScenarioRun implements Runnable {
} finally { } finally {
doAfterSimulation(); doAfterSimulation();
//remove Log4j Mapped Diagnostic Context after ScenarioRun //remove Log4j Mapped Diagnostic Context after ScenarioRun
MDC.remove("scenario.Name"); // MDC.remove("scenario.Name");
} }
} }
......
...@@ -4,6 +4,7 @@ import com.bazaarvoice.jolt.Chainr; ...@@ -4,6 +4,7 @@ import com.bazaarvoice.jolt.Chainr;
import com.bazaarvoice.jolt.Diffy; import com.bazaarvoice.jolt.Diffy;
import com.bazaarvoice.jolt.JsonUtils; import com.bazaarvoice.jolt.JsonUtils;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import java.io.IOException; import java.io.IOException;
...@@ -33,13 +34,16 @@ public class JoltIdentityTransformationTest { ...@@ -33,13 +34,16 @@ public class JoltIdentityTransformationTest {
testIdentity(scenarioFiles, "/identity_v2.json"); testIdentity(scenarioFiles, "/identity_v2.json");
} }
@Test // todo use testResources
@Test @Ignore
public void testIdenityTransformationV01() throws IOException { public void testIdenityTransformationV01() throws IOException {
List<Path> scenarioFiles = getScenarioFiles(Paths.get("../VadereModelTestsV0.1").toRealPath(LinkOption.NOFOLLOW_LINKS).toAbsolutePath()); List<Path> scenarioFiles = getScenarioFiles(Paths.get("../VadereModelTestsV0.1").toRealPath(LinkOption.NOFOLLOW_LINKS).toAbsolutePath());
testIdentity(scenarioFiles, "/identity_v1.json"); testIdentity(scenarioFiles, "/identity_v1.json");
} }
@Test // todo use testResources
@Test @Ignore
public void testTransformationV01_to_V02() throws IOException { public void testTransformationV01_to_V02() throws IOException {
List<Path> scenarioFiles = getScenarioFiles(Paths.get("../VadereModelTestsV0.1").toRealPath(LinkOption.NOFOLLOW_LINKS).toAbsolutePath()); List<Path> scenarioFiles = getScenarioFiles(Paths.get("../VadereModelTestsV0.1").toRealPath(LinkOption.NOFOLLOW_LINKS).toAbsolutePath());
Diffy diffy = new Diffy(); Diffy diffy = new Diffy();
...@@ -86,17 +90,9 @@ public class JoltIdentityTransformationTest { ...@@ -86,17 +90,9 @@ public class JoltIdentityTransformationTest {
} }
} }
@Test
public void DirInTarget() {
String path = "target/TestRun";
try {
Files.createDirectory(Paths.get(path));
} catch (IOException e) {
e.printStackTrace();
}
}
@Test // todo use testResources
@Test @Ignore
public void transformv1t0v2() throws IOException { public void transformv1t0v2() throws IOException {
Path scenario = Paths.get("../VadereModelTestsV0.1/TestOSM/scenarios/basic_1_chicken_osm1.scenario"); Path scenario = Paths.get("../VadereModelTestsV0.1/TestOSM/scenarios/basic_1_chicken_osm1.scenario");
List chainrSpecJson = JsonUtils.classpathToList("/transform_v1_to_v2.json"); List chainrSpecJson = JsonUtils.classpathToList("/transform_v1_to_v2.json");
...@@ -110,7 +106,8 @@ public class JoltIdentityTransformationTest { ...@@ -110,7 +106,8 @@ public class JoltIdentityTransformationTest {
System.out.println(diffy.diff(jsonOut1, jsonOut2).toString()); System.out.println(diffy.diff(jsonOut1, jsonOut2).toString());
} }
@Test // todo use testResources
@Test @Ignore
public void attr01() throws IOException { public void attr01() throws IOException {
List<Path> scenarioFiles = getScenarioFiles(Paths.get("../VadereModelTestsV0.1").toRealPath(LinkOption.NOFOLLOW_LINKS).toAbsolutePath()); List<Path> scenarioFiles = getScenarioFiles(Paths.get("../VadereModelTestsV0.1").toRealPath(LinkOption.NOFOLLOW_LINKS).toAbsolutePath());
LinkedHashMap<String, Object> out = new LinkedHashMap<>(); LinkedHashMap<String, Object> out = new LinkedHashMap<>();
......
package org.vadere.simulator.projects.migration;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;
import org.apache.log4j.spi.LoggingEvent;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
public class ScenarioAppender extends AppenderSkeleton {
private final Path basePath;
private HashMap<String, RollingFileAppender> appenders;
public ScenarioAppender(Path basePath) {
this.basePath = basePath;
this.appenders = new HashMap<>();
}
private RollingFileAppender createAppender(String name) {
Path loggingBasePath = basePath.resolve(name);
if (!loggingBasePath.toFile().exists()) {
try {
Files.createDirectories(loggingBasePath);
} catch (IOException e) {
e.printStackTrace();
}
}
String filePath = Paths.get(loggingBasePath.toString(), "log.out").toString();
RollingFileAppender appender = null;
try {
appender = new RollingFileAppender(new PatternLayout("%d{ABSOLUTE} %5p %c{1}:%L - %m%n"), filePath);
} catch (IOException e) {
e.printStackTrace();
}
appender.setName(name);
return appender;
}
@Override
protected void append(LoggingEvent event) {
Object obj = event.getMDC("scenario.Name");
if (obj != null) {
String appenderName = (String) obj;
if (appenders.containsKey(appenderName)) {
appenders.get(appenderName).doAppend(event);
} else {
RollingFileAppender appender = createAppender(appenderName);
appenders.put(appenderName, appender);
appender.doAppend(event);
}
}
}
@Override
public boolean requiresLayout() {
return false;
}
@Override
public void close() {
appenders.forEach((k, v) -> v.close());
}
}
package org.vadere.simulator.projects.migration;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import java.io.PrintStream;
public class StdOutRedirecter {
private Logger logger = LogManager.getLogger(StdOutRedirecter.class);
private PrintStream defaultOut;
private PrintStream defaultErr;
public StdOutRedirecter(){
// this.logger = logger;
}
public void redirect(){
defaultOut = System.out;
defaultErr = System.err;
System.setOut(useLogger(System.out));
System.setErr(useLogger(System.err));
}
private PrintStream useLogger(final PrintStream realPrintStream){
return new PrintStream(realPrintStream){
@Override
public void print(final String string){
logger.warn(string);
}
@Override
public void println(final String string){
logger.warn(string);
}
};
}
public void reset(){
System.setOut(defaultOut);
System.setErr(defaultErr);
}
}
package org.vadere.simulator.projects.migration;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
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 org.vadere.simulator.projects.dataprocessing.DataProcessingJsonManager;
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.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
public class TestSimulationRunner {
private static Logger logger = LogManager.getLogger(TestSimulationRunner.class);
private final String outputPath = "target/TestRuns/output";
private ThreadFactory threadFactory = r -> {
final Thread thread = new Thread(r);
thread.setUncaughtExceptionHandler( (t, e) -> {
System.out.println(t.getName());
});
return thread;
};
private void initOutputDir(){
try {
if (Paths.get(outputPath).toFile().exists()) {
Files.walk(Paths.get(outputPath).getParent())
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
}
Files.createDirectories(Paths.get(outputPath));
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void RunAllSimulations() {
// remove/clean dir from target folder and create new empty tree (used for output)
initOutputDir();
ExecutorService threadPool = ScenarioExecutorService.newFixedThreadPool(1, threadFactory);
ScenarioAppender scenarioAppender = new ScenarioAppender(Paths.get(outputPath));
LogManager.getRootLogger().addAppender(scenarioAppender);
// StdOutRedirecter redirecter = new StdOutRedirecter();
// redirecter.redirect();
List<Path> scenarios = getScenarioFiles(Paths.get("../VadereModelTests"));
// List<Path> scenarios = getScenarioFiles(Paths.get("../VadereModelTests/TestOSM"));
List<Path> chicken = scenarios.stream().filter(f -> f.getFileName().endsWith("basic_1_chicken_osm1.scenario")).collect(Collectors.toList());
List<Path> subset = scenarios.subList(0, 5);
subset.addAll(chicken);
System.out.println(subset.size());
for (Path scenarioPath : subset) {
Scenario s = null;
try {
s = ScenarioFactory.createScenarioWithScenarioFilePath(scenarioPath);
} catch (IOException e) {
e.printStackTrace();
}
logger.info("#####Start scenario: " + scenarioPath.getFileName());
s.getAttributesSimulation().setWriteSimulationData(true);
threadPool.submit(new ScenarioRun(s, outputPath, (listener) -> System.out.println("done")));
}
threadPool.shutdown();
if (!threadPool.isTerminated()) {
try {
logger.info("waiting 10 min....");
threadPool.awaitTermination(10, TimeUnit.MINUTES);
} catch (InterruptedException e) {
e.printStackTrace();
threadPool.shutdownNow();
} finally {
LogManager.getRootLogger().removeAppender(scenarioAppender);
System.out.println("before reset");
// redirecter.reset();
System.out.println("after reset");
}
}
}
private void addTrajectoriesOutputfile(final Scenario scenario){
DataProcessingJsonManager m = scenario.getDataProcessingJsonManager();
}
@Test
public void TestProjects(){
List<Path> projects = getProjectDirs(Paths.get("../VadereModelTests/"));
for (Path projectPath : projects) {
//
}
}
@Test
public void getProjectDirsTest(){
getProjectDirs(Paths.get("../VadereModelTests/")).forEach(p -> System.out.println(p.toString()));
}
private List<Path> getProjectDirs(Path vadereModelTest){
LinkedList<Path> projectPath = new LinkedList<>();
FileVisitor<Path> visitor = new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
if (Files.exists(dir.resolve("vadere.project"))){
projectPath.add(dir);
return FileVisitResult.SKIP_SUBTREE;
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
};
try {
Files.walkFileTree(vadereModelTest, visitor);
} catch (IOException e) {
e.printStackTrace();
}
return projectPath;
}
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;
}
}
...@@ -34,7 +34,7 @@ public class JacksonObjectMapper extends ObjectMapper { ...@@ -34,7 +34,7 @@ public class JacksonObjectMapper extends ObjectMapper {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
public JacksonObjectMapper() { public JacksonObjectMapper() {
configure(DeserializationFeature.ACCEPT_FLOAT_AS_INT, false); // otherwise 4.7 will automatically be casted to 4 for integers, with this it throws an error configure(DeserializationFeature.ACCEPT_FLOAT_AS_INT, true); // otherwise 4.7 will automatically be casted to 4 for integers, with this it throws an error
enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION); // forbids duplicate keys enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION); // forbids duplicate keys
disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); // to allow empty attributes like "attributes.SeatingAttr": {}, useful while in dev disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); // to allow empty attributes like "attributes.SeatingAttr": {}, useful while in dev
setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); // otherwise private fields won't be usable setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); // otherwise private fields won't be usable
......
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