Skip to content
Merged
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
28 changes: 28 additions & 0 deletions src/main/java/org/mtransit/parser/DefaultAgencyTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,34 @@ public String getAgencyColor(@NotNull GAgency gAgency, @NotNull GSpec gSpec) {
return getAgencyColor();
}

@Nullable
@Override
public String getAgencyIdCleanupRegex() {
if (Configs.getAgencyConfig() != null) {
return Configs.getAgencyConfig().getAgencyIdCleanupRegex();
}
return null; // OPT-IN feature
}

@Nullable
private Pattern agencyIdCleanupPattern = null;

private boolean agencyIdCleanupPatternSet = false;

private @Nullable Pattern getAgencyIdCleanupPattern() {
if (this.agencyIdCleanupPattern == null && !agencyIdCleanupPatternSet) {
this.agencyIdCleanupPattern = GTFSCommons.makeIdCleanupPattern(getAgencyIdCleanupRegex());
this.agencyIdCleanupPatternSet = true;
}
return this.agencyIdCleanupPattern;
}

@NotNull
@Override
public String cleanAgencyId(@NotNull String gAgencyId) {
return GTFSCommons.cleanOriginalString(gAgencyId, getAgencyIdCleanupPattern());
}

@Nullable
@Override
public String getAgencyId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ data class AgencyConfig(
*/
@SerialName("other_agency_ids")
val otherAgencyIds: List<String> = emptyList(), // OPT-IN filter
@SerialName("agency_id_cleanup_regex")
val agencyIdCleanupRegex: String? = null, // optional
/**
* Route type filter (integer from GTFS Static `routes.txt` > `route_type` field)
* (useful when multiple route type in same GTFS)
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/mtransit/parser/gtfs/GAgencyTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ public interface GAgencyTools {
@Nullable
String getAgencyId();

@Nullable
String getAgencyIdCleanupRegex();

@NotNull
String cleanAgencyId(@NotNull String gAgencyId);

@NotNull
Integer getAgencyRouteType();

Expand Down
19 changes: 6 additions & 13 deletions src/main/java/org/mtransit/parser/mt/GenerateMObjectsTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -589,23 +589,16 @@ private void parseGTrips(HashMap<String, MSchedule> mSchedules,
&& directionStopTimesHeadsign != null
&& !directionStopTimesHeadsign.isEmpty()) {
if (mDirectionStopTimesHeadsign.containsKey(mDirection.getId())) {
if (!mDirectionStopTimesHeadsign.get(mDirection.getId()).equals(directionStopTimesHeadsign)) {
if (!mDirectionStopTimesHeadsign.get(mDirection.getId()).contains(directionStopTimesHeadsign)) {
final String cDirectionStopTimesHeadsign = mDirectionStopTimesHeadsign.get(mDirection.getId());
if (!cDirectionStopTimesHeadsign.equals(directionStopTimesHeadsign)) {
if (!cDirectionStopTimesHeadsign.contains(directionStopTimesHeadsign)) {
MTLog.log("%s: Trip stop times head-sign different for same direction ID ('%s'!='%s')", this.routeId,
directionStopTimesHeadsign, mDirectionStopTimesHeadsign.get(mDirection.getId()));
directionStopTimesHeadsign, cDirectionStopTimesHeadsign);
}
String mergedHeadsignValue = MDirection.mergeHeadsignValue(
mDirectionStopTimesHeadsign.get(mDirection.getId()),
final String mergedHeadsignValue = MDirection.mergeHeadsignValue(
cDirectionStopTimesHeadsign,
directionStopTimesHeadsign
);
if (mergedHeadsignValue != null) {
mergedHeadsignValue = agencyTools.cleanDirectionHeadsign(
gRoute,
gTrip.getDirectionIdOrDefault(),
true,
mergedHeadsignValue
);
}
mDirectionStopTimesHeadsign.put(mDirection.getId(), mergedHeadsignValue);
Comment thread
mmathieum marked this conversation as resolved.
}
} else {
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/org/mtransit/parser/mt/MGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,10 @@ public static void dumpFiles(@NotNull GAgencyTools gAgencyTools,
// STRINGS
dumpStrings(mSpec, fileBase, deleteAll, dataDirF, rawDirF, dbConnection); // AFTER ALL OTHER TABLE W/ STRINGS
if (deleteAll) {
dumpValues(rawDirF, fileBase, null, null, null, null, null, -1, -1, null, true);
dumpValues(gAgencyTools, rawDirF, fileBase, null, null, null, null, null, -1, -1, null, true);
} else {
dumpCommonValues(rawDirF, gAgencyTools, mSpec, inputUrl);
dumpValues(
dumpValues(gAgencyTools,
rawDirF, fileBase, mSpec,
minMaxLatLng.first.first, minMaxLatLng.second.first, minMaxLatLng.first.second, minMaxLatLng.second.second,
mSpec.getFirstTimestampInSeconds(), mSpec.getLastTimestampInSeconds(), inputUrl,
Expand Down Expand Up @@ -1057,6 +1057,7 @@ private static String getLastModified(String gtfsDir) {
private static final String GTFS_RDS_AGENCY_EXTENDED_TYPE = "gtfs_rts_agency_extended_type"; // do not change to avoid breaking compat w/ old modules
public static final String GTFS_RDS_TIMEZONE = "gtfs_rts_timezone"; // do not change to avoid breaking compat w/ old modules
private static final String GTFS_RDS_COLOR = "gtfs_rts_color"; // do not change to avoid breaking compat w/ old modules
private static final String GTFS_RDS_AGENCY_ID_CLEANUP_REGEX = "gtfs_rts_agency_id_cleanup_regex"; // do not change to avoid breaking compat w/ old modules
private static final String GTFS_RDS_SERVICE_ID_CLEANUP_REGEX = "gtfs_rts_service_id_cleanup_regex"; // do not change to avoid breaking compat w/ old modules
private static final String GTFS_RDS_ROUTE_ID_CLEANUP_REGEX = "gtfs_rts_route_id_cleanup_regex"; // do not change to avoid breaking compat w/ old modules
private static final String GTFS_RDS_TRIP_ID_CLEANUP_REGEX = "gtfs_rts_trip_id_cleanup_regex"; // do not change to avoid breaking compat w/ old modules
Expand Down Expand Up @@ -1095,7 +1096,7 @@ private static void dumpCommonValues(File dumpDirF, GAgencyTools gAgencyTools, M
ow.write(Constants.NEW_LINE);
}
//noinspection DiscouragedApi
ow.write(getRESOURCES_STRING(GTFS_RDS_AGENCY_ID, mSpec.getFirstAgency().getId()));
ow.write(getRESOURCES_STRING(GTFS_RDS_AGENCY_ID, mSpec.getFirstAgency().getCleanAgencyId(gAgencyTools)));
ow.write(Constants.NEW_LINE);
ow.write(getRESOURCES_INTEGER(GTFS_RDS_AGENCY_TYPE, mSpec.getFirstAgency().getType()));
ow.write(Constants.NEW_LINE);
Expand All @@ -1107,6 +1108,10 @@ private static void dumpCommonValues(File dumpDirF, GAgencyTools gAgencyTools, M
ow.write(Constants.NEW_LINE);
ow.write(getRESOURCES_STRING(GTFS_RDS_COLOR, mSpec.getFirstAgency().getColor()));
ow.write(Constants.NEW_LINE);
if (gAgencyTools.getAgencyIdCleanupRegex() != null) {
ow.write(getRESOURCES_STRING(GTFS_RDS_AGENCY_ID_CLEANUP_REGEX, escapeResString(gAgencyTools.getAgencyIdCleanupRegex())));
ow.write(Constants.NEW_LINE);
}
if (gAgencyTools.getServiceIdCleanupRegex() != null) {
ow.write(getRESOURCES_STRING(GTFS_RDS_SERVICE_ID_CLEANUP_REGEX, escapeResString(gAgencyTools.getServiceIdCleanupRegex())));
ow.write(Constants.NEW_LINE);
Expand Down Expand Up @@ -1151,7 +1156,7 @@ public static String escapeResString(@NotNull String string) {
return string;
}

private static void dumpValues(File dumpDirF, String fileBase, MSpec mSpec, Double minLat, Double maxLat, Double minLng, Double maxLng,
private static void dumpValues(GAgencyTools gAgencyTools, File dumpDirF, String fileBase, MSpec mSpec, Double minLat, Double maxLat, Double minLng, Double maxLng,
int firstTimestampInSec, int lastTimestampInSec, @Nullable String inputUrl, boolean deleteAll) {
File file;
BufferedWriter ow = null;
Expand Down Expand Up @@ -1183,7 +1188,7 @@ private static void dumpValues(File dumpDirF, String fileBase, MSpec mSpec, Doub
ow.write(Constants.NEW_LINE);
}
//noinspection DiscouragedApi
ow.write(getRESOURCES_STRING(GTFS_RDS_AGENCY_ID, mSpec.getFirstAgency().getId()));
ow.write(getRESOURCES_STRING(GTFS_RDS_AGENCY_ID, mSpec.getFirstAgency().getCleanAgencyId(gAgencyTools)));
ow.write(Constants.NEW_LINE);
ow.write(getRESOURCES_INTEGER(GTFS_RDS_AGENCY_TYPE, mSpec.getFirstAgency().getType()));
ow.write(Constants.NEW_LINE);
Expand Down
27 changes: 14 additions & 13 deletions src/main/java/org/mtransit/parser/mt/data/MAgency.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.mtransit.parser.mt.data

import androidx.annotation.Discouraged
import org.mtransit.parser.db.SQLUtils.escapeId
import org.mtransit.parser.gtfs.GAgencyTools
import org.mtransit.parser.gtfs.data.GAgency
import org.mtransit.parser.gtfs.data.GIDs
Expand Down Expand Up @@ -29,19 +29,11 @@ data class MAgency(
agencyTools.agencyRouteType,
)

@Suppress("unused")
@get:Discouraged(message = "Not memory efficient")
val id: String get() = _id
fun getCleanAgencyId(agencyTools: GAgencyTools): String = _id.convertAgencyId(agencyTools)

private val _id: String
get() {
return GIDs.getString(idInt)
}
private val _id: String get() = GIDs.getString(idInt)

fun toStringPlus(): String {
return toString() +
"+(_id:$_id)"
}
fun toStringPlus() = toString() + "+(_id:$_id)"

override fun compareTo(other: MAgency?): Int {
return when {
Expand Down Expand Up @@ -98,5 +90,14 @@ data class MAgency(
}
return null
}

internal fun convert(agencyTools: GAgencyTools, agencyId: String): String =
agencyTools.cleanAgencyId(agencyId).escapeId()
}
}
}

fun String.convertAgencyId(agencyTools: GAgencyTools) =
MAgency.convert(
agencyTools = agencyTools,
agencyId = this,
)
2 changes: 1 addition & 1 deletion src/main/java/org/mtransit/parser/mt/data/MDirection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ data class MDirection @JvmOverloads constructor(
}
}

private const val SLASH = " / "
private const val SLASH = "/"

@JvmStatic
fun mergeHeadsignValue(
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/mtransit/parser/mt/data/MServiceIds.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ object MServiceIds {
fun containsAllIdInts(idInts: Iterable<Int>)
= idInts.all { idIntToId.containsKey(it) }

@JvmStatic
fun convert(agencyTools: GAgencyTools, serviceId: String, keep: Boolean = true, quotesString: Boolean = false): String =
internal fun convert(agencyTools: GAgencyTools, serviceId: String, keep: Boolean = true, quotesString: Boolean = false): String =
if (FeatureFlags.F_EXPORT_SERVICE_ID_INTS) {
getInt(agencyTools.cleanServiceId(serviceId, keep)).toString()
} else {
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/mtransit/parser/mt/data/MTripIds.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ object MTripIds {
synchronized(incrementLock) {
val iterator = idToIdInt.asMutableMap().entries.iterator()
while (iterator.hasNext()) {
val (tripId,tripIdInt) = iterator.next()
val (tripId, tripIdInt) = iterator.next()
if (tripId !in usedTripIds) {
idIntToId.remove(tripIdInt)
iterator.remove()
Expand Down Expand Up @@ -88,8 +88,7 @@ object MTripIds {
@JvmStatic
fun getAllSorted() = getAll().sorted()

@JvmStatic
fun convert(tripId: String, quotesString: Boolean = false) =
internal fun convert(tripId: String, quotesString: Boolean = false) =
if (FeatureFlags.F_EXPORT_TRIP_ID_INTS) {
getInt(tripId).toString()
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ class MDirectionHeadSignFinderTest {
// Act
val result = MDirectionHeadSignFinder.findDirectionHeadSign(RID, gRouteTrips, routeGTFS, directionId, agencyTools)
// Assert
assertEquals("foo foo / trip head-sign", result?.headSign)
assertEquals("foo foo/trip head-sign", result?.headSign)
}

@Test
Expand Down Expand Up @@ -622,7 +622,7 @@ class MDirectionHeadSignFinderTest {
// Act
val result = MDirectionHeadSignFinder.findDirectionHeadSign(RID, gRouteTrips, routeGTFS, directionId, agencyTools)
// Assert
assertEquals("foo foo / trip head-sign", result?.headSign)
assertEquals("foo foo/trip head-sign", result?.headSign)
}

@Test
Expand Down