Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Don't create unused optional outputs when running models
  • Loading branch information
nyalldawson committed Jun 21, 2017
1 parent 780f433 commit 065c984
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
43 changes: 38 additions & 5 deletions src/core/processing/qgsprocessingmodelalgorithm.cpp
Expand Up @@ -318,27 +318,60 @@ QVariantMap QgsProcessingModelAlgorithm::parametersForChildAlgorithm( const Chil
}
else
{
const QgsProcessingDestinationParameter *destParam = static_cast< const QgsProcessingDestinationParameter * >( def );
// is destination linked to one of the final outputs from this model?
if ( child.modelOutputs().contains( def->name() ) )
if ( child.modelOutputs().contains( destParam->name() ) )
{
QString outputName = child.modelOutputs().value( def->name() ).outputName();
QString outputName = child.modelOutputs().value( destParam->name() ).outputName();
QString paramName = child.childId() + ':' + outputName;
if ( modelParameters.contains( paramName ) )
childParams.insert( def->name(), modelParameters.value( paramName ) );
childParams.insert( destParam->name(), modelParameters.value( paramName ) );
}
else
{
// output is temporary
// TODO - skip if this output is optional and not used elsewhere in the model
childParams.insert( def->name(), "memory:" );

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

// not optional, or required elsewhere in model
if ( required )
childParams.insert( destParam->name(), destParam->generateTemporaryDestination() );
}
}
}
return childParams;
}

bool QgsProcessingModelAlgorithm::childOutputIsRequired( const QString &childId, const QString &outputName ) const
{
// look through all child algs
QMap< QString, ChildAlgorithm >::const_iterator childIt = mChildAlgorithms.constBegin();
for ( ; childIt != mChildAlgorithms.constEnd(); ++childIt )
{
if ( childIt->childId() == childId || !childIt->isActive() )
continue;

// look through all sources for child
QMap<QString, QgsProcessingModelAlgorithm::ChildParameterSource> candidateChildParams = childIt->parameterSources();
QMap<QString, QgsProcessingModelAlgorithm::ChildParameterSource>::const_iterator childParamIt = candidateChildParams.constBegin();
for ( ; childParamIt != candidateChildParams.constEnd(); ++childParamIt )
{
if ( childParamIt->source() == ChildParameterSource::ChildOutput
&& childParamIt->outputChildId() == childId
&& childParamIt->outputName() == outputName )
{
return true;
}
}
}
return false;
}

QVariantMap QgsProcessingModelAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const
{
QSet< QString > toExecute;
Expand Down
6 changes: 6 additions & 0 deletions src/core/processing/qgsprocessingmodelalgorithm.h
Expand Up @@ -765,6 +765,12 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm

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

/**
* Returns true if an output from a child algorithm is required elsewhere in
* the model.
*/
bool childOutputIsRequired( const QString &childId, const QString &outputName ) const;

/**
* Saves this model to a QVariantMap, wrapped in a QVariant.
* You can use QgsXmlUtils::writeVariant to save it to an XML document.
Expand Down

0 comments on commit 065c984

Please sign in to comment.