Sunday, 10 December 2017

Code of Web API ASP.Net

public class Employee
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string Surname { get; set; }
        public string Email { get; set; }

        public int AddressID { get; set; }
        public virtual Address Address { get; set; }

        public int CompanyID { get; set; }
        public virtual Company Company { get; set; }
    }

public class Company
    {
        public int ID { get; set; }
        public string Name { get; set; }

        public virtual List<Employee> Employees { get; set; }

        public Company()
        {
            Employees = new List<Employee>();
        }
    }

public class EntitiesContext : DbContext
    {
        public EntitiesContext()
            : base("EntitiesContext")
        {
            // Do not forget to set the connection string in Web.config..
            Configuration.ProxyCreationEnabled = false;
        }

        public DbSet<Employee> Employees { get; set; }
        public DbSet<Company> Companies { get; set; }
        public DbSet<Address> Addresses { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new EmployeeConfiguration());
            modelBuilder.Configurations.Add(new CompanyConfiguration());
        }
    }

public class EntitiesInitializer : DropCreateDatabaseIfModelChanges<EntitiesContext>
    {
        protected override void Seed(EntitiesContext context)
        {
            // Companies
            GetCompanies().ForEach(c => context.Companies.Add(c));

            // Addresses
            GetAddresses().ForEach(a => context.Addresses.Add(a));

            // Employees
            GetEmployees().ForEach(e => context.Employees.Add(e));

            base.Seed(context);
        }

        private static List<Company> GetCompanies()
        {
            List<Company> _companies = new List<Company>();

            for (int i = 1; i <= 10; i++)
            {
                _companies.Add(new Company()
                    {
                        ID = i,
                        Name = MockData.Company.Name()
                    });
            }

            return _companies;
        }

        private static List<Address> GetAddresses()
        {
            List<Address> _addresses = new List<Address>();

            for (int i = 1; i <= 200; i++)
            {
                _addresses.Add(new Address()
                {
                    ID = i,
                    City = MockData.Address.City(),
                    Country = MockData.Address.Country(),
                    State = MockData.Address.State(),
                    ZipCode = MockData.Address.ZipCode()
                });
            }

            return _addresses;
        }

        private static List<Employee> GetEmployees()
        {
            List<Employee> _employees = new List<Employee>();

            for (int i = 1; i <= 200; i++)
            {
                _employees.Add(new Employee()
                {
                    ID = i,
                    FirstName = MockData.Person.FirstName(),
                    Surname = MockData.Person.Surname(),
                    AddressID = i,
                    CompanyID = new Random().Next(1, 10),
                    Email = MockData.Internet.Email(),
                });
            }

            return _employees;
        }
    }

public class EmployeesController : ApiController
    {
        private EntitiesContext _dbContext;

        public EmployeesController()
        {
            this._dbContext = new EntitiesContext();
        }

        public IEnumerable<Employee> GetEmployees()
        {
            try
            {
                return _dbContext.Employees.ToArray();
            }
            catch(Exception ex)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            _dbContext.Dispose();
        }
    }

public class EmployeesController : EntitySetController<Employee, int>
    {
        private EntitiesContext _dbContext;

        public EmployeesController()
        {
            this._dbContext = new EntitiesContext();
        }

        [Queryable]
        public override IQueryable<Employee> Get()
        {
            return _dbContext.Employees;
        }

        protected override Employee GetEntityByKey(int key)
        {
            return _dbContext.Employees.Where(e => e.ID == key).FirstOrDefault();
        }

        // /odata/Employees(1)/Address
        public Address GetAddressFromEmployee(int key)
        {
            return _dbContext.Employees.Where(e => e.ID == key)
                .Select(e => e.Address).FirstOrDefault();
        }

        protected override Employee CreateEntity(Employee entity)
        {
            _dbContext.Employees.Add(entity);
            _dbContext.SaveChanges();
            return entity;
        }

        protected override Employee UpdateEntity(int key, Employee update)
        {
            if (!_dbContext.Employees.Any(e => e.ID == key))
            {
                throw new HttpResponseException(
                    Request.CreateODataErrorResponse(
                    HttpStatusCode.NotFound,
                    new ODataError
                    {
                        ErrorCode = "NotFound.",
                        Message = "Employee " + key + " not found."
                    }));
            }

            update.ID = key;

            _dbContext.Employees.Attach(update);
            _dbContext.Entry(update).State = System.Data.Entity.EntityState.Modified;
            _dbContext.SaveChanges();
            return update;
        }

        protected override Employee PatchEntity(int key, Delta<Employee> patch)
        {
            var employee = _dbContext.Employees.FirstOrDefault(e => e.ID == key);
            if (employee == null)
                throw new HttpResponseException(
                    Request.CreateODataErrorResponse(
                    HttpStatusCode.NotFound,
                    new ODataError
                    {
                        ErrorCode = "NotFound.",
                        Message = "Employee " + key + " not found."
                    }));

            patch.Patch(employee);
            _dbContext.SaveChanges();

            return employee;
        }

        public override void Delete(int key)
        {
            var employee = _dbContext.Employees.Where(a => a.ID == key).FirstOrDefault();
            if (employee != null)
            {
                _dbContext.Employees.Remove(employee);
                _dbContext.SaveChanges();
            }
            else
                throw new HttpResponseException(
                    Request.CreateODataErrorResponse(
                    HttpStatusCode.NotFound,
                    new ODataError
                    {
                        ErrorCode = "NotFound.",
                        Message = "Employee " + key + " not found."
                    }));
        }

        // The base POST method will call to form the location header
        protected override int GetKey(Employee entity)
        {
            return entity.ID;
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            _dbContext.Dispose();
        }
    }

Secure a Web API with Individual Accounts and Local Login in ASP.NET Web API 2.2

https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/individual-accounts-in-web-api

Web API in ASP.Net


ASP.Net Web API


Web API


UI Layer


Sunday, 3 December 2017

Creating an Image Processing Library with C#

http://studentguru.gr/b/jupiter/archive/2009/10/14/creating-an-image-processing-library-with-c-part-1

Saturday, 2 December 2017

Algorithms in C#

http://www.c-sharpcorner.com/technologies/algorithms-in-csharp

Deadlock in C# Threading

