Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finite State Machine with Guards in PHP?

Does anybody know a finite state machine that has guard feature in PHP ?

like image 248
Bamdad Dashtban Avatar asked Aug 29 '11 13:08

Bamdad Dashtban


1 Answers

Using PEAR's FSM (usage example), you can use the action callback to return the next state if the guard fails, like so:

$payload = '';
$fsm = new FSM('STATE1', $payload);
function guard1($symbol, $payload) {
    if ($payload == 'something') {
        // Guard success, allow transition
        return;
    }
    else {
        // Guard fail, return to previous state
        return 'STATE1';
    }
}
$fsm->addTransition('SYMBOL1', 'STATE1', 'STATE2', 'guard1');

$fsm->process('SYMBOL1');
like image 63
Rusty Fausak Avatar answered Sep 23 '22 02:09

Rusty Fausak