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

Fares V2 #8

Open
wants to merge 7 commits into
base: dev
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/main/java/com/conveyal/gtfs/GTFSFeed.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ public class GTFSFeed implements Cloneable, Closeable {
/* A place to store an event bus that is passed through constructor. */
public transient EventBus eventBus;

public final Map<String, Area> areas;
public final Map<String, StopArea> stop_areas;
public final Map<String, FareProduct> fare_products;
public final Map<String, FareMedia> fare_medias;
public final Map<String, TimeFrame> time_frames;
public final Map<String, FareLegRule> fare_leg_rules;
public final Map<String, FareTransferRule> fare_transfer_rules;
public final Map<String, Network> networks;
public final Map<String, RouteNetwork> route_networks;

/**
* The order in which we load the tables is important for two reasons.
* 1. We must load feed_info first so we know the feed ID before loading any other entities. This could be relaxed
Expand Down Expand Up @@ -177,6 +187,18 @@ else if (feedId == null || feedId.isEmpty()) {
new Trip.Loader(this).loadTable(zip);
new Frequency.Loader(this).loadTable(zip);
new StopTime.Loader(this).loadTable(zip); // comment out this line for quick testing using NL feed

// Fares v2.
new Area.Loader(this).loadTable(zip);
new StopArea.Loader(this).loadTable(zip);
new TimeFrame.Loader(this).loadTable(zip);
new Network.Loader(this).loadTable(zip);
new RouteNetwork.Loader(this).loadTable(zip);
new FareMedia.Loader(this).loadTable(zip);
new FareProduct.Loader(this).loadTable(zip);
new FareLegRule.Loader(this).loadTable(zip);
new FareTransferRule.Loader(this).loadTable(zip);

LOG.info("{} errors", errors.size());
for (GTFSError error : errors) {
LOG.info("{}", error);
Expand Down Expand Up @@ -219,6 +241,17 @@ public void toFile (String file) {
new StopTime.Writer(this).writeTable(zip);
new Pattern.Writer(this).writeTable(zip);

// Fares v2.
new Area.Writer(this).writeTable(zip);
new StopArea.Writer(this).writeTable(zip);
new TimeFrame.Writer(this).writeTable(zip);
new Network.Writer(this).writeTable(zip);
new RouteNetwork.Writer(this).writeTable(zip);
new FareMedia.Writer(this).writeTable(zip);
new FareProduct.Writer(this).writeTable(zip);
new FareLegRule.Writer(this).writeTable(zip);
new FareTransferRule.Writer(this).writeTable(zip);

zip.close();

LOG.info("GTFS file written");
Expand Down Expand Up @@ -608,8 +641,15 @@ private GTFSFeed (DB db) {
this.db = db;

agency = db.getTreeMap("agency");
areas = db.getTreeMap("area");
fare_leg_rules = db.getTreeMap("fare_leg_rules");
fare_medias = db.getTreeMap("fare_medias");
fare_products = db.getTreeMap("fare_products");
fare_transfer_rules = db.getTreeMap("fare_transfer_rules");
feedInfo = db.getTreeMap("feed_info");
networks = db.getTreeMap("networks");
routes = db.getTreeMap("routes");
route_networks = db.getTreeMap("route_networks");
trips = db.getTreeMap("trips");
stop_times = db.getTreeMap("stop_times");
frequencies = db.getTreeSet("frequencies");
Expand All @@ -618,6 +658,8 @@ private GTFSFeed (DB db) {
fares = db.getTreeMap("fares");
services = db.getTreeMap("services");
shape_points = db.getTreeMap("shape_points");
stop_areas = db.getTreeMap("stop_areas");
time_frames = db.getTreeMap("time_frames");
translations = db.getTreeMap("translations");
attributions = db.getTreeMap("attributions");

Expand Down
161 changes: 161 additions & 0 deletions src/main/java/com/conveyal/gtfs/graphql/GraphQLGtfsFaresV2Schema.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package com.conveyal.gtfs.graphql;

import com.conveyal.gtfs.graphql.fetchers.MapFetcher;
import com.conveyal.gtfs.model.Area;
import com.conveyal.gtfs.model.FareLegRule;
import com.conveyal.gtfs.model.FareMedia;
import com.conveyal.gtfs.model.FareProduct;
import com.conveyal.gtfs.model.FareTransferRule;
import com.conveyal.gtfs.model.Network;
import com.conveyal.gtfs.model.Route;
import com.conveyal.gtfs.model.RouteNetwork;
import com.conveyal.gtfs.model.StopArea;
import com.conveyal.gtfs.model.TimeFrame;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLObjectType;

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

import static com.conveyal.gtfs.graphql.GraphQLUtil.createFieldDefinition;
import static graphql.Scalars.GraphQLInt;
import static graphql.schema.GraphQLObjectType.newObject;

public class GraphQLGtfsFaresV2Schema {

private GraphQLGtfsFaresV2Schema() {}

public static final GraphQLObjectType stopAreaType = newObject().name("stopArea")
.description("A GTFS stop area object")
.field(MapFetcher.field("id", GraphQLInt))
.field(MapFetcher.field(StopArea.AREA_ID_NAME))
.field(MapFetcher.field(StopArea.STOP_ID_NAME))
.field(createFieldDefinition("stops", GraphQLGtfsSchema.stopType, "stops", StopArea.STOP_ID_NAME))
.build();

public static final GraphQLObjectType areaType = newObject().name("area")
.description("A GTFS area object")
.field(MapFetcher.field("id", GraphQLInt))
.field(MapFetcher.field(Area.AREA_ID_NAME))
.field(MapFetcher.field(Area.AREA_NAME_NAME))
.field(createFieldDefinition("stopAreas", stopAreaType, StopArea.TABLE_NAME, Area.AREA_ID_NAME))
.build();

public static final GraphQLObjectType timeFrameType = newObject().name("timeFrame")
.description("A GTFS time frame object")
.field(MapFetcher.field("id", GraphQLInt))
.field(MapFetcher.field(TimeFrame.TIME_FRAME_GROUP_ID_NAME))
.field(MapFetcher.field(TimeFrame.START_TIME_NAME))
.field(MapFetcher.field(TimeFrame.END_TIME_NAME))
.field(MapFetcher.field(TimeFrame.SERVICE_ID_NAME))
.build();

public static final GraphQLObjectType networkType = newObject().name("network")
.description("A GTFS network object")
.field(MapFetcher.field("id", GraphQLInt))
.field(MapFetcher.field(Network.NETWORK_ID_NAME))
.field(MapFetcher.field(Network.NETWORK_NAME_NAME))
.build();

public static final GraphQLObjectType routeNetworkType = newObject().name("routeNetwork")
.description("A GTFS route network object")
.field(MapFetcher.field("id", GraphQLInt))
.field(MapFetcher.field(RouteNetwork.NETWORK_ID_NAME))
.field(MapFetcher.field(RouteNetwork.ROUTE_ID_NAME))
.field(createFieldDefinition("networks", networkType, Network.TABLE_NAME, RouteNetwork.NETWORK_ID_NAME))
.build();

public static final GraphQLObjectType fareMediaType = newObject().name("fareMedia")
.description("A GTFS fare media object")
.field(MapFetcher.field("id", GraphQLInt))
.field(MapFetcher.field(FareMedia.FARE_MEDIA_ID_NAME))
.field(MapFetcher.field(FareMedia.FARE_MEDIA_NAME_NAME))
.field(MapFetcher.field(FareMedia.FARE_MEDIA_TYPE_NAME))
.build();

public static final GraphQLObjectType fareProductType = newObject().name("fareProduct")
.description("A GTFS fare product object")
.field(MapFetcher.field("id", GraphQLInt))
.field(MapFetcher.field(FareProduct.FARE_PRODUCT_ID_NAME))
.field(MapFetcher.field(FareProduct.FARE_PRODUCT_NAME_NAME))
.field(MapFetcher.field(FareProduct.FARE_MEDIA_ID_NAME))
.field(MapFetcher.field(FareProduct.AMOUNT_NAME))
.field(MapFetcher.field(FareProduct.CURRENCY_NAME))
.field(createFieldDefinition("fareMedia", fareMediaType, FareMedia.TABLE_NAME, FareProduct.FARE_MEDIA_ID_NAME))
.build();

public static final GraphQLObjectType fareLegRuleType = newObject().name("fareLegRule")
.description("A GTFS fare leg rule object")
.field(MapFetcher.field("id", GraphQLInt))
.field(MapFetcher.field(FareLegRule.LEG_GROUP_ID_NAME))
.field(MapFetcher.field(FareLegRule.NETWORK_ID_NAME))
.field(MapFetcher.field(FareLegRule.FROM_AREA_ID_NAME))
.field(MapFetcher.field(FareLegRule.TO_AREA_ID_NAME))
.field(MapFetcher.field(FareLegRule.FROM_TIMEFRAME_GROUP_ID_NAME))
.field(MapFetcher.field(FareLegRule.TO_TIMEFRAME_GROUP_ID_NAME))
.field(MapFetcher.field(FareLegRule.FARE_PRODUCT_ID_NAME))
.field(MapFetcher.field(FareLegRule.RULE_PRIORITY_NAME))
// Will return either routes or networks, not both.
.field(createFieldDefinition("routes", GraphQLGtfsSchema.routeType, Route.TABLE_NAME, FareLegRule.NETWORK_ID_NAME))
.field(createFieldDefinition("networks", networkType, Network.TABLE_NAME, Network.NETWORK_ID_NAME))
.field(createFieldDefinition("fareProducts", fareProductType, FareProduct.TABLE_NAME, FareLegRule.FARE_PRODUCT_ID_NAME))
// fromTimeFrame and toTimeFrame may return multiple time frames.
.field(createFieldDefinition(
"fromTimeFrame",
timeFrameType,
TimeFrame.TABLE_NAME,
FareLegRule.FROM_TIMEFRAME_GROUP_ID_NAME,
TimeFrame.TIME_FRAME_GROUP_ID_NAME
))
.field(createFieldDefinition(
"toTimeFrame",
timeFrameType,
TimeFrame.TABLE_NAME,
FareLegRule.TO_TIMEFRAME_GROUP_ID_NAME,
TimeFrame.TIME_FRAME_GROUP_ID_NAME
))
.field(createFieldDefinition("toArea", areaType, Area.TABLE_NAME, FareLegRule.TO_AREA_ID_NAME, Area.AREA_ID_NAME))
.field(createFieldDefinition("fromArea", areaType, Area.TABLE_NAME, FareLegRule.FROM_AREA_ID_NAME, Area.AREA_ID_NAME))
.build();

public static final GraphQLObjectType fareTransferRuleType = newObject().name("fareTransferRule")
.description("A GTFS fare transfer rule object")
.field(MapFetcher.field("id", GraphQLInt))
.field(MapFetcher.field(FareTransferRule.FROM_LEG_GROUP_ID_NAME))
.field(MapFetcher.field(FareTransferRule.TO_LEG_GROUP_ID_NAME))
.field(MapFetcher.field(FareTransferRule.TRANSFER_COUNT_NAME))
.field(MapFetcher.field(FareTransferRule.DURATION_LIMIT_NAME))
.field(MapFetcher.field(FareTransferRule.DURATION_LIMIT_TYPE_NAME))
.field(MapFetcher.field(FareTransferRule.FARE_PRODUCT_ID_NAME))
.field(createFieldDefinition("toArea", areaType, Area.TABLE_NAME, FareLegRule.TO_AREA_ID_NAME, Area.AREA_ID_NAME))
.field(createFieldDefinition("fareProducts", fareProductType, FareProduct.TABLE_NAME, FareProduct.FARE_PRODUCT_ID_NAME))
.field(createFieldDefinition(
"fromFareLegRule",
fareLegRuleType,
FareLegRule.TABLE_NAME,
FareTransferRule.FROM_LEG_GROUP_ID_NAME,
FareLegRule.LEG_GROUP_ID_NAME
))
.field(createFieldDefinition(
"toFareLegRule",
fareLegRuleType,
FareLegRule.TABLE_NAME,
FareTransferRule.TO_LEG_GROUP_ID_NAME,
FareLegRule.LEG_GROUP_ID_NAME
))
.build();

public static List<GraphQLFieldDefinition> getFaresV2FieldDefinitions() {
return Arrays.asList(
createFieldDefinition("area", areaType, Area.TABLE_NAME),
createFieldDefinition("stopArea", stopAreaType, StopArea.TABLE_NAME),
createFieldDefinition("fareTransferRule", fareTransferRuleType, FareTransferRule.TABLE_NAME),
createFieldDefinition("fareProduct", fareProductType, FareProduct.TABLE_NAME),
createFieldDefinition("fareMedia", fareMediaType, FareMedia.TABLE_NAME),
createFieldDefinition("fareLegRule", fareLegRuleType, FareLegRule.TABLE_NAME),
createFieldDefinition("timeFrame", timeFrameType, TimeFrame.TABLE_NAME),
createFieldDefinition("network", networkType, Network.TABLE_NAME),
createFieldDefinition("routeNetwork", routeNetworkType, RouteNetwork.TABLE_NAME)
);
}
}
Loading
Loading