Skip to content

Commit

Permalink
AER-2827 animal housing domain, validation and emission calculation (#…
Browse files Browse the repository at this point in the history
…261)

* Deprecated existing farm lodging domain classes
These are to be replaced by the farm animal housing classes that will be introduced.
* Domain objects for farm animal housing
Including json roundtrip with (I think) all of the aspects covered.
* Implemented emission calculator and validator for animal housing
Also added the necessary methods in EmissionSourceVisitor and such, with a default implementation, so we can go ahead with working on this without having to adjust something in the main branch for calculator.
  • Loading branch information
BertScholten authored Apr 24, 2024
1 parent 5239058 commit 16f2d56
Show file tree
Hide file tree
Showing 39 changed files with 2,011 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import nl.overheid.aerius.shared.domain.v2.source.EmissionSource;
import nl.overheid.aerius.shared.domain.v2.source.EmissionSourceFeature;
import nl.overheid.aerius.shared.domain.v2.source.EmissionSourceVisitor;
import nl.overheid.aerius.shared.domain.v2.source.FarmAnimalHousingEmissionSource;
import nl.overheid.aerius.shared.domain.v2.source.FarmLodgingEmissionSource;
import nl.overheid.aerius.shared.domain.v2.source.FarmlandEmissionSource;
import nl.overheid.aerius.shared.domain.v2.source.GenericEmissionSource;
Expand Down Expand Up @@ -171,6 +172,13 @@ public nl.overheid.aerius.gml.v5_1.source.EmissionSource visit(final FarmLodging
return new Farm2GML().convert(emissionSource);
}

@Override
public nl.overheid.aerius.gml.v5_1.source.EmissionSource visit(final FarmAnimalHousingEmissionSource emissionSource, final IsFeature feature)
throws AeriusException {
// In this version the animal housing is not implemented, just export as a generic source (other option would be an exception).
return new nl.overheid.aerius.gml.v5_1.source.EmissionSource();
}

@Override
public nl.overheid.aerius.gml.v5_1.source.EmissionSource visit(final FarmlandEmissionSource emissionSource, final IsFeature feature)
throws AeriusException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import nl.overheid.aerius.shared.domain.v2.source.EmissionSource;
import nl.overheid.aerius.shared.domain.v2.source.EmissionSourceFeature;
import nl.overheid.aerius.shared.domain.v2.source.EmissionSourceVisitor;
import nl.overheid.aerius.shared.domain.v2.source.FarmAnimalHousingEmissionSource;
import nl.overheid.aerius.shared.domain.v2.source.FarmLodgingEmissionSource;
import nl.overheid.aerius.shared.domain.v2.source.FarmlandEmissionSource;
import nl.overheid.aerius.shared.domain.v2.source.GenericEmissionSource;
Expand Down Expand Up @@ -166,6 +167,13 @@ public nl.overheid.aerius.gml.v6_0.source.EmissionSource visit(final FarmLodging
return new Farm2GML().convert(emissionSource);
}

@Override
public nl.overheid.aerius.gml.v6_0.source.EmissionSource visit(final FarmAnimalHousingEmissionSource emissionSource, final IsFeature feature)
throws AeriusException {
// TODO: actual IMAER part (AER-2828)
return new nl.overheid.aerius.gml.v6_0.source.EmissionSource();
}

