private void addActivity(ActivityBehavior behavior) {
   builder
       .createActivity(popCurrentId())
       .behavior(behavior)
       .transition(nextActivityId)
       .endActivity();
 }
 private void addStartState() {
   builder
       .createActivity("start")
       .initial()
       .behavior(behaviorFactory.startBehavior())
       .transition(genNextActivityId())
       .endActivity();
 }
  @Override
  public void addProcess(String id, List<ExecutionPoint> executionPoints) {
    builder = new ExtendedProcessDefinitionBuilder(id);
    addStartState();

    addFlow(executionPoints);

    addProcessEndState();
    ExtendedProcessDefinitionImpl definition = builder.buildProcessDefinition();
    registry.add(definition.getId(), definition);
  }
  private String[] splitExecution(
      String startActivityId,
      ActivityBehavior behaviorInstance,
      int numberOftransitions,
      List<String> transitionIds) {
    // add decision
    String[] flowIds = new String[numberOftransitions];
    builder.createActivity(startActivityId).behavior(behaviorInstance);
    for (int i = 0; i < numberOftransitions; i++) {
      flowIds[i] = "flow-start-" + popCurrentId();
      if (transitionIds != null && i < transitionIds.size()) {
        builder.transition(flowIds[i], transitionIds.get(i));
      } else {
        builder.transition(flowIds[i]);
      }
    }
    builder.endActivity();

    return flowIds;
  }
  private void addFlows(String[] flowIds, List<ExecutionFlow> flows, String endingActivityId) {
    for (int i = 0; i < flowIds.length; i++) {
      nextActivityId = flowIds[i];
      addFlow(flows.get(i));

      // end the flow
      builder
          .createActivity(nextActivityId)
          .behavior(behaviorFactory.emptyBehaviorInstance())
          .transition(endingActivityId)
          .endActivity();
    }
  }
  @Override
  public void addParallel(List<ExecutionFlow> threads) {
    if (threads.size() >= 2) {
      String forkActivityId = popCurrentId();
      String joinActivityId = "join-" + popCurrentId();

      // split executions with fork
      String[] flowIds =
          splitExecution(
              forkActivityId, behaviorFactory.forkBehaviorInstance(), threads.size(), null);
      addFlows(flowIds, threads, joinActivityId);

      // add join
      builder
          .createActivity(joinActivityId)
          .behavior(behaviorFactory.joinBehaviorInstance())
          .transition(genNextActivityId())
          .endActivity();
    } else if (threads.size() == 1) {
      addFlow(threads.get(0));
    }
  }
 private void addProcessEndState() {
   builder
       .createActivity(nextActivityId)
       .behavior(behaviorFactory.waitBehavior(null))
       .endActivity();
 }