Skip to content

Commit

Permalink
Merge pull request #3522 from marecabo/matsim-temp-dir-property
Browse files Browse the repository at this point in the history
Add matsim.tempDir property for specifying ImageIO cache directory
  • Loading branch information
jfbischoff authored Nov 5, 2024
2 parents 6143887 + 5974703 commit f515ff3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import java.io.File;

public final class ControlerDefaultsModule extends AbstractModule {

@Override
public void install() {
install(new EventsManagerModule());
Expand Down Expand Up @@ -85,9 +86,10 @@ public void install() {
// Maybe not the best place to but this but since ChartUtils is used by many modules, including default ones,
// the cache needs to be always set correctly.
addControlerListenerBinding().toInstance(new StartupListener() {
@Inject private OutputDirectoryHierarchy outputDirectoryHierarchy;
@Override public void notifyStartup(StartupEvent event) {
ImageIO.setCacheDirectory(new File(outputDirectoryHierarchy.getTempPath()));

@Override
public void notifyStartup(StartupEvent event) {
ImageIO.setCacheDirectory(new File(event.getServices().getControlerIO().getTempPath()));
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public enum OverwriteFileSetting {failIfDirectoryExists, overwriteExistingFiles,

private static final String DIRECTORY_ITERS = "ITERS";

public static final String MATSIM_TEMP_DIR_PROPERTY = "matsim.tempDir";

private static final Logger log = LogManager.getLogger(OutputDirectoryHierarchy.class);

private String runId = null;
Expand Down Expand Up @@ -108,7 +110,11 @@ public OutputDirectoryHierarchy(String outputPath, String runId, OverwriteFileSe
* @return path to a temp-directory.
*/
public final String getTempPath() {
return outputPath + "/tmp";
String matsimTempDir = System.getProperty(MATSIM_TEMP_DIR_PROPERTY);
if (matsimTempDir == null) {
matsimTempDir = outputPath + "/tmp";
}
return matsimTempDir;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,26 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.population.Activity;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.config.groups.ControllerConfigGroup;
import org.matsim.core.controler.OutputDirectoryHierarchy.OverwriteFileSetting;
import org.matsim.core.scenario.ScenarioUtils;
import org.matsim.core.utils.io.IOUtils;
import org.matsim.examples.ExamplesUtils;
import org.matsim.testcases.MatsimTestUtils;

import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URL;
import java.util.Set;
import java.util.stream.Collectors;

/**
* @author thibautd
Expand Down Expand Up @@ -146,4 +158,29 @@ void testDeleteIfDirectoryExists() {
"Directory was not cleared" );

}

@Test
void testTmpDir() {

String javaTempDir = System.getProperty("java.io.tmpdir");
System.setProperty(OutputDirectoryHierarchy.MATSIM_TEMP_DIR_PROPERTY, javaTempDir);
Assertions.assertNotNull(javaTempDir);

System.setProperty("matsim.preferLocalDtds", "true");

URL scenarioURL = ExamplesUtils.getTestScenarioURL("siouxfalls-2014");

Config config = ConfigUtils.loadConfig(IOUtils.extendUrl(scenarioURL, "config_default.xml"));
config.controller().setOverwriteFileSetting(OverwriteFileSetting.deleteDirectoryIfExists);
config.controller().setLastIteration(1);

Scenario scenario = ScenarioUtils.loadScenario(config);

Controler controller = new Controler(scenario);
controller.run();

String matsimTempDir = controller.getControlerIO().getTempPath();
Assertions.assertEquals(javaTempDir, matsimTempDir);
}

}

0 comments on commit f515ff3

Please sign in to comment.