Skip to content

Commit 628a9e0

Browse files
authored
AER-3988 Improve flow on handling import year (#350)
When importing the sources are checked for having emissions. However when both the specific import year and year in GML are for years for which no emission factors are available all sources that calculate emissions will be reported as having no emissions. This is not desired because upstream the year will be set to an valid year and emissions will be recalculated again making these earlier reported missing emissions invalid. Therefore it is better to always make sure the year used is a valid year. Because it's not known what a valid year is this is delegated by adding a method to GMLHelper to get the year. The implementation should always return a valid year, and also the implementation has the option to report a warning about the year in case it was adjusted. This implementation uses a default implementation in the interface, which makes it backwards compatible. With this new implementation we always assume the year is adjusted to a valid year it doesn't add much to also validate the year to be out of certain ranges. Therefore these GMLValidator#validateYear method has been removed.
1 parent acf4582 commit 628a9e0

9 files changed

Lines changed: 25 additions & 206 deletions

File tree

source/imaer-gml/src/main/java/nl/overheid/aerius/gml/GMLValidator.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727

2828
public final class GMLValidator {
2929

30-
private static final int YEAR_LOWER_BOUNDARY = 1900;
31-
private static final int YEAR_UPPER_BOUNDARY = 2100;
32-
3330
private GMLValidator() {
3431
// Util class
3532
}
@@ -40,14 +37,6 @@ public static void validateGMLVersion(final AeriusGMLVersion version, final List
4037
}
4138
}
4239

43-
public static void validateYear(final int year, final List<AeriusException> exceptions) {
44-
if (year < YEAR_LOWER_BOUNDARY) {
45-
exceptions.add(new AeriusException(ImaerExceptionReason.GML_VALIDATION_FAILED, "year must be greater than " + YEAR_LOWER_BOUNDARY));
46-
} else if (year > YEAR_UPPER_BOUNDARY) {
47-
exceptions.add(new AeriusException(ImaerExceptionReason.GML_VALIDATION_FAILED, "year must be less than " + YEAR_UPPER_BOUNDARY));
48-
}
49-
}
50-
5140
/**
5241
* Validates all meta data elements. Adds any violation to the list of exceptions.
5342
*

source/imaer-gml/src/main/java/nl/overheid/aerius/gml/base/GMLHelper.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package nl.overheid.aerius.gml.base;
1818

1919
import java.util.List;
20+
import java.util.Optional;
2021

2122
import nl.overheid.aerius.gml.base.source.ship.v31.GMLInlandShippingSupplier;
2223
import nl.overheid.aerius.shared.domain.geo.ReceptorGridSettings;
@@ -57,4 +58,19 @@ default EmissionSourceLimits getEmissionSourceGeometryLimits() {
5758
// Added default for backward compatibility. Will be removed when used in Calculator.
5859
return null;
5960
}
61+
62+
/**
63+
* Returns the year to use for importing and the year that will be used for calculation emissions.
64+
* Default implementation uses importYear or if not set the situationYear.
65+
* Overriding implementations can force the year to be a year within the range of years emission values are available for.
66+
* And to add a warning in case the year was changed.
67+
*
68+
* @param importYear year specified as being imported
69+
* @param situationYear year as specified in the situation
70+
* @param warnings list to add warnings in case the year is different from specified years.
71+
* @return the year to use for import
72+
*/
73+
default int yearToUseForImport(final Optional<Integer> importYear, final int situationYear, final List<AeriusException> warnings) {
74+
return importYear.orElse(situationYear);
75+
}
6076
}

