Skip to content

Commit

Permalink
First tests in org.eqasim.core (#181)
Browse files Browse the repository at this point in the history
* Feat: RemovePersonsWithActivityTypes

* fix: properly reading PublicTransitEvent objects from events file during analysis

Now adding a CustomEventMapper to the event readers that converts GenericEvent objects to PublicTransitEvent ones. This is done by the new convert method in the latter class.

* test: TestSimulationPipeline for various functionalities

Scope of the test:
- Simulation of a small scenario (added in this commit) with default Eqasim behaviour
- Shapefile export tools for transit schedules, transit stops and networks
- Run scripts in eqasim.core.analysis.run using output events

* fix: throwing exception instead of return null in PublicTransitEvent.convert

* fix: Renamed runCorsicaSimulation method to runMelunSimulation

---------
  • Loading branch information
tkchouaki authored Dec 15, 2023
1 parent bf6de83 commit b1331d7
Show file tree
Hide file tree
Showing 12 changed files with 1,065 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Collection;

import org.eqasim.core.components.transit.events.PublicTransitEvent;
import org.matsim.core.api.experimental.events.EventsManager;
import org.matsim.core.events.EventsUtils;
import org.matsim.core.events.MatsimEventsReader;
Expand All @@ -16,7 +17,9 @@ public LegReaderFromEvents(LegListener legListener) {
public Collection<LegItem> readLegs(String eventsPath) {
EventsManager eventsManager = EventsUtils.createEventsManager();
eventsManager.addHandler(legListener);
new MatsimEventsReader(eventsManager).readFile(eventsPath);
MatsimEventsReader matsimEventsReader = new MatsimEventsReader(eventsManager);
matsimEventsReader.addCustomEventMapper(PublicTransitEvent.TYPE, PublicTransitEvent::convert);
matsimEventsReader.readFile(eventsPath);
return legListener.getLegItems();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Collection;

import org.eqasim.core.components.transit.events.PublicTransitEvent;
import org.matsim.core.api.experimental.events.EventsManager;
import org.matsim.core.events.EventsUtils;
import org.matsim.core.events.MatsimEventsReader;
Expand All @@ -16,7 +17,9 @@ public TripReaderFromEvents(TripListener tripListener) {
public Collection<TripItem> readTrips(String eventsPath) {
EventsManager eventsManager = EventsUtils.createEventsManager();
eventsManager.addHandler(tripListener);
new MatsimEventsReader(eventsManager).readFile(eventsPath);
MatsimEventsReader matsimEventsReader = new MatsimEventsReader(eventsManager);
matsimEventsReader.addCustomEventMapper(PublicTransitEvent.TYPE, PublicTransitEvent::convert);
matsimEventsReader.readFile(eventsPath);
return tripListener.getTripItems();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,20 @@ public Map<String, String> getAttributes() {
attributes.put("travelDistance", String.valueOf(travelDistance));
return attributes;
}

public static PublicTransitEvent convert(GenericEvent genericEvent) {
if(!TYPE.equals(genericEvent.getEventType())) {
throw new IllegalStateException(String.format("Can't convert genericEvent of type '%s' to a PublicTransitEvent, a '%s' event type is needed", genericEvent.getAttributes(), TYPE));
}
Map<String, String> attributes = genericEvent.getAttributes();
Id<Person> personId = Id.createPersonId(attributes.get("person"));
Id<TransitLine> transitLineId = Id.create(attributes.get("line"), TransitLine.class);
Id<TransitRoute> transitRouteId = Id.create(attributes.get("route"), TransitRoute.class);
Id<TransitStopFacility> accessStopId = Id.create(attributes.get("accessStop"), TransitStopFacility.class);
Id<TransitStopFacility> egressStopId = Id.create(attributes.get("egressStop"), TransitStopFacility.class);
double vehicleDepartureTime = Double.parseDouble(attributes.get("vehicleDepartureTime"));
double travelDistance = Double.parseDouble(attributes.get("travelDistance"));

return new PublicTransitEvent(genericEvent.getTime(), personId, transitLineId, transitRouteId, accessStopId, egressStopId, vehicleDepartureTime, travelDistance);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.eqasim.core.tools;

import org.apache.log4j.Logger;
import org.matsim.api.core.v01.Identifiable;
import org.matsim.api.core.v01.Scenario;
import org.matsim.api.core.v01.population.Activity;
import org.matsim.api.core.v01.population.PopulationWriter;
import org.matsim.core.config.CommandLine;
import org.matsim.core.config.ConfigUtils;
import org.matsim.core.population.io.PopulationReader;
import org.matsim.core.scenario.ScenarioUtils;

import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

public class RemovePersonsWithActivityTypes {

private static final Logger logger = Logger.getLogger(RemovePersonsWithActivityTypes.class);

public static void main(String[] args) throws CommandLine.ConfigurationException {
CommandLine commandLine = new CommandLine.Builder(args).requireOptions("input-path", "output-path", "activity-types").build();

Set<String> activityTypes = Arrays.stream(commandLine.getOptionStrict("activity-types").split(",")).map(String::trim).collect(Collectors.toSet());

Scenario scenario = ScenarioUtils.createScenario(ConfigUtils.createConfig());
new PopulationReader(scenario).readFile(commandLine.getOptionStrict("input-path"));

logger.info("Old population size: " + scenario.getPopulation().getPersons().size());
scenario.getPopulation().getPersons().values().stream()
.filter(p -> p.getSelectedPlan().getPlanElements().stream()
.filter(e -> e instanceof Activity)
.map(e -> ((Activity) e).getType())
.anyMatch(activityTypes::contains))
.map(Identifiable::getId)
.collect(Collectors.toSet())
.forEach(scenario.getPopulation()::removePerson);
logger.info("new population size: " + scenario.getPopulation().getPersons().size());
new PopulationWriter(scenario.getPopulation()).write(commandLine.getOptionStrict("output-path"));
}
}
Loading

0 comments on commit b1331d7

Please sign in to comment.