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

Open
wants to merge 18 commits into
base: master
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
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,53 @@
/*
* 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;

/**
* 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))
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 @@ -7,48 +7,42 @@
import com.univocity.parsers.csv.CsvParserSettings;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.mobilitydata.gtfsvalidator.notice.CsvParsingFailedNotice;
import org.mobilitydata.gtfsvalidator.notice.EmptyFileNotice;
import org.mobilitydata.gtfsvalidator.notice.MissingRecommendedFileNotice;
import org.mobilitydata.gtfsvalidator.notice.MissingRequiredFileNotice;
import org.mobilitydata.gtfsvalidator.notice.NoticeContainer;
import org.mobilitydata.gtfsvalidator.parsing.CsvFile;
import org.mobilitydata.gtfsvalidator.parsing.CsvHeader;
import org.mobilitydata.gtfsvalidator.parsing.CsvRow;
import org.mobilitydata.gtfsvalidator.parsing.FieldCache;
import org.mobilitydata.gtfsvalidator.parsing.RowParser;
import org.mobilitydata.gtfsvalidator.validator.FileValidator;
import org.mobilitydata.gtfsvalidator.validator.SingleEntityValidator;
import org.mobilitydata.gtfsvalidator.validator.ValidatorProvider;
import org.mobilitydata.gtfsvalidator.validator.ValidatorUtil;

public final class AnyTableLoader {
/** This class loads csv files specifically. */
public final class AnyTableLoader extends TableLoader {

private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private static final List<Class<? extends FileValidator>> singleFileValidatorsWithParsingErrors =
new ArrayList<>();
private AnyTableLoader() {}
// Create the singleton and add a method to obtain it
private static final AnyTableLoader INSTANCE = new AnyTableLoader();

private static final List<Class<? extends SingleEntityValidator>>
singleEntityValidatorsWithParsingErrors = new ArrayList<>();

public List<Class<? extends FileValidator>> getValidatorsWithParsingErrors() {
return Collections.unmodifiableList(singleFileValidatorsWithParsingErrors);
public static AnyTableLoader getInstance() {
return INSTANCE;
}

public List<Class<? extends SingleEntityValidator>> getSingleEntityValidatorsWithParsingErrors() {
return Collections.unmodifiableList(singleEntityValidatorsWithParsingErrors);
}
private final FluentLogger logger = FluentLogger.forEnclosingClass();

public static GtfsTableContainer load(
GtfsTableDescriptor tableDescriptor,
@Override
public GtfsEntityContainer<?, ?> load(
GtfsFileDescriptor fileDescriptor,
ValidatorProvider validatorProvider,
InputStream csvInputStream,
NoticeContainer noticeContainer) {
GtfsTableDescriptor tableDescriptor = (GtfsTableDescriptor) fileDescriptor;
final String gtfsFilename = tableDescriptor.gtfsFilename();

CsvFile csvFile;
Expand All @@ -61,22 +55,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 All @@ -99,8 +90,8 @@ public static GtfsTableContainer load(
final List<GtfsEntity> entities = new ArrayList<>();
boolean hasUnparsableRows = false;
final List<SingleEntityValidator<GtfsEntity>> singleEntityValidators =
validatorProvider.createSingleEntityValidators(
tableDescriptor.getEntityClass(), singleEntityValidatorsWithParsingErrors::add);
createSingleEntityValidators(tableDescriptor.getEntityClass(), validatorProvider);

try {
for (CsvRow row : csvFile) {
if (row.getRowNumber() % 200000 == 0) {
Expand Down Expand Up @@ -133,26 +124,23 @@ 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);

ValidatorUtil.invokeSingleFileValidators(
validatorProvider.createSingleFileValidators(
table, singleFileValidatorsWithParsingErrors::add),
noticeContainer);
createSingleFileValidators(table, validatorProvider), noticeContainer);
return table;
}

private static NoticeContainer validateHeaders(
private NoticeContainer validateHeaders(
ValidatorProvider validatorProvider,
String gtfsFilename,
CsvHeader header,
Expand All @@ -178,7 +166,7 @@ private static NoticeContainer validateHeaders(
return headerNotices;
}

private static void logFieldCacheStats(
private void logFieldCacheStats(
String gtfsFilename,
FieldCache[] fieldCaches,
ImmutableList<GtfsColumnDescriptor> columnDescriptors) {
Expand All @@ -197,24 +185,4 @@ private static void logFieldCacheStats(
}
}

public static GtfsTableContainer loadMissingFile(
GtfsTableDescriptor tableDescriptor,
ValidatorProvider validatorProvider,
NoticeContainer noticeContainer) {
String gtfsFilename = tableDescriptor.gtfsFilename();
GtfsTableContainer table =
tableDescriptor.createContainerForInvalidStatus(
GtfsTableContainer.TableStatus.MISSING_FILE);
if (tableDescriptor.isRecommended()) {
noticeContainer.addValidationNotice(new MissingRecommendedFileNotice(gtfsFilename));
}
if (tableDescriptor.isRequired()) {
noticeContainer.addValidationNotice(new MissingRequiredFileNotice(gtfsFilename));
}
ValidatorUtil.invokeSingleFileValidators(
validatorProvider.createSingleFileValidators(
table, singleFileValidatorsWithParsingErrors::add),
noticeContainer);
return table;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.mobilitydata.gtfsvalidator.table;

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

/**
* This class is the parent of the containers holding table (csv) entities and containers holding
* geojson entities
*
* @param <T> The entity for this container (e.g. GtfsCalendarDate or {@link GtfsGeojsonFeature} )
* @param <D> The descriptor for the table for the container (e.g. GtfsCalendarDateTableDescriptor
* or {@link GtfsGeojsonFileDescriptor})
*/
public abstract class GtfsEntityContainer<T extends GtfsEntity, D extends GtfsFileDescriptor> {

private final D descriptor;
private final TableStatus tableStatus;

public GtfsEntityContainer(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;
}
}
}
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.
*/
public class GtfsFeedContainer {
private final Map<String, GtfsTableContainer<?>> tables = new HashMap<>();
private final Map<Class<? extends GtfsTableContainer>, GtfsTableContainer<?>> tablesByClass =
private final Map<String, GtfsEntityContainer<?, ?>> tables = new HashMap<>();
private final Map<Class<? extends GtfsEntityContainer>, GtfsEntityContainer<?, ?>> tablesByClass =
new HashMap<>();

public GtfsFeedContainer(List<GtfsTableContainer<?>> tableContainerList) {
for (GtfsTableContainer<?> table : tableContainerList) {
public GtfsFeedContainer(List<GtfsEntityContainer<?, ?>> tableContainerList) {
for (GtfsEntityContainer<?, ?> 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 GtfsEntityContainer<?, ?>> 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 GtfsEntityContainer<?, ?>> 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 (GtfsEntityContainer<?, ?> table : tables.values()) {
if (!table.isParsedSuccessfully()) {
return false;
}
}
return true;
}

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

public String tableTotalsText() {
List<String> totalList = new ArrayList<>();
for (GtfsTableContainer<?> table : tables.values()) {
for (GtfsEntityContainer<?, ?> table : tables.values()) {
if (table.getTableStatus() == TableStatus.MISSING_FILE
&& !table.getDescriptor().isRequired()) {
continue;
Expand Down
Loading