using System;
using System.Threading;
namespace deadlockincsharp
{
public class Akshay
    {
        static readonly object firstLock = new object();
        static readonly object secondLock = new object();
        static void ThreadJob()
        {
            Console.WriteLine("\t\t\t\tLocking firstLock");
            lock (firstLock)
            {
                Console.WriteLine("\t\t\t\tLocked firstLock");
                // Wait until we're fairly sure the first thread
                // has grabbed secondLock
                Thread.Sleep(1000);
                Console.WriteLine("\t\t\t\tLocking secondLock");
                lock (secondLock)
                {
                    Console.WriteLine("\t\t\t\tLocked secondLock");
                }
                Console.WriteLine("\t\t\t\tReleased secondLock");
            }
            Console.WriteLine("\t\t\t\tReleased firstLock");
        }
        static void Main()
        {
            new Thread(new ThreadStart(ThreadJob)).Start();
            // Wait until we're fairly sure the other thread
            // has grabbed firstLock
            Thread.Sleep(500);
            Console.WriteLine("Locking secondLock");
            lock (secondLock)
            {
                Console.WriteLine("Locked secondLock");
                Console.WriteLine("Locking firstLock");
                lock (firstLock)
                {
                    Console.WriteLine("Locked firstLock");
                }
                Console.WriteLine("Released firstLock");
            }
            Console.WriteLine("Released secondLock");
            Console.Read();
        }     
    }
}

object lockA = new object();
object lockB = new object();
        Thread 1 void t1()
        {
            lock (lockA)
            {
                lock (lockB)
                {
                    /* ... */
                }
            }
        }
        Thread 2 void t2()
        {
            lock (lockB)
            {
                lock (lockA)
                   {
                    /* ... */
                   }
            }
        }

Deadlock Sample in C#

public class DeadlockSamples
{
    private static object staticObjLockA = new Object();
    private static object staticObjLockB = new Object();

    public static void Main()
    {
        // Initialize thread with address of DoWork1
        Thread thread1 = new Thread(DoWork1);

        // Initilaize thread with address of DoWork2
        Thread thread2 = new Thread(DoWork2);

        // Start the Threads.
        thread1.Start();
        thread2.Start();

        thread1.Join();
        thread2.Join();

        // This statement will never be executed.
        Console.WriteLine("Done Processing...");
    }

    private static void DoWork1()
    {
        lock (staticObjLockA)
        {
            Console.WriteLine("Trying to acquire lock on staticObjLockB");

            // Sleep to yield.
            Thread.Sleep(1000);
            lock (staticObjLockB)
            {
                // This block will never be executed.
                Console.WriteLine("In DoWork1 Critical Section.");
                // Access some shared resource here.
            }
        }
    }

    private static void DoWork2()
    {
        lock (staticObjLockB)
        {
            Console.WriteLine("Trying to acquire lock on staticObjLockA");
            lock (staticObjLockA)
            {
                // This block will never be executed.
                Console.WriteLine("In DoWork2 Critical Section.");
                // Access some shared resource here.
            }
        }
    }
}

public class DeadlockSamplesMonitor
{
    private static object staticObjLockA = new Object();
    private static object staticObjLockB = new Object();

    public static void Main()
    {
        // Initialize thread with address of DoWork1
        Thread thread1 = new Thread(DoWork1);

        // Initilaize thread with address of DoWork2
        Thread thread2 = new Thread(DoWork2);

        // Start the Threads.
        thread1.Start();
        thread2.Start();

        thread1.Join();
        thread2.Join();
       
        Console.WriteLine("Done Processing...");
    }

    private static void DoWork1()
    {
        lock (staticObjLockA)
        {
            Console.WriteLine("Trying to acquire lock on staticObjLockB");

            // Sleep to yield.
            Thread.Sleep(1000);

            // This will try to acquire lock for 5 seconds.
            if (Monitor.TryEnter(staticObjLockB, 5000))
            {
                try
                {
                    // This block will never be executed.
                    Console.WriteLine("In DoWork1 Critical Section.");
                    // Access some shared resource here.
                }
                finally
                {
                    Monitor.Exit(staticObjLockB);
                }
            }
            else
            {
                // Print lock not able to acquire message.
                Console.WriteLine("Unable to acquire lock, exiting DoWork1.");
            }               
        }
    }

    private static void DoWork2()
    {
        lock (staticObjLockB)
        {
            Console.WriteLine("Trying to acquire lock on staticObjLockA");
            lock (staticObjLockA)
            {                   
                Console.WriteLine("In DoWork2 Critical Section.");
                // Access some shared resource here.
            }
        }
    }
}

WPF Programs

http://www.c-sharpcorner.com/technologies/wpf

WPF in C#

http://www.wpf-tutorial.com/about-wpf/what-is-wpf/

Wednesday, 29 November 2017

DSFM

http://bezensek.com/blog/2014/08/13/deterministic-finite-state-machine-implementation-in-c-number/

DFA in C#

using System;
using SCG = System.Collections.Generic;
using C5;

using state = System.Int32;
using input = System.Char;

namespace RegularExpressionEngine
{
  /// <summary>
  /// Implements a deterministic finite automata (DFA)
  /// </summary>
  class DFA
  {
    // Start state
    public state start;
    // Set of final states
    public Set<state> final;
    // Transition table
    public SCG.SortedList<KeyValuePair<state, input>, state> transTable;

    public DFA()
    {
      final = new Set<state>();

      transTable = new SCG.SortedList<KeyValuePair<state, input>, state>(new Comparer());
    }

    public string Simulate(string @in)
    {
      state curState = start;

      CharEnumerator i = @in.GetEnumerator();

      while(i.MoveNext())
      {
        KeyValuePair<state, input> transition = new KeyValuePair<state, input>(curState, i.Current);

        if(!transTable.ContainsKey(transition))
          return "Rejected";

        curState = transTable[transition];
      }

      if(final.Contains(curState))
        return "Accepted";
      else
        return "Rejected";
    }

    public void Show()
    {
      Console.Write("DFA start state: {0}\n", start);
      Console.Write("DFA final state(s): ");

      SCG.IEnumerator<state> iE = final.GetEnumerator();

      while(iE.MoveNext())
        Console.Write(iE.Current + " ");

      Console.Write("\n\n");

      foreach(SCG.KeyValuePair<KeyValuePair<state, input>, state> kvp in transTable)
        Console.Write("Trans[{0}, {1}] = {2}\n", kvp.Key.Key, kvp.Key.Value, kvp.Value);
    }
  }

