@Override
 public void init(StateMachineConfigBuilder<S, E> config) throws Exception {
   config.setSharedObject(StateMachineModelBuilder.class, getStateMachineModelBuilder());
   config.setSharedObject(
       StateMachineTransitionBuilder.class, getStateMachineTransitionBuilder());
   config.setSharedObject(StateMachineStateBuilder.class, getStateMachineStateBuilder());
   config.setSharedObject(
       StateMachineConfigurationBuilder.class, getStateMachineConfigurationBuilder());
 }
    /**
     * Builds a {@link StateMachine}.
     *
     * @return the state machine
     */
    public StateMachine<S, E> build() {
      try {
        builder.apply(adapter);
        StateMachineConfig<S, E> stateMachineConfig = builder.getOrBuild();

        TransitionsData<S, E> stateMachineTransitions = stateMachineConfig.getTransitions();
        StatesData<S, E> stateMachineStates = stateMachineConfig.getStates();
        ConfigurationData<S, E> stateMachineConfigurationConfig =
            stateMachineConfig.getStateMachineConfigurationConfig();

        ObjectStateMachineFactory<S, E> stateMachineFactory = null;
        if (stateMachineConfig.getModel() != null
            && stateMachineConfig.getModel().getFactory() != null) {
          stateMachineFactory =
              new ObjectStateMachineFactory<S, E>(
                  new DefaultStateMachineModel<S, E>(stateMachineConfigurationConfig, null, null),
                  stateMachineConfig.getModel().getFactory());
        } else {
          stateMachineFactory =
              new ObjectStateMachineFactory<S, E>(
                  new DefaultStateMachineModel<S, E>(
                      stateMachineConfigurationConfig, stateMachineStates, stateMachineTransitions),
                  null);
        }

        stateMachineFactory.setHandleAutostartup(stateMachineConfigurationConfig.isAutoStart());

        if (stateMachineConfigurationConfig.getBeanFactory() != null) {
          stateMachineFactory.setBeanFactory(stateMachineConfigurationConfig.getBeanFactory());
        }
        if (stateMachineConfigurationConfig.getTaskExecutor() != null) {
          stateMachineFactory.setTaskExecutor(stateMachineConfigurationConfig.getTaskExecutor());
        } else {
          stateMachineFactory.setTaskExecutor(new SyncTaskExecutor());
        }
        if (stateMachineConfigurationConfig.getTaskScheduler() != null) {
          stateMachineFactory.setTaskScheduler(stateMachineConfigurationConfig.getTaskScheduler());
        } else {
          stateMachineFactory.setTaskScheduler(new ConcurrentTaskScheduler());
        }
        return stateMachineFactory.getStateMachine();
      } catch (Exception e) {
        throw new StateMachineException("Error building state machine", e);
      }
    }