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; }
/** * @Given I have an automata that validates the text :text * * @param string $text */ public function iHaveAnAutomataThatValidatesTheText($text) { $state = new State(); $this->startingStates[] = $state; $total = strlen($text); for ($i = 0; $i < $total; $i++) { $newState = new State($i === $total - 1); $state->on(new CharSymbol($text[$i]))->visit($newState); $state = $newState; } }