/*
* Created by: Leslie Sanford
*
* Contact: jabberdabber@hotmail.com
*
* Last modified: 09/10/2005
*/
using System;
using System.CodeDom;
using System.Collections;
using System.Data;
namespace StateMachineToolkit
{
/// <summary>
/// Builds the methods that make up the state machine.
/// </summary>
internal class MethodBuilder
{
#region MethodBuilder Members
#region Fields
// The state table.
private DataTable stateTable;
// The guard table.
private DataTable guardTable;
// The action table.
private DataTable actionTable;
// Builds initializing methods.
private InitializeMethodBuilder initializeMethodBuilder;
// The collection of built methods.
private ArrayList methods = new ArrayList();
#endregion
#region Construction
/// <summary>
/// Initializes a new instance of the MethodBuilder class with all
/// of the tables necessary to build the state machine methods.
/// </summary>
/// <param name="stateTable">
/// The state table.
/// </param>
/// <param name="eventTable">
/// The event table.
/// </param>
/// <param name="guardTable">
/// The guard table.
/// </param>
/// <param name="actionTable">
/// The action table.
/// </param>
/// <param name="stateTransitionTable">
/// The state transition table.
/// </param>
/// <param name="relationshipTable">
/// The relationship table.
/// </param>
/// <param name="initialStateTable">
/// The initial state table.
/// </param>
public MethodBuilder(DataTable stateTable, DataTable eventTable,
DataTable guardTable, DataTable actionTable,
DataTable stateTransitionTable, DataTable relationshipTable,
DataTable initialStateTable)
{
this.stateTable = stateTable;
this.guardTable = guardTable;
this.actionTable = actionTable;
initializeMethodBuilder = new InitializeMethodBuilder(stateTable, eventTable,
guardTable, actionTable, stateTransitionTable, relationshipTable,
initialStateTable);
}
#endregion
#region Methods
/// <summary>
/// Builds the methods for the state machine.
/// </summary>
public void Build()
{
methods = new ArrayList();
initializeMethodBuilder.Build();
methods.AddRange(initializeMethodBuilder.Result);
//
// Build entry and exit actions.
//
BuildMethods(stateTable, "Name", "Entry", MemberAttributes.Family,
null, typeof(void));
BuildMethods(stateTable, "Name", "Exit", MemberAttributes.Family,
null, typeof(void));
CodeParameterDeclarationExpression args =
new CodeParameterDeclarationExpression(typeof(object[]), "args");
// Build guard methods.
BuildMethods(guardTable, "Name", string.Empty,
MemberAttributes.Family | MemberAttributes.Abstract, args,
typeof(bool));
// Build action methods.
BuildMethods(actionTable, "Name", string.Empty,
MemberAttributes.Family | MemberAttributes.Abstract, args,
typeof(void));
}
// Builds methods.
private void BuildMethods(DataTable table, string columnName,
string methodPrefix, MemberAttributes attributes,
CodeParameterDeclarationExpression args, Type returnType)
{
CodeMemberMethod method;
foreach(DataRow row in table.Rows)
{
method = new CodeMemberMethod();
method.Name = methodPrefix + row[columnName].ToString();
method.Attributes = attributes;
method.ReturnType = new CodeTypeReference(returnType);
if(args != null)
{
method.Parameters.Add(args);
}
methods.Add(method);
}
}
#endregion
#region Properties
/// <summary>
/// Gets the collection of built methods.
/// </summary>
public ICollection Result
{
get
{
return methods;
}
}
#endregion
#endregion
}
}