Beispiel #1
0
        /**
         * Test to see if the NFA was successfully converted into a valid DFA
         */
        public static Boolean NFAClosureTest(NFA nfa, DFA dfa)
        {
            // 1 Output to 1 Input Test
            var index = nfa.nextStates.Keys.ToArray();

            string[] input;
            int      nextStateCount;

            int num       = 0;
            int numErrors = 0;

            var node = dfa.nodes.ToArray();

            // For each transition on every node in the DFA, confirm that there is a 1-to-1 Input/Output for each
            // transition
            for (int i = 0; i < node.Length; i++)
            {
                input = dfa.nextStates[node[i]].Keys.ToArray();

                for (int j = 0; j < input.Length; j++)
                {
                    nextStateCount = dfa.nextStates[node[i]][input[j]].Count();

                    if (nextStateCount > 1)
                    {
                        Console.WriteLine("1-to-1 Input/Output Test Failed: " + "\n" + "\t" +
                                          "Node Index: " + node[i] + " has " + nextStateCount + " transitions on input" + "\n" + "\t"
                                          + input[j]);
                        return(false);
                    }
                }
            }

            Console.WriteLine("1-to-1 Input/Output Test Passed");

            // Proper States Test

            // Ensure that all the states in the DFA are comprised of states that existed in the NFA

            int[] stateArray;
            int   maxIndex = ((nfa.GetTerm().Length + 1) * 10) + nfa.GetAllowableErrors();
            int   minIndex = 10;

            for (int i = 0; i < node.Length; i++)
            {
                stateArray = dfa.states[node[i]].ToArray();

                for (int j = 0; j < stateArray.Length; j++)
                {
                    if (stateArray[j] > maxIndex || stateArray[j] < minIndex)
                    {
                        Console.WriteLine("Proper States Test Failed: Index:" + node[i]);
                        return(false);
                    }
                }
            }

            Console.WriteLine("Proper States Test Passed");

            // Any Transition Test

            // For the resulting DFA of a NFA under closure, confirm that all the information of the "any"
            // next states is incorporated in the new node

            /*HashSet<string> inputs = new HashSet<string>();
             * int[] stateArray;
             * Dictionary<string, HashSet<int>> nextStates = new Dictionary<string, HashSet<int>>();
             *
             * HashSet<string> cInputs;
             * HashSet<int> cStates;
             * Dictionary<int, Dictionary<string, HashSet<int>>> cNextStates = nfa.nextStates;
             *
             * var nodes = nfa.getNodes().ToArray();
             * var cNodes = dfa.nodes.ToArray();
             *
             * for (int i = 0; i < cNodes.Length; i++)
             * {
             *  stateArray = nfa.states[cNodes[i]].ToArray();
             *
             *  // For each node in this closed index,
             *  for (int j = 0; j < stateArray.Length; j++)
             *  {
             *      inputs.UnionWith(nfa.inputs[stateArray[j]]);
             *      nfa.mergeNextStates(nextStates, nfa.nextStates[stateArray[j]]);
             *  }
             *
             *  // Now that the next states are filled, can test to see if they are identical to the closed node's
             *
             *  // First check that the inputs are the same
             *  if (inputs == nfa.inputs[cNodes[i]])
             *  {
             *      // For each input, confirm that their next states are identical
             *      input = inputs.ToArray();
             *
             *      for (int j = 0; j < input.Length; j++)
             *      {
             *          var indice = nfa.getCombinedIndex(nextStates[input[j]].ToArray());
             *
             *          if (indice != nfa.getCombinedIndex(cNextStates[cNodes[i]][input[j]].ToArray()))
             *          {
             *              // Test fails
             *              return false;
             *          }
             *      }
             *  }
             *  else
             *  {
             *      // Test Fails
             *      return false;
             *  }
             * }*/

            return(true);
        }
Beispiel #2
0
        /**
         * Test to see if the NFA was generated correctly
         */
        public static Boolean NFAGenerationTest(NFA nfa)
        {
            // Min./Max. Index Test
            // All node indexes are less than the terms maximum index
            var maxErrors = nfa.GetAllowableErrors();
            var p         = maxErrors + 1;

            var minIndex = nfa.GetStartState();
            var maxIndex = (2 << (nfa.GetTerm().Length + p)) + (2 << maxErrors);

            var nodes = nfa.getNodes().ToArray();

            // Each node must be less than or equal to the maximum index in order to pass
            for (int i = 0; i < nodes.Length; i++)
            {
                if (nodes[i] < minIndex)
                {
                    Console.WriteLine("Minimum Index Test Failed: " + "\n" + "\t" +
                                      "Node Index: " + nodes[i] + " is under the minimum index of " + minIndex);
                    return(false);
                }
                else if (nodes[i] > maxIndex)
                {
                    Console.WriteLine("Maximum Index Test Failed: " + "\n" + "\t" +
                                      "Node Index: " + nodes[i] + " is over the maximum index of " + maxIndex);
                    return(false);
                }
            }

            Console.WriteLine("Min./Max. Index Test Passed");

            // Max Errors Test
            int numErrors = 0;
            int num       = 0;
            int power;

            for (int i = 0; i < nodes.Length; i++)
            {
                power     = nfa.GetPower(nodes[i]);
                numErrors = nfa.GetPower((int)(nodes[i] - Math.Pow(2, power)));
                if (numErrors > maxErrors)
                {
                    Console.WriteLine("Max Errors Test Failed: " + "\n" + "\t" +
                                      "Node Index: " + nodes[i] + " has " + ((nodes[i] % 10) - maxErrors)
                                      + " more error(s) than is allowed (" + maxErrors + ")");
                    return(false);
                }
            }

            Console.WriteLine("Max Errors Test Passed");

            // Valid Next States

            var index = nfa.nextStates.Keys.ToArray();

            string[] input;
            int[]    stateSet;

            for (int i = 0; i < index.Length; i++)
            {
                power     = nfa.GetPower(index[i]);
                num       = power - (p + 1);
                numErrors = nfa.GetPower((int)(nodes[i] - Math.Pow(2, power)));

                input = nfa.nextStates[index[i]].Keys.ToArray();

                for (int j = 0; j < input.Length; j++)
                {
                    stateSet = nfa.nextStates[index[i]][input[j]].ToArray();

                    for (int k = 0; k < stateSet.Length; k++)
                    {
                        var test = (2 << power) + (2 << (numErrors + (maxErrors - numErrors)));
                        if (stateSet[k] > test)
                        {
                            Console.WriteLine("Valid Next States Test Failed: " + "\n" + "\t" +
                                              "Node Index: " + index[i] + " has a next state of " + stateSet[k]);
                            return(false);
                        }
                    }
                }
            }

            Console.WriteLine("Valid Next States Passed");

            Console.WriteLine("NFA Generation Tests Passed");

            return(true);
        }