Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When checking that data is available for a declared variable name, re… #45

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() );
}
}
}
Loading