Search in sources :

Example 1 with Condition

use of org.camunda.bpm.engine.impl.Condition in project camunda-bpm-platform by camunda.

the class BpmnParse method validateExclusiveGateway.

public void validateExclusiveGateway(ActivityImpl activity) {
    if (activity.getOutgoingTransitions().size() == 0) {
        // TODO: double check if this is valid (I think in Activiti yes, since we
        // need start events we will need an end event as well)
        addError("Exclusive Gateway '" + activity.getId() + "' has no outgoing sequence flows.", null);
    } else if (activity.getOutgoingTransitions().size() == 1) {
        PvmTransition flow = activity.getOutgoingTransitions().get(0);
        Condition condition = (Condition) flow.getProperty(BpmnParse.PROPERTYNAME_CONDITION);
        if (condition != null) {
            addError("Exclusive Gateway '" + activity.getId() + "' has only one outgoing sequence flow ('" + flow.getId() + "'). This is not allowed to have a condition.", null);
        }
    } else {
        String defaultSequenceFlow = (String) activity.getProperty("default");
        boolean hasDefaultFlow = defaultSequenceFlow != null && defaultSequenceFlow.length() > 0;
        ArrayList<PvmTransition> flowsWithoutCondition = new ArrayList<PvmTransition>();
        for (PvmTransition flow : activity.getOutgoingTransitions()) {
            Condition condition = (Condition) flow.getProperty(BpmnParse.PROPERTYNAME_CONDITION);
            boolean isDefaultFlow = flow.getId() != null && flow.getId().equals(defaultSequenceFlow);
            boolean hasConditon = condition != null;
            if (!hasConditon && !isDefaultFlow) {
                flowsWithoutCondition.add(flow);
            }
            if (hasConditon && isDefaultFlow) {
                addError("Exclusive Gateway '" + activity.getId() + "' has outgoing sequence flow '" + flow.getId() + "' which is the default flow but has a condition too.", null);
            }
        }
        if (hasDefaultFlow || flowsWithoutCondition.size() > 1) {
            // this is an error
            for (PvmTransition flow : flowsWithoutCondition) {
                addError("Exclusive Gateway '" + activity.getId() + "' has outgoing sequence flow '" + flow.getId() + "' without condition which is not the default flow.", null);
            }
        } else if (flowsWithoutCondition.size() == 1) {
            // Havinf no default and exactly one flow without condition this is
            // considered the default one now (to not break backward compatibility)
            PvmTransition flow = flowsWithoutCondition.get(0);
            addWarning("Exclusive Gateway '" + activity.getId() + "' has outgoing sequence flow '" + flow.getId() + "' without condition which is not the default flow. We assume it to be the default flow, but it is bad modeling practice, better set the default flow in your gateway.", null);
        }
    }
}
Also used : ScriptCondition(org.camunda.bpm.engine.impl.scripting.ScriptCondition) Condition(org.camunda.bpm.engine.impl.Condition) PvmTransition(org.camunda.bpm.engine.impl.pvm.PvmTransition)

Example 2 with Condition

use of org.camunda.bpm.engine.impl.Condition in project camunda-bpm-platform by camunda.

the class ExclusiveGatewayActivityBehavior method doLeave.

/**
 * The default behaviour of BPMN, taking every outgoing sequence flow
 * (where the condition evaluates to true), is not valid for an exclusive
 * gateway.
 *
 * Hence, this behaviour is overriden and replaced by the correct behavior:
 * selecting the first sequence flow which condition evaluates to true
 * (or which hasn't got a condition) and leaving the activity through that
 * sequence flow.
 *
 * If no sequence flow is selected (ie all conditions evaluate to false),
 * then the default sequence flow is taken (if defined).
 */
@Override
public void doLeave(ActivityExecution execution) {
    LOG.leavingActivity(execution.getActivity().getId());
    PvmTransition outgoingSeqFlow = null;
    String defaultSequenceFlow = (String) execution.getActivity().getProperty("default");
    Iterator<PvmTransition> transitionIterator = execution.getActivity().getOutgoingTransitions().iterator();
    while (outgoingSeqFlow == null && transitionIterator.hasNext()) {
        PvmTransition seqFlow = transitionIterator.next();
        Condition condition = (Condition) seqFlow.getProperty(BpmnParse.PROPERTYNAME_CONDITION);
        if ((condition == null && (defaultSequenceFlow == null || !defaultSequenceFlow.equals(seqFlow.getId()))) || (condition != null && condition.evaluate(execution))) {
            LOG.outgoingSequenceFlowSelected(seqFlow.getId());
            outgoingSeqFlow = seqFlow;
        }
    }
    if (outgoingSeqFlow != null) {
        execution.leaveActivityViaTransition(outgoingSeqFlow);
    } else {
        if (defaultSequenceFlow != null) {
            PvmTransition defaultTransition = execution.getActivity().findOutgoingTransition(defaultSequenceFlow);
            if (defaultTransition != null) {
                execution.leaveActivityViaTransition(defaultTransition);
            } else {
                throw LOG.missingDefaultFlowException(execution.getActivity().getId(), defaultSequenceFlow);
            }
        } else {
            // No sequence flow could be found, not even a default one
            throw LOG.stuckExecutionException(execution.getActivity().getId());
        }
    }
}
Also used : Condition(org.camunda.bpm.engine.impl.Condition) PvmTransition(org.camunda.bpm.engine.impl.pvm.PvmTransition)

