Skip to content
Permalink
Browse files

Create specific expression context scope for child algorithms

Contains variables for model parameters, algorithm results for
other child algorithms which are not dependent on the
algorithm.

Allows removal of final pieces of ModelerAlgorithm code
  • Loading branch information
nyalldawson committed Jul 7, 2017
1 parent d8086e5 commit 82ef7d2a89e249c8d7def22a25a6f93c5505c432
@@ -877,6 +877,13 @@ Copies are protected to avoid slicing
:rtype: list of QgsProcessingModelAlgorithm.ChildParameterSource
%End

QgsExpressionContextScope *createExpressionContextScopeForChildAlgorithm( const QString &childId, QgsProcessingContext &context, const QVariantMap &modelParameters = QVariantMap(),
const QVariantMap &results = QVariantMap() ) const /Factory/;
%Docstring
Creates a new expression context scope for a child algorithm within the model.
:rtype: QgsExpressionContextScope
%End

protected:

virtual QVariantMap processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback );
@@ -343,27 +343,6 @@ def _layerVariables(self, element, alg=None):
variables['@%s_max' % name] = stats.maximumValue
return variables

def evaluateForModeler(self, value, model):
if isinstance(value, numbers.Number):
return value
variables = {}
for param in model.parameters:
if isinstance(param, ParameterNumber):
variables["@" + param.name()] = param.value
if isinstance(param, (ParameterRaster, ParameterVector)):
variables.update(self._layerVariables(param))

for alg in list(model.algs.values()):
for out in alg.algorithm.outputs:
if isinstance(out, OutputNumber):
variables["@%s_%s" % (alg.name(), out.name)] = out.value
if isinstance(out, (OutputRaster, OutputVector)):
variables.update(self._layerVariables(out, alg))
for k, v in list(variables.items()):
value = value.replace(k, str(v))

return value


class ParameterRange(Parameter):

@@ -34,8 +34,8 @@

from qgis.core import (QgsMessageLog,
QgsProcessingUtils,
QgsProcessingParameterDefinition)
from processing.modeler.ModelerAlgorithm import ModelerAlgorithm
QgsProcessingParameterDefinition,
QgsProcessingModelAlgorithm)

pluginPath = os.path.split(os.path.dirname(__file__))[0]
WIDGET, BASE = uic.loadUiType(
@@ -55,7 +55,7 @@ def __init__(self, alg):

self.alg = alg
self.descriptions = {}
if isinstance(self.alg, ModelerAlgorithm):
if isinstance(self.alg, QgsProcessingModelAlgorithm):
self.descriptions = self.alg.helpContent()
else:
if self.alg.descriptionFile is not None:
@@ -36,14 +36,9 @@
from qgis.core import (QgsExpression,
QgsProcessingParameterNumber,
QgsProcessingOutputNumber,
QgsProcessingOutputVectorLayer,
QgsProcessingOutputRasterLayer,
QgsProcessingParameterFeatureSource,
QgsProcessingModelAlgorithm,
QgsProcessingParameterRasterLayer)
QgsProcessingModelAlgorithm)
from qgis.gui import QgsExpressionBuilderDialog
from processing.modeler.ModelerAlgorithm import ValueFromInput, ValueFromOutput, CompoundValue
from processing.tools.dataobjects import createExpressionContext
from processing.tools.dataobjects import createExpressionContext, createContext

