Skip to content

Commit

Permalink
Add method to model API to get available dependencies for a child alg…
Browse files Browse the repository at this point in the history
…orithm
  • Loading branch information
nyalldawson committed Apr 16, 2020
1 parent b216759 commit e3955df
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
Expand Up @@ -191,6 +191,13 @@ Returns a list of the child algorithm IDs on which the child
algorithm with the specified ``childId`` depends upon.

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

QList< QgsProcessingModelChildDependency > availableDependenciesForChildAlgorithm( const QString &childId ) const;
%Docstring
Returns details of available dependencies for the child algorithm with matching id.

.. versionadded:: 3.14
%End

bool validateChildAlgorithm( const QString &childId, QStringList &issues /Out/ ) const;
Expand Down
25 changes: 25 additions & 0 deletions src/core/processing/models/qgsprocessingmodelalgorithm.cpp
Expand Up @@ -1718,6 +1718,31 @@ QSet< QString > QgsProcessingModelAlgorithm::dependsOnChildAlgorithms( const QSt
return algs;
}

QList<QgsProcessingModelChildDependency> QgsProcessingModelAlgorithm::availableDependenciesForChildAlgorithm( const QString &childId ) const
{
QSet< QString > dependent;
if ( !childId.isEmpty() )
{
dependent.unite( dependentChildAlgorithms( childId ) );
dependent.insert( childId );
}

QList<QgsProcessingModelChildDependency> res;
for ( auto it = mChildAlgorithms.constBegin(); it != mChildAlgorithms.constEnd(); ++it )
{
if ( !dependent.contains( it->childId() ) )
{
QgsProcessingModelChildDependency alg;
alg.childId = it->childId();
res << alg;

//TODO -- conditional branches!

}
}
return res;
}