  /// <summary>
  /// Implements a comparer that suits the transTable SordedList
  /// </summary>
  public class Comparer : SCG.IComparer<KeyValuePair<state, input>>
  {
    public int Compare(KeyValuePair<state, input> transition1, KeyValuePair<state, input> transition2)
    {
      if(transition1.Key == transition2.Key)
        return transition1.Value.CompareTo(transition2.Value);
      else
        return transition1.Key.CompareTo(transition2.Key);
    }
  }

}
As you see, a DFA has 3 variables: a start state, a set of final states and a transition table that maps transitions between states.
Below I present the SubsetMachine class that is responsible for the hard work of extracting an equivalent DFA from a given NFA:
//
//  Regular Expression Engine C# Sample Application
//  2006, by Leniel Braz de Oliveira Macaferi & Wellington Magalhães Leite.
//
//  UBM's Computer Engineering - 7th term [http://www.ubm.br/]
// 
//  This program sample was developed and turned in as a term paper for Lab. of
//  Compilers Construction. It was based on the source code provided by Eli Bendersky
//  [http://eli.thegreenplace.net/] and is provided "as is" without warranty.
//

using System;
using SCG = System.Collections.Generic;
using C5;

using state = System.Int32;
using input = System.Char;

namespace RegularExpressionEngine
{
  class SubsetMachine
  {
    private static int num = 0;

    /// <summary>
    /// Subset machine that employs the powerset construction or subset construction algorithm.
    /// It creates a DFA that recognizes the same language as the given NFA.
    /// </summary>
    public static DFA SubsetConstruct(NFA nfa)
    {
      DFA dfa = new DFA();

      // Sets of NFA states which is represented by some DFA state
      Set<Set<state>> markedStates = new Set<Set<state>>();
      Set<Set<state>> unmarkedStates = new Set<Set<state>>();

      // Gives a number to each state in the DFA
      HashDictionary<Set<state>, state> dfaStateNum = new HashDictionary<Set<state>, state>();

      Set<state> nfaInitial = new Set<state>();
      nfaInitial.Add(nfa.initial);

      // Initially, EpsilonClosure(nfa.initial) is the only state in the DFAs states and it's unmarked.
      Set<state> first = EpsilonClosure(nfa, nfaInitial);
      unmarkedStates.Add(first);

      // The initial dfa state
      state dfaInitial = GenNewState();
      dfaStateNum[first] = dfaInitial;
      dfa.start = dfaInitial;

      while(unmarkedStates.Count != 0)
      {
        // Takes out one unmarked state and posteriorly mark it.
        Set<state> aState = unmarkedStates.Choose();

        // Removes from the unmarked set.
        unmarkedStates.Remove(aState);

        // Inserts into the marked set.
        markedStates.Add(aState);

        // If this state contains the NFA's final state, add it to the DFA's set of
        // final states.
        if(aState.Contains(nfa.final))
          dfa.final.Add(dfaStateNum[aState]);

        SCG.IEnumerator<input> iE = nfa.inputs.GetEnumerator();

        // For each input symbol the nfa knows...
        while(iE.MoveNext())
        {
          // Next state
          Set<state> next = EpsilonClosure(nfa, nfa.Move(aState, iE.Current));

          // If we haven't examined this state before, add it to the unmarkedStates and make up a new number for it.
          if(!unmarkedStates.Contains(next) && !markedStates.Contains(next))
          {
            unmarkedStates.Add(next);
            dfaStateNum.Add(next, GenNewState());
          }

          KeyValuePair<state, input> transition = new KeyValuePair<state, input>();
          transition.Key = dfaStateNum[aState];
          transition.Value = iE.Current;

          dfa.transTable[transition] = dfaStateNum[next];
        }
      }

      return dfa;
    }

    /// <summary>
    /// Builds the Epsilon closure of states for the given NFA
    /// </summary>
    /// <param name="nfa"></param>
    /// <param name="states"></param>
    /// <returns></returns>
    static Set<state> EpsilonClosure(NFA nfa, Set<state> states)
    {
      // Push all states onto a stack
      SCG.Stack<state> uncheckedStack = new SCG.Stack<state>(states);

      // Initialize EpsilonClosure(states) to states
      Set<state> epsilonClosure = states;

      while(uncheckedStack.Count != 0)
      {
        // Pop state t, the top element, off the stack
        state t = uncheckedStack.Pop();

        int i = 0;

        // For each state u with an edge from t to u labeled Epsilon
        foreach(input input in nfa.transTable[t])
        {
          if(input == (char)NFA.Constants.Epsilon)
          {
            state u = Array.IndexOf(nfa.transTable[t], input, i);

            // If u is not already in epsilonClosure, add it and push it onto stack
            if(!epsilonClosure.Contains(u))
            {
              epsilonClosure.Add(u);
              uncheckedStack.Push(u);
            }
          }

          i = i + 1;
        }
      }

      return epsilonClosure;
    }

    /// <summary>
    /// Creates unique state numbers for DFA states
    /// </summary>
    /// <returns></returns>
    private static state GenNewState()
    {
      return num++;
    }

  }
}

NFA in C#

using System;
using SCG = System.Collections.Generic;
using C5;

using state = System.Int32;
using input = System.Char;

namespace RegularExpressionEngine
{

  /// <summary>
  /// Implements a non-deterministic finite automata
  /// </summary>
  class NFA
  {
    public state initial;
    public state final;
    private int size;
    // Inputs this NFA responds to
    public SortedArray<input> inputs;
    public input[][] transTable;

    /// <summary>
    /// Provides default values for epsilon and none
    /// </summary>
    public enum Constants
    {
      Epsilon = 'ε',
      None = '\0'
    }

    public NFA(NFA nfa)
    {
      initial = nfa.initial;
      final = nfa.final;
      size = nfa.size;
      inputs = nfa.inputs;
      transTable = nfa.transTable;
    }

    /// <summary>
    /// Constructed with the NFA size (amount of states), the initial state and the
    /// final state
    /// </summary>
    /// <param name="size_">Amount of states.</param>
    /// <param name="initial_">Initial state.</param>
    /// <param name="final_">Final state.</param>
    public NFA(int size_, state initial_, state final_)
    {
      initial = initial_;
      final = final_;
      size = size_;

      IsLegalState(initial);
      IsLegalState(final);

      inputs = new SortedArray<input>();

      // Initializes transTable with an "empty graph", no transitions between its
      // states
      transTable = new input[size][];

      for(int i = 0; i < size; ++i)
        transTable[i] = new input[size];
    }

    public bool IsLegalState(state s)
    {
      // We have 'size' states, numbered 0 to size-1
      if(s < 0 || s >= size)
        return false;

      return true;
    }

