Skip to content

Commit

Permalink
Merge pull request #45 from NOAA-OWP/issue43
Browse files Browse the repository at this point in the history
When checking that data is available for a declared variable name, re…
  • Loading branch information
james-d-brown authored Jul 17, 2024
2 parents b1047c0 + f57894e commit b1820f2
Showing 1 changed file with 63 additions and 36 deletions.
99 changes: 63 additions & 36 deletions wres-io/src/wres/io/project/ProjectUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,13 @@ private static void checkDeclaredAndIngestedVariablesAgree( EvaluationDeclaratio
Set<String> baseline,
Set<String> covariates )
{
String declaredLeftVariableName = DeclarationUtilities.getVariableName( declaration.left() );
String declaredRightVariableName = DeclarationUtilities.getVariableName( declaration.right() );
String declaredBaselineVariableName = null;

Set<String> declaredLeft = ProjectUtilities.getVariableNames( declaration.left() );
Set<String> declaredRight = ProjectUtilities.getVariableNames( declaration.right() );
Set<String> 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
Expand All @@ -307,51 +306,48 @@ 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 );
}

// 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<String> 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 ) );
}
Expand All @@ -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<String> getVariableNames( Dataset dataset )
{
Objects.requireNonNull( dataset );

Set<String> 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<String> variableNames,
Set<String> nameOptions,
DatasetOrientation orientation )
{
Expand All @@ -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.";
}

/**
Expand Down Expand Up @@ -1777,4 +1804,4 @@ private static boolean hasVariableName( Dataset dataset )
&& Objects.nonNull( dataset.variable()
.name() );
}
}
}

0 comments on commit b1820f2

Please sign in to comment.