Example 3 with Condition

use of org.camunda.bpm.engine.impl.Condition in project camunda-bpm-platform by camunda.

the class InclusiveGatewayActivityBehavior method execute.

public void execute(ActivityExecution execution) throws Exception {
    execution.inactivate();
    lockConcurrentRoot(execution);
    PvmActivity activity = execution.getActivity();
    if (activatesGateway(execution, activity)) {
        LOG.activityActivation(activity.getId());
        List<ActivityExecution> joinedExecutions = execution.findInactiveConcurrentExecutions(activity);
        String defaultSequenceFlow = (String) execution.getActivity().getProperty("default");
        List<PvmTransition> transitionsToTake = new ArrayList<PvmTransition>();
        // find matching non-default sequence flows
        for (PvmTransition outgoingTransition : execution.getActivity().getOutgoingTransitions()) {
            if (defaultSequenceFlow == null || !outgoingTransition.getId().equals(defaultSequenceFlow)) {
                Condition condition = (Condition) outgoingTransition.getProperty(BpmnParse.PROPERTYNAME_CONDITION);
                if (condition == null || condition.evaluate(execution)) {
                    transitionsToTake.add(outgoingTransition);
                }
            }
        }
        // if none found, add default flow
        if (transitionsToTake.isEmpty()) {
            if (defaultSequenceFlow != null) {
                PvmTransition defaultTransition = execution.getActivity().findOutgoingTransition(defaultSequenceFlow);
                if (defaultTransition == null) {
                    throw LOG.missingDefaultFlowException(execution.getActivity().getId(), defaultSequenceFlow);
                }
                transitionsToTake.add(defaultTransition);
            } else {
                // No sequence flow could be found, not even a default one
                throw LOG.stuckExecutionException(execution.getActivity().getId());
            }
        }
        // take the flows found
        execution.leaveActivityViaTransitions(transitionsToTake, joinedExecutions);
    } else {
        LOG.noActivityActivation(activity.getId());
    }
}
Also used : Condition(org.camunda.bpm.engine.impl.Condition) ArrayList(java.util.ArrayList) ActivityExecution(org.camunda.bpm.engine.impl.pvm.delegate.ActivityExecution) PvmActivity(org.camunda.bpm.engine.impl.pvm.PvmActivity) PvmTransition(org.camunda.bpm.engine.impl.pvm.PvmTransition)

Example 4 with Condition

use of org.camunda.bpm.engine.impl.Condition in project camunda-bpm-platform by camunda.

the class BpmnActivityBehavior method performOutgoingBehavior.

/**
 * Actual implementation of leaving an activity.
 *
 * @param execution
 *          The current execution context
 * @param checkConditions
 *          Whether or not to check conditions before determining whether or
 *          not to take a transition.
 * @param throwExceptionIfExecutionStuck
 *          If true, an {@link ProcessEngineException} will be thrown in case no
 *          transition could be found to leave the activity.
 */
