Skip to content

Commit

Permalink
Start on executing models
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 21, 2017
1 parent 47f2cc8 commit 1df9f6b
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 12 deletions.
7 changes: 5 additions & 2 deletions python/core/processing/qgsprocessingmodelalgorithm.sip
Expand Up @@ -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
Expand Down Expand Up @@ -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;

};


Expand Down
4 changes: 2 additions & 2 deletions python/plugins/processing/modeler/ModelerAlgorithm.py
Expand Up @@ -242,14 +242,14 @@ def resolveValue(self, value, param):

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:
Expand Down
77 changes: 71 additions & 6 deletions src/core/processing/qgsprocessingmodelalgorithm.cpp
Expand Up @@ -17,6 +17,7 @@

#include "qgsprocessingmodelalgorithm.h"
#include "qgsprocessingregistry.h"
#include "qgsprocessingfeedback.h"
#include "qgsxmlutils.h"
#include <QFile>
#include <QTextStream>
Expand Down Expand Up @@ -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() );
}

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 )
void QgsProcessingModelAlgorithm::setName( const QString &name )
{
mModelName = name;
}
Expand Down
7 changes: 5 additions & 2 deletions src/core/processing/qgsprocessingmodelalgorithm.h
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 1df9f6b

Please sign in to comment.