public function testGoodAssert() { $fsm = new Fsm('fsm'); $fsm->setStates(array(new State('s1'), new State('s2'), new State('s3')))->setTransitions(array(new Transition('t1', 's1', 's2'), new Transition('t2', 's2', 's3'))); $assert = new NoDuplicateTransitionNamesAssert(); $assert->validate($fsm, false); }
public function testGoodAssert() { $fsm = new Fsm('fsm'); $fsm->setStates(array(new State('s1', true), new State('s2'), new State('s3'))); $assert = new AtLeastOneInitialStateAssert(); $assert->validate($fsm); }
public function testGoodAssert() { $fsm = new Fsm('fsm'); $fsm->setStates(array(new State('s1'), new State('s2'), new State('s3'))); $assert = new NoDuplicateStatesAssert(); $assert->validate($fsm); }
private function newAccessor() { $fsm = new Fsm('my_fsm'); $fsm->setStates(array(new State('s1', true, true), new State('s2'), new State('s3'), new State('s4', false, true), new State('s5', false, true)))->setTransitions(array(new Transition('t1', 's1', 's2'), new Transition('t2', 's1', 's3'), new Transition('t3', 's3', 's1'), new Transition('t4', 's2', 's4'), new Transition('t5', 's2', 's5'))); $validator = new FsmValidator(); $validator->addAssert(new Assert\NoDuplicateStatesAssert())->addAssert(new Assert\NoDuplicateTransitionNamesAssert())->addAssert(new Assert\NoTransitionWithUndefinedStatesAssert())->addAssert(new Assert\OneInitialStateAssert()); $validator->validate($fsm); return new FsmAccessor($fsm, $validator, 'Document', 'myState'); }
public function testProperty() { $fsm1 = new Fsm('fsm1'); $fsm1->addProperty('color', 'blue')->addProperty('height', ''); $this->assertEquals('blue', $fsm1->getProperty('color')); $this->assertEmpty($fsm1->getProperty('heigth')); $this->assertNull($fsm1->getProperty('width')); $this->assertEquals('123', $fsm1->getProperty('width', '123')); $this->assertFalse($fsm1->getProperty('width', false)); $this->assertEmpty($fsm1->getProperty('jasvd', '')); }
/** * @expectedException \Michcald\Fsm\Exception\Validator\Assert\DuplicateStateException */ public function testNoDuplicateStateAssertException() { $fsm = new Fsm('fsm'); $fsm->setStates(array(new State('s1', true), new State('s1', false, true), new State('s2'))); $validator = new FsmValidator(); $validator->addAssert(new Assert\NoDuplicateStatesAssert()); $validator->validate($fsm); }
<?php require __DIR__ . '/../vendor/autoload.php'; use Michcald\Fsm\Model\Fsm; use Michcald\Fsm\Model\State; use Michcald\Fsm\Model\Transition; use Michcald\Fsm\Validator\FsmValidator; // initializing the FSM $fsm = new Fsm('fsm1'); $fsm->setStates(array(new State('s1', true), new State('s2'), new State('s3'), new State('s4', false, true)))->setTransitions(array(new Transition('t1', 's1', 's2'), new Transition('t2', 's1', 's3'), new Transition('t3', 's3', 's1'), new Transition('t4', 's2', 's4'))); // validating the FSM $validator = new FsmValidator(); $isValid = $validator->validate($fsm); if ($isValid) { printf('FSM <%s> is valid%s', $fsm->getName(), PHP_EOL); } else { printf('FSM <%s> is NOT valid%s', $fsm->getName(), PHP_EOL); } // or we can use exceptions // adding an invalid transition $fsm->addTransition(new Transition('t1', 's1', 's2')); try { $validator->validate($fsm); printf('FSM <%s> is valid%s', $fsm->getName(), PHP_EOL); } catch (\Exception $e) { printf('FSM <%s> is NOT valid%s', $fsm->getName(), PHP_EOL); }
<?php require __DIR__ . '/../vendor/autoload.php'; use Michcald\Fsm\Model\Fsm; use Michcald\Fsm\Model\State; use Michcald\Fsm\Model\Transition; $fsm = new Fsm('fsm1'); $fsm->setStates(array(new State('s1', true), new State('s2'), new State('s3'), new State('s4', false, true)))->setTransitions(array(new Transition('t1', 's1', 's2'), new Transition('t2', 's1', 's3'), new Transition('t3', 's3', 's1'), new Transition('t4', 's2', 's4'))); printf('# FSM <%s>%s', $fsm->getName(), PHP_EOL); printf('%s', PHP_EOL); printf('# States%s', PHP_EOL); foreach ($fsm->getStates() as $state) { printf("State <%s> of type <%s>%s", $state->getName(), $state->getIsInitial() ? 'INITIAL' : ($state->getIsFinal() ? 'FINAL' : 'NORMAL'), PHP_EOL); } printf('%s', PHP_EOL); printf('# Transitions%s', PHP_EOL); foreach ($fsm->getTransitions() as $transition) { printf("Transition <%s> from state <%s> to state <%s>%s", $transition->getName(), $transition->getFromStateName(), $transition->getToStateName(), PHP_EOL); }
use Michcald\Fsm\Validator\FsmValidator; class Document implements StatefulInterface { private $myState; public function setMyState($state) { $this->myState = $state; return $this; } 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);