source/imaer-gml/src/main/java/nl/overheid/aerius/importer/ImaerImporter.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public class ImaerImporter {
7373
private static final Logger LOGGER = LoggerFactory.getLogger(ImaerImporter.class);
7474

7575
private final GMLReaderFactory factory;
76+
private final GMLHelper gmlHelper;
7677
private final EPSG epsg;
7778
private final EmissionSourceLimits limits;
7879

@@ -83,6 +84,7 @@ public class ImaerImporter {
8384
* @throws AeriusException
8485
*/
8586
public ImaerImporter(final GMLHelper gmlHelper, final GMLReaderFactory gmlReaderFactory) throws AeriusException {
87+
this.gmlHelper = gmlHelper;
8688
factory = gmlReaderFactory;
8789
epsg = gmlHelper.getReceptorGridSettings().getEPSG();
8890
limits = gmlHelper.getEmissionSourceGeometryLimits();
@@ -146,14 +148,13 @@ public void importStream(final InputStream inputStream, final Set<ImportOption>
146148
}
147149

148150
final AeriusGMLVersion version = reader.getVersion();
149-
setImportResultMetaData(result, reader);
151+
setImportResultMetaData(result, reader, importYear);
150152
GMLValidator.validateMetaData(result.getImportedMetaData(), result.getExceptions(), ImportOption.VALIDATE_METADATA.in(importOptions)
151153
&& result.getArchiveMetaData() == null);
152-
GMLValidator.validateYear(result.getSituation().getYear(), result.getExceptions());
153154
GMLValidator.validateGMLVersion(version, result.getWarnings());
154155

155156
final ScenarioSituation situation = addSituationProperties(reader, result);
156-
addEmissionSources(reader, importOptions, result, importYear);
157+
addEmissionSources(reader, importOptions, result);
157158
addAeriusPoints(reader, importOptions, result);
158159
addCimlkMeasures(reader, importOptions, result, situation);
159160
addCimlkDispersionLines(reader, importOptions, situation);
@@ -194,15 +195,14 @@ protected GMLReader createGMLReader(final InputStream inputStream, final Set<Imp
194195
result.getExceptions(), result.getWarnings());
195196
}
196197

197-
private void addEmissionSources(final GMLReader reader, final Set<ImportOption> importOptions, final ImportParcel result,
198-
final Optional<Integer> importYear) throws AeriusException {
198+
private void addEmissionSources(final GMLReader reader, final Set<ImportOption> importOptions, final ImportParcel result) throws AeriusException {
199199
if (ImportOption.INCLUDE_SOURCES.in(importOptions)) {
200200
final List<EmissionSourceFeature> sources = reader.readEmissionSourceList();
201201
if (ImportOption.VALIDATE_SOURCES.in(importOptions)) {
202202
EmissionSourceValidator.validateSources(sources, result.getExceptions(), result.getWarnings(),
203203
factory.createValidationHelper());
204204
}
205-
reader.enforceEmissions(sources, importYear.orElse(result.getSituation().getYear()));
205+
reader.enforceEmissions(sources, result.getSituation().getYear());
206206
if (ImportOption.VALIDATE_SOURCES.in(importOptions)) {
207207
EmissionSourceValidator.validateSourcesWithEmissions(sources, result.getExceptions(), result.getWarnings());
208208
}
@@ -324,9 +324,9 @@ private static ArrayList<CalculationPointFeature> filterResultPoints(final List<
324324
return list;
325325
}
326326

327-
private static void setImportResultMetaData(final ImportParcel result, final GMLReader reader) throws AeriusException {
327+
private void setImportResultMetaData(final ImportParcel result, final GMLReader reader, final Optional<Integer> importYear) throws AeriusException {
328328
final GMLMetaDataReader metaDataReader = reader.metaDataReader();
329-
result.getSituation().setYear(metaDataReader.readYear());
329+
result.getSituation().setYear(gmlHelper.yearToUseForImport(importYear, reader.metaDataReader().readYear(), result.getWarnings()));
330330
result.setVersion(metaDataReader.readAeriusVersion());
331331
result.setDatabaseVersion(metaDataReader.readDatabaseVersion());
332332
result.setGmlCreator(metaDataReader.readGmlCreator());

source/imaer-gml/src/test/java/nl/overheid/aerius/gml/AssertGML.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ static GMLHelper mockGMLHelper() throws AeriusException {
236236
static GMLHelper mockGMLHelper(final CharacteristicsType ct) throws AeriusException {
237237
final GMLHelper gmlHelper = mock(GMLHelper.class);
238238
when(gmlHelper.getReceptorGridSettings()).thenReturn(ReceptorGridSettings.NL);
239+
when(gmlHelper.yearToUseForImport(any(), anyInt(), any())).thenCallRealMethod();
239240
mockEmissionSourceGeometryLimits(gmlHelper);
240241
when(gmlHelper.getCharacteristicsType()).thenReturn(ct);
241242
final TestValidationAndEmissionHelper validationAndEmissionHelper = new TestValidationAndEmissionHelper(

source/imaer-gml/src/test/java/nl/overheid/aerius/gml/GMLValidateErrorsTest.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -233,18 +233,6 @@ void testGMLUnknownError() throws IOException {
233233
assertResult("fout_666_unknown_error", "GML Unknown error", ImaerExceptionReason.INTERNAL_ERROR, IllegalArgumentException.class);
234234
}
235235

236-
@Test
237-
void testGMLYear2100() throws IOException {
238-
assertResult("fout_year_over_2100", "GML year greater than", ImaerExceptionReason.GML_VALIDATION_FAILED,
239-
e -> assertEquals("year must be less than 2100", e.getArgs()[0], "Year invalid"));
240-
}
241-
242-
@Test
243-
void testGMLYear1900() throws IOException {
244-
assertResult("fout_year_under_1900", "GML year under than", ImaerExceptionReason.GML_VALIDATION_FAILED,
245-
e -> assertEquals("year must be greater than 1900", e.getArgs()[0], "Year invalid"));
246-
}
247-
248236
@Test
249237
void testGMLCustomTimeVaryingProfileUnknownType() throws IOException {
250238
assertResult("fout_1023_custom_time_varying_profile", "GML custom time-varying profile type",

source/imaer-gml/src/test/java/nl/overheid/aerius/gml/InvalidGMLTest.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,6 @@ void testInvalidXML3() throws IOException, AeriusException {
8181
assertResult("invalid_xml_3", "Expected GML parse error", ImaerExceptionReason.GML_VALIDATION_FAILED);
8282
}
8383

84-
@Test
85-
void testInvalidYear() throws IOException, AeriusException {
86-
assertResult("invalid_year", "Expected incorrect year message", ImaerExceptionReason.GML_VALIDATION_FAILED);
87-
}
88-
8984
@Test
9085
void testUnsupportedAeriusGMLVersion() throws IOException, AeriusException {
9186
assertResult("unsupported_aerius_gml_version", "", ImaerExceptionReason.GML_VERSION_NOT_SUPPORTED);

source/imaer-gml/src/test/resources/gml/latest/validate/errors/fout_year_over_2100.gml

Lines changed: 0 additions & 57 deletions
This file was deleted.

source/imaer-gml/src/test/resources/gml/latest/validate/errors/fout_year_under_1900.gml

Lines changed: 0 additions & 57 deletions
This file was deleted.

source/imaer-gml/src/test/resources/gml/latest/validate/invalid_year.gml

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)