@Override
public nl.overheid.aerius.gml.v6_0.source.EmissionSource visit(final FarmlandEmissionSource emissionSource, final IsFeature feature)
throws AeriusException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import nl.overheid.aerius.shared.domain.v2.source.shipping.maritime.ShippingMovementType;
import nl.overheid.aerius.shared.emissions.ColdStartEmissionFactorSupplier;
import nl.overheid.aerius.shared.emissions.EmissionFactorSupplier;
import nl.overheid.aerius.shared.emissions.FarmAnimalHousingEmissionFactorSupplier;
import nl.overheid.aerius.shared.emissions.FarmEmissionFactorType;
import nl.overheid.aerius.shared.emissions.FarmLodgingEmissionFactorSupplier;
import nl.overheid.aerius.shared.emissions.FarmlandEmissionFactorSupplier;
Expand All @@ -53,6 +54,7 @@
import nl.overheid.aerius.shared.emissions.shipping.ShippingLaden;
import nl.overheid.aerius.shared.exception.AeriusException;
import nl.overheid.aerius.validation.ColdStartValidationHelper;
import nl.overheid.aerius.validation.FarmAnimalHousingValidationHelper;
import nl.overheid.aerius.validation.FarmLodgingValidationHelper;
import nl.overheid.aerius.validation.FarmlandValidationHelper;
import nl.overheid.aerius.validation.InlandShippingValidationHelper;
Expand All @@ -66,20 +68,21 @@
* Test data for validation.
*/
public class TestValidationAndEmissionHelper implements ValidationHelper, EmissionFactorSupplier,
FarmLodgingEmissionFactorSupplier, FarmlandEmissionFactorSupplier, ManureStorageEmissionFactorSupplier, OffRoadMobileEmissionFactorSupplier,
ColdStartEmissionFactorSupplier, RoadEmissionFactorSupplier, InlandShippingEmissionFactorSupplier, MaritimeShippingEmissionFactorSupplier,
FarmLodgingValidationHelper, FarmlandValidationHelper, ManureStorageValidationHelper, OffRoadValidationHelper, ColdStartValidationHelper,
RoadValidationHelper, InlandShippingValidationHelper, MaritimeShippingValidationHelper {
FarmLodgingEmissionFactorSupplier, FarmAnimalHousingEmissionFactorSupplier, FarmlandEmissionFactorSupplier, ManureStorageEmissionFactorSupplier,
OffRoadMobileEmissionFactorSupplier, ColdStartEmissionFactorSupplier, RoadEmissionFactorSupplier, InlandShippingEmissionFactorSupplier,
MaritimeShippingEmissionFactorSupplier, FarmLodgingValidationHelper, FarmAnimalHousingValidationHelper, FarmlandValidationHelper,
ManureStorageValidationHelper, OffRoadValidationHelper, ColdStartValidationHelper, RoadValidationHelper, InlandShippingValidationHelper,
MaritimeShippingValidationHelper {

private static final List<FarmConstructHelper> FARM_LODGING_CATEGORIES = Arrays.asList(
new FarmConstructHelper("A1.4", 9.2, false),
new FarmConstructHelper("B1.100", 0.7, false),
new FarmConstructHelper("C1.100", 1.9, false),
new FarmConstructHelper("D3.1", 4.5, false, "BWL2001.21"),
new FarmConstructHelper("D3.1", 4.5, false),
new FarmConstructHelper("D1.3.3", 2.5, false),
new FarmConstructHelper("D3.2.7.2.1", 1.5, false),
new FarmConstructHelper("F4.4", 0.2, true),
new FarmConstructHelper("A4.2", 1.1, true, "BWL2011.12"),
new FarmConstructHelper("A4.2", 1.1, true),
new FarmConstructHelper("A3.100", 4.4, false),
new FarmConstructHelper("A2.100", 4.1, false),
new FarmConstructHelper("A1.1", 5.7, false),
Expand All @@ -99,6 +102,36 @@ public class TestValidationAndEmissionHelper implements ValidationHelper, Emissi
new FarmFodderConstructHelper("PAS2015.05-01", 0.2, 0.2, 0.2, 0.3, 0.7, true),
new FarmFodderConstructHelper("PAS2015.04-01", 0.1, 0.1, 0.1, 0.3, 0.7, false));

private static final List<String> FARM_ANIMAL_CATEGORIES = Arrays.asList(
"HA1",
"HA2",
"HA3",
"HA4",
"HB1",
"HC1",
"HD1",
"HD3",
"HF1");

private static final List<FarmConstructHelper> FARM_ANIMAL_HOUSING_CATEGORIES = Arrays.asList(
new FarmConstructHelper("HA1.1", 5.7),
new FarmConstructHelper("HA1.28", 9.9),
new FarmConstructHelper("HA1.100", 13),
new FarmConstructHelper("HA2.100", 4.4),
new FarmConstructHelper("HA3.2", 1.9),
new FarmConstructHelper("HA4.100", 4.1),
new FarmConstructHelper("HB1.100", 0.7),
new FarmConstructHelper("HC1.100", 1.9),
new FarmConstructHelper("HD1.3.2", 0.25),
new FarmConstructHelper("HD3.1", 2.4),
new FarmConstructHelper("HD3.8.2", 2.5),
new FarmConstructHelper("HF1.4", 0.2));

private static final List<FarmConstructHelper> FARM_ADDITIONAL_HOUSING_SYSTEM_CATEGORIES = Arrays.asList(
new FarmConstructHelper("LW1.1", 0.7, true),
new FarmConstructHelper("LW2.9", 0.7, true),
new FarmConstructHelper("AV1.1", 0.4, false));

private static final List<String> FARMLAND_CATEGORIES = Arrays.asList(
"PASTURE",
"MANURE",
Expand Down Expand Up @@ -403,13 +436,15 @@ private static class FarmConstructHelper {
final String code;
final double emissionFactor;
final boolean scrubber;
final List<String> systemDefinitions;

FarmConstructHelper(final String code, final double emissionFactor, final boolean scrubber, final String... systemDefinitions) {
FarmConstructHelper(final String code, final double emissionFactor) {
this(code, emissionFactor, false);
}

FarmConstructHelper(final String code, final double emissionFactor, final boolean scrubber) {
this.code = code;
this.emissionFactor = emissionFactor;
this.scrubber = scrubber;
this.systemDefinitions = Arrays.asList(systemDefinitions);
}
}

Expand Down Expand Up @@ -486,6 +521,11 @@ public FarmLodgingEmissionFactorSupplier farmLodging() {
return this;
}

@Override
public FarmAnimalHousingEmissionFactorSupplier farmAnimalHousing() {
return this;
}

@Override
public FarmlandEmissionFactorSupplier farmland() {
return this;
Expand Down Expand Up @@ -526,6 +566,11 @@ public FarmLodgingValidationHelper farmLodgingValidation() {
return this;
}

@Override
public FarmAnimalHousingValidationHelper farmAnimalHousingValidation() {
return this;
}

@Override
public FarmlandValidationHelper farmlandValidation() {
return this;
Expand Down Expand Up @@ -573,6 +618,11 @@ public FarmEmissionFactorType getLodgingEmissionFactorType(final String lodgingC
return FarmEmissionFactorType.PER_ANIMAL_PER_YEAR;
}

@Override
public FarmEmissionFactorType getAnimalHousingEmissionFactorType(final String animalHousingCode) {
return FarmEmissionFactorType.PER_ANIMAL_PER_YEAR;
}

@Override
public Map<Substance, Double> getFarmSourceEmissionFactors(final String farmSourceCategoryCode) {
return Map.of(Substance.NH3, 0.104);
Expand Down Expand Up @@ -706,6 +756,57 @@ public boolean isValidFarmLodgingFodderMeasureCode(final String fodderMeasureCod
return farmFodderMeasure(fodderMeasureCode).isPresent();
}

@Override
public Map<Substance, Double> getAnimalHousingEmissionFactors(final String animalHousingCode) {
return farmAnimalHousing(animalHousingCode)
.map(c -> Map.of(Substance.NH3, c.emissionFactor))
.orElse(Map.of());
}

@Override
public boolean isAdditionalHousingSystemAirScrubber(final String additionalSystemCode) {
return farmAnimalHousing(additionalSystemCode)
.map(c -> c.scrubber)
.orElse(false);
}

@Override
public String getAnimalBasicHousingCode(final String animalCode) {
return animalCode + ".100";
}

@Override
public Map<Substance, Double> getAdditionalHousingSystemReductionFractions(final String additionalSystemCode, final String animalCode) {
return farmAnimalHousing(additionalSystemCode)
.map(c -> Map.of(Substance.NH3, c.emissionFactor))
.orElse(Map.of());
}

@Override
public boolean isValidFarmAnimalCode(final String animalCode) {
return farmAnimal(animalCode).isPresent();
}

@Override
public boolean isValidFarmAnimalHousingCode(final String animalHousingCode) {
return farmAnimalHousing(animalHousingCode).isPresent();
}

@Override
public boolean isValidFarmAnimalHousingCombination(final String animalCode, final String animalHousingCode) {
return animalHousingCode.startsWith(animalCode);
}

@Override
public boolean isValidFarmAdditionalSystemCode(final String systemCode) {
return farmAdditionalHousingSystem(systemCode).isPresent();
}

@Override
public boolean isValidFarmAdditionalSystemCombination(final String animalCode, final String systemCode) {
return animalCode.startsWith("HA");
}

@Override
public boolean isValidFarmlandActivityCode(final String activityCode) {
return FARMLAND_CATEGORIES.stream()
Expand Down Expand Up @@ -942,6 +1043,24 @@ private Optional<FarmFodderConstructHelper> farmFodderMeasure(final String fodde
.findFirst();
}

private Optional<String> farmAnimal(final String farmAnimalCode) {
return FARM_ANIMAL_CATEGORIES.stream()
.filter(c -> c.equalsIgnoreCase(farmAnimalCode))
.findFirst();
}

private Optional<FarmConstructHelper> farmAnimalHousing(final String farmAnimalHousingCode) {
return FARM_ANIMAL_HOUSING_CATEGORIES.stream()
.filter(c -> c.code.equalsIgnoreCase(farmAnimalHousingCode))
.findFirst();
}

private Optional<FarmConstructHelper> farmAdditionalHousingSystem(final String farmAdditionalSystemCode) {
return FARM_ADDITIONAL_HOUSING_SYSTEM_CATEGORIES.stream()
.filter(c -> c.code.equalsIgnoreCase(farmAdditionalSystemCode))
.findFirst();
}

private Optional<OffRoadConstructHelper> offRoad(final String offRoadMobileSourceCode) {
return OFF_ROAD_MOBILE_SOURCE_CATEGORIES.stream()
.filter(c -> c.code.equalsIgnoreCase(offRoadMobileSourceCode))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
@JsonSubTypes({
@Type(value = GenericEmissionSource.class, name = EmissionSourceType.Names.GENERIC),
@Type(value = FarmLodgingEmissionSource.class, name = EmissionSourceType.Names.FARM_LODGE),
@Type(value = FarmAnimalHousingEmissionSource.class, name = EmissionSourceType.Names.FARM_ANIMAL_HOUSING),
@Type(value = FarmlandEmissionSource.class, name = EmissionSourceType.Names.FARMLAND),
@Type(value = ManureStorageEmissionSource.class, name = EmissionSourceType.Names.MANURE_STORAGE),
@Type(value = OffRoadMobileEmissionSource.class, name = EmissionSourceType.Names.OFFROAD_MOBILE),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@
public enum EmissionSourceType {
/**
* Farm Lodge emission values.
* @Deprecated Replaced by Animal Housing approach
*/
@Deprecated(forRemoval = true)
FARM_LODGE(Names.FARM_LODGE),
/**
* Farm animal housing emission values.
*/
FARM_ANIMAL_HOUSING(Names.FARM_ANIMAL_HOUSING),
/**
* Farmland emission values.
*/
Expand Down Expand Up @@ -83,6 +89,7 @@ public enum EmissionSourceType {

public static final class Names {
public static final String FARM_LODGE = "FARM_LODGE";
public static final String FARM_ANIMAL_HOUSING = "FARM_ANIMAL_HOUSING";
public static final String FARMLAND = "FARMLAND";
public static final String MANURE_STORAGE = "MANURE_STORAGE";
public static final String GENERIC = "GENERIC";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,19 @@
*/
public interface EmissionSourceVisitor<T> {

/**
* @Deprecated Replaced by animal housing, will be removed in the future.
*/
@Deprecated(forRemoval = true)
T visit(FarmLodgingEmissionSource emissionSource, IsFeature feature) throws AeriusException;

default T visit(final FarmAnimalHousingEmissionSource emissionSource, final IsFeature feature) throws AeriusException {
return null;
}

T visit(FarmlandEmissionSource emissionSource, IsFeature feature) throws AeriusException;

T visit(final ManureStorageEmissionSource emissionSource, final IsFeature feature) throws AeriusException;
T visit(ManureStorageEmissionSource emissionSource, IsFeature feature) throws AeriusException;

T visit(OffRoadMobileEmissionSource emissionSource, IsFeature feature) throws AeriusException;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright the State of the Netherlands
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package nl.overheid.aerius.shared.domain.v2.source;

import java.time.LocalDate;

import nl.overheid.aerius.shared.domain.v2.geojson.IsFeature;
import nl.overheid.aerius.shared.domain.v2.source.farm.FarmAnimalHousing;
import nl.overheid.aerius.shared.exception.AeriusException;

/**
* Emission source for farm animal housing.
*/
public class FarmAnimalHousingEmissionSource extends EmissionSourceWithSubSources<FarmAnimalHousing> {

private static final long serialVersionUID = 1L;

private LocalDate established;

public LocalDate getEstablished() {
return established;
}

public void setEstablished(final LocalDate established) {
this.established = established;
}

@Override
<T> T accept(final EmissionSourceVisitor<T> visitor, final IsFeature feature) throws AeriusException {
return visitor.visit(this, feature);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
import nl.overheid.aerius.shared.domain.v2.source.farm.FarmLodging;
import nl.overheid.aerius.shared.exception.AeriusException;

/**
* @Deprecated Replaced by Animal Housing approach
*/
@Deprecated(forRemoval = true)
public class FarmLodgingEmissionSource extends EmissionSourceWithSubSources<FarmLodging> {

private static final long serialVersionUID = 1L;
Expand Down
Loading

0 comments on commit 16f2d56

Please sign in to comment.