    /// <summary>
    /// Adds a transition between two states.
    /// </summary>
    /// <param name="from"></param>
    /// <param name="to"></param>
    /// <param name="in"></param>
    public void AddTrans(state from, state to, input @in)
    {
      IsLegalState(from);
      IsLegalState(to);

      transTable[from][to] = @in;

      if(@in != (char)Constants.Epsilon)
        inputs.Add(@in);
    }

    /// <summary>
    /// Fills states 0 up to other.size with other's states.
    /// </summary>
    /// <param name="other"></param>
    public void FillStates(NFA other)
    {
      for(state i = 0; i < other.size; ++i)
        for(state j = 0; j < other.size; ++j)
          transTable[i][j] = other.transTable[i][j];

      SCG.IEnumerator<input> cE = other.inputs.GetEnumerator();

      while(cE.MoveNext())
        inputs.Add(cE.Current);
    }

    /// <summary>
    /// Renames all the NFA's states. For each nfa state: number += shift.
    /// Functionally, this doesn't affect the NFA, it only makes it larger and renames
    /// its states.
    /// </summary>
    /// <param name="shift"></param>
    public void ShiftStates(int shift)
    {
      int newSize = size + shift;

      if(shift < 1)
        return;

      // Creates a new, empty transition table (of the new size).
      input[][] newTransTable = new input[newSize][];

      for(int i = 0; i < newSize; ++i)
        newTransTable[i] = new input[newSize];

      // Copies all the transitions to the new table, at their new locations.
      for(state i = 0; i < size; ++i)
        for(state j = 0; j < size; ++j)
          newTransTable[i + shift][j + shift] = transTable[i][j];

      // Updates the NFA members.
      size = newSize;
      initial += shift;
      final += shift;
      transTable = newTransTable;
    }

    /// <summary>
    /// Appends a new, empty state to the NFA.
    /// </summary>
    public void AppendEmptyState()
    {
      transTable = Resize(transTable, size + 1);

      size += 1;
    }

    private static input[][] Resize(input[][] transTable, int newSize)
    {
      input[][] newTransTable = new input[newSize][];

      for(int i = 0; i < newSize; ++i)
        newTransTable[i] = new input[newSize];

      for(int i = 0; i <= transTable.Length - 1; i++)
        for(int j = 0; j <= transTable[i].Length - 1; j++)
        {
          if(transTable[i][j] != '\0')
            newTransTable[i][j] = transTable[i][j];
        }

      return newTransTable;
    }

    /// <summary>
    /// Returns a set of NFA states from which there is a transition on input symbol
    /// inp from some state s in states.
    /// </summary>
    /// <param name="states"></param>
    /// <param name="inp"></param>
    /// <returns></returns>
    public Set<state> Move(Set<state> states, input inp)
    {
      Set<state> result = new Set<state>();

      // For each state in the set of states
      foreach(state state in states)
      {
        int i = 0;

        // For each transition from this state
        foreach(input input in transTable[state])
        {
          // If the transition is on input inp, add it to the resulting set
          if(input == inp)
          {
            state u = Array.IndexOf(transTable[state], input, i);
            result.Add(u);
          }

          i = i + 1;
        }
      }

      return result;
    }

    /// <summary>
    /// Prints out the NFA.
    /// </summary>
    public void Show()
    {
      Console.WriteLine("This NFA has {0} states: 0 - {1}", size, size - 1);
      Console.WriteLine("The initial state is {0}", initial);
      Console.WriteLine("The final state is {0}\n", final);

      for(state from = 0; from < size; ++from)
      {
        for(state to = 0; to < size; ++to)
        {
          input @in = transTable[from][to];

          if(@in != (char)Constants.None)
          {
            Console.Write("Transition from {0} to {1} on input ", from, to);

            if(@in == (char)Constants.Epsilon)
              Console.Write("Epsilon\n");
            else
              Console.Write("{0}\n", @in);
          }
        }
      }
      Console.Write("\n\n");
    }

    /// <summary>
    ///
    /// </summary>
    /// <param name="tree"></param>
    /// <returns></returns>
    public static NFA TreeToNFA(ParseTree tree)
    {
        switch (tree.type)
        {
            case ParseTree.NodeType.Chr:
                return BuildNFABasic(tree.data.Value);
            case ParseTree.NodeType.Alter:
                return BuildNFAAlter(TreeToNFA(tree.left), TreeToNFA(tree.right));
            case ParseTree.NodeType.Concat:
                return BuildNFAConcat(TreeToNFA(tree.left), TreeToNFA(tree.right));
            case ParseTree.NodeType.Star:
                return BuildNFAStar(TreeToNFA(tree.left));
            case ParseTree.NodeType.Question:
                return BuildNFAAlter(TreeToNFA(tree.left), BuildNFABasic((char)Constants.Epsilon));
            default:
                return null;
        }
    }

    /////////////////////////////////////////////////////////////////
    //
    // NFA building functions
    //
    // Using Thompson Construction, build NFAs from basic inputs or
    // compositions of other NFAs.
    //

    /// <summary>
    /// Builds a basic, single input NFA
    /// </summary>
    /// <param name="in"></param>
    /// <returns></returns>
    public static NFA BuildNFABasic(input @in)
    {
      NFA basic = new NFA(2, 0, 1);

      basic.AddTrans(0, 1, @in);

      return basic;
    }

    /// <summary>
    /// Builds an alternation of nfa1 and nfa2 (nfa1|nfa2)
    /// </summary>
    /// <param name="nfa1"></param>
    /// <param name="nfa2"></param>
    /// <returns></returns>
    public static NFA BuildNFAAlter(NFA nfa1, NFA nfa2)
    {
      // How this is done: the new nfa must contain all the states in
      // nfa1 and nfa2, plus a new initial and final states.
      // First will come the new initial state, then nfa1's states, then
      // nfa2's states, then the new final state

      // make room for the new initial state
      nfa1.ShiftStates(1);

      // make room for nfa1
      nfa2.ShiftStates(nfa1.size);

      // create a new nfa and initialize it with (the shifted) nfa2
      NFA newNFA = new NFA(nfa2);

      // nfa1's states take their places in new_nfa
      newNFA.FillStates(nfa1);

      // Set new initial state and the transitions from it
      newNFA.AddTrans(0, nfa1.initial, (char)Constants.Epsilon);
      newNFA.AddTrans(0, nfa2.initial, (char)Constants.Epsilon);

      newNFA.initial = 0;

      // Make up space for the new final state
      newNFA.AppendEmptyState();

      // Set new final state
      newNFA.final = newNFA.size - 1;

      newNFA.AddTrans(nfa1.final, newNFA.final, (char)Constants.Epsilon);
      newNFA.AddTrans(nfa2.final, newNFA.final, (char)Constants.Epsilon);

      return newNFA;
    }

