Search in sources :

Example 1 with NodeInstanceResolverFactory

use of org.jbpm.workflow.instance.impl.NodeInstanceResolverFactory in project jbpm by kiegroup.

the class WorkItemNodeInstance method createWorkItem.

protected WorkItem createWorkItem(WorkItemNode workItemNode) {
    Work work = workItemNode.getWork();
    workItem = new WorkItemImpl();
    ((WorkItem) workItem).setName(work.getName());
    ((WorkItem) workItem).setProcessInstanceId(getProcessInstance().getId());
    ((WorkItem) workItem).setParameters(new HashMap<String, Object>(work.getParameters()));
    // if there are any dynamic parameters add them
    if (dynamicParameters != null) {
        ((WorkItem) workItem).getParameters().putAll(dynamicParameters);
    }
    for (Iterator<DataAssociation> iterator = workItemNode.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) {
                    ((WorkItem) workItem).setParameter(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 Work Item {}", work.getName());
                    logger.error("Continuing without setting parameter.");
                }
            }
            if (parameterValue != null) {
                ((WorkItem) workItem).setParameter(association.getTarget(), parameterValue);
            }
        } else {
            for (Iterator<Assignment> it = association.getAssignments().iterator(); it.hasNext(); ) {
                handleAssignment(it.next());
            }
        }
    }
    for (Map.Entry<String, Object> entry : workItem.getParameters().entrySet()) {
        if (entry.getValue() instanceof String) {
            String s = (String) entry.getValue();
            Map<String, String> replacements = new HashMap<String, String>();
            Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher(s);
            while (matcher.find()) {
                String paramName = matcher.group(1);
                if (replacements.get(paramName) == null) {
                    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, paramName);
                    if (variableScopeInstance != null) {
                        Object variableValue = variableScopeInstance.getVariable(paramName);
                        String variableValueString = variableValue == null ? "" : variableValue.toString();
                        replacements.put(paramName, variableValueString);
                    } else {
                        try {
                            Object variableValue = MVELSafeHelper.getEvaluator().eval(paramName, new NodeInstanceResolverFactory(this));
                            String variableValueString = variableValue == null ? "" : variableValue.toString();
                            replacements.put(paramName, variableValueString);
                        } catch (Throwable t) {
                            logger.error("Could not find variable scope for variable {}", paramName);
                            logger.error("when trying to replace variable in string for Work Item {}", work.getName());
                            logger.error("Continuing without setting parameter.");
                        }
                    }
                }
            }
            for (Map.Entry<String, String> replacement : replacements.entrySet()) {
                s = s.replace("#{" + replacement.getKey() + "}", replacement.getValue());
            }
            ((WorkItem) workItem).setParameter(entry.getKey(), s);
        }
    }
    return workItem;
}
Also used : Transformation(org.jbpm.workflow.core.node.Transformation) DataAssociation(org.jbpm.workflow.core.node.DataAssociation) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) WorkItem(org.drools.core.process.instance.WorkItem) DataTransformer(org.kie.api.runtime.process.DataTransformer) VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) Work(org.jbpm.process.core.Work) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) NodeInstanceResolverFactory(org.jbpm.workflow.instance.impl.NodeInstanceResolverFactory) Iterator(java.util.Iterator) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with NodeInstanceResolverFactory

use of org.jbpm.workflow.instance.impl.NodeInstanceResolverFactory in project jbpm by kiegroup.

the class RuleSetNodeInstance method resolveVariable.

private Object resolveVariable(Object s) {
    if (s instanceof String) {
        Matcher matcher = PatternConstants.PARAMETER_MATCHER.matcher((String) s);
        while (matcher.find()) {
            String paramName = matcher.group(1);
            VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, paramName);
            if (variableScopeInstance != null) {
                Object variableValue = variableScopeInstance.getVariable(paramName);
                if (variableValue != null) {
                    return variableValue;
                }
            } else {
                try {
                    Object variableValue = MVELSafeHelper.getEvaluator().eval(paramName, new NodeInstanceResolverFactory(this));
                    if (variableValue != null) {
                        return variableValue;
                    }
                } catch (Throwable t) {
                    logger.error("Could not find variable scope for variable {}", paramName);
                }
            }
        }
    }
    return s;
}
Also used : VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) Matcher(java.util.regex.Matcher) NodeInstanceResolverFactory(org.jbpm.workflow.instance.impl.NodeInstanceResolverFactory)

Example 3 with NodeInstanceResolverFactory

use of org.jbpm.workflow.instance.impl.NodeInstanceResolverFactory 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 4 with NodeInstanceResolverFactory

use of org.jbpm.workflow.instance.impl.NodeInstanceResolverFactory 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 5 with NodeInstanceResolverFactory

use of org.jbpm.workflow.instance.impl.NodeInstanceResolverFactory 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

NodeInstanceResolverFactory (org.jbpm.workflow.instance.impl.NodeInstanceResolverFactory)10 VariableScopeInstance (org.jbpm.process.instance.context.variable.VariableScopeInstance)9 HashMap (java.util.HashMap)7 Map (java.util.Map)4 Matcher (java.util.regex.Matcher)4 DataAssociation (org.jbpm.workflow.core.node.DataAssociation)3 Transformation (org.jbpm.workflow.core.node.Transformation)3 DataTransformer (org.kie.api.runtime.process.DataTransformer)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 WorkItem (org.drools.core.process.instance.WorkItem)1 WorkItemImpl (org.drools.core.process.instance.impl.WorkItemImpl)1 Work (org.jbpm.process.core.Work)1 ProcessInstance (org.jbpm.process.instance.ProcessInstance)1 ProcessInstanceImpl (org.jbpm.process.instance.impl.ProcessInstanceImpl)1 DynamicNode (org.jbpm.workflow.core.node.DynamicNode)1 WorkflowProcessInstanceImpl (org.jbpm.workflow.instance.impl.WorkflowProcessInstanceImpl)1 KieBase (org.kie.api.KieBase)1 Node (org.kie.api.definition.process.Node)1