Skip to content

Commit

Permalink
remove usage of path to fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rewertvsp committed Oct 17, 2024
1 parent abff745 commit 7881ec1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ public void handleEvent(CarrierShipmentDeliveryStartEvent event) {
vehicle2Load.put(vehicleId, list);
}

void writeLoadPerVehicle(Path analysisOutputDirectory, Scenario scenario) throws IOException {
void writeLoadPerVehicle(String analysisOutputDirectory, Scenario scenario) throws IOException {
log.info("Writing out vehicle load analysis ...");
//Load per vehicle
String fileName = analysisOutputDirectory.resolve("Load_perVehicle.tsv").toString();
String fileName = Path.of(analysisOutputDirectory).resolve("Load_perVehicle.tsv").toString();

BufferedWriter bw1 = new BufferedWriter(new FileWriter(fileName));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ public CarrierPlanAnalysis(String delimiter, Carriers carriers) {
this.carriers = carriers;
}

public void runAnalysisAndWriteStats(Path analysisOutputDirectory) throws IOException {
public void runAnalysisAndWriteStats(String analysisOutputDirectory) throws IOException {
log.info("Writing out carrier analysis ...");
//Load per vehicle
String fileName = analysisOutputDirectory.resolve("Carrier_stats.tsv").toString();
String fileName = Path.of(analysisOutputDirectory).resolve("Carrier_stats.tsv").toString();

try (BufferedWriter bw1 = new BufferedWriter(new FileWriter(fileName))) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ public void handleEvent(VehicleLeavesTrafficEvent event){
}
}

void writeTravelTimeAndDistancePerVehicle(Path analysisOutputDirectory, Scenario scenario) throws IOException {
void writeTravelTimeAndDistancePerVehicle(String analysisOutputDirectory, Scenario scenario) throws IOException {
log.info("Writing out Time & Distance & Costs ... perVehicle");
//Travel time and distance per vehicle
String fileName = analysisOutputDirectory.resolve("TimeDistance_perVehicle.tsv").toString();
String fileName = Path.of(analysisOutputDirectory).resolve("TimeDistance_perVehicle.tsv").toString();

BufferedWriter bw1 = new BufferedWriter(new FileWriter(fileName));

Expand Down Expand Up @@ -224,7 +224,7 @@ void writeTravelTimeAndDistancePerVehicle(Path analysisOutputDirectory, Scenario
log.info("Output written to {}", fileName);
}

void writeTravelTimeAndDistancePerVehicleType(Path analysisOutputDirectory, Scenario scenario) throws IOException {
void writeTravelTimeAndDistancePerVehicleType(String analysisOutputDirectory, Scenario scenario) throws IOException {
log.info("Writing out Time & Distance & Costs ... perVehicleType");

//----- All VehicleTypes in CarriervehicleTypes container. Used so that even unused vehTypes appear in the output
Expand All @@ -234,7 +234,7 @@ void writeTravelTimeAndDistancePerVehicleType(Path analysisOutputDirectory, Scen
vehicleTypesMap.putIfAbsent(vehicleType.getId(), vehicleType);
}

String fileName = analysisOutputDirectory.resolve("TimeDistance_perVehicleType.tsv").toString();
String fileName = Path.of(analysisOutputDirectory).resolve("TimeDistance_perVehicleType.tsv").toString();

BufferedWriter bw1 = new BufferedWriter(new FileWriter(fileName));
//Write headline:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;

//import static org.matsim.application.ApplicationUtils.globFile;

Expand All @@ -58,8 +57,8 @@ public class RunFreightAnalysisEventBased {
private static final Logger log = LogManager.getLogger(RunFreightAnalysisEventBased.class);

//Where is your simulation output, that should be analysed?
private Path EVENTS_PATH = null;
private final Path ANALYSIS_OUTPUT_PATH;
private String EVENTS_PATH = null;
private final String ANALYSIS_OUTPUT_PATH;
private Scenario scenario = null;
private Carriers carriers = null;
private final String delimiter = "\t";
Expand All @@ -72,7 +71,7 @@ public class RunFreightAnalysisEventBased {
* @param analysisOutputPath The directory where the result of the analysis should go to
* @param globalCrs The CRS of the simulation
*/
public RunFreightAnalysisEventBased(Path simOutputPath, Path analysisOutputPath, String globalCrs) {
public RunFreightAnalysisEventBased(String simOutputPath, String analysisOutputPath, String globalCrs) {

this.ANALYSIS_OUTPUT_PATH = analysisOutputPath;
// this.EVENTS_PATH = globFile(simOutputPath, "*output_events.*");
Expand All @@ -81,13 +80,13 @@ public RunFreightAnalysisEventBased(Path simOutputPath, Path analysisOutputPath,
// Path carriersPath = globFile(simOutputPath, "*output_carriers.*");
// Path carriersVehicleTypesPath = globFile(simOutputPath, "*output_carriersVehicleTypes.*");

this.EVENTS_PATH = simOutputPath.resolve("*output_events.xml.gz");
Path vehiclesPath = simOutputPath.resolve("*output_allVehicles.xml.gz");
Path networkPath = simOutputPath.resolve("*output_network.xml.gz");
Path carriersPath = simOutputPath.resolve("*output_carriers.xml.gz");
Path carriersVehicleTypesPath = simOutputPath.resolve("*output_carriersVehicleTypes.xml.gz");

createScenarioForFreightAnalysis(vehiclesPath, networkPath, carriersPath, carriersVehicleTypesPath, globalCrs);
// this.EVENTS_PATH = simOutputPath.resolve("*output_events.xml.gz");
// Path vehiclesPath = simOutputPath.resolve("*output_allVehicles.xml.gz");
// Path networkPath = simOutputPath.resolve("*output_network.xml.gz");
// Path carriersPath = simOutputPath.resolve("*output_carriers.xml.gz");
// Path carriersVehicleTypesPath = simOutputPath.resolve("*output_carriersVehicleTypes.xml.gz");
//
// createScenarioForFreightAnalysis(vehiclesPath, networkPath, carriersPath, carriersVehicleTypesPath, globalCrs);
}

/**
Expand All @@ -101,8 +100,8 @@ public RunFreightAnalysisEventBased(Path simOutputPath, Path analysisOutputPath,
* @param analysisOutputPath Path to the output directory
* @param globalCrs The CRS of the simulation
*/
public RunFreightAnalysisEventBased(Path networkPath, Path vehiclesPath, Path carriersPath, Path carriersVehicleTypesPath, Path eventsPath,
Path analysisOutputPath, String globalCrs) {
public RunFreightAnalysisEventBased(String networkPath, String vehiclesPath, String carriersPath, String carriersVehicleTypesPath, String eventsPath,
String analysisOutputPath, String globalCrs) {
this.EVENTS_PATH = eventsPath;
this.ANALYSIS_OUTPUT_PATH = analysisOutputPath;

Expand All @@ -115,16 +114,16 @@ public RunFreightAnalysisEventBased(Path networkPath, Path vehiclesPath, Path ca
* @param carriers The carriers to be analysed
* @param analysisOutputPath The directory where the result of the analysis should go to
*/
public RunFreightAnalysisEventBased(Carriers carriers, Path analysisOutputPath) {
public RunFreightAnalysisEventBased(Carriers carriers, String analysisOutputPath) {
this.carriers = carriers;
this.ANALYSIS_OUTPUT_PATH = analysisOutputPath;
}

private void createScenarioForFreightAnalysis(Path vehiclesPath, Path networkPath, Path carriersPath, Path carriersVehicleTypesPath,
private void createScenarioForFreightAnalysis(String vehiclesPath, String networkPath, String carriersPath, String carriersVehicleTypesPath,
String globalCrs) {
Config config = ConfigUtils.createConfig();
config.vehicles().setVehiclesFile(vehiclesPath.toString());
config.network().setInputFile(networkPath.toString());
config.vehicles().setVehiclesFile(vehiclesPath);
config.network().setInputFile(networkPath);
config.plans().setInputFile(null);
config.eventsManager().setNumberOfThreads(null);
config.eventsManager().setEstimatedNumberOfEvents(null);
Expand All @@ -133,7 +132,7 @@ private void createScenarioForFreightAnalysis(Path vehiclesPath, Path networkPat

//freight settings
FreightCarriersConfigGroup freightCarriersConfigGroup = ConfigUtils.addOrGetModule(config, FreightCarriersConfigGroup.class);
freightCarriersConfigGroup.setCarriersFile(carriersPath.toString());
freightCarriersConfigGroup.setCarriersFile(carriersPath);
freightCarriersConfigGroup.setCarriersVehicleTypesFile(carriersVehicleTypesPath.toString());

scenario = ScenarioUtils.loadScenario(config);
Expand Down Expand Up @@ -171,7 +170,7 @@ public void runCompleteAnalysis() throws IOException {
eventsManager.initProcessing();
MatsimEventsReader matsimEventsReader = CarrierEventsReaders.createEventsReader(eventsManager);

matsimEventsReader.readFile(EVENTS_PATH.toString());
matsimEventsReader.readFile(EVENTS_PATH);
eventsManager.finishProcessing();

log.info("Analysis completed.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
import org.matsim.testcases.MatsimTestUtils;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;

public class FreightAnalysisEventBasedTest {

Expand All @@ -39,18 +37,17 @@ public class FreightAnalysisEventBasedTest {
final static URL SCENARIO_URL = ExamplesUtils.getTestScenarioURL("freight-chessboard-9x9");

@Test
void runServiceEventTest() throws IOException, URISyntaxException {
void runServiceEventTest() throws IOException {
// Note: I had to manually change the files for this test to run, as I did not have access to the original input file of the events-file
// This results in the carrier-plans not being related to the actual events. This is however no problem for testing the core functionality,
// as those are two disjunct analysis outputs, which do not depend on each other. (aleks Sep'24)

RunFreightAnalysisEventBased analysisEventBased = new RunFreightAnalysisEventBased(
Path.of(IOUtils.extendUrl(SCENARIO_URL, "grid9x9.xml" ).toURI()).toAbsolutePath(),
Path.of(testUtils.getInputDirectory() + "in/output_allVehicles.xml"),
Path.of(IOUtils.extendUrl(SCENARIO_URL, "singleCarrierFiveActivities.xml" ).toURI()),
Path.of(IOUtils.extendUrl(SCENARIO_URL, "vehicleTypes.xml" ).toURI()),
Path.of(testUtils.getInputDirectory() + "in/serviceBasedEvents.xml"),
Path.of(testUtils.getOutputDirectory()),
IOUtils.extendUrl(SCENARIO_URL, "grid9x9.xml" ).toString(),
testUtils.getInputDirectory() + "in/output_allVehicles.xml",
IOUtils.extendUrl(SCENARIO_URL, "singleCarrierFiveActivities.xml" ).toString(),
IOUtils.extendUrl(SCENARIO_URL, "vehicleTypes.xml" ).toString(),
testUtils.getInputDirectory() + "in/serviceBasedEvents.xml",
testUtils.getOutputDirectory(),
null);
analysisEventBased.runCompleteAnalysis();

Expand All @@ -61,14 +58,14 @@ void runServiceEventTest() throws IOException, URISyntaxException {
}

@Test
void runShipmentEventTest() throws IOException, URISyntaxException {
void runShipmentEventTest() throws IOException {
RunFreightAnalysisEventBased analysisEventBased = new RunFreightAnalysisEventBased(
Path.of(IOUtils.extendUrl(SCENARIO_URL, "grid9x9.xml" ).toURI()),
Path.of(testUtils.getInputDirectory() + "in/carrierVehicles.xml"),
Path.of(IOUtils.extendUrl(SCENARIO_URL, "singleCarrierFiveActivities_Shipments.xml" ).toURI()),
Path.of(IOUtils.extendUrl(SCENARIO_URL, "vehicleTypes.xml" ).toURI()),
Path.of(testUtils.getInputDirectory() + "in/shipmentBasedEvents.xml"),
Path.of(testUtils.getOutputDirectory()),
IOUtils.extendUrl(SCENARIO_URL, "grid9x9.xml" ).toString(),
testUtils.getInputDirectory() + "in/carrierVehicles.xml",
IOUtils.extendUrl(SCENARIO_URL, "singleCarrierFiveActivities_Shipments.xml" ).toString(),
IOUtils.extendUrl(SCENARIO_URL, "vehicleTypes.xml" ).toString(),
testUtils.getInputDirectory() + "in/shipmentBasedEvents.xml",
testUtils.getOutputDirectory(),
null);
analysisEventBased.runCompleteAnalysis();

Expand Down

0 comments on commit 7881ec1

Please sign in to comment.