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

locations.geojson POC phase2: End-to-end partial support of json data #1810

Merged
merged 35 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
f8a966d
add base classes, main broken
davidgamez Aug 27, 2024
204f931
fix FeedMetadata compilation issues
davidgamez Aug 28, 2024
e7751f0
add generated files to gitignore
davidgamez Aug 28, 2024
1131d7d
Merge branch 'master' into poc/json-files
davidgamez Aug 28, 2024
f027e9a
update TableStatus use
davidgamez Aug 28, 2024
2eae957
add json container class
davidgamez Aug 28, 2024
ae500ef
prepare feed container to load json files
davidgamez Aug 28, 2024
acab716
skip abstract classes while initializing the gtfs descriptors
davidgamez Aug 28, 2024
138d550
add empty GtfsJsonFileLoader
davidgamez Aug 28, 2024
e1ea3f1
Corrected a NPE caused by containers missing for files not dataset
jcpitre Aug 29, 2024
435876e
We now have a working POC with a notice if feature id is present in s…
jcpitre Sep 4, 2024
75e8d80
Changed names of some classes
jcpitre Sep 4, 2024
1e9fac5
Reformatting
jcpitre Sep 4, 2024
209d203
Added new notices fields.
jcpitre Sep 4, 2024
b82b39b
Added class comments and renamed some classes.
jcpitre Sep 4, 2024
8bdab6d
Removed some commented out code
jcpitre Sep 4, 2024
6f82f03
Simplified the use of generics got geojson classes
jcpitre Sep 5, 2024
77805f3
Moved code form core to main.
jcpitre Sep 13, 2024
e3dbb2b
Added a validator for the duplicate feature id in locations..
jcpitre Sep 20, 2024
63d03e9
Merge branch 'master' into poc/json-files-phase2
jcpitre Sep 23, 2024
bffef74
add GtfsLocationsSchema and related classes
davidgamez Sep 9, 2024
c90bd7e
Started using the Schema so the rules.hyml will be generated properly.
jcpitre Sep 24, 2024
b794c3a
Added location_group_stops so the UniqueGeographyIdValidator is more …
jcpitre Sep 24, 2024
4badc74
Modified according to PR comments.
jcpitre Sep 26, 2024
2380dae
Merge branch 'master' into poc/json-files-phase2
jcpitre Sep 26, 2024
67504ab
Removed validators and notices not slated for 6.0
jcpitre Sep 27, 2024
3e6845c
Removed foreign key validations on locations_groups_stops since it's …
jcpitre Sep 27, 2024
2a423cc
Clean-up, added comments, removed unused classes.
jcpitre Sep 27, 2024
86ea2c3
Merge branch 'master' into poc/json-files-phase2
jcpitre Sep 27, 2024
63b7d62
Added some tests
jcpitre Sep 30, 2024
361d444
Remove old file with uppercase 'J' from index
jcpitre Oct 1, 2024
1ac48f5
Merge branch 'master' into poc/json-files-phase2
jcpitre Oct 1, 2024
92a9952
Added some comments
jcpitre Oct 2, 2024
ee77597
Merge branch 'master' into poc/json-files-phase2
jcpitre Oct 2, 2024
beac89b
Merge branch 'master' into poc/json-files-phase2
jcpitre Oct 2, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,4 @@ app/pkg/bin/
processor/notices/bin/
processor/notices/tests/bin/
web/service/bin/
/web/service/execution_result.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mobilitydata.gtfsvalidator.notice;

import static org.mobilitydata.gtfsvalidator.annotation.GtfsValidationNotice.SectionRef.FILE_REQUIREMENTS;
import static org.mobilitydata.gtfsvalidator.notice.SeverityLevel.ERROR;

import org.mobilitydata.gtfsvalidator.annotation.GtfsValidationNotice;
import org.mobilitydata.gtfsvalidator.annotation.GtfsValidationNotice.SectionRefs;
import org.mobilitydata.gtfsvalidator.annotation.GtfsValidationNotice.UrlRef;

