cancel
Showing results for 
Search instead for 
Did you mean: 

VariableScope#getVariables

etienne
Champ in-the-making
Champ in-the-making
Hi

I've gone through the implementation of org.activiti.engine.delegate.VariableScope#getVariables and the following question came up: if the same name is used for a variable on different scopes in the scope hierarchy, why does the variable furthest up the tree have precedence? Intuitively, I would have imagined that a more 'local' scope has precedence.

This is the code I mean:

for (VariableInstanceEntity variableInstance: variableInstances.values()) {
variables.put(variableInstance.getName(), variableInstance.getValue());
}
VariableScopeImpl parentScope = getParentVariableScope();
if (parentScope!=null) {
return parentScope.collectVariables(variables);
}

Thanks for sharing the thoughts behind this implementation 'strategy'.

Regards, Etienne
4 REPLIES 4

jbarrez
Star Contributor
Star Contributor
When I look at the getVariable(String) code, it is clear that the closest in the tree is returned (see first if). Only when the variableInstance is null, the tree is searched upwards.

public Object getVariable(String variableName) {
    ensureVariableInstancesInitialized();
    VariableInstanceEntity variableInstance = variableInstances.get(variableName);
    if (variableInstance!=null) {
      return variableInstance.getValue();
    }
    VariableScope parentScope = getParentVariableScope();
    if (parentScope!=null) {
      return parentScope.getVariable(variableName);
    }
    return null;
  }

Where is the code you pasted coming from?

etienne
Champ in-the-making
Champ in-the-making
VariableScopeImpl, line 77:

protected Map<String, Object> collectVariables(HashMap<String, Object> variables) {
    ensureVariableInstancesInitialized();
    for (VariableInstanceEntity variableInstance: variableInstances.values()) {
      variables.put(variableInstance.getName(), variableInstance.getValue());
    }
    VariableScopeImpl parentScope = getParentVariableScope();
    if (parentScope!=null) {
      return parentScope.collectVariables(variables);
    }
    return variables;
  }

And that's part of my confusion: getVariable() will return a local variable first, even in case of a name clash, but getVariables() will have non-local variables "win" in case of a name clash.

Regards, Etienne

trademak
Star Contributor
Star Contributor
Hi,

I've committed a fix for this (in two iterations).
At first I couldn't find a test case where it went wrong. But then I stumbled upon the GetExecutionVariablesCmd, which only used the variable names out of the collectVariables method.
So that's the reason it still executed without any errors. I refactored the GetExecutionVariablesCmd command so that it also uses the variable values and then the order of the local and parent variable retrieval does matter.

Thanks,

etienne
Champ in-the-making
Champ in-the-making
Hi Tijs

Cool. Thank you.

Regards, Etienne
Getting started

Tags


Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.