    /// <summary>
    /// Builds an alternation of nfa1 and nfa2 (nfa1|nfa2)
    /// </summary>
    /// <param name="nfa1"></param>
    /// <param name="nfa2"></param>
    /// <returns></returns>
    public static NFA BuildNFAConcat(NFA nfa1, NFA nfa2)
    {
      // How this is done: First will come nfa1, then nfa2 (its initial state replaced
      // with nfa1's final state)
      nfa2.ShiftStates(nfa1.size - 1);

      // Creates a new NFA and initialize it with (the shifted) nfa2
      NFA newNFA = new NFA(nfa2);

      // nfa1's states take their places in newNFA
      // note: nfa1's final state overwrites nfa2's initial state,
      // thus we get the desired merge automatically (the transition
      // from nfa2's initial state now transits from nfa1's final state)
      newNFA.FillStates(nfa1);

      // Sets the new initial state (the final state stays nfa2's final state,
      // and was already copied)
      newNFA.initial = nfa1.initial;

      return newNFA;
    }

    /// <summary>
    /// Builds a star (kleene closure) of nfa (nfa*)
    /// How this is done: First will come the new initial state, then NFA, then the new final state
    /// </summary>
    /// <param name="nfa"></param>
    /// <returns></returns>
    public static NFA BuildNFAStar(NFA nfa)
    {
      // Makes room for the new initial state
      nfa.ShiftStates(1);

      // Makes room for the new final state
      nfa.AppendEmptyState();

      // Adds new transitions
      nfa.AddTrans(nfa.final, nfa.initial, (char)Constants.Epsilon);
      nfa.AddTrans(0, nfa.initial, (char)Constants.Epsilon);
      nfa.AddTrans(nfa.final, nfa.size - 1, (char)Constants.Epsilon);
      nfa.AddTrans(0, nfa.size - 1, (char)Constants.Epsilon);

      nfa.initial = 0;
      nfa.final = nfa.size - 1;

      return nfa;
    }
  }

}
We pass the parse tree (see last post) to a function responsible for converting the parse tree to an NFA.
private NFA TreeToNFA(ParseTree tree)
{
  switch(tree.type)
  {
    case ParseTree.NodeType.Chr:
      return BuildNFABasic(tree.data.Value);
    case ParseTree.NodeType.Alter:
      return BuildNFAAlter(TreeToNFA(tree.left), TreeToNFA(tree.right));
    case ParseTree.NodeType.Concat:
      return BuildNFAConcat(TreeToNFA(tree.left), TreeToNFA(tree.right));
    case ParseTree.NodeType.Star:
      return BuildNFAStar(TreeToNFA(tree.left));
    case ParseTree.NodeType.Question:
      return BuildNFAAlter(TreeToNFA(tree.left), BuildNFABasic((char)Constants.Epsilon));
    default:
      return null;
  }
}

DFA Factory

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace harjoitustyo
{
    /// <summary>
    /// Creates and returns a DFA 
    /// </summary>
    public static class DFAFactory
    {
        public static DFA BuildDefaultDFA(string alphabet, string states, string transitionFunction, string startingState) {
            // Split Parameters for DFA from given strings.
            List<Transition> alphabetParameter = Utils.CreateTransitionsFromAlphabet(alphabet);
            List<State> statesParameter = Utils.CreateDefaultStatesFromString(states);

            // Split transitionFunction string and create transitions to states.
            var splittingResult = Utils.SplitParameter(transitionFunction);
            for (int i = 0; i < splittingResult.Length; i++)
            {
                var furtherSplitResult = Utils.SplitStateTransitions(splittingResult[i]);

                //Console.WriteLine("From state " + furtherSplitResult[0] + " there is transition " + furtherSplitResult[1] + " to state " + furtherSplitResult[2]);

                foreach (var state in statesParameter)
                {
                    if (state.StateName.Equals(furtherSplitResult[0]))
                    {
                        Transition tempTransition = null;
                        foreach (var trans in alphabetParameter)
                        {
                            if (trans.TransitionName.Equals(furtherSplitResult[1]))
                            {
                                tempTransition = trans;
                            }//if
                        }//foreach

                        State tempState = null;
                        foreach (var stat in statesParameter)
                        {
                            if (stat.StateName.Equals(furtherSplitResult[2]))
                            {
                                tempState = stat;
                            }//if
                        }//foreach

                        state.AddTransition(tempTransition, tempState);
                        Console.WriteLine("Added transition " + tempTransition.TransitionName + " leading to state " + tempState.StateName + " in state " + state.StateName + " transitions.");
                    }//if
                }//foreach

            }//for

            DFA dfa = new DFA(alphabetParameter, statesParameter);
           
            // Assign starting state
            foreach(State state in statesParameter){
                if(state.StateName.Equals(startingState)){
                    dfa.CurrentState = state;
                }//if
            }//foreach

            return dfa;
        }


        //public static DFA BuildDefaultDFA(string alphabet, string states, string transitionTable, string startingStateName)
        //{
            /*
            //Check that the given parameters are valid.
            if (alphabet == null || states == null || transitionTable == null || startingStateName == null)
            {
                Console.WriteLine("ERROR: given parameters to build default dfa are invalid");
                return null;
            }
            //Resolve alphabet from given string.
            string[] transitionNames = Utils.ResolveAlphabetFromString(alphabet);
            //Check for duplicate alphabet (transition names).
            Utils.CheckForDuplicates(transitionNames);
            //Create transition parameter for DFA from alphabet.
            List<Transition> transitionsParameter = new List<Transition>();
            for (int i = 0; i < transitionNames.Length; i++)
            {
                transitionsParameter.Add(new Transition(transitionNames[i], i));
            }
            //Resolve states' names
            string[] stateNames = Utils.ResolveStatesFromString(states);
            //Check for duplicate state names.
            Utils.CheckForDuplicates(stateNames);
            //Create state parameter for DFA from list of state names.
            List<State> statesParameter = new List<State>();
            for (int i = 0; i < stateNames.Length; i++)
            {
                statesParameter.Add(new DefaultState(stateNames[i], i));
            }
            //Create transitions.
            string[] transitionSets = Utils.ResolveTransitionSetsFromString(transitionTable);
            //Further split transition sets
            for (int i = 0; i < transitionSets.Length; i++)
            {
                string[] transitionAndState = Utils.ResolveTransitionFromTransitionSet(transitionSets[i]);
                State stateToAddTransition = null;
                foreach (State state in statesParameter)
                {
                    if (transitionAndState[0].Equals(state.StateName))
                    {
                        stateToAddTransition = state;
                    }//if
                }//foreach
                Transition transitionToAdd = null;
                foreach (Transition transition in transitionsParameter)
                {
                    if (transitionAndState[1].Equals(transition.TransitionName))
                    {
                        transitionToAdd = transition;
                    }//if
                }//foreach
                State stateToTransit = null;
                foreach (State state in statesParameter)
                {
                    if (transitionAndState[2].Equals(state.StateName))
                    {
                        stateToTransit = state;
                    }//if
                }//foreach
                stateToAddTransition.AddTransition(transitionToAdd, stateToTransit);
            }//for
            //Define starting state
            State startingState = null;
            foreach (State state in statesParameter)
            {
                if (state.StateName.Equals(startingStateName))
                {
                    startingState = state;
                }//if
            }//foreach
            //Build default DFA
            DFA defaultDFA = new DFA(transitionsParameter, statesParameter);
            defaultDFA.CurrentState = startingState;
           
            return defaultDFA;
            */ 
        //}//BuildDefaultDFA

    }//DFAFactory
}