/**
* Feature id from locations.geojson already used.
*
* <p>The id of one of the features of the locations.geojson file already exists in stops.txt or
* location_groups.txt
*/
@GtfsValidationNotice(
severity = ERROR,
sections = @SectionRefs(FILE_REQUIREMENTS),
urls = {
@UrlRef(
label = "Original Python validator implementation",
url = "https://github.com/google/transitfeed")
})
public class UniqueLocationIdViolationNotice extends ValidationNotice {

/** The id that already exists. */
private final String id;

/** The name of the file that already has this id. */
private final String fileWithIdAlreadyPresent;

/** The name of the field that contains this id. */
private final String fieldNameInFile;

/** The row of the record in the file where the id is already present. */
private final int csvRowNumber;

public UniqueLocationIdViolationNotice(
String id, String fileWithIdAlreadyPresent, String fieldNameInFile, int csvRowNumber) {

this.id = id;
this.fileWithIdAlreadyPresent = fileWithIdAlreadyPresent;
this.fieldNameInFile = fieldNameInFile;
this.csvRowNumber = csvRowNumber;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,19 @@ public static GtfsTableContainer load(
csvFile = new CsvFile(csvInputStream, gtfsFilename, settings);
} catch (TextParsingException e) {
noticeContainer.addValidationNotice(new CsvParsingFailedNotice(gtfsFilename, e));
return tableDescriptor.createContainerForInvalidStatus(
GtfsTableContainer.TableStatus.INVALID_HEADERS);
return tableDescriptor.createContainerForInvalidStatus(TableStatus.INVALID_HEADERS);
}
if (csvFile.isEmpty()) {
noticeContainer.addValidationNotice(new EmptyFileNotice(gtfsFilename));
return tableDescriptor.createContainerForInvalidStatus(
GtfsTableContainer.TableStatus.EMPTY_FILE);
return tableDescriptor.createContainerForInvalidStatus(TableStatus.EMPTY_FILE);
}
final CsvHeader header = csvFile.getHeader();
final ImmutableList<GtfsColumnDescriptor> columnDescriptors = tableDescriptor.getColumns();
final NoticeContainer headerNotices =
validateHeaders(validatorProvider, gtfsFilename, header, columnDescriptors);
noticeContainer.addAll(headerNotices);
if (headerNotices.hasValidationErrors()) {
return tableDescriptor.createContainerForInvalidStatus(
GtfsTableContainer.TableStatus.INVALID_HEADERS);
return tableDescriptor.createContainerForInvalidStatus(TableStatus.INVALID_HEADERS);
}
final int nColumns = columnDescriptors.size();
final ImmutableMap<String, GtfsFieldLoader> fieldLoadersMap = tableDescriptor.getFieldLoaders();
Expand Down Expand Up @@ -133,15 +130,13 @@ public static GtfsTableContainer load(
}
} catch (TextParsingException e) {
noticeContainer.addValidationNotice(new CsvParsingFailedNotice(gtfsFilename, e));
return tableDescriptor.createContainerForInvalidStatus(
GtfsTableContainer.TableStatus.UNPARSABLE_ROWS);
return tableDescriptor.createContainerForInvalidStatus(TableStatus.UNPARSABLE_ROWS);
} finally {
logFieldCacheStats(gtfsFilename, fieldCaches, columnDescriptors);
}
if (hasUnparsableRows) {
logger.atSevere().log("Failed to parse some rows in %s", gtfsFilename);
return tableDescriptor.createContainerForInvalidStatus(
GtfsTableContainer.TableStatus.UNPARSABLE_ROWS);
return tableDescriptor.createContainerForInvalidStatus(TableStatus.UNPARSABLE_ROWS);
}
GtfsTableContainer table =
tableDescriptor.createContainerForHeaderAndEntities(header, entities, noticeContainer);
Expand Down Expand Up @@ -197,14 +192,12 @@ private static void logFieldCacheStats(
}
}