protected void performOutgoingBehavior(ActivityExecution execution, boolean checkConditions, boolean throwExceptionIfExecutionStuck, List<ActivityExecution> reusableExecutions) {
    LOG.leavingActivity(execution.getActivity().getId());
    String defaultSequenceFlow = (String) execution.getActivity().getProperty("default");
    List<PvmTransition> transitionsToTake = new ArrayList<PvmTransition>();
    List<PvmTransition> outgoingTransitions = execution.getActivity().getOutgoingTransitions();
    for (PvmTransition outgoingTransition : outgoingTransitions) {
        if (defaultSequenceFlow == null || !outgoingTransition.getId().equals(defaultSequenceFlow)) {
            Condition condition = (Condition) outgoingTransition.getProperty(BpmnParse.PROPERTYNAME_CONDITION);
            if (condition == null || !checkConditions || condition.evaluate(execution)) {
                transitionsToTake.add(outgoingTransition);
            }
        }
    }
    if (transitionsToTake.size() == 1) {
        execution.leaveActivityViaTransition(transitionsToTake.get(0));
    } else if (transitionsToTake.size() >= 1) {
        if (reusableExecutions == null || reusableExecutions.isEmpty()) {
            execution.leaveActivityViaTransitions(transitionsToTake, Arrays.asList(execution));
        } else {
            execution.leaveActivityViaTransitions(transitionsToTake, reusableExecutions);
        }
    } else {
        if (defaultSequenceFlow != null) {
            PvmTransition defaultTransition = execution.getActivity().findOutgoingTransition(defaultSequenceFlow);
            if (defaultTransition != null) {
                execution.leaveActivityViaTransition(defaultTransition);
            } else {
                throw LOG.missingDefaultFlowException(execution.getActivity().getId(), defaultSequenceFlow);
            }
        } else if (!outgoingTransitions.isEmpty()) {
            throw LOG.missingConditionalFlowException(execution.getActivity().getId());
        } else {
            if (((ActivityImpl) execution.getActivity()).isCompensationHandler() && isAncestorCompensationThrowing(execution)) {
                execution.endCompensation();
            } else {
                LOG.missingOutgoingSequenceFlow(execution.getActivity().getId());
                execution.end(true);
                if (throwExceptionIfExecutionStuck) {
                    throw LOG.stuckExecutionException(execution.getActivity().getId());
                }
            }
        }
    }
}
Also used : Condition(org.camunda.bpm.engine.impl.Condition) ArrayList(java.util.ArrayList) PvmTransition(org.camunda.bpm.engine.impl.pvm.PvmTransition)

Example 5 with Condition

use of org.camunda.bpm.engine.impl.Condition in project camunda-bpm-platform by camunda.

the class BpmnParse method parseConditionalEventDefinition.

/**
 * Parses the given element and returns an ConditionalEventDefinition object.
 *
 * @param element the XML element which contains the conditional event information
 * @param conditionalActivity the conditional event activity
 * @return the conditional event definition which was parsed
 */
protected ConditionalEventDefinition parseConditionalEventDefinition(Element element, ActivityImpl conditionalActivity) {
    ConditionalEventDefinition conditionalEventDefinition = null;
    Element conditionExprElement = element.element(CONDITION);
    if (conditionExprElement != null) {
        Condition condition = parseConditionExpression(conditionExprElement);
        conditionalEventDefinition = new ConditionalEventDefinition(condition, conditionalActivity);
        String expression = conditionExprElement.getText().trim();
        conditionalEventDefinition.setConditionAsString(expression);
        conditionalActivity.getProcessDefinition().getProperties().set(BpmnProperties.HAS_CONDITIONAL_EVENTS, true);
        final String variableName = element.attributeNS(CAMUNDA_BPMN_EXTENSIONS_NS, "variableName");
        conditionalEventDefinition.setVariableName(variableName);
        final String variableEvents = element.attributeNS(CAMUNDA_BPMN_EXTENSIONS_NS, "variableEvents");
        final List<String> variableEventsList = parseCommaSeparatedList(variableEvents);
        conditionalEventDefinition.setVariableEvents(new HashSet<String>(variableEventsList));
        for (String variableEvent : variableEventsList) {
            if (!VARIABLE_EVENTS.contains(variableEvent)) {
                addWarning("Variable event: " + variableEvent + " is not valid. Possible variable change events are: " + Arrays.toString(VARIABLE_EVENTS.toArray()), element);
            }
        }
    } else {
        addError("Conditional event must contain an expression for evaluation.", element);
    }
    return conditionalEventDefinition;
}
Also used : ScriptCondition(org.camunda.bpm.engine.impl.scripting.ScriptCondition) Condition(org.camunda.bpm.engine.impl.Condition) Element(org.camunda.bpm.engine.impl.util.xml.Element)

Aggregations

Condition (org.camunda.bpm.engine.impl.Condition)7 PvmTransition (org.camunda.bpm.engine.impl.pvm.PvmTransition)4 ScriptCondition (org.camunda.bpm.engine.impl.scripting.ScriptCondition)4 ArrayList (java.util.ArrayList)2 Element (org.camunda.bpm.engine.impl.util.xml.Element)2 ProcessEngineException (org.camunda.bpm.engine.ProcessEngineException)1 PvmActivity (org.camunda.bpm.engine.impl.pvm.PvmActivity)1 ActivityExecution (org.camunda.bpm.engine.impl.pvm.delegate.ActivityExecution)1 ExecutableScript (org.camunda.bpm.engine.impl.scripting.ExecutableScript)1