diff --git a/contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/router/DvrpModeRoutingNetworkModule.java b/contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/router/DvrpModeRoutingNetworkModule.java index 3b8463a1119..826f60b03bc 100644 --- a/contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/router/DvrpModeRoutingNetworkModule.java +++ b/contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/router/DvrpModeRoutingNetworkModule.java @@ -21,6 +21,7 @@ package org.matsim.contrib.dvrp.router; import java.io.File; +import java.net.URL; import java.util.Collections; import java.util.Set; @@ -94,7 +95,7 @@ public void install() { return FreeSpeedTravelTimeMatrix.createFreeSpeedMatrix(network, zoneSystem, matrixParams, globalConfigGroup.getNumberOfThreads(), qSimConfigGroup.getTimeStepSize()); } else { - File cachePath = new File(ConfigGroup.getInputFileURL(getConfig().getContext(), matrixParams.cachePath).getPath()); + URL cachePath = ConfigGroup.getInputFileURL(getConfig().getContext(), matrixParams.cachePath); return FreeSpeedTravelTimeMatrix.createFreeSpeedMatrixFromCache(network, zoneSystem, matrixParams, globalConfigGroup.getNumberOfThreads(), qSimConfigGroup.getTimeStepSize(), cachePath); } diff --git a/contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/run/DvrpModule.java b/contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/run/DvrpModule.java index 769ea423515..a74c7c7aab0 100644 --- a/contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/run/DvrpModule.java +++ b/contribs/dvrp/src/main/java/org/matsim/contrib/dvrp/run/DvrpModule.java @@ -19,7 +19,7 @@ package org.matsim.contrib.dvrp.run; -import java.io.File; +import java.net.URL; import org.matsim.api.core.v01.network.Network; import org.matsim.contrib.common.zones.ZoneSystem; @@ -98,7 +98,7 @@ public TravelTimeMatrix get() { return FreeSpeedTravelTimeMatrix.createFreeSpeedMatrix(network, zoneSystem, params, numberOfThreads, qSimConfigGroup.getTimeStepSize()); } else { - File cachePath = new File(ConfigGroup.getInputFileURL(getConfig().getContext(), params.cachePath).getPath()); + URL cachePath = ConfigGroup.getInputFileURL(getConfig().getContext(), params.cachePath); return FreeSpeedTravelTimeMatrix.createFreeSpeedMatrixFromCache(network, zoneSystem, params, numberOfThreads, qSimConfigGroup.getTimeStepSize(), cachePath); } diff --git a/contribs/dvrp/src/main/java/org/matsim/contrib/zone/skims/FreeSpeedTravelTimeMatrix.java b/contribs/dvrp/src/main/java/org/matsim/contrib/zone/skims/FreeSpeedTravelTimeMatrix.java index 356625f4295..a060bc1adc3 100644 --- a/contribs/dvrp/src/main/java/org/matsim/contrib/zone/skims/FreeSpeedTravelTimeMatrix.java +++ b/contribs/dvrp/src/main/java/org/matsim/contrib/zone/skims/FreeSpeedTravelTimeMatrix.java @@ -20,18 +20,15 @@ package org.matsim.contrib.zone.skims; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.UncheckedIOException; +import java.io.*; +import java.net.URL; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Objects; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.matsim.api.core.v01.Id; import org.matsim.api.core.v01.IdSet; import org.matsim.api.core.v01.network.Network; @@ -43,15 +40,20 @@ import org.matsim.contrib.dvrp.trafficmonitoring.QSimFreeSpeedTravelTime; import org.matsim.contrib.zone.skims.SparseMatrix.NodeAndTime; import org.matsim.contrib.zone.skims.SparseMatrix.SparseRow; +import org.matsim.core.config.ConfigGroup; import org.matsim.core.router.util.TravelTime; import com.google.common.base.Verify; import com.google.common.collect.Sets; +import org.matsim.core.utils.io.IOUtils; /** * @author Michal Maciejewski (michalm) */ public class FreeSpeedTravelTimeMatrix implements TravelTimeMatrix { + + private final static Logger log = LogManager.getLogger(FreeSpeedTravelTimeMatrix.class); + public static FreeSpeedTravelTimeMatrix createFreeSpeedMatrix(Network dvrpNetwork, ZoneSystem zoneSystem, DvrpTravelTimeMatrixParams params, int numberOfThreads, double qSimTimeStepSize) { return new FreeSpeedTravelTimeMatrix(dvrpNetwork, zoneSystem, params, numberOfThreads, new QSimFreeSpeedTravelTime(qSimTimeStepSize)); @@ -89,24 +91,25 @@ public int getZonalTravelTime(Node fromNode, Node toNode, double departureTime) return freeSpeedTravelTimeMatrix.get(zoneSystem.getZoneForNodeId(fromNode.getId()).orElseThrow(), zoneSystem.getZoneForNodeId(toNode.getId()).orElseThrow()); } - public static FreeSpeedTravelTimeMatrix createFreeSpeedMatrixFromCache(Network dvrpNetwork, ZoneSystem zoneSystem, DvrpTravelTimeMatrixParams params, int numberOfThreads, double qSimTimeStepSize, File cachePath) { - boolean exists = cachePath.exists(); - - final FreeSpeedTravelTimeMatrix matrix; - if (exists) { - matrix = new FreeSpeedTravelTimeMatrix(dvrpNetwork, zoneSystem, cachePath); - } else { - matrix = createFreeSpeedMatrix(dvrpNetwork, zoneSystem, params, numberOfThreads, qSimTimeStepSize); - matrix.write(cachePath, dvrpNetwork); - } - + public static FreeSpeedTravelTimeMatrix createFreeSpeedMatrixFromCache(Network dvrpNetwork, ZoneSystem zoneSystem, DvrpTravelTimeMatrixParams params, int numberOfThreads, double qSimTimeStepSize, URL cachePath) { + + FreeSpeedTravelTimeMatrix matrix; + if (cachePath != null) { + try { + return new FreeSpeedTravelTimeMatrix(dvrpNetwork, zoneSystem, cachePath); + } catch (FileNotFoundException e) { + log.warn("Freespeed matrix cache file not found, will use as output path for on-the-fly creation."); + } + } + matrix = createFreeSpeedMatrix(dvrpNetwork, zoneSystem, params, numberOfThreads, qSimTimeStepSize); + matrix.write(cachePath, dvrpNetwork); return matrix; } - public FreeSpeedTravelTimeMatrix(Network dvrpNetwork, ZoneSystem zoneSystem, File cachePath) { + public FreeSpeedTravelTimeMatrix(Network dvrpNetwork, ZoneSystem zoneSystem, URL cachePath) throws FileNotFoundException { this.zoneSystem = zoneSystem; - try (DataInputStream inputStream = new DataInputStream(new FileInputStream(cachePath))) { + try (DataInputStream inputStream = new DataInputStream(IOUtils.getInputStream(cachePath))) { // number of zones int numberOfZones = inputStream.readInt(); Verify.verify(numberOfZones == zoneSystem.getZones().size()); @@ -175,13 +178,15 @@ public FreeSpeedTravelTimeMatrix(Network dvrpNetwork, ZoneSystem zoneSystem, Fil } } } + } catch (FileNotFoundException e) { + throw e; } catch (IOException e) { throw new UncheckedIOException(e); } - } + } - public void write(File outputPath, Network dvrpNetwork) { - try (DataOutputStream outputStream = new DataOutputStream(new FileOutputStream(outputPath))) { + public void write(URL outputPath, Network dvrpNetwork) { + try (DataOutputStream outputStream = new DataOutputStream(IOUtils.getOutputStream(outputPath, false))) { // obtain fixed order of zones List zones = new ArrayList<>(zoneSystem.getZones().values()); outputStream.writeInt(zones.size()); diff --git a/contribs/dvrp/src/test/java/org/matsim/contrib/zone/skims/FreeSpeedTravelTimeMatrixTest.java b/contribs/dvrp/src/test/java/org/matsim/contrib/zone/skims/FreeSpeedTravelTimeMatrixTest.java index 2cf4cc30934..752476e3e5a 100644 --- a/contribs/dvrp/src/test/java/org/matsim/contrib/zone/skims/FreeSpeedTravelTimeMatrixTest.java +++ b/contribs/dvrp/src/test/java/org/matsim/contrib/zone/skims/FreeSpeedTravelTimeMatrixTest.java @@ -23,6 +23,8 @@ import static org.assertj.core.api.Assertions.assertThat; import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; @@ -56,7 +58,7 @@ public FreeSpeedTravelTimeMatrixTest() { } @Test - void matrix() { + void matrix() throws MalformedURLException { DvrpTravelTimeMatrixParams params = new DvrpTravelTimeMatrixParams(); params.maxNeighborDistance = 0; ZoneSystem zoneSystem = new SquareGridZoneSystem(network, 100.); @@ -75,7 +77,7 @@ void matrix() { assertThat(matrix.getTravelTime(nodeC, nodeB, 0)).isEqualTo(10 + 1); // 1 s for moving over nodes // write and read cache - File cachePath = new File(utils.getOutputDirectory(), "cache.bin"); + URL cachePath = new File(utils.getOutputDirectory(), "cache.bin").toURI().toURL(); matrix.write(cachePath, network); matrix = FreeSpeedTravelTimeMatrix.createFreeSpeedMatrixFromCache(network, zoneSystem, null, 1, 1, cachePath); @@ -93,7 +95,7 @@ void matrix() { } @Test - void sparseMatrix() { + void sparseMatrix() throws MalformedURLException { DvrpTravelTimeMatrixParams params = new DvrpTravelTimeMatrixParams(); params.maxNeighborDistance = 9999; @@ -113,7 +115,7 @@ void sparseMatrix() { assertThat(matrix.getTravelTime(nodeC, nodeB, 0)).isEqualTo(10 + 9 + 2); // 2 s for moving over nodes // write and read cache - File cachePath = new File(utils.getOutputDirectory(), "cache.bin"); + URL cachePath = new File(utils.getOutputDirectory(), "cache.bin").toURI().toURL(); matrix.write(cachePath, network); matrix = FreeSpeedTravelTimeMatrix.createFreeSpeedMatrixFromCache(network, zoneSystem, null, 1, 1, cachePath);