public static GtfsTableContainer loadMissingFile(
GtfsTableDescriptor tableDescriptor,
public static GtfsContainer loadMissingFile(
GtfsDescriptor tableDescriptor,
ValidatorProvider validatorProvider,
NoticeContainer noticeContainer) {
String gtfsFilename = tableDescriptor.gtfsFilename();
GtfsTableContainer table =
tableDescriptor.createContainerForInvalidStatus(
GtfsTableContainer.TableStatus.MISSING_FILE);
GtfsContainer table = tableDescriptor.createContainerForInvalidStatus(TableStatus.MISSING_FILE);
if (tableDescriptor.isRecommended()) {
noticeContainer.addValidationNotice(new MissingRecommendedFileNotice(gtfsFilename));
}
Expand Down
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add class comments to help understanding the purpose of this abstract class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added class comments.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.mobilitydata.gtfsvalidator.table;

import java.util.List;
import java.util.Optional;

public abstract class GtfsContainer<T extends GtfsEntity, D extends GtfsDescriptor> {

private final D descriptor;
private final TableStatus tableStatus;

public GtfsContainer(D descriptor, TableStatus tableStatus) {
this.tableStatus = tableStatus;
this.descriptor = descriptor;
}

public TableStatus getTableStatus() {
return tableStatus;
}

public D getDescriptor() {
return descriptor;
}

public abstract Class<T> getEntityClass();

public int entityCount() {
return getEntities().size();
}

public abstract List<T> getEntities();

public abstract String gtfsFilename();

public abstract Optional<T> byTranslationKey(String recordId, String recordSubId);

public boolean isMissingFile() {
return tableStatus == TableStatus.MISSING_FILE;
}

public boolean isParsedSuccessfully() {
switch (tableStatus) {
case PARSABLE_HEADERS_AND_ROWS:
return true;
case MISSING_FILE:
return !descriptor.isRequired();
default:
return false;
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, class comment could be useful.

Copy link
Contributor Author

@jcpitre jcpitre Sep 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added class comments.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.mobilitydata.gtfsvalidator.table;

// TODO: review class name maybe GtfsFileDescriptor
public abstract class GtfsDescriptor<T extends GtfsEntity> {

public abstract <C extends GtfsContainer> C createContainerForInvalidStatus(
TableStatus tableStatus);

// True if the specified file is required in a feed.
private boolean required;

private TableStatus tableStatus;

public abstract boolean isRecommended();

public abstract Class<T> getEntityClass();

public abstract String gtfsFilename();

public boolean isRequired() {
return this.required;
}

public void setRequired(boolean required) {
this.required = required;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@

import com.google.common.base.Ascii;
import java.util.*;
import org.mobilitydata.gtfsvalidator.table.GtfsTableContainer.TableStatus;

/**
* Container for a whole parsed GTFS feed with all its tables.
*
* <p>The tables are kept as {@code GtfsTableContainer} instances.
* <p>The tables are kept as {@code GtfsContainer} instances.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[doc]:

Suggested change
* <p>The tables are kept as {@code GtfsContainer} instances.
* <p>The tables are kept as {@code GtfsEntityContainer} instances.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

*/
public class GtfsFeedContainer {
private final Map<String, GtfsTableContainer<?>> tables = new HashMap<>();
private final Map<Class<? extends GtfsTableContainer>, GtfsTableContainer<?>> tablesByClass =
private final Map<String, GtfsContainer<?, ?>> tables = new HashMap<>();
private final Map<Class<? extends GtfsContainer>, GtfsContainer<?, ?>> tablesByClass =
new HashMap<>();

public GtfsFeedContainer(List<GtfsTableContainer<?>> tableContainerList) {
for (GtfsTableContainer<?> table : tableContainerList) {
public GtfsFeedContainer(List<GtfsContainer<?, ?>> tableContainerList) {
for (GtfsContainer<?, ?> table : tableContainerList) {
tables.put(table.gtfsFilename(), table);
tablesByClass.put(table.getClass(), table);
}
Expand All @@ -49,11 +48,12 @@ public GtfsFeedContainer(List<GtfsTableContainer<?>> tableContainerList) {
* @param filename file name, including ".txt" extension
* @return GTFS table or empty if the table is not supported by schema
*/
public Optional<GtfsTableContainer<?>> getTableForFilename(String filename) {
return Optional.ofNullable(tables.getOrDefault(Ascii.toLowerCase(filename), null));
public <T extends GtfsContainer<?, ?>> Optional<T> getTableForFilename(String filename) {
return (Optional<T>)
Optional.ofNullable(tables.getOrDefault(Ascii.toLowerCase(filename), null));
}

public <T extends GtfsTableContainer<?>> T getTable(Class<T> clazz) {
public <T extends GtfsContainer<?, ?>> T getTable(Class<T> clazz) {
return (T) tablesByClass.get(clazz);
}

Expand All @@ -65,21 +65,21 @@ public <T extends GtfsTableContainer<?>> T getTable(Class<T> clazz) {
* @return true if all files were successfully parsed, false otherwise
*/
public boolean isParsedSuccessfully() {
for (GtfsTableContainer<?> table : tables.values()) {
for (GtfsContainer<?, ?> table : tables.values()) {
if (!table.isParsedSuccessfully()) {
return false;
}
}
return true;
}

public Collection<GtfsTableContainer<?>> getTables() {
public Collection<GtfsContainer<?, ?>> getTables() {
return tables.values();
}

public String tableTotalsText() {
List<String> totalList = new ArrayList<>();
for (GtfsTableContainer<?> table : tables.values()) {
for (GtfsContainer<?, ?> table : tables.values()) {
if (table.getTableStatus() == TableStatus.MISSING_FILE
&& !table.getDescriptor().isRequired()) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.flogger.FluentLogger;
import java.io.InputStream;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -46,7 +47,7 @@
*/
public class GtfsFeedLoader {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private final HashMap<String, GtfsTableDescriptor<?>> tableDescriptors = new HashMap<>();
private final HashMap<String, GtfsDescriptor<?>> tableDescriptors = new HashMap<>();
private int numThreads = 1;

/**
Expand All @@ -56,12 +57,15 @@ public class GtfsFeedLoader {
private final List<Class<? extends FileValidator>> multiFileValidatorsWithParsingErrors =
new ArrayList<>();

public GtfsFeedLoader(
ImmutableList<Class<? extends GtfsTableDescriptor<?>>> tableDescriptorClasses) {
for (Class<? extends GtfsTableDescriptor<?>> clazz : tableDescriptorClasses) {
GtfsTableDescriptor<?> descriptor;
public GtfsFeedLoader(ImmutableList<Class<? extends GtfsDescriptor<?>>> tableDescriptorClasses) {
for (Class<? extends GtfsDescriptor<?>> clazz : tableDescriptorClasses) {
GtfsDescriptor<?> descriptor;
try {
descriptor = clazz.asSubclass(GtfsTableDescriptor.class).getConstructor().newInstance();
// Skipping abstract classes. Example: GtfsTableDescriptor.
if (Modifier.isAbstract(clazz.getModifiers())) {
continue;
}
descriptor = clazz.asSubclass(GtfsDescriptor.class).getConstructor().newInstance();
} catch (ReflectiveOperationException e) {
logger.atSevere().withCause(e).log(
"Possible bug in GTFS annotation processor: expected a constructor without parameters"
Expand All @@ -73,7 +77,7 @@ public GtfsFeedLoader(
}
}

public Collection<GtfsTableDescriptor<?>> getTableDescriptors() {
public Collection<GtfsDescriptor<?>> getTableDescriptors() {
return Collections.unmodifiableCollection(tableDescriptors.values());
}

Expand All @@ -100,19 +104,36 @@ public GtfsFeedContainer loadAndValidate(
Map<String, GtfsTableDescriptor<?>> remainingDescriptors =
(Map<String, GtfsTableDescriptor<?>>) tableDescriptors.clone();
for (String filename : gtfsInput.getFilenames()) {
GtfsTableDescriptor<?> tableDescriptor = remainingDescriptors.remove(filename.toLowerCase());
GtfsDescriptor<?> tableDescriptor = remainingDescriptors.remove(filename.toLowerCase());
if (tableDescriptor == null) {
noticeContainer.addValidationNotice(new UnknownFileNotice(filename));
} else {
loaderCallables.add(
() -> {
NoticeContainer loaderNotices = new NoticeContainer();
GtfsTableContainer<?> tableContainer;
GtfsContainer<?, ?> tableContainer;
try (InputStream inputStream = gtfsInput.getFile(filename)) {
try {
tableContainer =
AnyTableLoader.load(
tableDescriptor, validatorProvider, inputStream, loaderNotices);
if (tableDescriptor instanceof GtfsTableDescriptor) {
tableContainer =
AnyTableLoader.load(
(GtfsTableDescriptor) tableDescriptor,
validatorProvider,
inputStream,
loaderNotices);
} else if (tableDescriptor instanceof GtfsGeojsonFeatureDescriptor) {
tableContainer =
JsonFileLoader.load(
(GtfsGeojsonFeatureDescriptor) tableDescriptor,
validatorProvider,
inputStream,
loaderNotices);
} else {
logger.atSevere().log(
"Runtime exception table descriptor not supported: %s",
tableDescriptor.getClass().getName());
throw new RuntimeException("Table descriptor is not a supported type");
}
} catch (RuntimeException e) {
// This handler should prevent ExecutionException for
// this thread. We catch an exception here for storing
Expand All @@ -130,9 +151,9 @@ public GtfsFeedContainer loadAndValidate(
});
}
}
ArrayList<GtfsTableContainer<?>> tableContainers = new ArrayList<>();
ArrayList<GtfsContainer<?, ?>> tableContainers = new ArrayList<>();
tableContainers.ensureCapacity(tableDescriptors.size());
for (GtfsTableDescriptor<?> tableDescriptor : remainingDescriptors.values()) {
for (GtfsDescriptor<?> tableDescriptor : remainingDescriptors.values()) {
tableContainers.add(
AnyTableLoader.loadMissingFile(tableDescriptor, validatorProvider, noticeContainer));
}
Expand Down Expand Up @@ -186,11 +207,10 @@ private static void addThreadExecutionError(
}

static class TableAndNoticeContainers {
final GtfsTableContainer tableContainer;
final GtfsContainer tableContainer;
final NoticeContainer noticeContainer;

public TableAndNoticeContainers(
GtfsTableContainer tableContainer, NoticeContainer noticeContainer) {
public TableAndNoticeContainers(GtfsContainer tableContainer, NoticeContainer noticeContainer) {
this.tableContainer = tableContainer;
this.noticeContainer = noticeContainer;
}
Expand Down
Loading
Loading