Search in sources :

Example 1 with FlowState

use of com.sequenceiq.cloudbreak.core.flow2.FlowState in project cloudbreak by hortonworks.

the class OfflineStateGenerator method generate.

private void generate() throws Exception {
    StringBuilder builder = new StringBuilder("digraph {\n");
    inject(flowConfiguration, "applicationContext", APP_CONTEXT);
    Flow flow = initializeFlow();
    StateMachine<FlowState, FlowEvent> stateMachine = getStateMachine(flow);
    FlowState init = stateMachine.getInitialState().getId();
    builder.append(generateStartPoint(init, flowConfiguration.getClass().getSimpleName())).append('\n');
    List<Transition<FlowState, FlowEvent>> transitions = (List<Transition<FlowState, FlowEvent>>) stateMachine.getTransitions();
    Map<String, FlowState> transitionsAlreadyDefined = new HashMap<>();
    transitionsAlreadyDefined.put(init.toString(), init);
    while (!transitions.isEmpty()) {
        for (Transition<FlowState, FlowEvent> transition : new ArrayList<>(transitions)) {
            FlowState source = transition.getSource().getId();
            FlowState target = transition.getTarget().getId();
            if (transitionsAlreadyDefined.values().contains(source)) {
                String id = generateTransitionId(source, target, transition.getTrigger().getEvent());
                if (!transitionsAlreadyDefined.keySet().contains(id)) {
                    if (target.action() != null && !transitionsAlreadyDefined.values().contains(target)) {
                        builder.append(generateState(target, target.action().getSimpleName())).append('\n');
                    }
                    builder.append(generateTransition(source, target, transition.getTrigger().getEvent())).append('\n');
                    transitionsAlreadyDefined.put(id, target);
                }
                transitions.remove(transition);
            }
        }
    }
    saveToFile(builder.append('}').toString());
}
Also used : FlowState(com.sequenceiq.cloudbreak.core.flow2.FlowState) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Flow(com.sequenceiq.cloudbreak.core.flow2.Flow) FlowEvent(com.sequenceiq.cloudbreak.core.flow2.FlowEvent) Transition(org.springframework.statemachine.transition.Transition) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

Flow (com.sequenceiq.cloudbreak.core.flow2.Flow)1 FlowEvent (com.sequenceiq.cloudbreak.core.flow2.FlowEvent)1 FlowState (com.sequenceiq.cloudbreak.core.flow2.FlowState)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Transition (org.springframework.statemachine.transition.Transition)1