Skip to content

Commit

Permalink
Merge pull request #54 from bcgov/feature/VDYP-433
Browse files Browse the repository at this point in the history
Refactor model objects so Site is a property of Species
  • Loading branch information
mjunkin authored Aug 13, 2024
2 parents a470550 + ad1337b commit 2015855
Show file tree
Hide file tree
Showing 37 changed files with 2,065 additions and 1,834 deletions.
2 changes: 0 additions & 2 deletions vdyp-lib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>ca.bc.gov.nrs.vdyp</groupId>
<artifactId>vdyp-lib</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Variable Density Yield Project - Library</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
import ca.bc.gov.nrs.vdyp.model.VdypUtilizationHolder;
import ca.bc.gov.nrs.vdyp.model.VolumeComputeMode;

public abstract class VdypStartApplication<P extends BaseVdypPolygon<L, Optional<Float>, S, I>, L extends BaseVdypLayer<S, I> & InputLayer, S extends BaseVdypSpecies, I extends BaseVdypSite>
public abstract class VdypStartApplication<P extends BaseVdypPolygon<L, Optional<Float>, S, I>, L extends BaseVdypLayer<S, I> & InputLayer, S extends BaseVdypSpecies<I>, I extends BaseVdypSite>
extends VdypApplication implements Closeable {

private static final Logger log = LoggerFactory.getLogger(VdypStartApplication.class);
Expand Down Expand Up @@ -170,8 +170,8 @@ protected static void doMain(VdypStartApplication<?, ?, ?, ?> app, final String.

public EstimationMethods estimationMethods;

static final Comparator<BaseVdypSpecies> PERCENT_GENUS_DESCENDING = Utils
.compareUsing(BaseVdypSpecies::getPercentGenus).reversed();
static final Comparator<BaseVdypSpecies<?>> PERCENT_GENUS_DESCENDING = Utils
.compareUsing(BaseVdypSpecies<?>::getPercentGenus).reversed();

/**
* When finding primary species these genera should be combined
Expand Down Expand Up @@ -263,7 +263,7 @@ public void close() throws IOException {
closeVriWriter();
}

protected Coefficients getCoeForSpecies(BaseVdypSpecies species, ControlKey controlKey) {
protected Coefficients getCoeForSpecies(BaseVdypSpecies<?> species, ControlKey controlKey) {
var coeMap = Utils.<Map<String, Coefficients>>expectParsedControl(controlMap, controlKey, java.util.Map.class);
return coeMap.get(species.getGenus());
}
Expand Down Expand Up @@ -300,7 +300,7 @@ protected float getPercentTotal(L layer) throws StandProcessingException {
return percentTotal;
}

protected abstract S copySpecies(S toCopy, Consumer<BaseVdypSpecies.Builder<S>> config);
protected abstract S copySpecies(S toCopy, Consumer<BaseVdypSpecies.Builder<S, I, ?>> config);

/**
* Returns the primary, and secondary if present species records as a one or two element list.
Expand Down Expand Up @@ -633,7 +633,8 @@ protected float estimatePrimaryBaseArea(

public S leadGenus(L fipLayer) {
return fipLayer.getSpecies().values().stream()
.sorted(Utils.compareUsing(BaseVdypSpecies::getFractionGenus).reversed()).findFirst().orElseThrow();
.sorted(Utils.compareUsing(BaseVdypSpecies<? extends BaseVdypSite>::getFractionGenus).reversed())
.findFirst().orElseThrow();
}

protected L getPrimaryLayer(P poly) throws StandProcessingException {
Expand Down Expand Up @@ -896,12 +897,17 @@ protected float estimatePrimaryQuadMeanDiameter(
return quadMeanDiameter;
}

protected Map<String, Float>
applyGroups(BaseVdypPolygon<?, ?, ?, ?> fipPolygon, Collection<VdypSpecies> vdypSpecies)
throws ProcessingException {
// Lookup volume group, Decay Group, and Breakage group for each species.
protected Map<String, Float> applyGroupsAndGetTargetPercentages(
BaseVdypPolygon<?, ?, ?, ?> fipPolygon, Collection<VdypSpecies> vdypSpecies
) throws ProcessingException {

Map<String, Float> targetPercentages = new HashMap<>(vdypSpecies.size());
applyGroups(fipPolygon, vdypSpecies);
return getTargetPercentages(vdypSpecies);
}

protected void applyGroups(BaseVdypPolygon<?, ?, ?, ?> fipPolygon, Collection<VdypSpecies> vdypSpecies)
throws ProcessingException {
// Lookup volume group, Decay Group, and Breakage group for each species.

BecDefinition bec = Utils.getBec(fipPolygon.getBiogeoclimaticZone(), controlMap);
var volumeGroupMap = getGroupMap(ControlKey.VOLUME_EQN_GROUPS);
Expand All @@ -919,6 +925,15 @@ protected float estimatePrimaryQuadMeanDiameter(
vSpec.setDecayGroup(decayGroup);
vSpec.setBreakageGroup(breakageGroup);

}

}

protected Map<String, Float> getTargetPercentages(Collection<VdypSpecies> vdypSpecies) {
Map<String, Float> targetPercentages = new HashMap<>(vdypSpecies.size());

for (var vSpec : vdypSpecies) {

targetPercentages.put(vSpec.getGenus(), vSpec.getPercentGenus());
}

Expand Down Expand Up @@ -1116,6 +1131,7 @@ private float meanVolumeSmall(VdypSpecies spec, float quadMeanDiameterSpecSmall,
);
}

// YUC1
public void computeUtilizationComponentsPrimary(
BecDefinition bec, VdypLayer vdypLayer, VolumeComputeMode volumeComputeMode,
CompatibilityVariableMode compatibilityVariableMode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ public float primaryHeightFromLeadHeightInitial(
* @throws ProcessingException
*/
public float estimateNonPrimaryLoreyHeight(
BaseVdypSpecies vspec, BaseVdypSpecies vspecPrime, BecDefinition bec, float leadHeight, float primaryHeight
BaseVdypSpecies<?> vspec, BaseVdypSpecies<?> vspecPrime, BecDefinition bec, float leadHeight,
float primaryHeight
) throws ProcessingException {
return estimateNonPrimaryLoreyHeight(vspec.getGenus(), vspecPrime.getGenus(), bec, leadHeight, primaryHeight);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import static ca.bc.gov.nrs.vdyp.common_calculators.enumerations.SiteIndexAgeType.SI_AT_TOTAL;
import static ca.bc.gov.nrs.vdyp.common_calculators.enumerations.SiteIndexEstimationType.*;

import java.util.function.DoubleBinaryOperator;
import java.util.function.DoubleUnaryOperator;

import ca.bc.gov.nrs.vdyp.common.Utils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import static ca.bc.gov.nrs.vdyp.common_calculators.enumerations.SiteIndexEstimationType.SI_EST_DIRECT;

import java.util.function.DoubleBinaryOperator;
import java.util.function.DoubleUnaryOperator;

import ca.bc.gov.nrs.vdyp.common.Utils;
import ca.bc.gov.nrs.vdyp.common_calculators.custom_exceptions.CommonCalculatorException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ca.bc.gov.nrs.vdyp.model;

import java.text.MessageFormat;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
Expand All @@ -11,12 +12,11 @@
import java.util.function.Consumer;
import java.util.stream.Collectors;

public abstract class BaseVdypLayer<S extends BaseVdypSpecies, I extends BaseVdypSite> {
public abstract class BaseVdypLayer<S extends BaseVdypSpecies<I>, I extends BaseVdypSite> {

private final PolygonIdentifier polygonIdentifier;
private final LayerType layerType;
private LinkedHashMap<String, S> species = new LinkedHashMap<>();
private LinkedHashMap<String, I> sites = new LinkedHashMap<>();
private Optional<Integer> inventoryTypeGroup = Optional.empty();

protected BaseVdypLayer(
Expand Down Expand Up @@ -52,17 +52,9 @@ public void setSpecies(Collection<S> species) {
}

public LinkedHashMap<String, I> getSites() {
return sites;
}

public void setSites(Map<String, I> sites) {
this.sites.clear();
this.sites.putAll(sites);
}

public void setSites(Collection<I> sites) {
this.sites.clear();
sites.forEach(spec -> this.sites.put(spec.getSiteGenus(), spec));
var result = new LinkedHashMap<String, I>(species.size());
species.forEach((key, spec) -> spec.getSite().ifPresent(site -> result.put(key, site)));
return result;
}

public Optional<Integer> getInventoryTypeGroup() {
Expand All @@ -73,7 +65,7 @@ public void setInventoryTypeGroup(Optional<Integer> inventoryTypeGroup) {
this.inventoryTypeGroup = inventoryTypeGroup;
}

public abstract static class Builder<T extends BaseVdypLayer<S, I>, S extends BaseVdypSpecies, I extends BaseVdypSite, SB extends BaseVdypSpecies.Builder<S>, IB extends BaseVdypSite.Builder<I>>
public abstract static class Builder<T extends BaseVdypLayer<S, I>, S extends BaseVdypSpecies<I>, I extends BaseVdypSite, SB extends BaseVdypSpecies.Builder<S, I, IB>, IB extends BaseVdypSite.Builder<I>>
extends ModelClassBuilder<T> {
protected Optional<PolygonIdentifier> polygonIdentifier = Optional.empty();
protected Optional<LayerType> layerType = Optional.empty();
Expand Down Expand Up @@ -124,16 +116,6 @@ public Builder<T, S, I, SB, IB> addSpecies(S species) {
return this;
}

public Builder<T, S, I, SB, IB> addSite(Consumer<IB> config) {
siteBuilders.add(config);
return this;
}

public Builder<T, S, I, SB, IB> addSite(I site) {
this.sites.add(site);
return this;
}

public Builder<T, S, I, SB, IB> inventoryTypeGroup(int inventoryTypeGroup) {
return this.inventoryTypeGroup(Optional.of(inventoryTypeGroup));
}
Expand All @@ -155,37 +137,7 @@ public Builder<T, S, I, SB, IB> copy(T toCopy) {
return this;
}

public <I2 extends BaseVdypSite> Builder<T, S, I, SB, IB>
adaptSites(BaseVdypLayer<?, I2> toCopy, BiConsumer<IB, I2> config) {
toCopy.getSites().values().forEach(siteToCopy -> {
this.adaptSite(siteToCopy, config);
});
return this;
}

public <I2 extends BaseVdypSite> Builder<T, S, I, SB, IB> adaptSite(I2 toCopy, BiConsumer<IB, I2> config) {
this.addSite(builder -> {
builder.adapt(toCopy);
builder.polygonIdentifier = Optional.empty();
builder.layerType = Optional.empty();
config.accept(builder, toCopy);
});
return this;
}

public Builder<T, S, I, SB, IB> copySites(T toCopy, BiConsumer<IB, I> config) {
toCopy.getSites().values().forEach(siteToCopy -> {
this.addSite(builder -> {
builder.copy(siteToCopy);
builder.polygonIdentifier = Optional.empty();
builder.layerType = Optional.empty();
config.accept(builder, siteToCopy);
});
});
return this;
}

public <S2 extends BaseVdypSpecies> Builder<T, S, I, SB, IB>
public <S2 extends BaseVdypSpecies<I2>, I2 extends BaseVdypSite> Builder<T, S, I, SB, IB>
adaptSpecies(BaseVdypLayer<S2, ?> toCopy, BiConsumer<SB, S2> config) {
toCopy.getSpecies().values().forEach(speciesToCopy -> {
this.addSpecies(builder -> {
Expand Down Expand Up @@ -232,24 +184,28 @@ protected void check(Collection<String> errors) {

protected abstract S buildSpecies(Consumer<SB> config);

protected abstract I buildSite(Consumer<IB> config);

/**
* Build any builders for child objects and store the results. This will clear the stored child builders.
*/
public void buildChildren() {
speciesBuilders.stream().map(this::buildSpecies).collect(Collectors.toCollection(() -> species));
speciesBuilders.clear();
siteBuilders.stream().map(this::buildSite).collect(Collectors.toCollection(() -> sites));
siteBuilders.clear();
}

@Override
protected void postProcess(T result) {
super.postProcess(result);
buildChildren();
result.setSpecies(species);
result.setSites(sites);
}

@Override
protected String getBuilderId() {
return MessageFormat.format(
"Layer {0} {1}", //
polygonIdentifier.map(Object::toString).orElse("N/A"), //
layerType.map(Object::toString).orElse("N/A") //
);
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ca.bc.gov.nrs.vdyp.model;

import java.text.MessageFormat;
import java.util.Collection;
import java.util.EnumMap;
import java.util.LinkedHashMap;
Expand All @@ -12,7 +13,7 @@
import java.util.function.Function;
import java.util.stream.Collectors;

public abstract class BaseVdypPolygon<L extends BaseVdypLayer<SP, SI>, PA, SP extends BaseVdypSpecies, SI extends BaseVdypSite> {
public abstract class BaseVdypPolygon<L extends BaseVdypLayer<SP, SI>, PA, SP extends BaseVdypSpecies<SI>, SI extends BaseVdypSite> {

private PolygonIdentifier polygonIdentifier; // FIP_P/POLYDESC
private PA percentAvailable; // FIP_P2/PCTFLAND
Expand Down Expand Up @@ -107,10 +108,10 @@ protected abstract static class Builder< //
T extends BaseVdypPolygon<L, PA, SP, SI>, //
L extends BaseVdypLayer<SP, SI>, //
PA, //
SP extends BaseVdypSpecies, //
SP extends BaseVdypSpecies<SI>, //
SI extends BaseVdypSite, //
LB extends BaseVdypLayer.Builder<L, SP, SI, SPB, SIB>, //
SPB extends BaseVdypSpecies.Builder<SP>, //
SPB extends BaseVdypSpecies.Builder<SP, SI, SIB>, //
SIB extends BaseVdypSite.Builder<SI>> //

extends ModelClassBuilder<T> {
Expand Down Expand Up @@ -254,5 +255,13 @@ public void buildChildren() {
layersBuilders.clear();
}

@Override
protected String getBuilderId() {
return MessageFormat.format(
"Polygon {0}", //
polygonIdentifier.map(Object::toString).orElse("N/A") //
);
}

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ca.bc.gov.nrs.vdyp.model;

import java.text.MessageFormat;
import java.util.Collection;
import java.util.Optional;

Expand Down Expand Up @@ -173,6 +174,16 @@ protected void check(Collection<String> errors) {
requirePresent(siteGenus, "siteGenus", errors);
}

@Override
protected String getBuilderId() {
return MessageFormat.format(
"Site {0} {1} {2}", //
polygonIdentifier.map(Object::toString).orElse("N/A"), //
layerType.map(Object::toString).orElse("N/A"), //
siteGenus.map(Object::toString).orElse("N/A")//
);
}

}

}
Loading

0 comments on commit 2015855

Please sign in to comment.