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

[processing] Fix feature iteration against a model #60194

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,13 @@ Returns the model results, populated when the context is used to run a model alg
%End


void clearModelResult();
%Docstring
Clears model results previously populated when the context was used to run a model algorithm.

.. versionadded:: 3.42
%End

private:
QgsProcessingContext( const QgsProcessingContext &other );
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,13 @@ Returns the model results, populated when the context is used to run a model alg
%End


void clearModelResult();
%Docstring
Clears model results previously populated when the context was used to run a model algorithm.

.. versionadded:: 3.42
%End

private:
QgsProcessingContext( const QgsProcessingContext &other );
};
Expand Down
3 changes: 3 additions & 0 deletions python/plugins/processing/gui/AlgorithmExecutor.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,9 @@ def executeIterating(alg, parameters, paramToIter, context, feedback):
if feedback.isCanceled():
return False

# clear any model result stored in the last iteration
context.clearModelResult()

parameters[paramToIter] = f
for out in alg.destinationParameterDefinitions():
if out.name() not in outputs:
Expand Down
4 changes: 4 additions & 0 deletions src/core/processing/models/qgsprocessingmodelalgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,9 @@ QVariantMap QgsProcessingModelAlgorithm::processAlgorithm( const QVariantMap &pa
break;

if ( executed.contains( childId ) )
{
continue;
}

bool canExecute = true;
const QSet< QString > dependencies = dependsOnChildAlgorithms( childId );
Expand All @@ -387,7 +389,9 @@ QVariantMap QgsProcessingModelAlgorithm::processAlgorithm( const QVariantMap &pa
}

if ( !canExecute )
{
continue;
}

executedAlg = true;

Expand Down
5 changes: 5 additions & 0 deletions src/core/processing/qgsprocessingcontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,8 @@ std::unique_ptr< QgsProcessingModelInitialRunConfig > QgsProcessingContext::take
{
return std::move( mModelConfig );
}

void QgsProcessingContext::clearModelResult()
{
mModelResult.clear();
}
7 changes: 7 additions & 0 deletions src/core/processing/qgsprocessingcontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,13 @@ class CORE_EXPORT QgsProcessingContext
*/
QgsProcessingModelResult &modelResult() SIP_SKIP { return mModelResult; }

/**
* Clears model results previously populated when the context was used to run a model algorithm.
*
* \since QGIS 3.42
Copy link
Contributor

@agiudiceandrea agiudiceandrea Jan 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the PR must be backported to release-3_40 branch, shouldn't this method be tagged as "since QGIS 3.40" or will it be adapted somehow for QGIS 3.40 in the backporting PR?

*/
void clearModelResult();

private:

QgsProcessingContext::Flags mFlags = QgsProcessingContext::Flags();
Expand Down
15 changes: 15 additions & 0 deletions tests/src/analysis/testqgsprocessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,21 @@ void TestQgsProcessing::context()
QCOMPARE( context3.modelResult().childResults().value( QStringLiteral( "CHILD1" ) ).outputs().value( QStringLiteral( "RESULT1" ) ).toInt(), 1 );
QCOMPARE( context3.modelResult().childResults().value( QStringLiteral( "CHILD2" ) ).outputs().value( QStringLiteral( "RESULT2" ) ).toInt(), 2 );

QgsProcessingContext context4;
context4.takeResultsFrom( context );
context4.modelResult().rawChildInputs().insert( QStringLiteral( "test_input" ), 1 );
context4.modelResult().rawChildOutputs().insert( QStringLiteral( "test_output" ), 1 );
context4.modelResult().executedChildIds() << "alg:test";
QCOMPARE( context4.modelResult().childResults().count(), 2 );
QCOMPARE( context4.modelResult().rawChildInputs().count(), 1 );
QCOMPARE( context4.modelResult().rawChildOutputs().count(), 1 );
QCOMPARE( context4.modelResult().executedChildIds().count(), 1 );
context4.clearModelResult();
QCOMPARE( context4.modelResult().childResults().count(), 0 );
QCOMPARE( context4.modelResult().rawChildInputs().count(), 0 );
QCOMPARE( context4.modelResult().rawChildOutputs().count(), 0 );
QCOMPARE( context4.modelResult().executedChildIds().count(), 0 );

// make sure postprocessor is correctly deleted
ppDeleted = false;
pp = new TestPostProcessor( &ppDeleted );
Expand Down
Loading