Skip to content

Instantly share code, notes, and snippets.

@dant3
Last active Dec 29, 2015
Embed
What would you like to do?
Simple Test for ParallelFSM with squirrel (https://github.com/hekailiang/squirrel)
import org.junit.Test;
import org.squirrelframework.foundation.fsm.*;
import org.squirrelframework.foundation.fsm.impl.AbstractStateMachine;
import java.util.*;
public class ParallelFSMTest {
@Test
public void parallelStatesTest() {
final Random random = new Random(System.currentTimeMillis());
// setup FSM
final StateMachineBuilder<FSM, St, Ev, Object> builder =
StateMachineBuilderFactory.create(FSM.class, St.class, Ev.class, Object.class);
builder.defineParallelStatesOn(St.Root, St.St1_1, St.St2_1);
builder.externalTransition().from(St.St1_1).to(St.St1_2).on(Ev.Ev1_1);
builder.externalTransition().from(St.St1_2).to(St.St1_1).on(Ev.Ev1_2);
builder.externalTransition().from(St.St2_1).to(St.St2_2).on(Ev.Ev2_1);
builder.externalTransition().from(St.St2_2).to(St.St2_1).on(Ev.Ev2_2);
final FSM fsm = builder.newStateMachine(St.Root);
fsm.addTransitionCompleteListener(new StateMachine.TransitionCompleteListener<FSM, St, Ev, Object>() {
@Override
public void transitionComplete(StateMachine.TransitionCompleteEvent<FSM, St, Ev, Object> event) {
System.err.println("FSM transition completed from " + event.getSourceState().toString() + " to " + event.getTargetState().toString());
}
});
fsm.start(null);
// run random events firing to see how transactions goes
final List<Ev> possibleEvents = Arrays.asList(Ev.values());
final int eventsCount = possibleEvents.size();
for (int i = 0; i < 100; ++i) {
Ev eventToThrow = possibleEvents.get(random.nextInt(eventsCount));
System.err.println("FSM will get event " + eventToThrow.toString());
fsm.fire(eventToThrow, null);
}
}
private static class FSM extends AbstractStateMachine<FSM, St, Ev, Object> {
protected FSM(ImmutableState<FSM, St, Ev, Object> initialState, Map<St, ImmutableState<FSM, St, Ev, Object>> states) {
super(initialState, states);
}
}
private static enum St {
Root,
St1_1,
St1_2,
St2_1,
St2_2
}
private static enum Ev {
Ev1_1,
Ev1_2,
Ev2_1,
Ev2_2
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment