From f57894ea9dfd951cb3cb1351a22f8c3214a045be Mon Sep 17 00:00:00 2001 From: James Brown <64858662+james-d-brown@users.noreply.github.com> Date: Wed, 17 Jul 2024 18:30:31 +0100 Subject: [PATCH] When checking that data is available for a declared variable name, relax to allow either the variable name or one of the declared aliases to select data, #43. --- .../src/wres/io/project/ProjectUtilities.java | 99 ++++++++++++------- 1 file changed, 63 insertions(+), 36 deletions(-) diff --git a/wres-io/src/wres/io/project/ProjectUtilities.java b/wres-io/src/wres/io/project/ProjectUtilities.java index 9cea91f93b..0a0e3fb0e9 100644 --- a/wres-io/src/wres/io/project/ProjectUtilities.java +++ b/wres-io/src/wres/io/project/ProjectUtilities.java @@ -290,14 +290,13 @@ private static void checkDeclaredAndIngestedVariablesAgree( EvaluationDeclaratio Set baseline, Set covariates ) { - String declaredLeftVariableName = DeclarationUtilities.getVariableName( declaration.left() ); - String declaredRightVariableName = DeclarationUtilities.getVariableName( declaration.right() ); - String declaredBaselineVariableName = null; - + Set declaredLeft = ProjectUtilities.getVariableNames( declaration.left() ); + Set declaredRight = ProjectUtilities.getVariableNames( declaration.right() ); + Set declaredBaseline = Collections.emptySet(); if ( DeclarationUtilities.hasBaseline( declaration ) ) { - declaredBaselineVariableName = DeclarationUtilities.getVariableName( declaration.baseline() - .dataset() ); + declaredBaseline = ProjectUtilities.getVariableNames( declaration.baseline() + .dataset() ); } // Where a name has been declared and the ingested names are available, the declared name must be among them @@ -307,30 +306,33 @@ private static void checkDeclaredAndIngestedVariablesAgree( EvaluationDeclaratio StringBuilder covariateError = new StringBuilder(); String start = " - "; - if ( Objects.nonNull( declaredLeftVariableName ) + if ( !declaredLeft.isEmpty() && !left.isEmpty() - && !left.contains( declaredLeftVariableName ) ) + && left.stream() + .noneMatch( declaredLeft::contains ) ) { leftError += System.lineSeparator() + start; - leftError += ProjectUtilities.getVariableErrorMessage( declaredLeftVariableName, + leftError += ProjectUtilities.getVariableErrorMessage( declaredLeft, left, DatasetOrientation.LEFT ); } - if ( Objects.nonNull( declaredRightVariableName ) + if ( !declaredRight.isEmpty() && !right.isEmpty() - && !right.contains( declaredRightVariableName ) ) + && right.stream() + .noneMatch( declaredRight::contains ) ) { rightError += System.lineSeparator() + start; - rightError += ProjectUtilities.getVariableErrorMessage( declaredRightVariableName, + rightError += ProjectUtilities.getVariableErrorMessage( declaredRight, right, DatasetOrientation.RIGHT ); } - if ( Objects.nonNull( declaredBaselineVariableName ) + if ( !declaredBaseline.isEmpty() && !baseline.isEmpty() - && !baseline.contains( declaredBaselineVariableName ) ) + && baseline.stream() + .noneMatch( declaredBaseline::contains ) ) { baselineError += System.lineSeparator() + start; - baselineError += ProjectUtilities.getVariableErrorMessage( declaredRightVariableName, + baselineError += ProjectUtilities.getVariableErrorMessage( declaredBaseline, baseline, DatasetOrientation.BASELINE ); } @@ -338,20 +340,14 @@ private static void checkDeclaredAndIngestedVariablesAgree( EvaluationDeclaratio // Iterate through the covariates for ( CovariateDataset covariate : declaration.covariates() ) { - if ( Objects.nonNull( covariate.dataset() - .variable() ) - && Objects.nonNull( covariate.dataset() - .variable() - .name() ) - && !covariates.contains( covariate.dataset() - .variable() - .name() ) ) + Set declaredNames = ProjectUtilities.getVariableNames( covariate.dataset() ); + if ( !declaredNames.isEmpty() + && covariates.stream() + .noneMatch( declaredNames::contains ) ) { covariateError.append( System.lineSeparator() ) .append( start ); - covariateError.append( ProjectUtilities.getVariableErrorMessage( covariate.dataset() - .variable() - .name(), + covariateError.append( ProjectUtilities.getVariableErrorMessage( declaredNames, covariates, DatasetOrientation.COVARIATE ) ); } @@ -373,13 +369,43 @@ private static void checkDeclaredAndIngestedVariablesAgree( EvaluationDeclaratio } /** - * Generates an error message when a declared variable is not among the ingested options. - * @param variableName the variable name + * Looks for a variable name. + * @param dataset the dataset + * @return the declared variable name or null if no variable was declared + * @throws NullPointerException if the dataset is null + */ + + private static Set getVariableNames( Dataset dataset ) + { + Objects.requireNonNull( dataset ); + + Set names = new TreeSet<>(); + + if ( Objects.nonNull( dataset.variable() ) ) + { + if ( Objects.nonNull( dataset.variable() + .name() ) ) + { + String variableName = dataset.variable() + .name(); + names.add( variableName ); + } + + names.addAll( dataset.variable() + .aliases() ); + } + + return Collections.unmodifiableSet( names ); + } + + /** + * Generates an error message when a declared variable or one of its aliases are not among the ingested options. + * @param variableNames the declared variable names * @param nameOptions the name options * @param orientation the dataset orientation * @return the error message */ - private static String getVariableErrorMessage( String variableName, + private static String getVariableErrorMessage( Set variableNames, Set nameOptions, DatasetOrientation orientation ) { @@ -389,16 +415,17 @@ private static String getVariableErrorMessage( String variableName, article = "a"; } - return "The declared 'name' of the 'variable' for " + return "The declared 'name' and 'aliases' of the 'variable' for " + article + " '" + orientation - + "' dataset was '" - + variableName - + "', but this could not be found among the variable names of the ingested data sources, which " - + "were: " + + "' dataset included: " + + variableNames + + ". However, none of these names were found among the variable names of the ingested data sources, " + + "whose names included: " + nameOptions - + "."; + + ". Please declare the 'name' or 'aliases' of a 'variable' that selects data for at least one of the " + + "geographic features contained in this evaluation and try again."; } /** @@ -1777,4 +1804,4 @@ private static boolean hasVariableName( Dataset dataset ) && Objects.nonNull( dataset.variable() .name() ); } -} +} \ No newline at end of file