public function testBadCreate() { $s1 = new State('s1'); $t1 = new Transition('t1', 's1', 's2'); $fsm1 = new Fsm('fsm1'); $fsm1->addState($s1)->addTransition($t1); $this->assertCount(1, $fsm1->getStates()); $this->assertCount(1, $fsm1->getTransitions()); $this->assertCount(0, $fsm1->getInitialStates()); $this->assertCount(0, $fsm1->getFinalStates()); $this->assertNull($fsm1->getStateByName('s4')); $this->assertNull($fsm1->getStateByName('')); $this->assertNotNull($fsm1->getTransitionByName('t1')); $this->assertNull($fsm1->getTransitionByName('t31')); $this->assertNull($fsm1->getTransitionByName('')); }
} public function getMyState() { return $this->myState; } } // definig the FSM $fsm = new Fsm('fsm1'); $fsm->setStates(array(new State('s1', true), new State('s2'), new State('s3'), new State('s4', false, true))); $fsm->setTransitions(array(new Transition('t1', 's1', 's2'), new Transition('t2', 's1', 's3'), new Transition('t3', 's3', 's1'), new Transition('t4', 's2', 's4'))); $doc = new Document(); $doc->setMyState('s1'); $accessor = new FsmAccessor($fsm, new FsmValidator(), 'Document', 'myState'); try { if ($accessor->isInitialState($doc)) { printf('The object is in the INITIAL state%s', PHP_EOL); } $accessor->doTransition($doc, 't1'); printTransition($fsm->getTransitionByName('t1')); $accessor->doTransition($doc, 't4'); printTransition($fsm->getTransitionByName('t4')); if ($accessor->isFinalState($doc)) { printf('The object has reached an FINAL state%s', PHP_EOL); } } catch (\Exception $e) { throw $e; } function printTransition(Transition $transition) { printf('Transition <%s> from state <%s> to state <%s> completed%s', $transition->getName(), $transition->getFromStateName(), $transition->getToStateName(), PHP_EOL); }