Skip to content

Commit 1df9f6b

Browse files
committedJun 21, 2017
Start on executing models
1 parent 47f2cc8 commit 1df9f6b

File tree

4 files changed

+83
-12
lines changed

4 files changed

+83
-12
lines changed
 

‎python/core/processing/qgsprocessingmodelalgorithm.sip

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -557,8 +557,6 @@ Copies are protected to avoid slicing
557557

558558
virtual bool canExecute( QString *errorMessage /Out/ = 0 ) const;
559559

560-
virtual QVariantMap processAlgorithm( const QVariantMap &parameters,
561-
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const;
562560

563561
void setName( const QString &name );
564562
%Docstring
@@ -757,6 +755,11 @@ Copies are protected to avoid slicing
757755
:rtype: bool
758756
%End
759757

758+
protected:
759+
760+
virtual QVariantMap processAlgorithm( const QVariantMap &parameters,
761+
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const;
762+
760763
};
761764

762765

‎python/plugins/processing/modeler/ModelerAlgorithm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,14 +242,14 @@ def resolveValue(self, value, param):
242242

243243
def processAlgorithm(self, parameters, context, feedback):
244244
executed = []
245-
toExecute = [alg for alg in list(self.algs.values()) if alg.isActive()]
245+
toExecute = [alg for alg in list(self.childAlgorithms().values()) if alg.isActive()]
246246
while len(executed) < len(toExecute):
247247
for alg in toExecute:
248248
if alg.childId() not in executed:
249249
canExecute = True
250250
required = self.dependsOnChildAlgorithms(alg.childId())
251251
for requiredAlg in required:
252-
if requiredAlg != alg.childId() and requiredAlg not in executed:
252+
if requiredAlg not in executed:
253253
canExecute = False
254254
break
255255
if canExecute:

‎src/core/processing/qgsprocessingmodelalgorithm.cpp

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "qgsprocessingmodelalgorithm.h"
1919
#include "qgsprocessingregistry.h"
20+
#include "qgsprocessingfeedback.h"
2021
#include "qgsxmlutils.h"
2122
#include <QFile>
2223
#include <QTextStream>
@@ -285,13 +286,77 @@ QString QgsProcessingModelAlgorithm::svgIconPath() const
285286

286287
QVariantMap QgsProcessingModelAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const
287288
{
288-
Q_UNUSED( parameters );
289-
Q_UNUSED( context );
290-
Q_UNUSED( feedback );
291-
return QVariantMap();
292-
}
289+
QSet< QString > toExecute;
290+
QMap< QString, ChildAlgorithm >::const_iterator childIt = mChildAlgorithms.constBegin();
291+
for ( ; childIt != mChildAlgorithms.constEnd(); ++childIt )
292+
{
293+
if ( childIt->isActive() )
294+
toExecute.insert( childIt->childId() );
295+
}
296+
297+
QMap< QString, QVariantMap > resultsMap;
298+
QSet< QString > executed;
299+
while ( executed.count() < toExecute.count() )
300+
{
301+
Q_FOREACH ( const QString &childId, toExecute )
302+
{
303+
if ( executed.contains( childId ) )
304+
continue;
305+
306+
bool canExecute = true;
307+
Q_FOREACH ( const QString &dependency, dependsOnChildAlgorithms( childId ) )
308+
{
309+
if ( !executed.contains( dependency ) )
310+
{
311+
canExecute = false;
312+
break;
313+
}
314+
}
315+
316+
if ( !canExecute )
317+
continue;
318+
319+
feedback->pushDebugInfo( QObject::tr( "Prepare algorithm: %1" ).arg( childId ) );
320+
321+
const ChildAlgorithm &child = mChildAlgorithms[ childId ];
322+
323+
// self.prepareAlgorithm( alg )
324+
325+
feedback->setProgressText( QObject::tr( "Running %1 [%2/%3]" ).arg( child.description() ).arg( executed.count() + 1 ).arg( toExecute.count() ) );
326+
//feedback->pushDebugInfo( "Parameters: " + ', '.join( [str( p ).strip() +
327+
// '=' + str( p.value ) for p in alg.algorithm.parameters] ) )
328+
//t0 = time.time()
329+
QVariantMap results = child.algorithm()->run( parameters, context, feedback );
330+
resultsMap.insert( childId, results );
331+
332+
//dt = time.time() - t0
333+
#if 0
334+
335+
# copy algorithm output value(s) back to model in case the algorithm modified those
336+
for out in alg.algorithm().outputs :
337+
if not out.flags() & QgsProcessingParameterDefinition.FlagHidden:
338+
if out.name() in alg.modelOutputs():
339+
modelOut = self.getOutputFromName( self.getSafeNameForOutput( alg.childId(), out.name() ) )
340+
if modelOut:
341+
modelOut.value = out.value
342+
#endif
343+
344+
executed.insert( childId );
345+
//feedback->pushDebugInfo( QObject::tr( "OK. Execution took %1 ms (%2 outputs)." ).arg( dt, len( alg.algorithm.modelOutputs() ) ) )
346+
#if 0
347+
except GeoAlgorithmExecutionException as e:
348+
feedback.pushDebugInfo( self.tr( 'Failed', 'ModelerAlgorithm' ) )
349+
raise GeoAlgorithmExecutionException(
350+
self.tr( 'Error executing algorithm {0}\n{1}', 'ModelerAlgorithm' ).format( alg.description, e.msg ) )
351+
#endif
352+
}
353+
}
354+
feedback->pushDebugInfo( QObject::tr( "Model processed ok. Executed %1 algorithms total" ).arg( executed.count() ) );
355+
356+
return QVariantMap();
357+
}
293358

294-
void QgsProcessingModelAlgorithm::setName( const QString &name )
359+
void QgsProcessingModelAlgorithm::setName( const QString &name )
295360
{
296361
mModelName = name;
297362
}

‎src/core/processing/qgsprocessingmodelalgorithm.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,8 +559,6 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm
559559
QString svgIconPath() const override;
560560

561561
bool canExecute( QString *errorMessage SIP_OUT = nullptr ) const override;
562-
QVariantMap processAlgorithm( const QVariantMap &parameters,
563-
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const override;
564562

565563
/**
566564
* Sets the model \a name.
@@ -747,6 +745,11 @@ class CORE_EXPORT QgsProcessingModelAlgorithm : public QgsProcessingAlgorithm
747745
*/
748746
bool fromFile( const QString &path );
749747

748+
protected:
749+
750+
QVariantMap processAlgorithm( const QVariantMap &parameters,
751+
QgsProcessingContext &context, QgsProcessingFeedback *feedback ) const override;
752+
750753
private:
751754

752755
QString mModelName;

0 commit comments

Comments
 (0)
Please sign in to comment.