Search in sources :

Example 6 with VariableScopeInstance

use of org.jbpm.process.instance.context.variable.VariableScopeInstance in project jbpm by kiegroup.

the class RuleSetNodeInstance method processOutputs.

private void processOutputs(Map<String, Object> objects) {
    RuleSetNode ruleSetNode = getRuleSetNode();
    if (ruleSetNode != null) {
        for (Iterator<DataAssociation> iterator = ruleSetNode.getOutAssociations().iterator(); iterator.hasNext(); ) {
            DataAssociation association = iterator.next();
            if (association.getTransformation() != null) {
                Transformation transformation = association.getTransformation();
                DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
                if (transformer != null) {
                    Object parameterValue = transformer.transform(transformation.getCompiledExpression(), objects);
                    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, association.getTarget());
                    if (variableScopeInstance != null && parameterValue != null) {
                        variableScopeInstance.setVariable(association.getTarget(), parameterValue);
                    } else {
                        logger.warn("Could not find variable scope for variable {}", association.getTarget());
                        logger.warn("Continuing without setting variable.");
                    }
                    if (parameterValue != null) {
                        variableScopeInstance.setVariable(association.getTarget(), parameterValue);
                    }
                }
            } else if (association.getAssignments() == null || association.getAssignments().isEmpty()) {
                VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, association.getTarget());
                if (variableScopeInstance != null) {
                    Object value = objects.get(association.getSources().get(0));
                    if (value == null) {
                        try {
                            value = MVELSafeHelper.getEvaluator().eval(association.getSources().get(0), new MapVariableResolverFactory(objects));
                        } catch (Throwable t) {
                        // do nothing
                        }
                    }
                    Variable varDef = variableScopeInstance.getVariableScope().findVariable(association.getTarget());
                    DataType dataType = varDef.getType();
                    // exclude java.lang.Object as it is considered unknown type
                    if (!dataType.getStringType().endsWith("java.lang.Object") && value instanceof String) {
                        value = dataType.readValue((String) value);
                    }
                    variableScopeInstance.setVariable(association.getTarget(), value);
                } else {
                    logger.warn("Could not find variable scope for variable {}", association.getTarget());
                }
            }
        }
    }
}
Also used : Transformation(org.jbpm.workflow.core.node.Transformation) Variable(org.jbpm.process.core.context.variable.Variable) DataTransformer(org.kie.api.runtime.process.DataTransformer) VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) RuleSetNode(org.jbpm.workflow.core.node.RuleSetNode) DataAssociation(org.jbpm.workflow.core.node.DataAssociation) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) DataType(org.jbpm.process.core.datatype.DataType)

Example 7 with VariableScopeInstance

use of org.jbpm.process.instance.context.variable.VariableScopeInstance in project jbpm by kiegroup.

the class RuleSetNodeInstance method getSourceParameters.

protected Map<String, Object> getSourceParameters(DataAssociation association) {
    Map<String, Object> parameters = new HashMap<String, Object>();
    for (String sourceParam : association.getSources()) {
        Object parameterValue = null;
        VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, sourceParam);
        if (variableScopeInstance != null) {
            parameterValue = variableScopeInstance.getVariable(sourceParam);
        } else {
            try {
                parameterValue = MVELSafeHelper.getEvaluator().eval(sourceParam, new NodeInstanceResolverFactory(this));
            } catch (Throwable t) {
                logger.warn("Could not find variable scope for variable {}", sourceParam);
            }
        }
        if (parameterValue != null) {
            parameters.put(association.getTarget(), parameterValue);
        }
    }
    return parameters;
}
Also used : VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) HashMap(java.util.HashMap) NodeInstanceResolverFactory(org.jbpm.workflow.instance.impl.NodeInstanceResolverFactory)

Example 8 with VariableScopeInstance

use of org.jbpm.process.instance.context.variable.VariableScopeInstance in project jbpm by kiegroup.

the class RuleSetNodeInstance method evaluateParameters.

protected Map<String, Object> evaluateParameters(RuleSetNode ruleSetNode) {
    Map<String, Object> replacements = new HashMap<String, Object>();
    for (Iterator<DataAssociation> iterator = ruleSetNode.getInAssociations().iterator(); iterator.hasNext(); ) {
        DataAssociation association = iterator.next();
        if (association.getTransformation() != null) {
            Transformation transformation = association.getTransformation();
            DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
            if (transformer != null) {
                Object parameterValue = transformer.transform(transformation.getCompiledExpression(), getSourceParameters(association));
                if (parameterValue != null) {
                    replacements.put(association.getTarget(), parameterValue);
                }
            }
        } else if (association.getAssignments() == null || association.getAssignments().isEmpty()) {
            Object parameterValue = null;
            VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, association.getSources().get(0));
            if (variableScopeInstance != null) {
                parameterValue = variableScopeInstance.getVariable(association.getSources().get(0));
            } else {
                try {
                    parameterValue = MVELSafeHelper.getEvaluator().eval(association.getSources().get(0), new NodeInstanceResolverFactory(this));
                } catch (Throwable t) {
                    logger.error("Could not find variable scope for variable {}", association.getSources().get(0));
                    logger.error("when trying to execute RuleSetNode {}", ruleSetNode.getName());
                    logger.error("Continuing without setting parameter.");
                }
            }
            if (parameterValue != null) {
                replacements.put(association.getTarget(), parameterValue);
            }
        }
    }
    for (Map.Entry<String, Object> entry : ruleSetNode.getParameters().entrySet()) {
        if (entry.getValue() instanceof String) {
            Object value = resolveVariable(entry.getValue());
            if (value != null) {
                replacements.put(entry.getKey(), value);
            }
        }
    }
    return replacements;
}
Also used : Transformation(org.jbpm.workflow.core.node.Transformation) HashMap(java.util.HashMap) DataAssociation(org.jbpm.workflow.core.node.DataAssociation) DataTransformer(org.kie.api.runtime.process.DataTransformer) VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) NodeInstanceResolverFactory(org.jbpm.workflow.instance.impl.NodeInstanceResolverFactory) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with VariableScopeInstance

