コード例 #1
0
ファイル: test.php プロジェクト: vassilmladenov/nfaToDfa
 public function testPowerSet()
 {
     require "app/state.php";
     //initialize test input
     $input = array("A", "B");
     //get the power set
     $result = powerSet($input);
     //Make sure the outputs are correct
     $this->assertContains(array(), $result);
     $this->assertContains(array("A"), $result);
     $this->assertContains(array("B"), $result);
     $this->assertContains(array("A", "B"), $result);
 }
コード例 #2
0
ファイル: state.php プロジェクト: vassilmladenov/nfaToDfa
/**
 * Main logic of the server, performs the NFA to DFA conversion via subset transformation
 * @param  array $stateNames  array of NFA state names for easy access
 * @param  array $transitions array of transitions for easy access
 * @param  array $nfaStates   array of States of an NFA generated by jsonToStateArray function
 * @return array              array of States of a DFA, state names will be stringified
 */
function transformToDfa($stateNames, $transitions, $nfaStates)
{
    $dfaStates = [];
    // generate an array of blank DFAStates based on the power set of the NFA States
    $powerSet = powerSet($stateNames);
    foreach ($powerSet as $subset) {
        $name = generateDFAStateName($subset);
        $d = new DFAState($name);
        $d->setSubstates($subset);
        $dfaStates[$name] = $d;
    }
    /**
     * Take a DFAState and merge all of the states in the transition sets of its
     * component substates
     *
     * For instance, let NFAStates:
     * A where d(A, 0) = [B]
     * B where d(B, 0) = [A]
     *
     * then for DFAState:
     * AB, we need d(AB, 0) = AB
     */
    foreach ($dfaStates as $dfaState) {
        foreach ($transitions as $transition) {
            $collector = [];
            // will hold the set of destination states
            foreach ($dfaState->substates as $substateName) {
                $nfaState = $nfaStates[$substateName];
                // get access to the NFA object by name
                $toStates = $nfaState->adjacencyList[$transition];
                // merge the states in the adjacencyLists of all the component NFA states
                // use array_unique to weed out duplicates
                $collector = array_unique(array_merge($collector, $toStates));
            }
            // need to call sort so that the DFAState names are consistent, i.e. BA vs. AB
            sort($collector);
            $toState = generateDFAStateName($collector);
            $dfaState->setTransition($transition, $toState);
        }
    }
    return $dfaStates;
}