Search in sources :

Example 6 with Transformation

use of org.jbpm.workflow.core.node.Transformation 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 7 with Transformation

use of org.jbpm.workflow.core.node.Transformation in project jbpm by kiegroup.

the class WorkItemNodeInstance method triggerCompleted.

public void triggerCompleted(WorkItem workItem) {
    this.workItem = workItem;
    WorkItemNode workItemNode = getWorkItemNode();
    if (workItemNode != null && workItem.getState() == WorkItem.COMPLETED) {
        validateWorkItemResultVariable(getProcessInstance().getProcessName(), workItemNode.getOutAssociations(), workItem);
        for (Iterator<DataAssociation> iterator = getWorkItemNode().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(), workItem.getResults());
                    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, association.getTarget());
                    if (variableScopeInstance != null && parameterValue != null) {
                        variableScopeInstance.getVariableScope().validateVariable(getProcessInstance().getProcessName(), association.getTarget(), parameterValue);
                        variableScopeInstance.setVariable(association.getTarget(), parameterValue);
                    } else {
                        logger.warn("Could not find variable scope for variable {}", association.getTarget());
                        logger.warn("when trying to complete Work Item {}", workItem.getName());
                        logger.warn("Continuing without setting variable.");
                    }
                    if (parameterValue != null) {
                        ((WorkItem) workItem).setParameter(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 = workItem.getResult(association.getSources().get(0));
                    if (value == null) {
                        try {
                            value = MVELSafeHelper.getEvaluator().eval(association.getSources().get(0), new WorkItemResolverFactory(workItem));
                        } 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") && !dataType.getStringType().endsWith("Object") && value instanceof String) {
                        value = dataType.readValue((String) value);
                    } else {
                        variableScopeInstance.getVariableScope().validateVariable(getProcessInstance().getProcessName(), association.getTarget(), value);
                    }
                    variableScopeInstance.setVariable(association.getTarget(), value);
                } else {
                    logger.warn("Could not find variable scope for variable {}", association.getTarget());
                    logger.warn("when trying to complete Work Item {}", workItem.getName());
                    logger.warn("Continuing without setting variable.");
                }
            } else {
                try {
                    for (Iterator<Assignment> it = association.getAssignments().iterator(); it.hasNext(); ) {
                        handleAssignment(it.next());
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    // handle dynamic nodes
    if (getNode() == null) {
        setMetaData("NodeType", workItem.getName());
        mapDynamicOutputData(workItem.getResults());
    }
    if (isInversionOfControl()) {
        KieRuntime kruntime = ((ProcessInstance) getProcessInstance()).getKnowledgeRuntime();
        kruntime.update(kruntime.getFactHandle(this), this);
    } else {
        triggerCompleted();
    }
}
Also used : Transformation(org.jbpm.workflow.core.node.Transformation) Variable(org.jbpm.process.core.context.variable.Variable) DataAssociation(org.jbpm.workflow.core.node.DataAssociation) KieRuntime(org.kie.api.runtime.KieRuntime) WorkItem(org.drools.core.process.instance.WorkItem) WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) WorkItemHandlerNotFoundException(org.drools.core.WorkItemHandlerNotFoundException) WorkItemResolverFactory(org.jbpm.workflow.instance.impl.WorkItemResolverFactory) WorkflowRuntimeException(org.jbpm.workflow.instance.WorkflowRuntimeException) DataTransformer(org.kie.api.runtime.process.DataTransformer) VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) WorkItemNode(org.jbpm.workflow.core.node.WorkItemNode) Iterator(java.util.Iterator) DataType(org.jbpm.process.core.datatype.DataType) ProcessInstance(org.jbpm.process.instance.ProcessInstance)

Example 8 with Transformation

use of org.jbpm.workflow.core.node.Transformation 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 9 with Transformation

use of org.jbpm.workflow.core.node.Transformation 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 10 with Transformation

use of org.jbpm.workflow.core.node.Transformation in project jbpm by kiegroup.

the class SubProcessNodeInstance method handleOutMappings.

private void handleOutMappings(ProcessInstance processInstance) {
    VariableScopeInstance subProcessVariableScopeInstance = (VariableScopeInstance) processInstance.getContextInstance(VariableScope.VARIABLE_SCOPE);
    SubProcessNode subProcessNode = getSubProcessNode();
    if (subProcessNode != null) {
        for (Iterator<org.jbpm.workflow.core.node.DataAssociation> iterator = subProcessNode.getOutAssociations().iterator(); iterator.hasNext(); ) {
            org.jbpm.workflow.core.node.DataAssociation mapping = iterator.next();
            if (mapping.getTransformation() != null) {
                Transformation transformation = mapping.getTransformation();
                DataTransformer transformer = DataTransformerRegistry.get().find(transformation.getLanguage());
                if (transformer != null) {
                    Object parameterValue = transformer.transform(transformation.getCompiledExpression(), subProcessVariableScopeInstance.getVariables());
                    VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, mapping.getTarget());
                    if (variableScopeInstance != null && parameterValue != null) {
                        variableScopeInstance.setVariable(mapping.getTarget(), parameterValue);
                    } else {
                        logger.warn("Could not find variable scope for variable {}", mapping.getTarget());
                        logger.warn("Continuing without setting variable.");
                    }
                }
            } else {
                VariableScopeInstance variableScopeInstance = (VariableScopeInstance) resolveContextInstance(VariableScope.VARIABLE_SCOPE, mapping.getTarget());
                if (variableScopeInstance != null) {
                    Object value = subProcessVariableScopeInstance.getVariable(mapping.getSources().get(0));
                    if (value == null) {
                        try {
                            value = MVELSafeHelper.getEvaluator().eval(mapping.getSources().get(0), new VariableScopeResolverFactory(subProcessVariableScopeInstance));
                        } catch (Throwable t) {
                        // do nothing
                        }
                    }
                    variableScopeInstance.setVariable(mapping.getTarget(), value);
                } else {
                    logger.error("Could not find variable scope for variable {}", mapping.getTarget());
                    logger.error("when trying to complete SubProcess node {}", getSubProcessNode().getName());
                    logger.error("Continuing without setting variable.");
                }
            }
        }
    } else {
        // handle dynamic sub processes without data output mapping
        mapDynamicOutputData(subProcessVariableScopeInstance.getVariables());
    }
}
Also used : Transformation(org.jbpm.workflow.core.node.Transformation) DataAssociation(org.jbpm.workflow.core.node.DataAssociation) VariableScopeResolverFactory(org.jbpm.workflow.instance.impl.VariableScopeResolverFactory) VariableScopeInstance(org.jbpm.process.instance.context.variable.VariableScopeInstance) DataTransformer(org.kie.api.runtime.process.DataTransformer) SubProcessNode(org.jbpm.workflow.core.node.SubProcessNode) DataAssociation(org.jbpm.workflow.core.node.DataAssociation)

