private static void Main() { var Q = new List <string> { "q0", "q1", "q2", "q3" }; var Sigma = new List <char> { '0', '1' }; var Delta = new List <Transition> { new Transition("q0", '0', "q0"), new Transition("q0", '1', "q0"), new Transition("q0", '1', "q1"), new Transition("q1", '0', "q2"), new Transition("q1", '1', "q2"), new Transition("q2", '0', "q3"), new Transition("q2", '1', "q3"), }; var Q0 = new List <string> { "q0" }; var F = new List <string> { "q3" }; var ndfsm = new NDFSM(Q, Sigma, Delta, Q0, F); var dfsm = PowersetConstruction.PowersetConstruction.Convert(ndfsm); }
public static DFSM CalculateOverloadStates(IEnumerable <Function> group) { var functionGroup = group.ToList(); // Create a set of unique parameter types. var uniqueTypes = functionGroup.SelectMany(method => method.Parameters) .Select(p => p.Type).Distinct().ToList(); // Consider the alphabet as sequential ordered numbers, one per type. var Sigma = Enumerable.Range(0, uniqueTypes.Count).Select(i => (char)i).ToArray(); var Q = new List <string> { "S" }; var overloadStates = Enumerable.Range(0, functionGroup.Count).Select(i => $"F{i}") .ToArray(); Q.AddRange(overloadStates); var Delta = new List <Transition>(); // Setup states and transitions. for (var methodIndex = 0; methodIndex < functionGroup.Count; methodIndex++) { var method = functionGroup[methodIndex]; var curState = "S"; for (var paramIndex = 0; paramIndex < method.Parameters.Count; paramIndex++) { var param = method.Parameters[paramIndex]; var typeIndex = uniqueTypes.FindIndex(p => p.Equals(param.Type)); var isLastTransition = paramIndex == method.Parameters.Count - 1; var nextState = isLastTransition ? $"F{methodIndex}" : $"{methodIndex}_{paramIndex}"; if (!isLastTransition) { Q.Add(nextState); } Delta.Add(new Transition(curState, (char)typeIndex, nextState)); curState = nextState; } } var Q0 = new List <string> { "S" }; var F = overloadStates; var NDFSM = new NDFSM(Q, Sigma, Delta, Q0, F); var DFSM = Minimize.PowersetConstruction(NDFSM); // Add the zero-parameters overload manually if one exists since it got optimized out. var zeroParamsOverload = functionGroup.SingleOrDefault(f => f.Parameters.Count == 0); if (zeroParamsOverload != null) { var index = functionGroup.FindIndex(f => f == zeroParamsOverload); DFSM.F.Insert(index, $"F{index}"); } #if OPTIMIZE_STATES DFSM = Minimize.MinimizeDFSM(DFSM); #endif // The construction step above can result in unordered final states, so re-order them to // make the following code generation steps easier. DFSM.F = DFSM.F.OrderBy(f => f).ToList(); return(DFSM); }