Skip to content

Commit

Permalink
Add method to validate a whole model
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 14, 2020
1 parent e45b739 commit b9a9989
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
Expand Up @@ -71,6 +71,17 @@ Sets the model ``name``.
Sets the model ``group``.

.. seealso:: :py:func:`group`
%End

bool validate( QStringList &issues /Out/ ) const;
%Docstring
Validates the model, returning ``True`` if all child algorithms in the model are valid.


:return: - ``True`` if the child is valid
- issues: will be set to a list of issues encountered during the validation

.. versionadded:: 3.14
%End

QMap<QString, QgsProcessingModelChildAlgorithm> childAlgorithms() const;
Expand Down
24 changes: 24 additions & 0 deletions src/core/processing/models/qgsprocessingmodelalgorithm.cpp
Expand Up @@ -1130,6 +1130,30 @@ void QgsProcessingModelAlgorithm::setGroup( const QString &group )
mModelGroup = group;
}

bool QgsProcessingModelAlgorithm::validate( QStringList &issues ) const
{
issues.clear();
bool res = true;

if ( mChildAlgorithms.empty() )
{
res = false;
issues << QObject::tr( "Model does not contain any algorithms" );
}

for ( auto it = mChildAlgorithms.constBegin(); it != mChildAlgorithms.constEnd(); ++it )
{
QStringList childIssues;
res = validateChildAlgorithm( it->childId(), childIssues ) && res;

for ( const QString &issue : qgis::as_const( childIssues ) )
{
issues << QStringLiteral( "<b>%1</b>: %2" ).arg( it->description(), issue );
}
}
return res;
}

QMap<QString, QgsProcessingModelChildAlgorithm> QgsProcessingModelAlgorithm::childAlgorithms() const
{
return mChildAlgorithms;
Expand Down
9 changes: 9 additions & 0 deletions src/core/processing/models/qgsprocessingmodelalgorithm.h
Expand Up @@ -72,6 +72,15 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm
*/
void setGroup( const QString &group );

/**
* Validates the model, returning TRUE if all child algorithms in the model are valid.
*
* \param issues will be set to a list of issues encountered during the validation
* \returns TRUE if the child is valid
* \since QGIS 3.14
*/
bool validate( QStringList &issues SIP_OUT ) const;

/**
* Returns the map of child algorithms contained in the model. The keys
* are the child algorithm ids (see QgsProcessingModelAlgorithm::ChildAlgorithm::childId()).
Expand Down
15 changes: 14 additions & 1 deletion tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -9899,19 +9899,29 @@ void TestQgsProcessing::modelAcceptableValues()
void TestQgsProcessing::modelValidate()
{
QgsProcessingModelAlgorithm m;
QStringList errors;
QVERIFY( !m.validate( errors ) );
QCOMPARE( errors.size(), 1 );
QCOMPARE( errors.at( 0 ), QStringLiteral( "Model does not contain any algorithms" ) );

QgsProcessingModelParameter stringParam1( "string" );
m.addModelParameter( new QgsProcessingParameterString( "string" ), stringParam1 );
QgsProcessingModelChildAlgorithm alg2c1;
alg2c1.setChildId( "cx1" );
alg2c1.setAlgorithmId( "native:centroids" );
alg2c1.setDescription( QStringLiteral( "centroids" ) );
m.addChildAlgorithm( alg2c1 );

QStringList errors;
QVERIFY( !m.validateChildAlgorithm( QStringLiteral( "cx1" ), errors ) );
QCOMPARE( errors.size(), 2 );
QCOMPARE( errors.at( 0 ), QStringLiteral( "Parameter <i>INPUT</i> is mandatory" ) );
QCOMPARE( errors.at( 1 ), QStringLiteral( "Parameter <i>ALL_PARTS</i> is mandatory" ) );

QVERIFY( !m.validate( errors ) );
QCOMPARE( errors.size(), 2 );
QCOMPARE( errors.at( 0 ), QStringLiteral( "<b>centroids</b>: Parameter <i>INPUT</i> is mandatory" ) );
QCOMPARE( errors.at( 1 ), QStringLiteral( "<b>centroids</b>: Parameter <i>ALL_PARTS</i> is mandatory" ) );

QgsProcessingModelChildParameterSource badSource;
badSource.setSource( QgsProcessingModelChildParameterSource::StaticValue );
badSource.setStaticValue( 56 );
Expand Down Expand Up @@ -9952,6 +9962,9 @@ void TestQgsProcessing::modelValidate()

QVERIFY( m.validateChildAlgorithm( QStringLiteral( "cx1" ), errors ) );
QCOMPARE( errors.size(), 0 );

QVERIFY( m.validate( errors ) );
QCOMPARE( errors.size(), 0 );
}

void TestQgsProcessing::tempUtils()
Expand Down

0 comments on commit b9a9989

Please sign in to comment.