protected function setUp() { $this->env = new \Twig_Environment( new \Twig_Loader_Array( array( 'state' => '{{ finite_state(object) }}', 'transitions' => '{% for transition in finite_transitions(object) %}{{ transition }}{% endfor %}', 'properties' => '{% for property, val in finite_properties(object) %}{{ property }}{% endfor %}', 'has' => '{{ finite_has(object, property) ? "yes" : "no" }}', 'can' => '{{ finite_can(object, transition) ? "yes" : "no" }}' ) ) ); $container = new \Pimple(array( 'state_machine' => function() { $sm = new StateMachine; $sm->addState(new State('s1', State::TYPE_INITIAL, array(), array('foo' => true, 'bar' => false))); $sm->addTransition('t12', 's1', 's2'); $sm->addTransition('t23', 's2', 's3'); return $sm; } )); $this->context = new Context(new PimpleFactory($container, 'state_machine'));; $this->env->addExtension(new FiniteExtension($this->context)); }
protected function setUp() { $container = new \Pimple(array('state_machine' => function () { $sm = new StateMachine(); $sm->addTransition('t12', 's1', 's2'); $sm->addTransition('t23', 's2', 's3'); return $sm; })); $this->object = new PimpleFactory($container, 'state_machine'); }
public function setUp() { $container = new \Pimple(array('state_machine' => function () { $sm = new StateMachine(); $sm->addState(new State('s1', State::TYPE_INITIAL, array(), array('foo' => true, 'bar' => false))); $sm->addTransition('t12', 's1', 's2'); $sm->addTransition('t23', 's2', 's3'); return $sm; })); $this->object = new Context(new PimpleFactory($container, 'state_machine')); }
public function setUp() { $this->accessor = $accessor = $this->getMock('Finite\\State\\Accessor\\StateAccessorInterface'); $container = new \Pimple(array('state_machine' => function () use($accessor) { $sm = new StateMachine(null, null, $accessor); $sm->addTransition('t12', 's1', 's2'); $sm->addTransition('t23', 's2', 's3'); return $sm; })); $this->object = new PimpleFactory($container, 'state_machine'); }
public function setUp() { $this->accessor = $accessor = $this->getMock('Finite\\State\\Accessor\\StateAccessorInterface'); $container = new \Pimple(array('state_machine' => function () use($accessor) { $sm = new StateMachine(null, null, $accessor); $sm->addState(new State('s1', State::TYPE_INITIAL, array(), array('foo' => true, 'bar' => false))); $sm->addTransition('t12', 's1', 's2'); $sm->addTransition('t23', 's2', 's3'); return $sm; })); $this->object = new Context(new PimpleFactory($container, 'state_machine')); }
public function testSkipEdgeException() { $visitor = $this->mockVisitor(); $visitor->expects($this->once())->method('getEdgeAttributes')->willThrowException(new SkipElementException()); $g = $this->providerGraphviz(); $sm = new StateMachine(); $sm->addState(new State('s1', State::TYPE_INITIAL)); $sm->addState(new State('s2', State::TYPE_FINAL)); $sm->addTransition('t12', 's1', 's2'); $g->addVisitor($visitor); $dot = $g->render($sm); $this->assertNotContains('"t12"', $dot); $this->assertContains('"s1"', $dot); $this->assertContains('"s2"', $dot); }
protected function addTransitions() { foreach ($this->transitionsProvider() as $transitions) { $this->object->addTransition($transitions[0], $transitions[1], $transitions[2]); } }
use App\Things\Thing; use Finite\State\State; use Finite\State\StateInterface; use Finite\StateMachine\StateMachine; // Composer Autoloader require_once '../vendor/autoload.php'; // Create a new State Machine $stateMachine = new StateMachine(); // define some States $stateMachine->addState(new State('state1', StateInterface::TYPE_INITIAL)); $stateMachine->addState(new State('state2')); $stateMachine->addState(new State('state3')); $stateMachine->addState(new State('state4', StateInterface::TYPE_FINAL)); $thing = new Thing(); // add some transitions $stateMachine->addTransition('from_1_to_2', 'state1', 'state2'); $stateMachine->addTransition('from_2_to_3', 'state2', 'state3'); $stateMachine->addTransition('from_3_to_4', 'state3', 'state4'); $stateMachine->addTransition('from_4_to_2', 'state4', 'state2'); // add stateful object into state machine $stateMachine->setObject($thing); $stateMachine->initialize(); // current state echo $stateMachine->getCurrentState(); // can transition to a valid state var_dump($stateMachine->can('from_1_to_2')); // can transition to a invalid state var_dump($stateMachine->can('from_2_to_3')); // switch state $stateMachine->apply('from_1_to_2'); var_dump($stateMachine->can('from_2_to_3'));