Aggregations

Transformation (org.jbpm.workflow.core.node.Transformation)22 DataTransformer (org.kie.api.runtime.process.DataTransformer)22 DataAssociation (org.jbpm.workflow.core.node.DataAssociation)13 HashMap (java.util.HashMap)9 VariableScopeInstance (org.jbpm.process.instance.context.variable.VariableScopeInstance)6 WorkflowProcess (org.jbpm.workflow.core.WorkflowProcess)6 Assignment (org.jbpm.workflow.core.node.Assignment)5 LinkedList (java.util.LinkedList)4 Map (java.util.Map)3 EventTransformerImpl (org.jbpm.process.core.event.EventTransformerImpl)3 NodeInstanceResolverFactory (org.jbpm.workflow.instance.impl.NodeInstanceResolverFactory)3 NodeList (org.w3c.dom.NodeList)3 Text (org.w3c.dom.Text)3 Iterator (java.util.Iterator)2 Matcher (java.util.regex.Matcher)2 WorkItem (org.drools.core.process.instance.WorkItem)2 ProcessDialect (org.jbpm.process.builder.dialect.ProcessDialect)2 Variable (org.jbpm.process.core.context.variable.Variable)2 DataType (org.jbpm.process.core.datatype.DataType)2 ProcessInstance (org.jbpm.process.instance.ProcessInstance)2