bool QgsProcessingModelAlgorithm::validateChildAlgorithm( const QString &childId, QStringList &issues ) const
{
issues.clear();
Expand Down
7 changes: 7 additions & 0 deletions src/core/processing/models/qgsprocessingmodelalgorithm.h
Expand Up @@ -170,6 +170,13 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm
*/
QSet< QString > dependsOnChildAlgorithms( const QString &childId ) const;

/**
* Returns details of available dependencies for the child algorithm with matching id.
*
* \since QGIS 3.14
*/
QList< QgsProcessingModelChildDependency > availableDependenciesForChildAlgorithm( const QString &childId ) const;

/**
* Validates the child algorithm with matching ID, returning TRUE if
* all mandatory inputs to the algorithm have valid values.
Expand Down
23 changes: 23 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -8750,13 +8750,15 @@ void TestQgsProcessing::modelerAlgorithm()
QgsProcessingModelAlgorithm alg3( "test", "testGroup" );
QVERIFY( alg3.dependentChildAlgorithms( "notvalid" ).isEmpty() );
QVERIFY( alg3.dependsOnChildAlgorithms( "notvalid" ).isEmpty() );
QVERIFY( alg3.availableDependenciesForChildAlgorithm( QStringLiteral( "notvalid" ) ).isEmpty() );

// add a child
QgsProcessingModelChildAlgorithm c7;
c7.setChildId( "c7" );
alg3.addChildAlgorithm( c7 );
QVERIFY( alg3.dependentChildAlgorithms( "c7" ).isEmpty() );
QVERIFY( alg3.dependsOnChildAlgorithms( "c7" ).isEmpty() );
QVERIFY( alg3.availableDependenciesForChildAlgorithm( QStringLiteral( "c7" ) ).isEmpty() );

// direct dependency
QgsProcessingModelChildAlgorithm c8;
Expand All @@ -8769,6 +8771,9 @@ void TestQgsProcessing::modelerAlgorithm()
QVERIFY( alg3.dependentChildAlgorithms( "c7" ).contains( "c8" ) );
QCOMPARE( alg3.dependsOnChildAlgorithms( "c8" ).count(), 1 );
QVERIFY( alg3.dependsOnChildAlgorithms( "c8" ).contains( "c7" ) );
QVERIFY( alg3.availableDependenciesForChildAlgorithm( QStringLiteral( "c7" ) ).isEmpty() );
QCOMPARE( alg3.availableDependenciesForChildAlgorithm( QStringLiteral( "c8" ) ).size(), 1 );
QCOMPARE( alg3.availableDependenciesForChildAlgorithm( QStringLiteral( "c8" ) ).at( 0 ).childId, QStringLiteral( "c7" ) );

// dependency via parameter source
QgsProcessingModelChildAlgorithm c9;
Expand All @@ -8789,6 +8794,13 @@ void TestQgsProcessing::modelerAlgorithm()
QVERIFY( alg3.dependsOnChildAlgorithms( "c9" ).contains( "c7" ) );
QVERIFY( alg3.dependsOnChildAlgorithms( "c9" ).contains( "c8" ) );

QVERIFY( alg3.availableDependenciesForChildAlgorithm( QStringLiteral( "c7" ) ).isEmpty() );
QCOMPARE( alg3.availableDependenciesForChildAlgorithm( QStringLiteral( "c8" ) ).size(), 1 );
QCOMPARE( alg3.availableDependenciesForChildAlgorithm( QStringLiteral( "c8" ) ).at( 0 ).childId, QStringLiteral( "c7" ) );
QCOMPARE( alg3.availableDependenciesForChildAlgorithm( QStringLiteral( "c9" ) ).size(), 2 );
QVERIFY( alg3.availableDependenciesForChildAlgorithm( QStringLiteral( "c9" ) ).contains( QgsProcessingModelChildDependency( QStringLiteral( "c7" ) ) ) );
QVERIFY( alg3.availableDependenciesForChildAlgorithm( QStringLiteral( "c9" ) ).contains( QgsProcessingModelChildDependency( QStringLiteral( "c8" ) ) ) );

QgsProcessingModelChildAlgorithm c9b;
c9b.setChildId( "c9b" );
c9b.addParameterSources( "x", QgsProcessingModelChildParameterSources() << QgsProcessingModelChildParameterSource::fromChildOutput( "c9", "x" ) );
Expand All @@ -8814,6 +8826,17 @@ void TestQgsProcessing::modelerAlgorithm()
QVERIFY( alg3.dependsOnChildAlgorithms( "c9b" ).contains( "c8" ) );
QVERIFY( alg3.dependsOnChildAlgorithms( "c9b" ).contains( "c9" ) );

QVERIFY( alg3.availableDependenciesForChildAlgorithm( QStringLiteral( "c7" ) ).isEmpty() );
QCOMPARE( alg3.availableDependenciesForChildAlgorithm( QStringLiteral( "c8" ) ).size(), 1 );
QCOMPARE( alg3.availableDependenciesForChildAlgorithm( QStringLiteral( "c8" ) ).at( 0 ).childId, QStringLiteral( "c7" ) );
QCOMPARE( alg3.availableDependenciesForChildAlgorithm( QStringLiteral( "c9" ) ).size(), 2 );
QVERIFY( alg3.availableDependenciesForChildAlgorithm( QStringLiteral( "c9" ) ).contains( QgsProcessingModelChildDependency( QStringLiteral( "c7" ) ) ) );
QVERIFY( alg3.availableDependenciesForChildAlgorithm( QStringLiteral( "c9" ) ).contains( QgsProcessingModelChildDependency( QStringLiteral( "c8" ) ) ) );
QCOMPARE( alg3.availableDependenciesForChildAlgorithm( QStringLiteral( "c9b" ) ).size(), 3 );
QVERIFY( alg3.availableDependenciesForChildAlgorithm( QStringLiteral( "c9b" ) ).contains( QgsProcessingModelChildDependency( QStringLiteral( "c7" ) ) ) );
QVERIFY( alg3.availableDependenciesForChildAlgorithm( QStringLiteral( "c9b" ) ).contains( QgsProcessingModelChildDependency( QStringLiteral( "c8" ) ) ) );
QVERIFY( alg3.availableDependenciesForChildAlgorithm( QStringLiteral( "c9b" ) ).contains( QgsProcessingModelChildDependency( QStringLiteral( "c9" ) ) ) );

alg3.removeChildAlgorithm( "c9b" );


Expand Down

0 comments on commit e3955df

Please sign in to comment.