Skip to content

Commit

Permalink
Moreaccessor methods from parser to BecLookup
Browse files Browse the repository at this point in the history
  • Loading branch information
smithkm committed Jan 11, 2024
1 parent e26b4fa commit fff973c
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,39 +91,6 @@ public static BecLookup getBecs(Map<String, Object> 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<BecDefinition> getBecsByRegion(Map<String, Object> 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<String> getBecAliases(Map<String, Object> 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<BecDefinition> getBecsByScope(Map<String, Object> control, String scope) {
return getBecs(control).getBecsForScope(scope);
}

@Override
public String getControlKey() {
return CONTROL_KEY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public boolean isStopLine(String line) {
@Override
public MatrixMap2<String, String, Coefficients> parse(InputStream is, Map<String, Object> control)
throws IOException, ResourceParseException {
var becAliases = BecDefinitionParser.getBecAliases(control);
var becAliases = BecDefinitionParser.getBecs(control).getBecAliases();
var speciesIndecies = GenusDefinitionParser.getSpeciesAliases(control);
MatrixMap2<String, String, Coefficients> result = new MatrixMap2Impl<>(
becAliases, speciesIndecies, (k1, k2) -> Coefficients.empty(NUM_COEFFICIENTS, 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static <U> ControlledValueParser<Optional<U>> optional(ControlledValuePar
*/
static final ControlledValueParser<String> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public MatrixMap2<String, String, Integer> parse(InputStream is, Map<String, Obj

final var sp0Keys = GenusDefinitionParser.getSpeciesAliases(control);

final var becKeys = BecDefinitionParser.getBecAliases(control);
final var becKeys = BecDefinitionParser.getBecs(control).getBecAliases();

Map<String, Map<String, Integer>> resultMap = lineParser.parse(is, new HashMap<>(), (v, r) -> {
final String sp0Alias = (String) v.get("sp0Alias");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public boolean isStopLine(String line) {
@Override
public MatrixMap3<Integer, String, String, Coefficients> parse(InputStream is, Map<String, Object> 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);

Expand All @@ -54,7 +54,7 @@ public MatrixMap3<Integer, String, String, Coefficients> 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);
}
Expand Down
19 changes: 16 additions & 3 deletions vdyp-core/src/main/java/ca/bc/gov/nrs/vdyp/model/BecLookup.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,13 @@ public Collection<BecDefinition> 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<BecDefinition> getBecsForScope(String scope) {
if (scope.isBlank()) {
Expand All @@ -91,4 +94,14 @@ public Collection<BecDefinition> 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<String> getBecAliases() {
return getBecs().stream().map(BecDefinition::getAlias).toList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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")));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public static void populateControlMapEquationGroups(
BiFunction<String, String, int[]> mapper
) {

var becAliases = BecDefinitionParser.getBecAliases(controlMap);
var becAliases = BecDefinitionParser.getBecs(controlMap).getBecAliases();
var genusAliases = GenusDefinitionParser.getSpeciesAliases(controlMap);

var volume = new MatrixMap2Impl<String, String, Integer>(genusAliases, becAliases, mapper.andThen(x -> x[0]));
Expand Down

0 comments on commit fff973c

Please sign in to comment.