harjoitustyo Program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace harjoitustyo
{
    class Program
    {
        // Parameters for default dfa.
        private static string _alphabet = "a,b,c"; //names of the transitions.
        private static string _states = "A,B,C"; //names of the states.
        private static string _transitionFunction = "{A-a-B};{B-a-C};{B-b-A};{C-c-A}"; //Transition functions: for example {A,a,B} means State A has a transition a to state B.
        private static string _startingState = "A"; //stating state.

        private static DFA dfa;

        static void Main(string[] args)
        {
            Console.WriteLine("Hello and welcome to the DFA generator!");
            PrintInstructions();

            HandleInput();

            //dfa = DFAFactory.BuildDefaultDFA(_alphabet, _states, _transitionFunction, _startingState);
            //dfa.PerformTransition("a");

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
           
        }//Main

        public static void PrintInstructions() {
            Console.WriteLine("Press Esc to exit, u to create a user defined default dfa or g to build pre-defined default dfa.");
        }//PrintInstructions

        public static void PrintDFAInstructions() {
            Console.WriteLine("Press t to transit to state");
        }//PrintDFAInstructions

        public static void HandleInput() {
            if (Console.ReadKey(true).Key == ConsoleKey.Escape) {
                //Don't do anything   
            }
           
            if(Console.ReadKey(true).Key == ConsoleKey.G){
                dfa = DFAFactory.BuildDefaultDFA(_alphabet, _states, _transitionFunction, _startingState);
                OperateDFA();
            }
           
            if (Console.ReadKey(true).Key == ConsoleKey.U) {
                //Query user for DFA parameters
                Console.WriteLine("Write DFA alphabet in form of symbol,symbol,symbol without spaces between symbols - for example a,b,c");
                string alphabet = Console.ReadLine();
                Console.WriteLine("Write states in form of State,State,State without spaces between state names - for example A,B,C");
                string states = Console.ReadLine();
                Console.WriteLine("Write transition function in form of {starting state-transition-goal state};{starting state-transition-goal state} - for example {A-a-B}");
                string transitionFunction = Console.ReadLine();
                Console.WriteLine("Write the name of the starting state");
                string startingState = Console.ReadLine();

                //Build default dfa
                dfa = DFAFactory.BuildDefaultDFA(alphabet, states,transitionFunction,startingState);
                OperateDFA();
            }
        }//HandleInput

        public static void OperateDFA(){
            bool isOperating = true;
            while(isOperating){
                //Print available states
                Console.WriteLine("DFA has following states: ");
                foreach(State state in dfa.States){
                    Console.WriteLine(state.StateName);
                }
               
                //Print available transitions
                Console.WriteLine("DFA has following transitions: ");
                foreach(Transition transition in dfa.Alphabet){
                    Console.WriteLine(transition.TransitionName);
                }

                //Ask for user input
                Console.WriteLine("Enter transition's name");
                dfa.PerformTransition(Console.ReadLine());
                Console.WriteLine("Current state is: " + dfa.CurrentState.StateName);
                Console.WriteLine("Press Esc to quit or enter transition's name");
                if (Console.ReadKey(true).Key == ConsoleKey.Escape)
                {
                    isOperating = false; 
                }//if
            }//while
        }//OperateDFA

       
    }
}

States

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace harjoitustyo
{
    public abstract class State
    {
        // Class variables
        protected string _stateName;
        public string StateName { get { return _stateName; } }

        protected int _stateID;
        public int StateID { get { return _stateID; } }

        protected Dictionary<int, int> stateTransitions = new Dictionary<int, int>();
        public Dictionary<int, int> StateTransitions { get { return stateTransitions; } }

        public State(string stateName, int stateID)
        {
            this._stateName = stateName;
            this._stateID = stateID;
        }

        // Class Behaviour
        public void AddTransition(Transition transition, State state)
        {
           
            // Check that the given parameters are valid.
            if (transition==null || state == null)
            {
                Console.WriteLine("ERROR in State AddTransition: given parameters are not valid!");
                return;
            }//if
           
            // Check for duplicates - a deterministic finite automata cannot have a transition with a same id multiple times.
            if (stateTransitions.ContainsKey(transition.TransitionID))
            {
                Console.WriteLine("ERROR in State AddTransition: transition with id of " + transition.TransitionID + " is already included in state's transitions!");
                return;
            }

            // Add transition
            stateTransitions.Add(transition.TransitionID, state._stateID);
        }//AddTransition

        public void RemoveTransition(Transition transition)
        {
            //Check whether given parameter is valid.
            if (isTransitionValid(transition)) { }

            //Check whether given transitions exists and remove transition from state
            if (stateTransitions.ContainsKey(transition.TransitionID))
            {
                stateTransitions.Remove(transition.TransitionID);
                Console.WriteLine("Removed transition with the id of " + transition.TransitionID.ToString() +
                    " and name of " + transition.TransitionName + " from state " + _stateName + " with id " + _stateID);
                return;
            }//if

            //If transition wasn't found, write error
            Console.WriteLine("ERROR in RemoveTransition: transition was not found in state's transitions");

        }// RemoveTransition

        public int GetStateToTransit(Transition transition)
        {
            // Check whether given parameter is valid
            if (isTransitionValid(transition)) { }

            // Check whether state has transition and return state id the transition is pointing to
            if (stateTransitions.ContainsKey(transition.TransitionID))
            {
                return stateTransitions[transition.TransitionID];
            }

            // if transition wasn't found, return -1, which is default id for non-existing state. In other words transition points to state itself.
            return -1;

        }//GetStateToTransit

        public virtual void stateBehaviour()
        {
            Console.WriteLine("This is default state behaviour!");
        }

        public virtual void onEnteringState()
        {
            Console.WriteLine("Activing state with id of " + _stateID.ToString() + " and name of " + _stateName);
        }

        public virtual void onLeavingState()
        {
            Console.WriteLine("Deactiving state with id of " + _stateID.ToString() + " and name of " + _stateName);
        }

        private bool isTransitionValid(Transition transition)
        {
            if (transition == null)
            {
                Console.WriteLine("ERROR: given transition parameter is null");
                return false;
            }//if

            return true;
        }//isTransitionValid

    }//State
}

