the state will be:
- the state that was explicitely set via setState
- the state we have moved to after the last transition
- the initial state. if we haven't had a transition yet and no current
state has been set, the initial state will be retrieved (the state with State::TYPE_INITIAL)
public getCurrentState ( ) : |
||
return |
$machine->addTransition(new Transition($drunk, $sleep, 'crash')); $machine->addTransition(new Transition($drunk, $high, 'weedz')); $machine->addTransition(new Transition($sleep, $hungry, 'wakeup')); $machine->addTransition(new Transition($drunk, $dead, 'moreboozzz')); $machine->addTransition(new Transition($smoking, $hungry, 'munchies')); $machine->addTransition(new Transition($eating, $smoking, 'izzum')); //http://www.urbandictionary.com/define.php?term=Izzum $machine->addTransition(new Transition($smoking, $high, 'foshizzle')); $machine->addTransition(new Transition($high, $dead, 'moreweedzzz')); $machine->addTransition(new Transition($high, $sleep, 'pzzah')); //start the interactive demo //with some coloring that works in the bash shell echo PHP_EOL . "[01;32mIzzum statemachine interactive demo. press ctrl+c to stop it.[0m" . PHP_EOL . PHP_EOL; //loop the machine while (true) { $state = $machine->getCurrentState(); echo "current state: {$state}" . PHP_EOL; echo "possible transitions from {$state}: " . PHP_EOL; if ($state->isFinal()) { //too much good times echo "[01;35mAhw man...! Try not to drink/smoke as much next time, it's bad for you ;)[0m" . PHP_EOL . PHP_EOL; exit; } foreach ($state->getTransitions() as $transition) { echo "'" . $transition->getName() . "' aka event '" . $transition->getEvent() . "'" . PHP_EOL; } echo PHP_EOL; //get input from the user $event = readline("[01;32mEnter an [01;36mevent [01;32mor [01;34mtransition [01;32mname: [0m"); try { $status = 0;
$adapter = new Session(); $machine = new StateMachine(new Context(new Identifier('session-example', 'rainbow-machine'), null, $adapter)); //add the transitions, going from one color to the next and back to the first $machine->addTransition(new Transition($new, $red)); $machine->addTransition(new Transition($red, $orange)); $machine->addTransition(new Transition($orange, $yellow)); $machine->addTransition(new Transition($yellow, $green)); $machine->addTransition(new Transition($green, $blue)); $machine->addTransition(new Transition($blue, $indigo)); $machine->addTransition(new Transition($indigo, $violet)); $machine->addTransition(new Transition($violet, $red)); //initialize the first time to 'red' and then cycle through the //colors for each page refresh $machine->run(); //get some data to put in the output $current = $machine->getCurrentState(); $next_transitions = implode(',', $current->getTransitions()); $next = $current->getTransitions()[0]->getStateTo(); //generate the ouput $output = <<<EOT <html> <header> <title>rainbows all over the place: gimme some more izzum jo!</title> <style> body { background-color: {$current}; color: black; padding:10px; margin:10px; font-family: "Verdana"; font-size: 0.8em;