function it_reverses_the_automata(State $state, Transition $transition, Symbol $symbol) { $state->isFinal()->willReturn(true); $state->getTransitions()->willReturn($transition); $transition->getSymbol()->willReturn($symbol); $transition->getState()->willReturn($state); $this->reverse()->shouldBeAnInstanceOf(NFA::class); }
/** * @return NFA */ public function reverse() { $nfa = new NFA(); $map = [spl_object_hash($this->state) => ['original' => $this->state, 'reversed' => new NFAState(true), 'starting' => $this->state->isFinal()]]; for ($i = 0; $i < count($map); $i++) { $key = array_keys($map)[$i]; if ($map[$key]['starting']) { $nfa->addStartingState($map[$key]['reversed']); } foreach ($map[$key]['original']->getTransitions() as $transition) { $state = $transition->getState(); $hash = spl_object_hash($state); if (!array_key_exists($hash, $map)) { $map[$hash] = ['original' => $state, 'reversed' => new NFAState(false), 'starting' => $state->isFinal()]; } $map[$hash]['reversed']->on($transition->getSymbol())->visit($map[$key]['reversed']); } } return $nfa; }