Tests

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace harjoitustyo
{
    class Tests
    {
        // Test variables;
        private string _alphabet = "a,b,c"; //names of the transitions
        private string _states = "A,B,C"; //names of the states
        private string _transitionFunction = "{A-a-B};{B-a-C};{B-b-A};{C-c-A}"; //Transition functions: for example {A,a,B} means State A has a transition a to state B.
        private string _startingState = "A";
       

        /// <summary>
        /// Drives a set of tests.
        /// </summary>
        public void DriveTests() {
            //Utils component tests
            //TestParameterSplitting();
            //TestStateAndTransitionsSplitting();
            //TestParameterDuplicates();
           
            //Transition component tests
            //CreateTestTransitions();
            //CreateTestTransitionsFromString();
           
            //State component tests
            //CreateTestStates();
            //CreateTestStatesFromString();
            //TryAddingTransitionsToState();
            //TryRemovingTransitionsFromState();
            //TryGettingStateToTransit();

            //DFA component tests
            //CreateDFA();
            //TryAddingStatesToDFA();
            //TryRemovingStatesFromDFA();
            //TryChangingDFAState();
        }

        #region Utils component tests
        // TEST:Success.
        private void TestParameterSplitting() {
            // Test alphabet string
            string[] alphabetResult = Utils.SplitParameter(_alphabet);
            Console.WriteLine("Alphabet: ");
            for (int i = 0; i < alphabetResult.Length; i++) {
                Console.WriteLine(alphabetResult[i]);
            }//for

            //Test states string
            var statesResult = Utils.SplitParameter(_states);
            Console.WriteLine("States: ");
            for (int i = 0; i < statesResult.Length; i++)
            {
                Console.WriteLine(statesResult[i]);
            }//for

            //Test transitionFunction string
            var transitionFunctionResult = Utils.SplitParameter(_transitionFunction);
            Console.WriteLine("Transition sets: ");
            for (int i = 0; i < transitionFunctionResult.Length; i++)
            {
                Console.WriteLine(transitionFunctionResult[i]);
            }
        }

        //TEST:Success.
        private void TestStateAndTransitionsSplitting() {
            var splittingResult = Utils.SplitParameter(_transitionFunction);
            for (int i = 0; i < splittingResult.Length; i++) {
                var furtherSplitResult = Utils.SplitStateTransitions(splittingResult[i]);

                Console.WriteLine("From state " + furtherSplitResult[0] + " there is transition " + furtherSplitResult[1] + " to state " + furtherSplitResult[2]);
               
            }//for
        }//TestStateAndTransitionsSplitting

        //TEST:Success.
        private void TestParameterDuplicates() {
            var duplicate = "a,a,b,c";
            var resultAfterSplit = Utils.SplitParameter(duplicate);
            var isDuplicate = Utils.CheckForDuplicates(resultAfterSplit);

        }
        #endregion

        #region Transition Component tests
        private Transition _transitionA;
        private Transition _transitionB;

        //TEST: Success
        private void CreateTestTransitions() {
            _transitionA = new Transition("a", 0);
            _transitionB = new Transition("b", 0);

            Console.WriteLine("Transition name: " + _transitionA.TransitionName + " transition ID: " + _transitionA.TransitionID.ToString());
            Console.WriteLine("Transition name: " + _transitionB.TransitionName + " transition ID: " + _transitionB.TransitionID.ToString());
        }//CreateTestTransitions

        //TEST: Success.
        private void CreateTestTransitionsFromString() {
            var listOfTestTransitions = Utils.CreateTransitionsFromAlphabet(_alphabet);
            if(listOfTestTransitions != null){
                Console.WriteLine("Number of transitions: " + listOfTestTransitions.Count.ToString());
            }//if
           
        }
        #endregion

        #region State Component Tests
        private State _testStateA;
        private State _testStateB;

        //TEST: Success.
        private void CreateTestStates() {
            _testStateA = new DefaultState("A", 0);
            _testStateB = new DefaultState("B", 0);

            Console.WriteLine("State name is: " + _testStateA.StateName + " and ID: " + _testStateA.StateID.ToString());
            Console.WriteLine("State name is: " + _testStateB.StateName + " and ID: " + _testStateB.StateID.ToString());
        }//CreateTestStates

        //TEST: Success
        private void CreateTestStatesFromString() {
            var listOfTestStates = Utils.CreateDefaultStatesFromString(_states);
            if (listOfTestStates != null)
            {
                Console.WriteLine("Number of states: " + listOfTestStates.Count.ToString());
            }//if
        }

        private List<Transition> _testTransitionsFromString;
        private List<State> _testStatesFromString;

        //TEST: Success.
        private void TryAddingTransitionsToState() {
            _testTransitionsFromString = Utils.CreateTransitionsFromAlphabet(_alphabet); //List of transitions
            _testStatesFromString = Utils.CreateDefaultStatesFromString(_states); //List of states

            var splittingResult = Utils.SplitParameter(_transitionFunction);
            for (int i = 0; i < splittingResult.Length; i++)
            {
                var furtherSplitResult = Utils.SplitStateTransitions(splittingResult[i]);

                Console.WriteLine("From state " + furtherSplitResult[0] + " there is transition " + furtherSplitResult[1] + " to state " + furtherSplitResult[2]);

                foreach(var state in _testStatesFromString){
                    if(state.StateName.Equals(furtherSplitResult[0])){
                       
                        Transition tempTransition = null;
                        foreach (var trans in _testTransitionsFromString) {
                            if(trans.TransitionName.Equals(furtherSplitResult[1])){
                                tempTransition = trans;
                            }//if
                        }//foreach
                       
                        State tempState = null;
                        foreach (var stat in _testStatesFromString) {
                            if(stat.StateName.Equals(furtherSplitResult[2])){
                                tempState = stat;
                            }//if
                        }//foreach

                        state.AddTransition(tempTransition, tempState);
                        Console.WriteLine("Added transition " + tempTransition.TransitionName + " leading to state " + tempState.StateName + " in state " + state.StateName + " transitions.");
                    }//if
                }//foreach

            }//for
        }//TryAddingTransitionsToState

        //TEST: Success.
        private void TryRemovingTransitionsFromState() {
            TryAddingTransitionsToState();

            State state = _testStatesFromString[1];
            Console.WriteLine("State name: " + state.StateName);

            var testStatesTransitions = state.StateTransitions;

            //print state's transitions
            foreach (var entry in testStatesTransitions) {
                Console.WriteLine("Transition ID: " + entry.Key.ToString() + " State ID: " + entry.Value.ToString());
            }

            state.RemoveTransition(_testTransitionsFromString[1]);

            //print state's transitions
            foreach (var entry in testStatesTransitions)
            {
                Console.WriteLine("Transition ID: " + entry.Key.ToString() + " State ID: " + entry.Value.ToString());
            }
        }

        //TEST: success
        private void TryGettingStateToTransit() {
            TryAddingTransitionsToState();

            State state = _testStatesFromString[1];
            Console.WriteLine("State name: " + state.StateName);

            var testStatesTransitions = state.StateTransitions;

            //print state's transitions
            foreach (var entry in testStatesTransitions)
            {
                Console.WriteLine("Transition ID: " + entry.Key.ToString() + " State ID: " + entry.Value.ToString());
            }

            Console.WriteLine("Transition name: " + _testTransitionsFromString[1].TransitionName + " transition ID: " + _testTransitionsFromString[1].TransitionID.ToString());

            int stateIdToTransit = state.GetStateToTransit(_testTransitionsFromString[1]); //should return 0.
            Console.WriteLine("State to transit has id of: " + stateIdToTransit.ToString());

            stateIdToTransit = state.GetStateToTransit(_testTransitionsFromString[2]); //should return -1.
            Console.WriteLine("State to transit has id of: " + stateIdToTransit.ToString());
        }

        /*
        private void StringSplitTest() {
            string alphabet = "a,b,c";
            string states = "A,B,C";
            string transitions = "{A,a,B};{B,a,C};{B,b,A};{C,c,A}";
            string startingState = "A";
            string testString ="{A,a,B};{B,a,C};{B,b,A};{C,c,A}"; //Transition string
            //string[] sets = testString.Split(';');
            string[] delimiters = { "{","}",";"};
            string[] sets = testString.Split(delimiters,StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < sets.Length; i++) {
                Console.WriteLine(sets[i]);
            }
            Console.WriteLine(sets.Length.ToString());
            //resolve state and transitions
            foreach(string sub in sets){
                //further split
                string[] temp = sub.Split(',');
                Console.WriteLine("From state " + temp[0] + " there is a transtion " + temp[1] + " to state " + temp[2]);
            }
        }
         */
        #endregion

        #region DFA Component tests
        private DFA _dfa;

        private void CreateDFA() {
            _testTransitionsFromString = Utils.CreateTransitionsFromAlphabet(_alphabet);
            _testStatesFromString = Utils.CreateDefaultStatesFromString(_states);
            TryAddingTransitionsToState();
            _dfa = new DFA(_testTransitionsFromString, _testStatesFromString);

            Console.WriteLine("Created DFA!");
        }//CreateDFA

        private List<State> _listOfStatesInDFA;

        //TEST:Success.
        private void TryAddingStatesToDFA() {
            CreateDFA();
           
            _listOfStatesInDFA = _dfa.States;
           
            Console.WriteLine("DFA has following states: ");
            foreach (State state in _listOfStatesInDFA) {
                Console.WriteLine(state.StateID.ToString() + " " + state.StateName);
            }//foreach

            _dfa.AddState(new DefaultState("A", 0)); //should fail because of same id and name.
            _dfa.AddState(new DefaultState("D", 3)); //should succeed.
            _dfa.AddState(new DefaultState("A", 4)); //should fail because of same name.

            Console.WriteLine("DFA has following states after adding: ");
            foreach (State state in _listOfStatesInDFA)
            {
                Console.WriteLine(state.StateID.ToString() + " " + state.StateName);
            }//foreach
           
            Console.WriteLine("Done adding states to dfa");
        }

        //TEST: Success.
        private void TryRemovingStatesFromDFA() {
            CreateDFA();
            TryAddingStatesToDFA();

            _dfa.CurrentState = _listOfStatesInDFA[0];

            foreach (State state in _listOfStatesInDFA)
            {
                Console.WriteLine(state.StateID.ToString() + " " + state.StateName);
            }//foreach

            _dfa.RemoveState(_listOfStatesInDFA[2]); //should succeed.

            foreach (State state in _listOfStatesInDFA)
            {
                Console.WriteLine(state.StateID.ToString() + " " + state.StateName);
            }//foreach

            State failTestState = new DefaultState("B", 2); //should fail.

            _dfa.RemoveState(failTestState);

            Console.WriteLine("Done removing states from DFA");
        }

        //TEST: Succes.
        private void TryChangingDFAState() {
            //Create DFA with states and transitions
            CreateDFA();
            TryAddingStatesToDFA();

            //assign one of the states active
            _dfa.CurrentState = _listOfStatesInDFA[0];

            Console.WriteLine("Currently active state: " + _dfa.CurrentState.StateName);

            //perform transition
            _dfa.PerformTransition(_testTransitionsFromString[0]); //should succeed.
           
            Console.WriteLine("Currently active state: " + _dfa.CurrentState.StateName);

            _dfa.PerformTransition(_testTransitionsFromString[2]); //should fail
            Console.WriteLine("Currently active state: " + _dfa.CurrentState.StateName);
        }
        #endregion

    }//Tests
   
}