Skip to content

Commit 065c984

Browse files
committed
Don't create unused optional outputs when running models
1 parent 780f433 commit 065c984

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

src/core/processing/qgsprocessingmodelalgorithm.cpp

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -318,27 +318,60 @@ QVariantMap QgsProcessingModelAlgorithm::parametersForChildAlgorithm( const Chil
318318
}
319319
else
320320
{
321+
const QgsProcessingDestinationParameter *destParam = static_cast< const QgsProcessingDestinationParameter * >( def );
321322
// is destination linked to one of the final outputs from this model?
322-
if ( child.modelOutputs().contains( def->name() ) )
323+
if ( child.modelOutputs().contains( destParam->name() ) )
323324
{
324-
QString outputName = child.modelOutputs().value( def->name() ).outputName();
325+
QString outputName = child.modelOutputs().value( destParam->name() ).outputName();
325326
QString paramName = child.childId() + ':' + outputName;
326327
if ( modelParameters.contains( paramName ) )
327-
childParams.insert( def->name(), modelParameters.value( paramName ) );
328+
childParams.insert( destParam->name(), modelParameters.value( paramName ) );
328329
}
329330
else
330331
{
331332
// output is temporary
332-
// TODO - skip if this output is optional and not used elsewhere in the model
333-
childParams.insert( def->name(), "memory:" );
334333

334+
// check whether it's optional, and if so - is it required?
335+
bool required = true;
336+
if ( destParam->flags() & QgsProcessingParameterDefinition::FlagOptional )
337+
{
338+
required = childOutputIsRequired( child.childId(), destParam->name() );
339+
}
335340

341+
// not optional, or required elsewhere in model
342+
if ( required )
343+
childParams.insert( destParam->name(), destParam->generateTemporaryDestination() );
336344
}
337345
}
338346
}
339347
return childParams;
340348
}
341349

350+
bool QgsProcessingModelAlgorithm::childOutputIsRequired( const QString &childId, const QString &outputName ) const
351+
{
352+
// look through all child algs
353+
QMap< QString, ChildAlgorithm >::const_iterator childIt = mChildAlgorithms.constBegin();
354+
for ( ; childIt != mChildAlgorithms.constEnd(); ++childIt )
355+
{
356+
if ( childIt->childId() == childId || !childIt->isActive() )
357+
continue;
358+
359+
// look through all sources for child
360+
QMap<QString, QgsProcessingModelAlgorithm::ChildParameterSource> candidateChildParams = childIt->parameterSources();
361+
QMap<QString, QgsProcessingModelAlgorithm::ChildParameterSource>::const_iterator childParamIt = candidateChildParams.constBegin();
362+
for ( ; childParamIt != candidateChildParams.constEnd(); ++childParamIt )
363+
{
364+
if ( childParamIt->source() == ChildParameterSource::ChildOutput
365+
&& childParamIt->outputChildId() == childId
366+
&& childParamIt->outputName() == outputName )
367+
{
368+
return true;
369+
}
370+
}
371+
}
372+
return false;
373+
}
374+
342375
QVariantMap QgsProcessingModelAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const
343376
{
344377
QSet< QString > toExecute;

src/core/processing/qgsprocessingmodelalgorithm.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,12 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm
765765

766766
QVariantMap parametersForChildAlgorithm( const ChildAlgorithm &child, const QVariantMap &modelParameters, const QMap<QString, QVariantMap> &results ) const;
767767

768+
/**
769+
* Returns true if an output from a child algorithm is required elsewhere in
770+
* the model.
771+
*/
772+
bool childOutputIsRequired( const QString &childId, const QString &outputName ) const;
773+
768774
/**
769775
* Saves this model to a QVariantMap, wrapped in a QVariant.
770776
* You can use QgsXmlUtils::writeVariant to save it to an XML document.

0 commit comments

Comments
 (0)