From fff973cc3cbe0cecf1f8b30475d68213edab0860 Mon Sep 17 00:00:00 2001 From: Kevin Smith Date: Wed, 10 Jan 2024 19:15:58 -0800 Subject: [PATCH] Moreaccessor methods from parser to BecLookup --- .../vdyp/io/parse/BecDefinitionParser.java | 33 ------------------- .../nrs/vdyp/io/parse/CoefficientParser.java | 2 +- .../vdyp/io/parse/ControlledValueParser.java | 2 +- .../vdyp/io/parse/EquationGroupParser.java | 2 +- .../vdyp/io/parse/UtilComponentParser.java | 4 +-- .../ca/bc/gov/nrs/vdyp/model/BecLookup.java | 19 +++++++++-- .../io/parse/BecDefinitionParserTest.java | 13 ++++++++ .../ca/bc/gov/nrs/vdyp/test/TestUtils.java | 2 +- 8 files changed, 35 insertions(+), 42 deletions(-) diff --git a/vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/io/parse/BecDefinitionParser.java b/vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/io/parse/BecDefinitionParser.java index 974a22bd3..860f188d1 100644 --- a/vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/io/parse/BecDefinitionParser.java +++ b/vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/io/parse/BecDefinitionParser.java @@ -91,39 +91,6 @@ public static BecLookup getBecs(Map control) { return Utils.expectParsedControl(control, CONTROL_KEY, BecLookup.class); } - /** - * Get all the BECs in the specified region - * - * @param control Control map containing the parsed BEC definitions. - * @return - */ - public static Collection getBecsByRegion(Map control, Region region) { - return getBecs(control).getBecsForRegion(region); - } - - /** - * Get all the aliases for defined BECs - * - * @param control Control map containing the parsed BEC definitions. - * @return - */ - public static Collection getBecAliases(Map control) { - return getBecs(control).getBecs().stream().map(BecDefinition::getAlias).toList(); - } - - /** - * Find a set of BECs for the given scope. If the scope is blank, that's all - * BECs, if it's a Region alias, it's all BECs for that region, otherwise its - * treated as a BEC alias and the BEC matching it is returned. - * - * @param control Control map containing the parsed BEC definitions. - * @param scope The scope to match - * @return A collection of matching BECs, or the empty set if none match. - */ - public static Collection getBecsByScope(Map control, String scope) { - return getBecs(control).getBecsForScope(scope); - } - @Override public String getControlKey() { return CONTROL_KEY; diff --git a/vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/io/parse/CoefficientParser.java b/vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/io/parse/CoefficientParser.java index a193d37d6..0fcc08f78 100644 --- a/vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/io/parse/CoefficientParser.java +++ b/vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/io/parse/CoefficientParser.java @@ -48,7 +48,7 @@ public boolean isStopLine(String line) { @Override public MatrixMap2 parse(InputStream is, Map control) throws IOException, ResourceParseException { - var becAliases = BecDefinitionParser.getBecAliases(control); + var becAliases = BecDefinitionParser.getBecs(control).getBecAliases(); var speciesIndecies = GenusDefinitionParser.getSpeciesAliases(control); MatrixMap2 result = new MatrixMap2Impl<>( becAliases, speciesIndecies, (k1, k2) -> Coefficients.empty(NUM_COEFFICIENTS, 0) diff --git a/vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/io/parse/ControlledValueParser.java b/vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/io/parse/ControlledValueParser.java index ca173df93..32f8d5f07 100644 --- a/vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/io/parse/ControlledValueParser.java +++ b/vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/io/parse/ControlledValueParser.java @@ -113,7 +113,7 @@ public static ControlledValueParser> optional(ControlledValuePar */ static final ControlledValueParser BEC = (string, control) -> { var result = string.strip(); - if (!BecDefinitionParser.getBecAliases(control).contains(result)) { + if (!BecDefinitionParser.getBecs(control).getBecAliases().contains(result)) { throw new ValueParseException(string, string + " is not a valid BEC"); } return result; diff --git a/vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/io/parse/EquationGroupParser.java b/vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/io/parse/EquationGroupParser.java index 4bc7c0a77..c158c3bd8 100644 --- a/vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/io/parse/EquationGroupParser.java +++ b/vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/io/parse/EquationGroupParser.java @@ -49,7 +49,7 @@ public MatrixMap2 parse(InputStream is, Map> resultMap = lineParser.parse(is, new HashMap<>(), (v, r) -> { final String sp0Alias = (String) v.get("sp0Alias"); diff --git a/vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/io/parse/UtilComponentParser.java b/vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/io/parse/UtilComponentParser.java index 23d53b5bc..892f7b1f4 100644 --- a/vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/io/parse/UtilComponentParser.java +++ b/vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/io/parse/UtilComponentParser.java @@ -42,7 +42,7 @@ public boolean isStopLine(String line) { @Override public MatrixMap3 parse(InputStream is, Map control) throws IOException, ResourceParseException { - final var becIndices = BecDefinitionParser.getBecAliases(control); + final var becIndices = BecDefinitionParser.getBecs(control).getBecAliases(); final var speciesIndicies = GenusDefinitionParser.getSpeciesAliases(control); final var ucIndices = Arrays.asList(1, 2, 3, 4); @@ -54,7 +54,7 @@ public MatrixMap3 parse(InputStream is, M var sp0 = (String) v.get(SPECIES_KEY); var scope = (String) v.get(BEC_SCOPE_KEY); - var becs = BecDefinitionParser.getBecsByScope(control, scope); + var becs = BecDefinitionParser.getBecs(control).getBecsForScope(scope); if (becs.isEmpty()) { throw new ValueParseException(scope, "Could not find any BECs for scope " + scope); } diff --git a/vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/model/BecLookup.java b/vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/model/BecLookup.java index 72060f315..4bd1772cf 100644 --- a/vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/model/BecLookup.java +++ b/vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/model/BecLookup.java @@ -78,10 +78,13 @@ public Collection getBecsForRegion(Region region) { } /** - * Get all becs for a scope + * Find a set of BECs for the given scope. If the scope is blank, that's all + * BECs, if it's a Region alias, it's all BECs for that region, otherwise its + * treated as a BEC alias and the BEC matching it is returned. + * + * @param scope The scope to match + * @return A collection of matching BECs, or the empty set if none match. * - * @param scope Scope to search for - * @return */ public Collection getBecsForScope(String scope) { if (scope.isBlank()) { @@ -91,4 +94,14 @@ public Collection getBecsForScope(String scope) { .orElseGet(() -> this.get(scope).map(Collections::singletonList).orElseGet(Collections::emptyList)); } + /** + * Get all the aliases for defined BECs + * + * @param control Control map containing the parsed BEC definitions. + * @return + */ + public Collection getBecAliases() { + return getBecs().stream().map(BecDefinition::getAlias).toList(); + } + } diff --git a/vdyp-core/src/test/java/ca/bc/gov/nrs/vdyp/io/parse/BecDefinitionParserTest.java b/vdyp-core/src/test/java/ca/bc/gov/nrs/vdyp/io/parse/BecDefinitionParserTest.java index a55a99a37..491f770f7 100644 --- a/vdyp-core/src/test/java/ca/bc/gov/nrs/vdyp/io/parse/BecDefinitionParserTest.java +++ b/vdyp-core/src/test/java/ca/bc/gov/nrs/vdyp/io/parse/BecDefinitionParserTest.java @@ -5,7 +5,9 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.hasProperty; +import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.sameInstance; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.ArrayList; import java.util.Collections; @@ -14,11 +16,13 @@ import static org.hamcrest.Matchers.equalTo; +import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; import ca.bc.gov.nrs.vdyp.model.BecDefinition; import ca.bc.gov.nrs.vdyp.model.BecLookup; import ca.bc.gov.nrs.vdyp.model.Region; +import ca.bc.gov.nrs.vdyp.test.TestUtils; class BecDefinitionParserTest { @@ -254,4 +258,13 @@ void testParse() throws Exception { assertThat(result.get("SWB").get().getVolumeBec(), sameInstance(result.get("SWB").get())); } + @Test + void testParseNoDefault() throws Exception { + var parser = new BecDefinitionParser(); + try (var is = TestUtils.makeStream("")) { + var ex = assertThrows(IllegalStateException.class, () -> parser.parse(is, Collections.emptyMap())); + assertThat(ex, hasProperty("message", Matchers.is("Could not find default BEC ESSF"))); + } + } + } diff --git a/vdyp-core/src/test/java/ca/bc/gov/nrs/vdyp/test/TestUtils.java b/vdyp-core/src/test/java/ca/bc/gov/nrs/vdyp/test/TestUtils.java index 9eab2da45..c6d001615 100644 --- a/vdyp-core/src/test/java/ca/bc/gov/nrs/vdyp/test/TestUtils.java +++ b/vdyp-core/src/test/java/ca/bc/gov/nrs/vdyp/test/TestUtils.java @@ -158,7 +158,7 @@ public static void populateControlMapEquationGroups( BiFunction mapper ) { - var becAliases = BecDefinitionParser.getBecAliases(controlMap); + var becAliases = BecDefinitionParser.getBecs(controlMap).getBecAliases(); var genusAliases = GenusDefinitionParser.getSpeciesAliases(controlMap); var volume = new MatrixMap2Impl(genusAliases, becAliases, mapper.andThen(x -> x[0]));