Skip to content

Instantly share code, notes, and snippets.

@svenefftinge
Created May 7, 2012 07:38
Show Gist options
  • Save svenefftinge/2626515 to your computer and use it in GitHub Desktop.
Save svenefftinge/2626515 to your computer and use it in GitHub Desktop.
What's generated for Mrs. H's statemachine (see https://github.com/svenefftinge/fowlers-statemachine-with-expressions)
import com.google.inject.Inject;
import com.google.inject.Provider;
public class MrsH {
@Inject
private DoorService door;
@Inject
private PanelService panel;
protected void doIdle() {
this.door.open();
this.panel.close();
}
protected void doUnlockedPanel() {
this.door.close();
this.panel.open();
}
public void run(final Provider<String> eventSource) {
boolean executeActions = true;
String currentState = "idle";
String lastEvent = null;
while (true) {
if (currentState.equals("idle")) {
if (executeActions) {
doIdle();
executeActions = false;
}
System.out.println("Your are now in state 'idle'. Possible events are [doorClosed].");
lastEvent = eventSource.get();
if ("doorClosed".equals(lastEvent)) {
currentState = "active";
executeActions = true;
}
}
if (currentState.equals("active")) {
System.out.println("Your are now in state 'active'. Possible events are [drawOpened, lightOn].");
lastEvent = eventSource.get();
if ("drawOpened".equals(lastEvent)) {
currentState = "waitingForLight";
executeActions = true;
}
if ("lightOn".equals(lastEvent)) {
currentState = "waitingForDraw";
executeActions = true;
}
}
if (currentState.equals("waitingForLight")) {
System.out.println("Your are now in state 'waitingForLight'. Possible events are [lightOn].");
lastEvent = eventSource.get();
if ("lightOn".equals(lastEvent)) {
currentState = "unlockedPanel";
executeActions = true;
}
}
if (currentState.equals("waitingForDraw")) {
System.out.println("Your are now in state 'waitingForDraw'. Possible events are [drawOpened].");
lastEvent = eventSource.get();
if ("drawOpened".equals(lastEvent)) {
currentState = "unlockedPanel";
executeActions = true;
}
}
if (currentState.equals("unlockedPanel")) {
if (executeActions) {
doUnlockedPanel();
executeActions = false;
}
System.out.println("Your are now in state 'unlockedPanel'. Possible events are [panelClosed].");
lastEvent = eventSource.get();
if ("panelClosed".equals(lastEvent)) {
currentState = "idle";
executeActions = true;
}
}
if ("doorOpened".equals(lastEvent)) {
System.out.println("Resetting state machine.");
currentState = "idle";
executeActions = true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment