Skip to content

Commit

Permalink
cleaning up the code and try to satisfy sonarcloud
Browse files Browse the repository at this point in the history
  • Loading branch information
tschlenther committed Feb 22, 2024
1 parent 80de108 commit a4dc4f4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 39 deletions.
7 changes: 1 addition & 6 deletions src/main/java/org/matsim/run/RunLeipzigScenario.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import playground.vsp.simpleParkingCostHandler.ParkingCostConfigGroup;

import javax.annotation.Nullable;
import java.net.URISyntaxException;
import java.util.*;

/**
Expand Down Expand Up @@ -187,11 +186,7 @@ protected Config prepareConfig(Config config) {

if (networkOpt.hasDrtArea()) {
//drt
try {
DrtCaseSetup.prepareConfig(config, /* drtCase, */ new ShpOptions(networkOpt.getDrtArea(), null, null));
} catch (URISyntaxException e) {
log.fatal(e);
}
DrtCaseSetup.prepareConfig(config, /* drtCase, */ new ShpOptions(networkOpt.getDrtArea(), null, null));
}

config.qsim().setUsingTravelTimeCheckInTeleportation(true);
Expand Down
39 changes: 17 additions & 22 deletions src/main/java/org/matsim/run/prepare/DrtCaseSetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@
public final class DrtCaseSetup {

private static final Logger log = LogManager.getLogger(DrtCaseSetup.class);
/*private static final ShpOptions flexaArea2021 = new ShpOptions(Path.of(
"input/v1.2/drtServiceArea/leipzig_flexa_service_area_2021.shp"),
null, null); */

private static final String errorMessage = "Unexpected value: ";

//this is not nice because the static set is only filled in prepareConfig
static Set<String> drtModes = new HashSet<>();
Expand All @@ -76,7 +71,7 @@ private DrtCaseSetup(){}
/**
* prepare config for drt simulation.
*/
public static void prepareConfig(Config config, ShpOptions drtAreas) throws URISyntaxException {
public static void prepareConfig(Config config, ShpOptions drtAreas) {

MultiModeDrtConfigGroup multiModeDrtConfigGroup = ConfigUtils.addOrGetModule(config, MultiModeDrtConfigGroup.class);
DvrpConfigGroup dvrpConfigGroup = ConfigUtils.addOrGetModule(config, DvrpConfigGroup.class);
Expand All @@ -94,19 +89,17 @@ public static void prepareConfig(Config config, ShpOptions drtAreas) throws URIS
drtFareParams.dailySubscriptionFee = 0.;


log.info("reading " + drtAreas.getShapeFile().toString());
log.info(String.format("reading %s", drtAreas.getShapeFile().toString()));
for (SimpleFeature feature : drtAreas.readFeatures()) {
// String name = (String) feature.getAttribute("Name");

String drtMode = String.valueOf(feature.getAttribute("mode"));
if (drtMode.equals("null")) {
throw new IllegalArgumentException("could not find 'mode' attribute in the given shape file at " + drtAreas.getShapeFile().toString());
throw new IllegalArgumentException(String.format("could not find 'mode' attribute in the given shape file at %s", drtAreas.getShapeFile().toString()));
} else {
drtModes.add(drtMode);
}
}



if (multiModeDrtConfigGroup.getModalElements().isEmpty()) {
for (String mode : drtModes){
createDrtModeConfigGroup(multiModeDrtConfigGroup, mode);
Expand Down Expand Up @@ -148,24 +141,24 @@ public static void prepareScenario(Scenario scenario, ShpOptions drtAreas, Strin
CreateDrtStopsFromNetwork drtStopsCreator = new CreateDrtStopsFromNetwork();
MultiModeDrtConfigGroup multiModeDrtConfigGroup = ConfigUtils.addOrGetModule(scenario.getConfig(), MultiModeDrtConfigGroup.class);

log.info("reading " + drtAreas.getShapeFile().toString());
log.info(String.format("reading %s", drtAreas.getShapeFile()));
for (SimpleFeature feature : drtAreas.readFeatures()) {
String drtMode = String.valueOf(feature.getAttribute("mode"));
if (drtMode.equals("null")) {
throw new IllegalArgumentException("could not find 'mode' attribute in the given shape file at " + drtAreas.getShapeFile().toString());
throw new IllegalArgumentException(String.format("could not find 'mode' attribute in the given shape file at %s", drtAreas.getShapeFile().toString()));
}
Integer noVehicles = (Integer) feature.getAttribute("noVehicles");
if (noVehicles.equals(null)){
throw new IllegalArgumentException("could not find 'noVehicles' attribute in the given shape file at " + drtAreas.getShapeFile().toString());
if (noVehicles == null){
throw new IllegalArgumentException(String.format("could not find 'noVehicles' attribute in the given shape file at %s", drtAreas.getShapeFile().toString()));
}

log.info("filtering network for mode " + drtMode + ". Before, the number of links equals " + scenario.getNetwork().getLinks().size());
log.info(String.format("filtering network for mode %s. Before, the number of links equals %d.", drtMode, scenario.getNetwork().getLinks().size()));
Network filteredNetwork = NetworkUtils.createNetwork();
TransportModeNetworkFilter filter = new TransportModeNetworkFilter(scenario.getNetwork());
filter.filter(filteredNetwork, Sets.newHashSet(drtMode));
log.info("filtered network contains " + filteredNetwork.getLinks().size() + " links");
log.info(String.format("filtered network contains %d links", filteredNetwork.getLinks().size()));

log.info("attempting to create " + noVehicles + " drt vehicles for mode " + drtMode);
log.info(String.format("attempting to create %s drt vehicles for mode ", drtMode));
new LeipzigDrtVehicleCreator().createDrtVehiclesForSingleArea(scenario.getVehicles(), filteredNetwork,
feature, noVehicles, drtMode);

Expand Down Expand Up @@ -309,10 +302,12 @@ private static void preparePtDrtIntermodality(Controler controler, ShpOptions sh

new PrepareTransitSchedule().prepareDrtIntermodality(controler.getScenario().getTransitSchedule(), shp, railwaysOnly);

ConfigUtils.addOrGetModule(controler.getConfig(), MultiModeDrtConfigGroup.class).getModalElements().stream().findFirst().ifPresent(drtConfigGroup ->
drtConfigGroup.getDrtFareParams().ifPresent(drtFareParams ->
//this only works if prepareConfig was called with the same ShpOptions
prepareDrtFareCompensation(controler, drtModes, drtFareParams.baseFare)));
ConfigUtils.addOrGetModule(controler.getConfig(), MultiModeDrtConfigGroup.class).getModalElements().stream()
.findFirst()
.flatMap(DrtConfigGroup::getDrtFareParams)
.ifPresent(drtFareParams ->
//this only works if prepareConfig was called with the same ShpOptions
prepareDrtFareCompensation(controler, drtModes, drtFareParams.baseFare));
}

private static void prepareDrtFareCompensation(Controler controler, Set<String> nonPtModes, Double ptBaseFare) {
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/org/matsim/run/prepare/NetworkOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ public class NetworkOptions {

@CommandLine.Option(names = "--drt-area", description = "Path to SHP file specifying where DRT mode is allowed")
private Path drtArea;
//modes now have to be provided in the drt-area shape file, per 'mode' attribute with
// @CommandLine.Option(names = "--drt-modes", description = "List of modes to add. Use comma as delimiter", defaultValue = TransportMode.drt)
// private String drtModes;
@CommandLine.Option(names = "--car-free-area", description = "Path to SHP file specifying car-free area")
private Path carFreeArea;
@CommandLine.Option(names = "--car-free-modes", description = "List of modes to remove. Use comma as delimiter", defaultValue = TransportMode.car)
Expand Down
14 changes: 6 additions & 8 deletions src/test/java/org/matsim/run/RunLeipzigIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.matsim.run;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.matsim.analysis.ParkingLocation;
Expand All @@ -22,15 +20,15 @@
import java.nio.file.Path;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

public class RunLeipzigIntegrationTest {


private static final String URL = String.format("https://svn.vsp.tu-berlin.de/repos/public-svn/matsim/scenarios/countries/de/leipzig/leipzig-v%s/input/",
RunLeipzigScenario.VERSION);
private static final String stadtShp = String.format("input/v1.3/drtServiceArea/Leipzig_stadt.shp", RunLeipzigScenario.VERSION);
private static final String flexaShp = String.format("input/v1.3/drtServiceArea/leipzig_flexa_service_area_2021.shp", RunLeipzigScenario.VERSION);
private static final String stadtShp = String.format("input/v%s/drtServiceArea/Leipzig_stadt.shp", RunLeipzigScenario.VERSION);
private static final String flexaShp = String.format("input/v%s/drtServiceArea/leipzig_flexa_service_area_2021.shp", RunLeipzigScenario.VERSION);

@Rule
public MatsimTestUtils utils = new MatsimTestUtils();
Expand Down Expand Up @@ -124,12 +122,12 @@ public final void runDrtTest() {
, "--post-processing", "disabled");

Network network = NetworkUtils.readNetwork(output + "/" + config.controler().getRunId() + ".output_network.xml.gz");
assertTrue(! network.getLinks().get(Id.createLinkId("24232899")).getAllowedModes().contains("drtNorth"));
assertTrue(! network.getLinks().get(Id.createLinkId("24232899")).getAllowedModes().contains("drtSoutheast"));
assertFalse(network.getLinks().get(Id.createLinkId("24232899")).getAllowedModes().contains("drtNorth"));
assertFalse(network.getLinks().get(Id.createLinkId("24232899")).getAllowedModes().contains("drtSoutheast"));
assertTrue(network.getLinks().get(Id.createLinkId("307899688#1")).getAllowedModes().contains("drtNorth"));
assertTrue(network.getLinks().get(Id.createLinkId("26588307#0")).getAllowedModes().contains("drtSoutheast"));

assertTrue(MultiModeDrtConfigGroup.get(config)!= null);
assertNotNull(MultiModeDrtConfigGroup.get(config));

//TODO add more tests, drt trips, etc.
}
Expand Down

0 comments on commit a4dc4f4

Please sign in to comment.