Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable and require Java code formatting #2963

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
2 changes: 1 addition & 1 deletion .github/workflows/verify-push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ jobs:

- name: Build module (with dependencies)
if: ${{matrix.module != 'matsim' || steps.detect-changes.outputs.outside-contribs == 'true'}}
run: mvn install --batch-mode --also-make --projects ${{matrix.module}} -DskipTests -Dsource.skip
run: mvn install --batch-mode --also-make --projects ${{matrix.module}} -DskipTests -Dsource.skip -Dfmt.skip

- name: Test module
if: ${{matrix.module != 'matsim' || steps.detect-changes.outputs.outside-contribs == 'true'}}
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,13 @@ An example how to get started with your own project, including a complete pom.xm

Code examples are at https://github.com/matsim-org/matsim-code-examples.

## Code formatting

We use [fmt-maven-plugin](https://github.com/spotify/fmt-maven-plugin) for formatting Java code according to [Google Java Style](https://google.github.io/styleguide/javaguide.html).
This maven plugin internally uses [google-java-format](https://github.com/google/google-java-format) to format code or check if it has been properly formatted.

To format code, run `mvn com.spotify.fmt:fmt-maven-plugin:format`.
For ease of coding, we suggest configuring the code formatting settings in your IDE (see instructions for [IntelliJ](https://github.com/google/google-java-format#intellij-android-studio-and-other-jetbrains-ides)
and [Eclipse](https://github.com/google/google-java-format#eclipse)).

Java code formatting is checked in CI. Passing this check is required to be able to merge a PR to protected branches.
18 changes: 9 additions & 9 deletions benchmark/src/main/java/org/matsim/benchmark/Benchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@

package org.matsim.benchmark;

import java.io.IOException;
import org.matsim.core.config.Config;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.controler.Controler;
import org.matsim.core.utils.io.IOUtils;
import org.matsim.examples.ExamplesUtils;

import java.io.IOException;

public class Benchmark {

public static void main(String[] args) throws IOException {
Config config = ConfigUtils.loadConfig(IOUtils.extendUrl(ExamplesUtils.getTestScenarioURL("benchmark"), "config.xml"));
Controler ctl = new Controler(config);
ctl.getConfig().controller().setCreateGraphs(false);
ctl.run();
}

public static void main(String[] args) throws IOException {
Config config =
ConfigUtils.loadConfig(
IOUtils.extendUrl(ExamplesUtils.getTestScenarioURL("benchmark"), "config.xml"));
Controler ctl = new Controler(config);
ctl.getConfig().controller().setCreateGraphs(false);
ctl.run();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.Coord;
Expand All @@ -37,71 +36,75 @@
* @author dziemke
*/
class AccessibilityAggregator implements FacilityDataExchangeInterface {
private final Logger LOG = LogManager.getLogger(AccessibilityAggregator.class);
private final Logger LOG = LogManager.getLogger(AccessibilityAggregator.class);

private Map<Tuple<ActivityFacility, Double>, Map<String,Double>> accessibilitiesMap = new ConcurrentHashMap<>();
private Map<Tuple<ActivityFacility, Double>, Map<String, Double>> accessibilitiesMap =
new ConcurrentHashMap<>();

@Override
public void setFacilityAccessibilities(ActivityFacility measurePoint, Double timeOfDay, String mode, double accessibility) {
Tuple<ActivityFacility, Double> key = new Tuple<>(measurePoint, timeOfDay);
if (!accessibilitiesMap.containsKey(key)) {
Map<String,Double> accessibilitiesByMode = new HashMap<>();
accessibilitiesMap.put(key, accessibilitiesByMode);
}
accessibilitiesMap.get(key).put(mode, accessibility);
}
@Override
public void setFacilityAccessibilities(
ActivityFacility measurePoint, Double timeOfDay, String mode, double accessibility) {
Tuple<ActivityFacility, Double> key = new Tuple<>(measurePoint, timeOfDay);
if (!accessibilitiesMap.containsKey(key)) {
Map<String, Double> accessibilitiesByMode = new HashMap<>();
accessibilitiesMap.put(key, accessibilitiesByMode);
}
accessibilitiesMap.get(key).put(mode, accessibility);
}

@Override
public void finish() {
}
@Override
public void finish() {}

public Map<Tuple<ActivityFacility, Double>, Map<String,Double>> getAccessibilitiesMap() {
Map<Tuple<ActivityFacility, Double>, Map<String, Double>> accessibilitiesMap2 = sortMeasurePointsByYAndXCoord();
return accessibilitiesMap2;
}
public Map<Tuple<ActivityFacility, Double>, Map<String, Double>> getAccessibilitiesMap() {
Map<Tuple<ActivityFacility, Double>, Map<String, Double>> accessibilitiesMap2 =
sortMeasurePointsByYAndXCoord();
return accessibilitiesMap2;
}

private Map<Tuple<ActivityFacility, Double>, Map<String, Double>> sortMeasurePointsByYAndXCoord() {
LOG.info("Start sorting measure points.");
Map<Double, List<Double>> coordMap = new TreeMap<>();
List<Double> yValues = new LinkedList<>();
private Map<Tuple<ActivityFacility, Double>, Map<String, Double>>
sortMeasurePointsByYAndXCoord() {
LOG.info("Start sorting measure points.");
Map<Double, List<Double>> coordMap = new TreeMap<>();
List<Double> yValues = new LinkedList<>();

for (Tuple<ActivityFacility, Double> tuple : accessibilitiesMap.keySet()) {
ActivityFacility activityFacility = tuple.getFirst();
double y = activityFacility.getCoord().getY();
if (!yValues.contains(y)) {
yValues.add(y);
}
}
yValues.sort(Comparator.naturalOrder());
for (Tuple<ActivityFacility, Double> tuple : accessibilitiesMap.keySet()) {
ActivityFacility activityFacility = tuple.getFirst();
double y = activityFacility.getCoord().getY();
if (!yValues.contains(y)) {
yValues.add(y);
}
}
yValues.sort(Comparator.naturalOrder());

for (double yGiven : yValues) {
List<Double> xValues = new LinkedList<>();
for (Tuple<ActivityFacility, Double> tuple : accessibilitiesMap.keySet()) {
ActivityFacility activityFacility = tuple.getFirst();
double y = activityFacility.getCoord().getY();
if (y == yGiven) {
double x = activityFacility.getCoord().getX();
if (!xValues.contains(x)) {
xValues.add(x);
}
}
}
xValues.sort(Comparator.naturalOrder());
coordMap.put(yGiven, xValues);
}
for (double yGiven : yValues) {
List<Double> xValues = new LinkedList<>();
for (Tuple<ActivityFacility, Double> tuple : accessibilitiesMap.keySet()) {
ActivityFacility activityFacility = tuple.getFirst();
double y = activityFacility.getCoord().getY();
if (y == yGiven) {
double x = activityFacility.getCoord().getX();
if (!xValues.contains(x)) {
xValues.add(x);
}
}
}
xValues.sort(Comparator.naturalOrder());
coordMap.put(yGiven, xValues);
}

Map<Tuple<ActivityFacility, Double>, Map<String,Double>> accessibilitiesMap2 = new LinkedHashMap<>();
for (double y : coordMap.keySet()) {
for (double x : coordMap.get(y)) {
for (Tuple<ActivityFacility, Double> tuple : accessibilitiesMap.keySet()) {
Coord coord = tuple.getFirst().getCoord();
if (coord.getX() == x && coord.getY() == y) {
accessibilitiesMap2.put(tuple, accessibilitiesMap.get(tuple));
}
}
}
}
LOG.info("Finish sorting measure points.");
return accessibilitiesMap2;
}
Map<Tuple<ActivityFacility, Double>, Map<String, Double>> accessibilitiesMap2 =
new LinkedHashMap<>();
for (double y : coordMap.keySet()) {
for (double x : coordMap.get(y)) {
for (Tuple<ActivityFacility, Double> tuple : accessibilitiesMap.keySet()) {
Coord coord = tuple.getFirst().getCoord();
if (coord.getX() == x && coord.getY() == y) {
accessibilitiesMap2.put(tuple, accessibilitiesMap.get(tuple));
}
}
}
}
LOG.info("Finish sorting measure points.");
return accessibilitiesMap2;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import java.util.Arrays;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.locationtech.jts.geom.Envelope;
Expand All @@ -38,75 +37,91 @@
* @author dziemke
*/
class AccessibilityComputationNairobiLandUseLocalCopy {
public static final Logger LOG = LogManager.getLogger(AccessibilityComputationNairobiLandUseLocalCopy.class);

public static void main(String[] args) {
int tileSize_m = 500;
boolean push2Geoserver = false; // Set true for run on server
boolean createQGisOutput = true; // Set false for run on server

final Config config = ConfigUtils.createConfig(new AccessibilityConfigGroup());

Envelope envelope = new Envelope(246000, 271000, 9853000, 9863000); // Central part of Nairobi
String scenarioCRS = "EPSG:21037"; // EPSG:21037 = Arc 1960 / UTM zone 37S, for Nairobi, Kenya

config.network().setInputFile("../nairobi/data/nairobi/input/2015-10-15_network.xml");
config.facilities().setInputFile("../nairobi/data/land_use/Nairobi_LU_2010/facilities.xml");
String runId = "ke_nairobi_landuse_hexagons_" + tileSize_m;
config.controller().setOutputDirectory("../nairobi/data/nairobi/output/" + runId + "_lcpt_par4_car_tr-7_500/");
config.controller().setRunId(runId);

config.controller().setOverwriteFileSetting(OverwriteFileSetting.deleteDirectoryIfExists);
config.controller().setLastIteration(0);

AccessibilityConfigGroup acg = ConfigUtils.addOrGetModule(config, AccessibilityConfigGroup.class);
acg.setAreaOfAccessibilityComputation(AreaOfAccesssibilityComputation.fromBoundingBoxHexagons);
acg.setEnvelope(envelope);
acg.setTileSize_m(tileSize_m);
acg.setComputingAccessibilityForMode(Modes4Accessibility.freespeed, false);
acg.setComputingAccessibilityForMode(Modes4Accessibility.car, true);
//acg.setComputingAccessibilityForMode(Modes4Accessibility.bike, true);
acg.setOutputCrs(scenarioCRS);

//acg.setUseParallelization(false);

ConfigUtils.setVspDefaults(config);

final Scenario scenario = ScenarioUtils.loadScenario(config);

// final List<String> activityTypes = Arrays.asList(new String[]{"educational", "commercial", "industrial", "recreational", "water_body"});
final List<String> activityTypes = Arrays.asList(new String[]{"educational"});
final ActivityFacilities densityFacilities = AccessibilityUtils.createFacilityForEachLink(Labels.DENSITIY, scenario.getNetwork());

final Controler controler = new Controler(scenario);

for (String activityType : activityTypes) {
AccessibilityModule module = new AccessibilityModule();
module.setConsideredActivityType(activityType);
module.addAdditionalFacilityData(densityFacilities);
module.setPushing2Geoserver(push2Geoserver);
module.setCreateQGisOutput(createQGisOutput);
controler.addOverridingModule(module);
}

controler.run();

if (createQGisOutput) {
final Integer range = 9; // In the current implementation, this must always be 9
final Double lowerBound = -3.5; // (upperBound - lowerBound) ideally nicely divisible by (range - 2)
final Double upperBound = 3.5;
final int populationThreshold = (int) (10 / (1000/tileSize_m * 1000/tileSize_m)); // People per km^2 or roads (?)

String osName = System.getProperty("os.name");
String workingDirectory = config.controller().getOutputDirectory();
for (String actType : activityTypes) {
String actSpecificWorkingDirectory = workingDirectory + actType + "/";
for (Modes4Accessibility mode : acg.getIsComputingMode()) {
VisualizationUtils.createQGisOutputRuleBasedStandardColorRange(actType, mode.toString(), envelope, workingDirectory,
scenarioCRS, lowerBound, upperBound, range, populationThreshold);
VisualizationUtils.createSnapshot(actSpecificWorkingDirectory, mode.toString(), osName);
}
}
}
}
public static final Logger LOG =
LogManager.getLogger(AccessibilityComputationNairobiLandUseLocalCopy.class);

public static void main(String[] args) {
int tileSize_m = 500;
boolean push2Geoserver = false; // Set true for run on server
boolean createQGisOutput = true; // Set false for run on server

final Config config = ConfigUtils.createConfig(new AccessibilityConfigGroup());

Envelope envelope = new Envelope(246000, 271000, 9853000, 9863000); // Central part of Nairobi
String scenarioCRS = "EPSG:21037"; // EPSG:21037 = Arc 1960 / UTM zone 37S, for Nairobi, Kenya

config.network().setInputFile("../nairobi/data/nairobi/input/2015-10-15_network.xml");
config.facilities().setInputFile("../nairobi/data/land_use/Nairobi_LU_2010/facilities.xml");
String runId = "ke_nairobi_landuse_hexagons_" + tileSize_m;
config
.controller()
.setOutputDirectory("../nairobi/data/nairobi/output/" + runId + "_lcpt_par4_car_tr-7_500/");
config.controller().setRunId(runId);

config.controller().setOverwriteFileSetting(OverwriteFileSetting.deleteDirectoryIfExists);
config.controller().setLastIteration(0);

AccessibilityConfigGroup acg =
ConfigUtils.addOrGetModule(config, AccessibilityConfigGroup.class);
acg.setAreaOfAccessibilityComputation(AreaOfAccesssibilityComputation.fromBoundingBoxHexagons);
acg.setEnvelope(envelope);
acg.setTileSize_m(tileSize_m);
acg.setComputingAccessibilityForMode(Modes4Accessibility.freespeed, false);
acg.setComputingAccessibilityForMode(Modes4Accessibility.car, true);
// acg.setComputingAccessibilityForMode(Modes4Accessibility.bike, true);
acg.setOutputCrs(scenarioCRS);

// acg.setUseParallelization(false);

ConfigUtils.setVspDefaults(config);

final Scenario scenario = ScenarioUtils.loadScenario(config);

// final List<String> activityTypes = Arrays.asList(new String[]{"educational", "commercial",
// "industrial", "recreational", "water_body"});
final List<String> activityTypes = Arrays.asList(new String[] {"educational"});
final ActivityFacilities densityFacilities =
AccessibilityUtils.createFacilityForEachLink(Labels.DENSITIY, scenario.getNetwork());

final Controler controler = new Controler(scenario);

for (String activityType : activityTypes) {
AccessibilityModule module = new AccessibilityModule();
module.setConsideredActivityType(activityType);
module.addAdditionalFacilityData(densityFacilities);
module.setPushing2Geoserver(push2Geoserver);
module.setCreateQGisOutput(createQGisOutput);
controler.addOverridingModule(module);
}

controler.run();

if (createQGisOutput) {
final Integer range = 9; // In the current implementation, this must always be 9
final Double lowerBound =
-3.5; // (upperBound - lowerBound) ideally nicely divisible by (range - 2)
final Double upperBound = 3.5;
final int populationThreshold =
(int) (10 / (1000 / tileSize_m * 1000 / tileSize_m)); // People per km^2 or roads (?)

String osName = System.getProperty("os.name");
String workingDirectory = config.controller().getOutputDirectory();
for (String actType : activityTypes) {
String actSpecificWorkingDirectory = workingDirectory + actType + "/";
for (Modes4Accessibility mode : acg.getIsComputingMode()) {
VisualizationUtils.createQGisOutputRuleBasedStandardColorRange(
actType,
mode.toString(),
envelope,
workingDirectory,
scenarioCRS,
lowerBound,
upperBound,
range,
populationThreshold);
VisualizationUtils.createSnapshot(actSpecificWorkingDirectory, mode.toString(), osName);
}
}
}
}
}
Loading