Search in sources :

Example 6 with Variable

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

the class PropertyHandler method start.

@SuppressWarnings("unchecked")
public Object start(final String uri, final String localName, final Attributes attrs, final ExtensibleXmlParser parser) throws SAXException {
    parser.startElementBuilder(localName, attrs);
    final String id = attrs.getValue("id");
    final String name = attrs.getValue("name");
    final String itemSubjectRef = attrs.getValue("itemSubjectRef");
    Object parent = parser.getParent();
    if (parent instanceof ContextContainer) {
        ContextContainer contextContainer = (ContextContainer) parent;
        VariableScope variableScope = (VariableScope) contextContainer.getDefaultContext(VariableScope.VARIABLE_SCOPE);
        List variables = variableScope.getVariables();
        Variable variable = new Variable();
        // if name is given use it as variable name instead of id
        if (name != null && name.length() > 0) {
            variable.setName(name);
        } else {
            variable.setName(id);
        }
        variable.setMetaData("ItemSubjectRef", itemSubjectRef);
        variables.add(variable);
        ((ProcessBuildData) parser.getData()).setMetaData("Variable", variable);
        return variable;
    }
    return new Variable();
}
Also used : ContextContainer(org.jbpm.process.core.ContextContainer) Variable(org.jbpm.process.core.context.variable.Variable) ProcessBuildData(org.jbpm.compiler.xml.ProcessBuildData) List(java.util.List) VariableScope(org.jbpm.process.core.context.variable.VariableScope)

Example 7 with Variable

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

the class DataObjectHandler method start.

@SuppressWarnings("unchecked")
public Object start(final String uri, final String localName, final Attributes attrs, final ExtensibleXmlParser parser) throws SAXException {
    parser.startElementBuilder(localName, attrs);
    final String id = attrs.getValue("id");
    final String itemSubjectRef = attrs.getValue("itemSubjectRef");
    Object parent = parser.getParent();
    if (parent instanceof ContextContainer) {
        ContextContainer contextContainer = (ContextContainer) parent;
        VariableScope variableScope = (VariableScope) contextContainer.getDefaultContext(VariableScope.VARIABLE_SCOPE);
        List variables = variableScope.getVariables();
        Variable variable = new Variable();
        variable.setMetaData("DataObject", "true");
        variable.setName(id);
        // retrieve type from item definition
        DataType dataType = new ObjectDataType();
        Map<String, ItemDefinition> itemDefinitions = (Map<String, ItemDefinition>) ((ProcessBuildData) parser.getData()).getMetaData("ItemDefinitions");
        if (itemDefinitions != null) {
            ItemDefinition itemDefinition = itemDefinitions.get(itemSubjectRef);
            if (itemDefinition != null) {
                String structureRef = itemDefinition.getStructureRef();
                if ("java.lang.Boolean".equals(structureRef) || "Boolean".equals(structureRef)) {
                    dataType = new BooleanDataType();
                } else if ("java.lang.Integer".equals(structureRef) || "Integer".equals(structureRef)) {
                    dataType = new IntegerDataType();
                } else if ("java.lang.Float".equals(structureRef) || "Float".equals(structureRef)) {
                    dataType = new FloatDataType();
                } else if ("java.lang.String".equals(structureRef) || "String".equals(structureRef)) {
                    dataType = new StringDataType();
                } else if ("java.lang.Object".equals(structureRef) || "Object".equals(structureRef)) {
                    // use FQCN of Object
                    dataType = new ObjectDataType("java.lang.Object");
                } else {
                    dataType = new ObjectDataType(structureRef, parser.getClassLoader());
                }
            }
        }
        variable.setType(dataType);
        variables.add(variable);
        return variable;
    }
    return new Variable();
}
Also used : FloatDataType(org.jbpm.process.core.datatype.impl.type.FloatDataType) Variable(org.jbpm.process.core.context.variable.Variable) IntegerDataType(org.jbpm.process.core.datatype.impl.type.IntegerDataType) ItemDefinition(org.jbpm.bpmn2.core.ItemDefinition) ObjectDataType(org.jbpm.process.core.datatype.impl.type.ObjectDataType) StringDataType(org.jbpm.process.core.datatype.impl.type.StringDataType) ContextContainer(org.jbpm.process.core.ContextContainer) DataType(org.jbpm.process.core.datatype.DataType) ObjectDataType(org.jbpm.process.core.datatype.impl.type.ObjectDataType) IntegerDataType(org.jbpm.process.core.datatype.impl.type.IntegerDataType) StringDataType(org.jbpm.process.core.datatype.impl.type.StringDataType) BooleanDataType(org.jbpm.process.core.datatype.impl.type.BooleanDataType) FloatDataType(org.jbpm.process.core.datatype.impl.type.FloatDataType) List(java.util.List) BooleanDataType(org.jbpm.process.core.datatype.impl.type.BooleanDataType) Map(java.util.Map) VariableScope(org.jbpm.process.core.context.variable.VariableScope)

Example 8 with Variable

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

the class RuleFlowProcessFactory method variable.

public RuleFlowProcessFactory variable(String name, DataType type, Object value, String metaDataName, Object metaDataValue) {
    Variable variable = new Variable();
    variable.setName(name);
    variable.setType(type);
    variable.setValue(value);
    if (metaDataName != null && metaDataValue != null) {
        variable.setMetaData(metaDataName, metaDataValue);
    }
    getRuleFlowProcess().getVariableScope().getVariables().add(variable);
    return this;
}
Also used : Variable(org.jbpm.process.core.context.variable.Variable)

Example 9 with Variable

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

the class VariableScopeInstance method setContextInstanceContainer.

public void setContextInstanceContainer(ContextInstanceContainer contextInstanceContainer) {
    super.setContextInstanceContainer(contextInstanceContainer);
    for (Variable variable : getVariableScope().getVariables()) {
        setVariable(variable.getName(), variable.getValue());
    }
    if (contextInstanceContainer instanceof CompositeContextNodeInstance) {
        this.variableIdPrefix = ((Node) ((CompositeContextNodeInstance) contextInstanceContainer).getNode()).getUniqueId();
        this.variableInstanceIdPrefix = ((CompositeContextNodeInstance) contextInstanceContainer).getUniqueId();
    }
}
Also used : Variable(org.jbpm.process.core.context.variable.Variable) CompositeContextNodeInstance(org.jbpm.workflow.instance.node.CompositeContextNodeInstance)

Example 10 with Variable

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

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