Skip to content

Commit

Permalink
refactor CleanPopulation (#3496)
Browse files Browse the repository at this point in the history
* refactor CleanPopulation

* implement as PersonAlgorithm

* make trips2Legs private final

---------

Co-authored-by: rakow <[email protected]>
  • Loading branch information
simei94 and rakow authored Sep 30, 2024
1 parent 61e9eb3 commit c19c81b
Showing 1 changed file with 57 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.matsim.api.core.v01.population.*;
import org.matsim.application.MATSimAppCommand;
import org.matsim.core.population.PopulationUtils;
import org.matsim.core.population.algorithms.PersonAlgorithm;
import org.matsim.core.population.algorithms.TripsToLegsAlgorithm;
import org.matsim.core.router.RoutingModeMainModeIdentifier;
import picocli.CommandLine;
Expand All @@ -24,7 +25,7 @@
mixinStandardHelpOptions = true,
showDefaultValues = true
)
public class CleanPopulation implements MATSimAppCommand {
public class CleanPopulation implements MATSimAppCommand, PersonAlgorithm {

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

Expand All @@ -46,6 +47,10 @@ public class CleanPopulation implements MATSimAppCommand {
@CommandLine.Option(names = "--output", description = "Output file name", required = true)
private Path output;

// Using the analysis main mode identifier instead of the routing mode based one on purpose
// to be able to process older population files without any routing modes!
private final TripsToLegsAlgorithm trips2Legs = new TripsToLegsAlgorithm(new RoutingModeMainModeIdentifier());

public static void main(String[] args) {
System.exit(new CommandLine(new CleanPopulation()).execute(args));
}
Expand All @@ -63,43 +68,64 @@ public Integer call() throws Exception {
if (output.getParent() != null)
Files.createDirectories(output.getParent());

// Using the analysis main mode identifier instead of the routing mode based one on purpose
// to be able to process older population files without any routing modes!
TripsToLegsAlgorithm trips2Legs = new TripsToLegsAlgorithm(new RoutingModeMainModeIdentifier());

for (Person person : population.getPersons().values()) {
run(person);
}

PopulationUtils.writePopulation(population, output.toString());

return 0;
}

@Override
public void run(Person person) {
if (rmUnselected) {
removeUnselectedPlans(person);
}

if (rmUnselected) {
Plan selected = person.getSelectedPlan();
for (Plan plan : Lists.newArrayList(person.getPlans())) {
if (plan != selected)
person.removePlan(plan);
for (Plan plan : person.getPlans()) {
if (tripsToLegs)
trips2Legs.run(plan);

for (PlanElement el : plan.getPlanElements()) {
if (rmRoutes) {
removeRouteFromLeg(el);
}
}

for (Plan plan : person.getPlans()) {
if (tripsToLegs)
trips2Legs.run(plan);

for (PlanElement el : plan.getPlanElements()) {
if (rmRoutes) {
if (el instanceof Leg) {
((Leg) el).setRoute(null);
}
}

if (rmActivityLocations) {
if (el instanceof Activity) {
((Activity) el).setLinkId(null);
((Activity) el).setFacilityId(null);
}
}
if (rmActivityLocations) {
removeActivityLocation(el);
}
}
}
}

PopulationUtils.writePopulation(population, output.toString());
/**
* Remove link and facility information from activity.
*/
public static void removeActivityLocation(PlanElement el) {
if (el instanceof Activity act) {
act.setLinkId(null);
act.setFacilityId(null);
}
}

return 0;
/**
* Remove route information from leg.
*/
public static void removeRouteFromLeg(PlanElement el) {
if (el instanceof Leg leg) {
leg.setRoute(null);
}
}

/**
* Remove all unselected plans for given person.
*/
public static void removeUnselectedPlans(Person person) {
Plan selected = person.getSelectedPlan();
for (Plan plan : Lists.newArrayList(person.getPlans())) {
if (plan != selected)
person.removePlan(plan);
}
}
}
}

0 comments on commit c19c81b

Please sign in to comment.