Search in sources :

Example 16 with Variable

use of org.jbpm.process.core.context.variable.Variable 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 17 with Variable

use of org.jbpm.process.core.context.variable.Variable 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 18 with Variable

use of org.jbpm.process.core.context.variable.Variable in project jbpm by kiegroup.

the class WorkItemTest method getWorkItemProcess.

private RuleFlowProcess getWorkItemProcess(String processId, String workName) {
    RuleFlowProcess process = new RuleFlowProcess();
    process.setId(processId);
    List<Variable> variables = new ArrayList<Variable>();
    Variable variable = new Variable();
    variable.setName("UserName");
    variable.setType(new StringDataType());
    variables.add(variable);
    variable = new Variable();
    variable.setName("Person");
    variable.setType(new ObjectDataType(Person.class.getName()));
    variables.add(variable);
    variable = new Variable();
    variable.setName("MyObject");
    variable.setType(new ObjectDataType());
    variables.add(variable);
    variable = new Variable();
    variable.setName("Number");
    variable.setType(new IntegerDataType());
    variables.add(variable);
    process.getVariableScope().setVariables(variables);
    StartNode startNode = new StartNode();
    startNode.setName("Start");
    startNode.setId(1);
    WorkItemNode workItemNode = new WorkItemNode();
    workItemNode.setName("workItemNode");
    workItemNode.setId(2);
    workItemNode.addInMapping("Comment", "Person.name");
    workItemNode.addInMapping("Attachment", "MyObject");
    workItemNode.addOutMapping("Result", "MyObject");
    workItemNode.addOutMapping("Result.length()", "Number");
    Work work = new WorkImpl();
    work.setName(workName);
    Set<ParameterDefinition> parameterDefinitions = new HashSet<ParameterDefinition>();
    ParameterDefinition parameterDefinition = new ParameterDefinitionImpl("ActorId", new StringDataType());
    parameterDefinitions.add(parameterDefinition);
    parameterDefinition = new ParameterDefinitionImpl("Content", new StringDataType());
    parameterDefinitions.add(parameterDefinition);
    parameterDefinition = new ParameterDefinitionImpl("Comment", new StringDataType());
    parameterDefinitions.add(parameterDefinition);
    work.setParameterDefinitions(parameterDefinitions);
    work.setParameter("ActorId", "#{UserName}");
    work.setParameter("Content", "#{Person.name}");
    workItemNode.setWork(work);
    EndNode endNode = new EndNode();
    endNode.setName("End");
    endNode.setId(3);
    connect(startNode, workItemNode);
    connect(workItemNode, endNode);
    process.addNode(startNode);
    process.addNode(workItemNode);
    process.addNode(endNode);
    return process;
}
Also used : RuleFlowProcess(org.jbpm.ruleflow.core.RuleFlowProcess) StartNode(org.jbpm.workflow.core.node.StartNode) Variable(org.jbpm.process.core.context.variable.Variable) IntegerDataType(org.jbpm.process.core.datatype.impl.type.IntegerDataType) ArrayList(java.util.ArrayList) ObjectDataType(org.jbpm.process.core.datatype.impl.type.ObjectDataType) ParameterDefinitionImpl(org.jbpm.process.core.impl.ParameterDefinitionImpl) StringDataType(org.jbpm.process.core.datatype.impl.type.StringDataType) EndNode(org.jbpm.workflow.core.node.EndNode) WorkItemNode(org.jbpm.workflow.core.node.WorkItemNode) Work(org.jbpm.process.core.Work) WorkImpl(org.jbpm.process.core.impl.WorkImpl) ParameterDefinition(org.jbpm.process.core.ParameterDefinition) HashSet(java.util.HashSet)

Example 19 with Variable

use of org.jbpm.process.core.context.variable.Variable in project jbpm by kiegroup.

the class ForEachNode method setVariable.

public void setVariable(String variableName, DataType type) {
    this.variableName = variableName;
    VariableScope variableScope = (VariableScope) getCompositeNode().getDefaultContext(VariableScope.VARIABLE_SCOPE);
    List<Variable> variables = variableScope.getVariables();
    if (variables == null) {
        variables = new ArrayList<Variable>();
        variableScope.setVariables(variables);
    }
    Variable variable = new Variable();
    variable.setName(variableName);
    variable.setType(type);
    variables.add(variable);
}
Also used : Variable(org.jbpm.process.core.context.variable.Variable) VariableScope(org.jbpm.process.core.context.variable.VariableScope)

