Start on executing models

This commit is contained in:
Nyall Dawson 2017-06-20 22:14:02 +10:00
parent 47f2cc895c
commit 1df9f6b6f0
4 changed files with 83 additions and 12 deletions

View File

@ -557,8 +557,6 @@ Copies are protected to avoid slicing
virtual bool canExecute( QString *errorMessage /Out/ = 0 ) const;
virtual QVariantMap processAlgorithm( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const;
void setName( const QString &name );
%Docstring
@ -757,6 +755,11 @@ Copies are protected to avoid slicing
:rtype: bool
%End
protected:
virtual QVariantMap processAlgorithm( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const;
};

View File

@ -242,14 +242,14 @@ class ModelerAlgorithm(QgsProcessingModelAlgorithm):
def processAlgorithm(self, parameters, context, feedback):
executed = []
toExecute = [alg for alg in list(self.algs.values()) if alg.isActive()]
toExecute = [alg for alg in list(self.childAlgorithms().values()) if alg.isActive()]
while len(executed) < len(toExecute):
for alg in toExecute:
if alg.childId() not in executed:
canExecute = True
required = self.dependsOnChildAlgorithms(alg.childId())
for requiredAlg in required:
if requiredAlg != alg.childId() and requiredAlg not in executed:
if requiredAlg not in executed:
canExecute = False
break
if canExecute:

View File

@ -17,6 +17,7 @@
#include "qgsprocessingmodelalgorithm.h"
#include "qgsprocessingregistry.h"
#include "qgsprocessingfeedback.h"
#include "qgsxmlutils.h"
#include <QFile>
#include <QTextStream>
@ -285,13 +286,77 @@ QString QgsProcessingModelAlgorithm::svgIconPath() const
QVariantMap QgsProcessingModelAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const
{
Q_UNUSED( parameters );
Q_UNUSED( context );
Q_UNUSED( feedback );
return QVariantMap();
}
QSet< QString > toExecute;
QMap< QString, ChildAlgorithm >::const_iterator childIt = mChildAlgorithms.constBegin();
for ( ; childIt != mChildAlgorithms.constEnd(); ++childIt )
{
if ( childIt->isActive() )
toExecute.insert( childIt->childId() );
}
void QgsProcessingModelAlgorithm::setName( const QString &name )
QMap< QString, QVariantMap > resultsMap;
QSet< QString > executed;
while ( executed.count() < toExecute.count() )
{
Q_FOREACH ( const QString &childId, toExecute )
{
if ( executed.contains( childId ) )
continue;
bool canExecute = true;
Q_FOREACH ( const QString &dependency, dependsOnChildAlgorithms( childId ) )
{
if ( !executed.contains( dependency ) )
{
canExecute = false;
break;
}
}
if ( !canExecute )
continue;
feedback->pushDebugInfo( QObject::tr( "Prepare algorithm: %1" ).arg( childId ) );
const ChildAlgorithm &child = mChildAlgorithms[ childId ];
// self.prepareAlgorithm( alg )
feedback->setProgressText( QObject::tr( "Running %1 [%2/%3]" ).arg( child.description() ).arg( executed.count() + 1 ).arg( toExecute.count() ) );
//feedback->pushDebugInfo( "Parameters: " + ', '.join( [str( p ).strip() +
// '=' + str( p.value ) for p in alg.algorithm.parameters] ) )
//t0 = time.time()
QVariantMap results = child.algorithm()->run( parameters, context, feedback );
resultsMap.insert( childId, results );
//dt = time.time() - t0
#if 0
# copy algorithm output value(s) back to model in case the algorithm modified those
for out in alg.algorithm().outputs :
if not out.flags() & QgsProcessingParameterDefinition.FlagHidden:
if out.name() in alg.modelOutputs():
modelOut = self.getOutputFromName( self.getSafeNameForOutput( alg.childId(), out.name() ) )
if modelOut:
modelOut.value = out.value
#endif
executed.insert( childId );
//feedback->pushDebugInfo( QObject::tr( "OK. Execution took %1 ms (%2 outputs)." ).arg( dt, len( alg.algorithm.modelOutputs() ) ) )
#if 0
except GeoAlgorithmExecutionException as e:
feedback.pushDebugInfo( self.tr( 'Failed', 'ModelerAlgorithm' ) )
raise GeoAlgorithmExecutionException(
self.tr( 'Error executing algorithm {0}\n{1}', 'ModelerAlgorithm' ).format( alg.description, e.msg ) )
#endif
}
}
feedback->pushDebugInfo( QObject::tr( "Model processed ok. Executed %1 algorithms total" ).arg( executed.count() ) );
return QVariantMap();
}
void QgsProcessingModelAlgorithm::setName( const QString &name )
{
mModelName = name;
}

View File

@ -559,8 +559,6 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm
QString svgIconPath() const override;
bool canExecute( QString *errorMessage SIP_OUT = nullptr ) const override;
QVariantMap processAlgorithm( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const override;
/**
* Sets the model \a name.
@ -747,6 +745,11 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm
*/
bool fromFile( const QString &path );
protected:
QVariantMap processAlgorithm( const QVariantMap &parameters,
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const override;
private:
QString mModelName;