use of org.jbpm.process.instance.context.variable.VariableScopeInstance in project jbpm by kiegroup.

the class StateBasedNodeInstance method mapDynamicOutputData.

protected void mapDynamicOutputData(Map<String, Object> results) {
    if (results != null && !results.isEmpty()) {
        VariableScope variableScope = (VariableScope) ((ContextContainer) getProcessInstance().getProcess()).getDefaultContext(VariableScope.VARIABLE_SCOPE);
        VariableScopeInstance variableScopeInstance = (VariableScopeInstance) getProcessInstance().getContextInstance(VariableScope.VARIABLE_SCOPE);
        for (Entry<String, Object> result : results.entrySet()) {
            String variableName = result.getKey();
            Variable variable = variableScope.findVariable(variableName);
            if (variable == null) {
                // check if there is any match for case file data
                variableName = VariableScope.CASE_FILE_PREFIX + variableName;
                // check only those that are defined and avoid dynamically created case file variables
                List<String> definedVariables = Arrays.asList(variableScope.getVariableNames());
                if (definedVariables.contains(variableName)) {
                    variable = variableScope.findVariable(variableName);
                }
            }
            if (variable != null) {
                variableScopeInstance.getVariableScope().validateVariable(getProcessInstance().getProcessName(), variableName, result.getValue());
                variableScopeInstance.setVariable(variableName, result.getValue());
            }
        }
    }
}
Also used : Variable(org.jbpm.process.core.context.variable.Variable) VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) VariableScope(org.jbpm.process.core.context.variable.VariableScope)

Example 10 with VariableScopeInstance

use of org.jbpm.process.instance.context.variable.VariableScopeInstance in project jbpm by kiegroup.

the class SubProcessNodeInstance method getSourceParameters.

protected Map<String, Object> getSourceParameters(DataAssociation association) {
    Map<String, Object> parameters = new HashMap<String, Object>();
    for (String sourceParam : association.getSources()) {
        Object parameterValue = null;
        VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, sourceParam);
        if (variableScopeInstance != null) {
            parameterValue = variableScopeInstance.getVariable(sourceParam);
        } else {
            try {
                parameterValue = MVELSafeHelper.getEvaluator().eval(sourceParam, new NodeInstanceResolverFactory(this));
            } catch (Throwable t) {
                logger.warn("Could not find variable scope for variable {}", sourceParam);
            }
        }
        if (parameterValue != null) {
            parameters.put(association.getTarget(), parameterValue);
        }
    }
    return parameters;
}
Also used : VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) HashMap(java.util.HashMap) NodeInstanceResolverFactory(org.jbpm.workflow.instance.impl.NodeInstanceResolverFactory)

Aggregations

VariableScopeInstance (org.jbpm.process.instance.context.variable.VariableScopeInstance)38 HashMap (java.util.HashMap)17 Map (java.util.Map)12 NodeInstanceResolverFactory (org.jbpm.workflow.instance.impl.NodeInstanceResolverFactory)9 ExclusiveGroupInstance (org.jbpm.process.instance.context.exclusive.ExclusiveGroupInstance)8 NodeInstance (org.kie.api.runtime.process.NodeInstance)8 ArrayList (java.util.ArrayList)6 Matcher (java.util.regex.Matcher)6 SwimlaneContextInstance (org.jbpm.process.instance.context.swimlane.SwimlaneContextInstance)6 DataAssociation (org.jbpm.workflow.core.node.DataAssociation)6 Transformation (org.jbpm.workflow.core.node.Transformation)6 DataTransformer (org.kie.api.runtime.process.DataTransformer)6 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)6 ContextInstance (org.jbpm.process.instance.ContextInstance)5 WorkflowProcessInstanceImpl (org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl)5 Process (org.kie.api.definition.process.Process)5 MarshallerReaderContext (org.drools.core.marshalling.impl.MarshallerReaderContext)4 MarshallerWriteContext (org.drools.core.marshalling.impl.MarshallerWriteContext)4 Context (org.jbpm.process.core.Context)4 SwimlaneContext (org.jbpm.process.core.context.swimlane.SwimlaneContext)4