Click here to Skip to main content
15,884,537 members
Articles / Programming Languages / C#

Generic DFA State Machine for .NET

Rate me:
Please Sign up or sign in to vote.
4.75/5 (42 votes)
21 May 2003BSD6 min read 224.3K   3.6K   75  
Seemless NFA to DFA transfers with GraphViz graphing integration
using System;
using leppie.FA;
using System.IO;
using leppie;

class Test 
{
   static void Main()
   {
      HiPerfTimer timer = new HiPerfTimer();
      CharState root = new CharState();
      Int32State introot = new Int32State();
      ObjectState objroot = new ObjectState();
      StringState strroot = new StringState();

      StreamReader reader = File.OpenText("words.small");
      while (reader.Peek()> -1)
      {
         char[] str = reader.ReadLine().ToCharArray();
         root.Add(str);
         /*
          * tests
                     int[] istr = new int[str.Length];
                     for (int i = 0; i < istr.Length; i++)
                        istr[i] = (int) str[i];

                     introot.Add(istr);

                     object[] ostr = new object[str.Length];
                     for (int i = 0; i < ostr.Length; i++)
                        ostr[i] = (object) str[i];

                     objroot.Add(ostr);
          */
      }
      reader.Close();


      char[] hello = "shello".ToCharArray();
      timer.Start();
      bool res = root.Accepts(hello);
      timer.Stop();
      Console.WriteLine(res);

      reader = File.OpenText("test.txt");
      while (reader.Peek() > -1)
      {
         strroot.Add(reader.ReadLine().Split(' '));
      }

      reader.Close();
      
      timer.Start();
      string[][] sresults = strroot.AcceptStates("leppie".Split(' '));
      timer.Stop();

      for (int i = 0; i < sresults.Length; i++)
         Console.WriteLine(String.Join(" ", sresults[i]));

      timer.Start();
      sresults = strroot.AcceptStates("leppie can".Split(' '));
      timer.Stop();

      for (int i = 0; i < sresults.Length; i++)
         Console.WriteLine(String.Join(" ", sresults[i]));

      timer.Start();
      // who can swim?
      sresults = strroot.Match( new String[]{null, "can", "swim"});
      timer.Stop();
      for (int i = 0; i < sresults.Length; i++)
         Console.WriteLine(String.Join(" ", sresults[i]));

      timer.Start();
      //who 'can' do what?
      sresults = strroot.Match( new String[]{null, "can", null});
      timer.Stop();

      for (int i = 0; i < sresults.Length; i++)
         Console.WriteLine(String.Join(" ", sresults[i]));

      timer.Start();
      char[][] cres = root.Match(new Char[] {'\0', 'e', 'l', '\0'});
      timer.Stop();
      
      if (cres != null)
         for (int i = 0; i < cres.Length; i++)
            Console.WriteLine(new String(cres[i]));

      /*
       * test
               int[] ihello = new int[hello.Length];
               hello.CopyTo(ihello, 0);

               res = introot.Accepts(ihello);
               Console.WriteLine(res);

               object[] ohello = new object[hello.Length];
               hello.CopyTo(ohello, 0);

               res = objroot.Accepts(ohello);
               Console.WriteLine(res);
      */
      timer.Start();
      cres = root.AcceptStates("he".ToCharArray());
      timer.Stop();
      Console.WriteLine(cres);

      timer.Start();
      AtomicState cstate = root.GetAtomicStateAt( "he".ToCharArray());
      timer.Stop();
      Console.WriteLine(cres);

//      if (cres != null)
//         for (int i = 0; i < cres.Length; i++)
//            Console.WriteLine(new String(cres[i]));

      timer.Start();
      cres = root.AcceptStates(null);
      timer.Stop();

      Console.WriteLine(cres.Length);
      
      timer.Start();
      cres = root.AcceptStates("haematorrhachi".ToCharArray());
      timer.Stop();
      Console.WriteLine(cres);
      timer.Start();
      cres = root.AcceptStates("heavenly".ToCharArray());
      timer.Stop();
      Console.WriteLine(cres);
      TypeState typeroot = new TypeState();
      Type[] types = System.Reflection.Assembly.GetAssembly(typeof(AtomicState)).GetTypes();
      
      foreach (Type type in types)
      {
         System.Collections.ArrayList arr = new System.Collections.ArrayList();
         Type etype = type;
         do 
         {
            arr.Insert(0, etype);
            etype = etype.BaseType;
         }
         while (etype != null);

         typeroot.Add( (Type[]) arr.ToArray(typeof(Type)));
      }
      string filename = "graph.dot";
      string gfx = leppie.FA.Support.GraphViz.Generate(typeroot, true, leppie.FA.Support.GraphStateShape.box);
      TextWriter writer = File.CreateText(filename);
      writer.Write(gfx);
      writer.Close();
      System.Diagnostics.Process.Start("dot", String.Format("-Tpng {0} -o {0}.jpg", filename));
      return;
   }
}


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
Software Developer
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions