Skip to content

Commit

Permalink
Rename interface OtpTransitDao to OtpTransitService.
Browse files Browse the repository at this point in the history
(Refactor OTP to have new OTP classes to replace the OBA GTFS classes #2494)
  • Loading branch information
Thomas Gran committed Nov 28, 2017
1 parent c9e2e7b commit 762a593
Show file tree
Hide file tree
Showing 31 changed files with 173 additions and 157 deletions.
Expand Up @@ -24,7 +24,7 @@
import org.opentripplanner.model.calendar.CalendarServiceData;
import org.opentripplanner.model.calendar.LocalizedServiceId;
import org.opentripplanner.model.calendar.ServiceDate;
import org.opentripplanner.model.OtpTransitDao;
import org.opentripplanner.model.OtpTransitService;
import org.opentripplanner.model.CalendarService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -51,18 +51,18 @@ public class CalendarServiceDataFactoryImpl {

private static final Logger LOG = LoggerFactory.getLogger(CalendarServiceDataFactoryImpl.class);

private final OtpTransitDao dao;
private final OtpTransitService transitService;

public static CalendarService createCalendarService(OtpTransitDao dao) {
return new CalendarServiceImpl(createCalendarServiceData(dao));
public static CalendarService createCalendarService(OtpTransitService transitService) {
return new CalendarServiceImpl(createCalendarServiceData(transitService));
}

public static CalendarServiceData createCalendarServiceData(OtpTransitDao dao) {
return new CalendarServiceDataFactoryImpl(dao).createData();
public static CalendarServiceData createCalendarServiceData(OtpTransitService transitService) {
return new CalendarServiceDataFactoryImpl(transitService).createData();
}

public static CalendarServiceData createCalendarSrvDataWithoutDatesForLocalizedSrvId(OtpTransitDao dao) {
return (new CalendarServiceDataFactoryImpl(dao) {
public static CalendarServiceData createCalendarSrvDataWithoutDatesForLocalizedSrvId(OtpTransitService transitService) {
return (new CalendarServiceDataFactoryImpl(transitService) {
@Override void addDatesForLocalizedServiceId(
AgencyAndId serviceId, List<ServiceDate> serviceDates, CalendarServiceData data
) {
Expand All @@ -71,8 +71,8 @@ public static CalendarServiceData createCalendarSrvDataWithoutDatesForLocalizedS
}).createData();
}

private CalendarServiceDataFactoryImpl(OtpTransitDao dao) {
this.dao = dao;
private CalendarServiceDataFactoryImpl(OtpTransitService transitService) {
this.transitService = transitService;
}

CalendarServiceData createData() {
Expand All @@ -81,7 +81,7 @@ CalendarServiceData createData() {

setTimeZonesForAgencies(data);

List<AgencyAndId> serviceIds = dao.getAllServiceIds();
List<AgencyAndId> serviceIds = transitService.getAllServiceIds();

int index = 0;

Expand Down Expand Up @@ -112,7 +112,7 @@ CalendarServiceData createData() {

void addDatesForLocalizedServiceId (
AgencyAndId serviceId, List<ServiceDate> serviceDates, CalendarServiceData data) {
List<String> tripAgencyIds = dao.getTripAgencyIdsReferencingServiceId(serviceId);
List<String> tripAgencyIds = transitService.getTripAgencyIdsReferencingServiceId(serviceId);
Set<TimeZone> timeZones = new HashSet<>();
for (String tripAgencyId : tripAgencyIds) {
TimeZone timeZone = data.getTimeZoneForAgencyId(tripAgencyId);
Expand All @@ -133,19 +133,19 @@ void addDatesForLocalizedServiceId (
private Set<ServiceDate> getServiceDatesForServiceId(AgencyAndId serviceId,
TimeZone serviceIdTimeZone) {
Set<ServiceDate> activeDates = new HashSet<>();
ServiceCalendar c = dao.getCalendarForServiceId(serviceId);
ServiceCalendar c = transitService.getCalendarForServiceId(serviceId);

if (c != null) {
addDatesFromCalendar(c, serviceIdTimeZone, activeDates);
}
for (ServiceCalendarDate cd : dao.getCalendarDatesForServiceId(serviceId)) {
for (ServiceCalendarDate cd : transitService.getCalendarDatesForServiceId(serviceId)) {
addAndRemoveDatesFromCalendarDate(cd, activeDates);
}
return activeDates;
}

private void setTimeZonesForAgencies(CalendarServiceData data) {
for (Agency agency : dao.getAllAgencies()) {
for (Agency agency : transitService.getAllAgencies()) {
TimeZone timeZone = TimeZone.getTimeZone(agency.getTimezone());
if (timeZone.getID().equals("GMT") && !agency.getTimezone().toUpperCase()
.equals("GMT")) {
Expand Down
Expand Up @@ -17,7 +17,7 @@ the License, or (at your option) any later version.
import org.opentripplanner.model.AgencyAndId;
import org.opentripplanner.model.calendar.CalendarServiceData;
import org.opentripplanner.model.calendar.LocalizedServiceId;
import org.opentripplanner.model.OtpTransitDao;
import org.opentripplanner.model.OtpTransitService;

/**
* This is actually kind of a hack, and assumes that there is only one copy of CalendarServiceData
Expand All @@ -32,9 +32,9 @@ public MultiCalendarServiceImpl() {
super(new CalendarServiceData());
}

public void addData(CalendarServiceData data, OtpTransitDao dao) {
public void addData(CalendarServiceData data, OtpTransitService transitService) {
CalendarServiceData _data = super.getData();
for (Agency agency : dao.getAllAgencies()) {
for (Agency agency : transitService.getAllAgencies()) {
String agencyId = agency.getId();
_data.putTimeZoneForAgencyId(agencyId, data.getTimeZoneForAgencyId(agencyId));
}
Expand Down
Expand Up @@ -13,7 +13,7 @@ the License, or (at your option) any later version.

package org.opentripplanner.graph_builder.module;

import java.awt.*;
import java.awt.Color;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
Expand All @@ -40,13 +40,13 @@ the License, or (at your option) any later version.
import org.onebusaway.gtfs.serialization.GtfsReader;
import org.onebusaway.gtfs.services.GenericMutableDao;
import org.onebusaway.gtfs.services.GtfsMutableRelationalDao;
import org.opentripplanner.model.OtpTransitDao;
import org.opentripplanner.calendar.impl.MultiCalendarServiceImpl;
import org.opentripplanner.graph_builder.model.GtfsBundle;
import org.opentripplanner.graph_builder.services.GraphBuilderModule;
import org.opentripplanner.gtfs.BikeAccess;
import org.opentripplanner.gtfs.GtfsContext;
import org.opentripplanner.gtfs.GtfsLibrary;
import org.opentripplanner.model.OtpTransitService;
import org.opentripplanner.routing.edgetype.factory.GTFSPatternHopFactory;
import org.opentripplanner.routing.edgetype.factory.GtfsStopContext;
import org.opentripplanner.routing.graph.Graph;
Expand Down Expand Up @@ -104,27 +104,35 @@ public void buildGraph(Graph graph, HashMap<Class<?>, Object> extra) {
// because the time zone from the first agency is cached
graph.clearTimeZone();

MultiCalendarServiceImpl service = new MultiCalendarServiceImpl();
MultiCalendarServiceImpl calendarService = new MultiCalendarServiceImpl();
GtfsStopContext stopContext = new GtfsStopContext();

try {
for (GtfsBundle gtfsBundle : gtfsBundles) {
// apply global defaults to individual GTFSBundles (if globals have been set)
if (cacheDirectory != null && gtfsBundle.cacheDirectory == null)
if (cacheDirectory != null && gtfsBundle.cacheDirectory == null) {
gtfsBundle.cacheDirectory = cacheDirectory;
if (useCached != null && gtfsBundle.useCached == null)
}

if (useCached != null && gtfsBundle.useCached == null) {
gtfsBundle.useCached = useCached;
}

OtpTransitService transitService = mapDao(loadBundle(gtfsBundle));

OtpTransitDao dao = mapDao(loadBundle(gtfsBundle));
GtfsContext context = GtfsLibrary
.createContext(gtfsBundle.getFeedId(), transitService, calendarService);

GtfsContext context = GtfsLibrary.createContext(gtfsBundle.getFeedId(), dao, service);
GTFSPatternHopFactory hf = new GTFSPatternHopFactory(context);

hf.setStopContext(stopContext);
hf.setFareServiceFactory(_fareServiceFactory);
hf.setMaxStopToShapeSnapDistance(gtfsBundle.getMaxStopToShapeSnapDistance());

service.addData(createCalendarSrvDataWithoutDatesForLocalizedSrvId(dao), dao);
calendarService.addData(
createCalendarSrvDataWithoutDatesForLocalizedSrvId(transitService),
transitService
);

hf.subwayAccessTime = gtfsBundle.subwayAccessTime;
hf.maxInterlineDistance = gtfsBundle.maxInterlineDistance;
Expand All @@ -145,8 +153,11 @@ public void buildGraph(Graph graph, HashMap<Class<?>, Object> extra) {
}

// We need to save the calendar service data so we can use it later
graph.putService(org.opentripplanner.model.calendar.CalendarServiceData.class, service.getData());
graph.updateTransitFeedValidity(service.getData());
graph.putService(
org.opentripplanner.model.calendar.CalendarServiceData.class,
calendarService.getData()
);
graph.updateTransitFeedValidity(calendarService.getData());

graph.hasTransit = true;
graph.calculateTransitCenter();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/opentripplanner/gtfs/GtfsContext.java
Expand Up @@ -13,12 +13,12 @@ the License, or (at your option) any later version.

package org.opentripplanner.gtfs;

import org.opentripplanner.model.OtpTransitDao;
import org.opentripplanner.model.OtpTransitService;
import org.opentripplanner.model.CalendarService;
import org.opentripplanner.graph_builder.module.GtfsFeedId;

public interface GtfsContext {
GtfsFeedId getFeedId();
OtpTransitDao getDao();
OtpTransitService getOtpTransitService();
CalendarService getCalendarService();
}
70 changes: 36 additions & 34 deletions src/main/java/org/opentripplanner/gtfs/GtfsLibrary.java
Expand Up @@ -13,41 +13,41 @@ the License, or (at your option) any later version.

package org.opentripplanner.gtfs;


import java.io.File;
import java.io.IOException;

import org.opentripplanner.model.AgencyAndId;
import org.opentripplanner.model.Route;
import org.opentripplanner.model.OtpTransitDao;
import org.opentripplanner.model.CalendarService;
import org.opentripplanner.graph_builder.module.GtfsFeedId;
import org.opentripplanner.gtfs.mapping.OtpTransitDaoMapper;
import org.opentripplanner.model.AgencyAndId;
import org.opentripplanner.model.CalendarService;
import org.opentripplanner.model.OtpTransitService;
import org.opentripplanner.model.Route;
import org.opentripplanner.routing.core.TraverseMode;

import java.io.File;
import java.io.IOException;

import static org.opentripplanner.calendar.impl.CalendarServiceDataFactoryImpl.createCalendarService;

public class GtfsLibrary {

public static final char ID_SEPARATOR = ':'; // note this is different than what OBA GTFS uses to match our 1.0 API

public static GtfsContext createContext(GtfsFeedId feedId, OtpTransitDao dao) {
CalendarService calendarService = createCalendarService(dao);
return createContext(feedId, dao, calendarService);
public static GtfsContext createContext(GtfsFeedId feedId, OtpTransitService transitService) {
CalendarService calendarService = createCalendarService(transitService);
return createContext(feedId, transitService, calendarService);
}

public static GtfsContext createContext(GtfsFeedId feedId, OtpTransitDao dao, CalendarService calendarService) {
return new GtfsContextImpl(feedId, dao, calendarService);
public static GtfsContext createContext(GtfsFeedId feedId, OtpTransitService transitService,
CalendarService calendarService) {
return new GtfsContextImpl(feedId, transitService, calendarService);
}

public static GtfsContext readGtfs(File path) throws IOException {
GtfsImport gtfsImport = new GtfsImport(path);

GtfsFeedId feedId = gtfsImport.getFeedId();
OtpTransitDao otpDao = OtpTransitDaoMapper.mapDao(gtfsImport.getDao());
CalendarService calendarService = createCalendarService(otpDao);
OtpTransitService transitService = OtpTransitDaoMapper.mapDao(gtfsImport.getDao());
CalendarService calendarService = createCalendarService(transitService);

return new GtfsContextImpl(feedId, otpDao, calendarService);
return new GtfsContextImpl(feedId, transitService, calendarService);
}

/* Using in index since we can't modify OBA libs and the colon in the expected separator in the 1.0 API. */
Expand All @@ -74,34 +74,35 @@ public static String getRouteName(Route route) {
public static TraverseMode getTraverseMode(Route route) {
int routeType = route.getType();
/* TPEG Extension https://groups.google.com/d/msg/gtfs-changes/keT5rTPS7Y0/71uMz2l6ke0J */
if (routeType >= 100 && routeType < 200){ // Railway Service
if (routeType >= 100 && routeType < 200) { // Railway Service
return TraverseMode.RAIL;
}else if (routeType >= 200 && routeType < 300){ //Coach Service
} else if (routeType >= 200 && routeType < 300) { //Coach Service
return TraverseMode.BUS;
}else if (routeType >= 300 && routeType < 500){ //Suburban Railway Service and Urban Railway service
} else if (routeType >= 300
&& routeType < 500) { //Suburban Railway Service and Urban Railway service
if (routeType >= 401 && routeType <= 402) {
return TraverseMode.SUBWAY;
}
return TraverseMode.RAIL;
}else if (routeType >= 500 && routeType < 700){ //Metro Service and Underground Service
} else if (routeType >= 500 && routeType < 700) { //Metro Service and Underground Service
return TraverseMode.SUBWAY;
}else if (routeType >= 700 && routeType < 900){ //Bus Service and Trolleybus service
} else if (routeType >= 700 && routeType < 900) { //Bus Service and Trolleybus service
return TraverseMode.BUS;
}else if (routeType >= 900 && routeType < 1000){ //Tram service
} else if (routeType >= 900 && routeType < 1000) { //Tram service
return TraverseMode.TRAM;
}else if (routeType >= 1000 && routeType < 1100){ //Water Transport Service
} else if (routeType >= 1000 && routeType < 1100) { //Water Transport Service
return TraverseMode.FERRY;
}else if (routeType >= 1100 && routeType < 1200){ //Air Service
} else if (routeType >= 1100 && routeType < 1200) { //Air Service
return TraverseMode.AIRPLANE;
}else if (routeType >= 1200 && routeType < 1300){ //Ferry Service
} else if (routeType >= 1200 && routeType < 1300) { //Ferry Service
return TraverseMode.FERRY;
}else if (routeType >= 1300 && routeType < 1400){ //Telecabin Service
} else if (routeType >= 1300 && routeType < 1400) { //Telecabin Service
return TraverseMode.GONDOLA;
}else if (routeType >= 1400 && routeType < 1500){ //Funicalar Service
} else if (routeType >= 1400 && routeType < 1500) { //Funicalar Service
return TraverseMode.FUNICULAR;
}else if (routeType >= 1500 && routeType < 1600){ //Taxi Service
} else if (routeType >= 1500 && routeType < 1600) { //Taxi Service
throw new IllegalArgumentException("Taxi service not supported" + routeType);
}else if (routeType >= 1600 && routeType < 1700){ //Self drive
} else if (routeType >= 1600 && routeType < 1700) { //Self drive
return TraverseMode.CAR;
}
/* Original GTFS route types. Should these be checked before TPEG types? */
Expand Down Expand Up @@ -131,13 +132,14 @@ private static class GtfsContextImpl implements GtfsContext {

private GtfsFeedId _feedId;

private OtpTransitDao _dao;
private OtpTransitService transitService;

private CalendarService _calendar;

public GtfsContextImpl(GtfsFeedId feedId, OtpTransitDao dao, CalendarService calendar) {
public GtfsContextImpl(GtfsFeedId feedId, OtpTransitService transitService,
CalendarService calendar) {
_feedId = feedId;
_dao = dao;
this.transitService = transitService;
_calendar = calendar;
}

Expand All @@ -147,8 +149,8 @@ public GtfsFeedId getFeedId() {
}

@Override
public OtpTransitDao getDao() {
return _dao;
public OtpTransitService getOtpTransitService() {
return transitService;
}

@Override
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/opentripplanner/gtfs/MockGtfs.java
Expand Up @@ -3,7 +3,7 @@
import org.opentripplanner.model.AgencyAndId;
import org.opentripplanner.gtfs.mapping.AgencyAndIdMapper;
import org.opentripplanner.gtfs.mapping.OtpTransitDaoMapper;
import org.opentripplanner.model.OtpTransitDao;
import org.opentripplanner.model.OtpTransitService;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -40,11 +40,11 @@ public void putLines(String fileName, String... rows) {
gtfsDelegate.putLines(fileName, rows);
}

public OtpTransitDao read() throws IOException {
public OtpTransitService read() throws IOException {
return OtpTransitDaoMapper.mapDao(gtfsDelegate.read());
}

public OtpTransitDao read(org.onebusaway.gtfs.serialization.GtfsReader reader) throws IOException {
public OtpTransitService read(org.onebusaway.gtfs.serialization.GtfsReader reader) throws IOException {
return OtpTransitDaoMapper.mapDao(gtfsDelegate.read(reader));
}

Expand Down
Expand Up @@ -13,8 +13,8 @@ the License, or (at your option) any later version.

package org.opentripplanner.gtfs.mapping;

import org.opentripplanner.model.impl.OtpTransitDaoBuilder;
import org.opentripplanner.model.OtpTransitDao;
import org.opentripplanner.model.impl.OtpTransitBuilder;
import org.opentripplanner.model.OtpTransitService;

public class OtpTransitDaoMapper {
private final AgencyMapper agencyMapper = new AgencyMapper();
Expand Down Expand Up @@ -49,12 +49,12 @@ public class OtpTransitDaoMapper {
routeMapper, fareAttributeMapper
);

public static OtpTransitDao mapDao(org.onebusaway.gtfs.services.GtfsRelationalDao data) {
public static OtpTransitService mapDao(org.onebusaway.gtfs.services.GtfsRelationalDao data) {
return new OtpTransitDaoMapper().map(data);
}

private OtpTransitDao map(org.onebusaway.gtfs.services.GtfsRelationalDao data) {
OtpTransitDaoBuilder builder = new OtpTransitDaoBuilder();
private OtpTransitService map(org.onebusaway.gtfs.services.GtfsRelationalDao data) {
OtpTransitBuilder builder = new OtpTransitBuilder();

builder.getAgencies().addAll(agencyMapper.map(data.getAllAgencies()));
builder.getCalendarDates().addAll(serviceCalendarDateMapper.map(data.getAllCalendarDates()));
Expand Down
Expand Up @@ -6,7 +6,7 @@
/**
* Methods for accessing imported entities.
*/
public interface OtpTransitDao {
public interface OtpTransitService {

Collection<Agency> getAllAgencies();

Expand Down

0 comments on commit 762a593

Please sign in to comment.