pluginPath = os.path.split(os.path.dirname(__file__))[0]
NUMBER_WIDGET, NUMBER_BASE = uic.loadUiType(
@@ -77,47 +72,14 @@ def __init__(self, param, modelParametersDialog):

def showExpressionsBuilder(self):
context = createExpressionContext()
dlg = QgsExpressionBuilderDialog(None, str(self.leText.text()), self, 'generic', context)
processing_context = createContext()
scope = self.modelParametersDialog.model.createExpressionContextScopeForChildAlgorithm(self.modelParametersDialog.childId, processing_context)
context.appendScope(scope)

highlighted = scope.variableNames()
context.setHighlightedVariables(highlighted)

context.popScope()
values = self.modelParametersDialog.getAvailableValuesOfType(QgsProcessingParameterNumber, QgsProcessingOutputNumber)
variables = {}
for value in values:
if isinstance(value, QgsProcessingModelAlgorithm.ChildParameterSource):
if value.source() == QgsProcessingModelAlgorithm.ChildParameterSource.ModelParameter:
name = value.parameterName()
element = self.modelParametersDialog.model.parameterDefinition(name)
desc = element.description()
elif value.source() == QgsProcessingModelAlgorithm.ChildParameterSource.ChildOutput:
name = "%s_%s" % (value.outputChildId(), value.outputName())
alg = self.modelParametersDialog.model.childAlgorithm(value.outputChildId())
out = alg.algorithm().outputDefinition(value.outputName())
desc = self.tr("Output '{0}' from algorithm '{1}'").format(out.description(), alg.description())
variables[name] = desc
values = self.modelParametersDialog.getAvailableValuesOfType([QgsProcessingParameterFeatureSource, QgsProcessingParameterRasterLayer],
[QgsProcessingOutputVectorLayer, QgsProcessingOutputRasterLayer])
for value in values:
if isinstance(value, QgsProcessingModelAlgorithm.ChildParameterSource):
if value.source() == QgsProcessingModelAlgorithm.ChildParameterSource.ModelParameter:
name = value.parameterName()
element = self.modelParametersDialog.model.parameterDefinition(name)
desc = element.description()
elif value.source() == QgsProcessingModelAlgorithm.ChildParameterSource.ChildOutput:
name = "%s_%s" % (value.outputChildId(), value.outputName())
alg = self.modelParametersDialog.model.childAlgorithm(value.outputChildId())
out = alg.algorithm().outputDefinition(value.outputName())
desc = self.tr("Output '{0}' from algorithm '{1}'").format(out.description(), alg.description())
variables['%s_minx' % name] = self.tr("Minimum X of {0}").format(desc)
variables['%s_miny' % name] = self.tr("Minimum Y of {0}").format(desc)
variables['%s_maxx' % name] = self.tr("Maximum X of {0}").format(desc)
variables['%s_maxy' % name] = self.tr("Maximum Y of {0}").format(desc)
if isinstance(element, (QgsProcessingParameterRasterLayer, QgsProcessingOutputRasterLayer)):
variables['%s_min' % name] = self.tr("Minimum value of {0}").format(desc)
variables['%s_max' % name] = self.tr("Maximum value of {0}").format(desc)
variables['%s_avg' % name] = self.tr("Mean value of {0}").format(desc)
variables['%s_stddev' % name] = self.tr("Standard deviation of {0}").format(desc)
for variable, desc in variables.items():
dlg.expressionBuilder().registerItem("Modeler", variable, "@" + variable, desc, highlightedItem=True)
dlg = QgsExpressionBuilderDialog(None, str(self.leText.text()), self, 'generic', context)

dlg.setWindowTitle(self.tr('Expression based input'))
if dlg.exec_() == QDialog.Accepted:
@@ -30,10 +30,9 @@
from qgis.PyQt.QtWidgets import QFileDialog, QMessageBox
from qgis.PyQt.QtCore import QFileInfo

from qgis.core import QgsApplication, QgsSettings
from qgis.core import QgsApplication, QgsSettings, QgsProcessingModelAlgorithm

from processing.gui.ToolboxAction import ToolboxAction
from processing.modeler.ModelerAlgorithm import ModelerAlgorithm
from processing.modeler.WrongModelException import WrongModelException
from processing.modeler.ModelerUtils import ModelerUtils

@@ -59,7 +58,7 @@ def execute(self):
settings.setValue('Processing/lastModelsDir',
QFileInfo(filename).absoluteDir().absolutePath())

alg = ModelerAlgorithm()
alg = QgsProcessingModelAlgorithm()
if not alg.fromFile(filename):
QMessageBox.warning(
self.toolbox,
@@ -26,10 +26,9 @@
__revision__ = '$Format:%H$'

import os
from qgis.core import QgsApplication
from qgis.core import QgsApplication, QgsProcessingModelAlgorithm
from qgis.PyQt.QtWidgets import QMessageBox
from processing.gui.ContextAction import ContextAction
from processing.modeler.ModelerAlgorithm import ModelerAlgorithm


class DeleteModelAction(ContextAction):
@@ -38,7 +37,7 @@ def __init__(self):
self.name = self.tr('Delete model', 'DeleteModelAction')

def isEnabled(self):
return isinstance(self.itemData, ModelerAlgorithm)
return isinstance(self.itemData, QgsProcessingModelAlgorithm)

def execute(self):
reply = QMessageBox.question(
@@ -25,9 +25,8 @@

__revision__ = '$Format:%H$'

from qgis.core import QgsApplication
from qgis.core import QgsApplication, QgsProcessingModelAlgorithm
from processing.gui.ContextAction import ContextAction
from processing.modeler.ModelerAlgorithm import ModelerAlgorithm
from processing.modeler.ModelerDialog import ModelerDialog


@@ -37,7 +36,7 @@ def __init__(self):
self.name = self.tr('Edit model', 'EditModelAction')

def isEnabled(self):
return isinstance(self.itemData, ModelerAlgorithm)
return isinstance(self.itemData, QgsProcessingModelAlgorithm)

def execute(self):
alg = self.itemData

This file was deleted.

@@ -32,12 +32,12 @@
from qgis.core import (QgsApplication,
QgsProcessingProvider,
QgsMessageLog,
QgsProcessingModelAlgorithm,
QgsProcessingUtils,
QgsXmlUtils)

from processing.core.ProcessingConfig import ProcessingConfig, Setting
from processing.modeler.ModelerUtils import ModelerUtils
from processing.modeler.ModelerAlgorithm import ModelerAlgorithm
from processing.modeler.WrongModelException import WrongModelException
from processing.modeler.EditModelAction import EditModelAction
from processing.modeler.CreateNewModelAction import CreateNewModelAction
@@ -109,7 +109,7 @@ def loadFromFolder(self, folder):
try:
fullpath = os.path.join(path, descriptionFile)

alg = ModelerAlgorithm()
alg = QgsProcessingModelAlgorithm()
if alg.fromFile(fullpath):
if alg.name():
alg.setSourceFilePath(fullpath)

0 comments on commit 82ef7d2

Please sign in to comment.
You can’t perform that action at this time.