internal FA Synthesize(CCharRangeSet set) { FAGraphQueryInfo FAInfo; m_currentFA = new FA(); m_ThompsonInfo = new ThompsonInfo(m_currentFA, m_ThompsonInfoKey); FAInfo = new FAGraphQueryInfo(m_currentFA, FA.m_FAINFOKEY); //2.Create nodes initial-final CGraphNode init = m_currentFA.CreateGraphNode <CGraphNode>(); CGraphNode final = m_currentFA.CreateGraphNode <CGraphNode>(); m_ThompsonInfo.InitNodeInfo(init, new ThompsonNodeFAInfo()); m_ThompsonInfo.InitNodeInfo(final, new ThompsonNodeFAInfo()); m_currentFA.M_Initial = init; m_currentFA.SetFinalState(final); m_currentFA.M_Alphabet.AddSet(set); //3.Draw the edge including the character CGraphEdge newEdge = m_currentFA.AddGraphEdge <CGraphEdge, CGraphNode>(init, final, GraphType.GT_DIRECTED); FAInfo.Info(newEdge).M_TransitionCharSet = set; return(m_currentFA); }
internal FA SynthesizeOneOrNone(FA synth) { FA tempFA = new FA(); //2.Merge graph CGraph.CMergeGraphOperation merged = tempFA.Merge(synth, CGraph.CMergeGraphOperation.MergeOptions.MO_DEFAULT); merged.MergeGraphInfo(synth, GraphElementType.ET_EDGE, FA.m_FAINFOKEY); merged.MergeGraphInfo(synth, GraphElementType.ET_GRAPH, FA.m_FAINFOKEY); merged.MergeGraphInfo(synth, GraphElementType.ET_NODE, FA.m_FAINFOKEY); CGraphNode il = tempFA.CreateGraphNode <CGraphNode>(); tempFA.AddGraphEdge <CGraphEdge, CGraphNode>(il, merged.GetMirrorNode(synth.M_Initial), GraphType.GT_DIRECTED); CGraphNode fl = tempFA.CreateGraphNode <CGraphNode>(); tempFA.AddGraphEdge <CGraphEdge, CGraphNode>(merged.GetMirrorNode(synth.GetFinalStates()[0]), fl, GraphType.GT_DIRECTED); //4.Create the initial and the final node tempFA.M_Initial = il; tempFA.SetFinalState(fl); tempFA.UpdateAlphabet(); tempFA.AddGraphEdge <CGraphEdge, CGraphNode>(il, fl, GraphType.GT_DIRECTED); //7.Return result return(tempFA); }
private FA CreateNewFA(FA synth) { CGraphNode oldEntryNode, oldExitNode; FA m_currentFA = new FA(); m_ThompsonInfo = new ThompsonInfo(m_currentFA, m_ThompsonInfoKey); m_ThompsonInfo.InitFAInfo(new ThompsonFAInfo()); //2.Merge graph m_mergeOperation = m_currentFA.Merge(synth, CGraph.CMergeGraphOperation.MergeOptions.MO_DEFAULT); m_mergeOperation.MergeGraphInfo(synth, GraphElementType.ET_EDGE, FA.m_FAINFOKEY); m_mergeOperation.MergeGraphInfo(synth, GraphElementType.ET_GRAPH, FA.m_FAINFOKEY); m_mergeOperation.MergeGraphInfo(synth, GraphElementType.ET_NODE, FA.m_FAINFOKEY); m_mergeOperation.MergeGraphInfo(synth, GraphElementType.ET_EDGE, m_ThompsonInfoKey); m_mergeOperation.MergeGraphInfo(synth, GraphElementType.ET_GRAPH, m_ThompsonInfoKey); m_mergeOperation.MergeGraphInfo(synth, GraphElementType.ET_NODE, m_ThompsonInfoKey); // Create boundary nodes m_newFASource = m_currentFA.CreateGraphNode <CGraphNode>(); m_ThompsonInfo.InitNodeInfo(m_newFASource, new ThompsonNodeFAInfo()); m_currentFA.AddGraphEdge <CGraphEdge, CGraphNode>(m_newFASource, m_mergeOperation.GetMirrorNode(synth.M_Initial), GraphType.GT_DIRECTED); m_newFATarget = m_currentFA.CreateGraphNode <CGraphNode>(); m_ThompsonInfo.InitNodeInfo(m_newFATarget, new ThompsonNodeFAInfo()); m_currentFA.AddGraphEdge <CGraphEdge, CGraphNode>(m_mergeOperation.GetMirrorNode(synth.GetFinalStates()[0]), m_newFATarget, GraphType.GT_DIRECTED); //4.Create the initial and the final node oldEntryNode = m_mergeOperation.GetMirrorNode(synth.M_Initial); oldExitNode = m_mergeOperation.GetMirrorNode(synth.GetFinalStates()[0]); m_currentFA.M_Initial = m_newFASource; m_currentFA.SetFinalState(m_newFATarget); m_currentFA.UpdateAlphabet(); // Update closure information from operand closure FAInfo currentFAInfo = UpdateClosureInformation(synth, m_currentFA, m_mergeOperation); // Update closure information from current closure m_currentFAloop = new FALoop(); m_currentFAloop.MEntryNode = oldEntryNode; m_currentFAloop.MExitNode = oldExitNode; m_currentFAloop.MLoopSerial = m_currentClosureSerial; CIt_GraphNodes it = new CIt_GraphNodes(m_currentFA); for (it.Begin(); !it.End(); it.Next()) { if (it.M_CurrentItem != m_currentFA.M_Initial && it.M_CurrentItem != m_currentFA.GetFinalStates()[0]) { m_currentFAloop.MParticipatingNodes.Add(it.M_CurrentItem); } } // Add new closure to the current FA currentFAInfo.AddFALoop(m_currentFAloop); return(m_currentFA); }
internal FA Sythesize(FA l, FA r, CGraph.CMergeGraphOperation.MergeOptions options) { //1.Create FA m_currentFA = new FA(); m_ThompsonInfo = new ThompsonInfo(m_currentFA, m_ThompsonInfoKey); //2.Merge left graph CGraph.CMergeGraphOperation lmerge = m_currentFA.Merge(l, options); //Console.WriteLine(l.ToString()); lmerge.MergeGraphInfo(l, GraphElementType.ET_EDGE, FA.m_FAINFOKEY); lmerge.MergeGraphInfo(l, GraphElementType.ET_GRAPH, FA.m_FAINFOKEY); lmerge.MergeGraphInfo(l, GraphElementType.ET_NODE, FA.m_FAINFOKEY); lmerge.MergeGraphInfo(l, GraphElementType.ET_EDGE, m_ThompsonInfoKey); lmerge.MergeGraphInfo(l, GraphElementType.ET_GRAPH, m_ThompsonInfoKey); lmerge.MergeGraphInfo(l, GraphElementType.ET_NODE, m_ThompsonInfoKey); CGraphNode il = lmerge.GetMirrorNode(l.M_Initial); CGraphNode fl = lmerge.GetMirrorNode(l.GetFinalStates()[0]); //3.Merge right graph CGraph.CMergeGraphOperation rmerge = m_currentFA.Merge(r, options); rmerge.MergeGraphInfo(r, GraphElementType.ET_EDGE, FA.m_FAINFOKEY); rmerge.MergeGraphInfo(r, GraphElementType.ET_GRAPH, FA.m_FAINFOKEY); rmerge.MergeGraphInfo(r, GraphElementType.ET_NODE, FA.m_FAINFOKEY); rmerge.MergeGraphInfo(r, GraphElementType.ET_EDGE, m_ThompsonInfoKey); rmerge.MergeGraphInfo(r, GraphElementType.ET_GRAPH, m_ThompsonInfoKey); rmerge.MergeGraphInfo(r, GraphElementType.ET_NODE, m_ThompsonInfoKey); CGraphNode ir = rmerge.GetMirrorNode(r.M_Initial); CGraphNode fr = rmerge.GetMirrorNode(r.GetFinalStates()[0]); //4.Create the initial and the final node CGraphNode FAinit = m_currentFA.CreateGraphNode <CGraphNode>(); CGraphNode FAfinal = m_currentFA.CreateGraphNode <CGraphNode>(); m_ThompsonInfo.InitNodeInfo(FAinit, new ThompsonNodeFAInfo()); m_ThompsonInfo.InitNodeInfo(FAfinal, new ThompsonNodeFAInfo()); m_currentFA.M_Initial = FAinit; m_currentFA.SetFinalState(FAfinal); m_currentFA.UpdateAlphabet(); m_currentFA.AddGraphEdge <CGraphEdge, CGraphNode>(FAinit, il, GraphType.GT_DIRECTED); m_currentFA.AddGraphEdge <CGraphEdge, CGraphNode>(FAinit, ir, GraphType.GT_DIRECTED); m_currentFA.AddGraphEdge <CGraphEdge, CGraphNode>(fr, FAfinal, GraphType.GT_DIRECTED); m_currentFA.AddGraphEdge <CGraphEdge, CGraphNode>(fl, FAfinal, GraphType.GT_DIRECTED); // Update closure information from operand closure // Update closure information from operand closure UpdateClosureInformation(l, m_currentFA, lmerge); UpdateClosureInformation(r, m_currentFA, rmerge); //7.Return result return(m_currentFA); }
internal FA Sythesize(FA l, FA r, CGraph.CMergeGraphOperation.MergeOptions options) { //1.Create FA FA templateFA = new FA(); //2.Merge left graph CGraph.CMergeGraphOperation lmerge = templateFA.Merge(l, options); //Console.WriteLine(l.ToString()); lmerge.MergeGraphInfo(l, GraphElementType.ET_EDGE, FA.m_FAINFOKEY); lmerge.MergeGraphInfo(l, GraphElementType.ET_GRAPH, FA.m_FAINFOKEY); lmerge.MergeGraphInfo(l, GraphElementType.ET_NODE, FA.m_FAINFOKEY); CGraphNode il = lmerge.GetMirrorNode(l.M_Initial); CGraphNode fl = lmerge.GetMirrorNode(l.GetFinalStates()[0]); //3.Merge right graph CGraph.CMergeGraphOperation rmerge = templateFA.Merge(r, options); rmerge.MergeGraphInfo(r, GraphElementType.ET_EDGE, FA.m_FAINFOKEY); rmerge.MergeGraphInfo(r, GraphElementType.ET_GRAPH, FA.m_FAINFOKEY); rmerge.MergeGraphInfo(r, GraphElementType.ET_NODE, FA.m_FAINFOKEY); CGraphNode ir = rmerge.GetMirrorNode(r.M_Initial); CGraphNode fr = rmerge.GetMirrorNode(r.GetFinalStates()[0]); //4.Create the initial and the final node CGraphNode FAinit = templateFA.CreateGraphNode <CGraphNode>(); CGraphNode FAfinal = templateFA.CreateGraphNode <CGraphNode>(); templateFA.M_Initial = FAinit; templateFA.SetFinalState(FAfinal); templateFA.UpdateAlphabet(); templateFA.AddGraphEdge <CGraphEdge, CGraphNode>(FAinit, il, GraphType.GT_DIRECTED); templateFA.AddGraphEdge <CGraphEdge, CGraphNode>(FAinit, ir, GraphType.GT_DIRECTED); templateFA.AddGraphEdge <CGraphEdge, CGraphNode>(fr, FAfinal, GraphType.GT_DIRECTED); templateFA.AddGraphEdge <CGraphEdge, CGraphNode>(fl, FAfinal, GraphType.GT_DIRECTED); //7.Return result return(templateFA); }
/// <summary> /// Creates a unique dfa node for a given configuration /// The method assures that the configuration-dfa node mapping /// is unique /// </summary> /// <param name="q">The q.</param> /// <returns></returns> public CGraphNode CreateDFANode(HashSet <CGraphNode> q) { CGraphNode DFAnode = null; HashSet <string> prefixes = new HashSet <string>(); string prefix = ""; //check if q configuration corresponds to a DFA state DFAnode = GetDFANode(q); //if not create a new DFA node if (DFAnode == null) { DFAnode = m_DFA.CreateGraphNode <CGraphNode>(); foreach (CGraphNode node in q) { if (!prefixes.Contains(m_NFAStateInfo.Info(node).M_NodeLabelPrefix)) { prefixes.Add(m_NFAStateInfo.Info(node).M_NodeLabelPrefix); m_DFAStateInfo.Info(DFAnode).M_NodeLabelPrefix = m_NFAStateInfo.Info(node).M_NodeLabelPrefix; } foreach (uint lineDependency in m_NFAStateInfo.Info(node).M_LineDependencies) { m_DFA.SetFANodeLineDependency(lineDependency, DFAnode); } } foreach (string s in prefixes) { prefix += s; } m_DFA.PrefixElementLabel(prefix, DFAnode); if (ContainsFinalState(q)) { m_DFA.SetFinalState(DFAnode); } if (ContainsInitialState(q)) { m_DFA.M_Initial = DFAnode; } m_mappings[DFAnode] = q; } //else return the existing DFA node return(DFAnode); }
public void Init() { // List of DFA nodes in minimized DFA node List <CGraphNode> configurationAcc, configurationNonAcc; // Create configuration (min-DFA node) for accepted nodes CGraphNode acceptedConf = m_minimizedDFA.CreateGraphNode <CGraphNode>(); // Create a list of initial-DFA nodes that are registered as accepted nodes configurationAcc = new List <CGraphNode>(); // Store the list for initial-DFA accepted nodes in the min-DFA for accepted nodes SetNodesInConfiguration(acceptedConf, configurationAcc); // Create configuration (min-DFA node) for non-accepted nodes CGraphNode non_acceptedConf = m_minimizedDFA.CreateGraphNode <CGraphNode>(); // Create a list for initial-DFA nodes that are registered as non-accepted nodes configurationNonAcc = new List <CGraphNode>(); // Store the list of initial-DFA non-accepted node in the min-DFA for accepted nodes SetNodesInConfiguration(non_acceptedConf, configurationNonAcc); // Separate accepted from non-accepted nodes into two distinct // configurations. Iterate over the input DFA and place the // accepted node into configurationAcc and none accepted nodes // into configurationNonAcc CIt_GraphNodes it = new CIt_GraphNodes(m_DFA); for (it.Begin(); !it.End(); it.Next()) { if (m_DFA.GetFinalStates().Contains(it.M_CurrentItem)) { // Record in the input DFA node the in which minimized DFA node // will be placed SetNodeConfiguration(it.M_CurrentItem, acceptedConf); configurationAcc.Add(it.M_CurrentItem); } else { // Record in the input DFA node the in which minimized DFA node // will be placed SetNodeConfiguration(it.M_CurrentItem, non_acceptedConf); configurationNonAcc.Add(it.M_CurrentItem); } } // ************************* Debug Initialization *************************** m_DFA.RegisterGraphPrinter(new FATextPrinter(m_DFA)); m_DFA.Generate("HopCroft_InitialDFA_.txt"); m_reporting.SetInitialMinDFAStatesContents(acceptedConf, configurationAcc, non_acceptedConf, configurationNonAcc); int nodeCount = 0; int iteration_count = 0; CIt_GraphNodes minDFA_it = new CIt_GraphNodes(m_minimizedDFA); // Iterate while the algorithm reaches a fixed point state while (nodeCount != m_minimizedDFA.M_NumberOfNodes) { nodeCount = m_minimizedDFA.M_NumberOfNodes; // ************************* Debug *************************** CIterationRecord currentIteration = m_reporting.AddIteration(iteration_count); for (minDFA_it.Begin(); !minDFA_it.End(); minDFA_it.Next()) { // ************************* Debug *************************** currentIteration.M_NodesToInspect.Add(minDFA_it.M_CurrentItem); currentIteration.M_InitialNodesConfiguration.Add(minDFA_it.M_CurrentItem, new List <CGraphNode>(GetNodesInConfiguration(minDFA_it.M_CurrentItem))); Split(minDFA_it.M_CurrentItem); } } // Draw the final edges // Edges between nodes of the initial DFA are mapped to edges between // configurations in minimized-DFA. Thus, edges are drawn between two // min-DFA related configurations when their corresponding nodes are // connected and their transition character set is combined CIt_GraphNodes minit1 = new CIt_GraphNodes(m_minimizedDFA); CIt_GraphNodes minit2 = new CIt_GraphNodes(m_minimizedDFA); List <CGraphNode> confs, conft; CGraphEdge edge, newedge; for (minit1.Begin(); !minit1.End(); minit1.Next()) { for (minit2.Begin(); !minit2.End(); minit2.Next()) { confs = GetNodesInConfiguration(minit1.M_CurrentItem); conft = GetNodesInConfiguration(minit2.M_CurrentItem); foreach (CGraphNode snode in confs) { foreach (CGraphNode tnode in conft) { edge = m_DFA.Edge(snode, tnode); if (edge != null) { // Disallow duplicate edges between nodes of the minimized DFA newedge = m_minimizedDFA.Edge(minit1.M_CurrentItem, minit2.M_CurrentItem); if (newedge == null) { newedge = m_minimizedDFA.AddGraphEdge <CGraphEdge, CGraphNode>(minit1.M_CurrentItem, minit2.M_CurrentItem, GraphType.GT_DIRECTED); } // Add transition characters if (GetMinDFAStateTransitionCharacterSet(newedge) == null) { SetMinDFAStateTransitionCharacterSet(newedge, new CCharRangeSet(false)); m_minimizedDFA.SetFAEdgeInfo(newedge, new CCharRangeSet(false)); } CCharRangeSet charset = GetMinDFAStateTransitionCharacterSet(newedge); m_minimizedDFA.GetFAEdgeInfo(newedge).AddSet(m_DFA.GetFAEdgeInfo(edge)); } } } } } // Detect accepted nodes in minimized DFA CIt_GraphNodes it1 = new CIt_GraphNodes(m_minimizedDFA); for (it1.Begin(); !it1.End(); it1.Next()) { List <CGraphNode> configuration = GetNodesInConfiguration(it1.M_CurrentItem); List <CGraphNode> finals = m_DFA.GetFinalStates(); foreach (CGraphNode node in configuration) { if (finals.Contains(node)) { m_minimizedDFA.SetFinalState(it1.M_CurrentItem); } } } // Detect initial state of minimized DFA for (it1.Begin(); !it1.End(); it1.Next()) { List <CGraphNode> configuration = GetNodesInConfiguration(it1.M_CurrentItem); CGraphNode initial = m_DFA.M_Initial; foreach (CGraphNode node in configuration) { if (initial == node) { m_minimizedDFA.M_Initial = it1.M_CurrentItem; } } } // Set Final minimized-DFA labels for (it1.Begin(); !it1.End(); it1.Next()) { List <CGraphNode> conf = GetNodesInConfiguration(it1.M_CurrentItem); foreach (CGraphNode iNode in conf) { HashSet <string> prefs = m_DFA.GetFANodePrefixLabels(iNode); foreach (string s in prefs) { m_minimizedDFA.SetFANodePrefix(s, it1.M_CurrentItem); } foreach (uint dependency in m_DFA.GetFANodeLineDependencies(iNode)) { m_minimizedDFA.SetFANodeLineDependency(dependency, it1.M_CurrentItem); } } m_minimizedDFA.PrefixElementLabel(m_minimizedDFA.GetFANodePrefix(it1.M_CurrentItem), it1.M_CurrentItem); } m_reporting.Report("HOPCROFTReport.txt"); FASerializer serializer = new FASerializer(m_minimizedDFA); serializer.Print(); m_DFA.RegisterGraphPrinter(new FAGraphVizPrinter(m_minimizedDFA, new UOPCore.Options <ThompsonOptions>())); m_DFA.Generate(@"minimizedDFA.dot", true); }