Example 20 with Variable

use of org.jbpm.process.core.context.variable.Variable in project jbpm by kiegroup.

the class ServicesProcessDataEventListener method onBuildComplete.

@SuppressWarnings("unchecked")
@Override
public void onBuildComplete(Process process) {
    // process java dialect types
    Set<String> referencedTypes = (Set<String>) process.getMetaData().get("JavaDialectReferencedTypes");
    if (referencedTypes != null && !referencedTypes.isEmpty()) {
        processDescriptor.getReferencedClasses().addAll(referencedTypes);
    }
    Set<String> unqualifiedClasses = (Set<String>) process.getMetaData().get("JavaDialectUnqualifiedTypes");
    if (unqualifiedClasses != null && !unqualifiedClasses.isEmpty()) {
        processDescriptor.getUnqualifiedClasses().addAll(unqualifiedClasses);
    }
    // process java return value types
    referencedTypes = (Set<String>) process.getMetaData().get("JavaReturnValueReferencedTypes");
    if (referencedTypes != null && !referencedTypes.isEmpty()) {
        processDescriptor.getReferencedClasses().addAll(referencedTypes);
    }
    unqualifiedClasses = (Set<String>) process.getMetaData().get("JavaReturnValueUnqualifiedTypes");
    if (unqualifiedClasses != null && !unqualifiedClasses.isEmpty()) {
        processDescriptor.getUnqualifiedClasses().addAll(unqualifiedClasses);
    }
    // process mvel dialect types
    referencedTypes = (Set<String>) process.getMetaData().get("MVELDialectReferencedTypes");
    if (referencedTypes != null && !referencedTypes.isEmpty()) {
        processDescriptor.getReferencedClasses().addAll(referencedTypes);
    }
    // process mvel return value types
    referencedTypes = (Set<String>) process.getMetaData().get("MVELReturnValueReferencedTypes");
    if (referencedTypes != null && !referencedTypes.isEmpty()) {
        processDescriptor.getReferencedClasses().addAll(referencedTypes);
    }
    // process unqualified classes
    resolveUnqualifiedClasses();
    // process variables
    if (variables != null) {
        for (Variable data : variables) {
            String type = data.getType().getStringType();
            String itemSubjectRef = (String) data.getMetaData("ItemSubjectRef");
            if (itemSubjectRef != null && itemDefinitions != null) {
                ItemDefinition itemDef = itemDefinitions.get(itemSubjectRef);
                type = itemDef.getStructureRef();
            }
            processDescriptor.getInputs().put(data.getName(), type);
        }
    }
    // process signals
    if (signals != null) {
        processDescriptor.setSignals(signals);
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Variable(org.jbpm.process.core.context.variable.Variable) ItemDefinition(org.jbpm.bpmn2.core.ItemDefinition)

Aggregations

Variable (org.jbpm.process.core.context.variable.Variable)38 ArrayList (java.util.ArrayList)18 ObjectDataType (org.jbpm.process.core.datatype.impl.type.ObjectDataType)17 RuleFlowProcess (org.jbpm.ruleflow.core.RuleFlowProcess)16 EndNode (org.jbpm.workflow.core.node.EndNode)14 StartNode (org.jbpm.workflow.core.node.StartNode)14 ActionNode (org.jbpm.workflow.core.node.ActionNode)12 VariableScope (org.jbpm.process.core.context.variable.VariableScope)11 DroolsAction (org.jbpm.workflow.core.DroolsAction)10 DroolsConsequenceAction (org.jbpm.workflow.core.impl.DroolsConsequenceAction)10 Test (org.junit.Test)10 Action (org.jbpm.process.instance.impl.Action)9 TestProcessEventListener (org.jbpm.process.test.TestProcessEventListener)9 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)9 KieSession (org.kie.api.runtime.KieSession)9 ProcessContext (org.kie.api.runtime.process.ProcessContext)9 EventTypeFilter (org.jbpm.process.core.event.EventTypeFilter)8 ConnectionImpl (org.jbpm.workflow.core.impl.ConnectionImpl)8 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)8 EventNode (org.jbpm.workflow.core.node.EventNode)7