//形成|或运算自动机 private static NfaPair EitherNfaPair(NfaPair np1, NfaPair np2) { NfaPair np = new NfaPair(); np.start = new NfaNode(); np.end = new NfaNode(); np.count += 2; if (np1.count < np2.count) { NfaPair t = np1; np1 = np2; np2 = t; } np.start.edge = -1; np.start.next1 = np1.start; np.start.next2 = np2.start; np1.end.edge = -1; np1.end.next1 = np.end; np2.end.edge = -1; np2.end.next1 = np.end; np.count += np1.count + np2.count; return(np); }
//显示Dfa public static void DrawDfa(ListView lv, NfaPair np) { GetAllDfaNode(np); int totalColumn = NfaManager.chars.Count + NfaManager.charstrings.Count, counts = 0; //建立表 GridView gv = lv.View as GridView; gv.Columns.Clear(); GridViewColumn col = new GridViewColumn(); col.Header = "集合"; col.DisplayMemberBinding = new Binding(string.Format("DATA[{0}]", counts++)); gv.Columns.Add(col); foreach (var item in NfaManager.chars) { GridViewColumn c = new GridViewColumn(); c.Header = ((char)item).ToString(); mychars.Add(((char)item).ToString()); c.DisplayMemberBinding = new Binding(string.Format("DATA[{0}]", counts++)); gv.Columns.Add(c); } foreach (var item in NfaManager.charstrings) { GridViewColumn c = new GridViewColumn(); c.Header = item; mycharstrings.Add(item); c.DisplayMemberBinding = new Binding(string.Format("DATA[{0}]", counts++)); gv.Columns.Add(c); } lv.ItemsSource = dfanodes; }
//Nfa到Dfa private static void GetAllDfaNode(NfaPair np) { int pos = 0, totalColumn = NfaManager.chars.Count + NfaManager.charstrings.Count; HashSet <int> s0 = np.start.GetClosure(); queue.Add(s0); //第一列那列放入queue,每行放入dfanodes for (; pos < queue.Count; pos++) { HashSet <int> theset = queue[pos]; //设置dfanode DfaNode dfanode = new DfaNode(); dfanode.data = new List <HashSet <int> >(); dfanode.nfastates = theset; if (theset.Contains(np.end.nodenum)) { dfanode.isEnd = true; } //所有字符的对应闭包入队列 foreach (var pattchar in NfaManager.chars) { HashSet <int> temp = new HashSet <int>(); foreach (var nodenum in theset) { HashSet <int> t = NfaManager.nfanodes[nodenum].GetClosureWithChar(pattchar); foreach (var t_item in t) { temp.Add(t_item); } } if (temp.Count > 0 && !FindInQueue(temp)) { queue.Add(temp); } dfanode.data.Add(temp); } //所有字符集的对应闭包入队列 foreach (var pattchar in NfaManager.charstrings) { HashSet <int> temp = new HashSet <int>(); foreach (var nodenum in theset) { HashSet <int> t = NfaManager.nfanodes[nodenum].GetClosureWithChar(pattchar); foreach (var t_item in t) { temp.Add(t_item); } } if (temp.Count > 0 && !FindInQueue(temp)) { queue.Add(temp); } dfanode.data.Add(temp); } dfanodes.Add(dfanode); } }
//连接&与运算自动机 private static NfaPair CombineNfaPair(NfaPair before, NfaPair after) { before.end.edge = -1; before.end.next1 = after.start; before.end = after.end; before.count += after.count; return(before); }
//由字符串表示的字符集构建单个自动机 private static NfaPair CreatNfaPair(string charsetstring) { NfaPair np = new NfaPair(); NfaNode s = new NfaNode(), e = new NfaNode(); s.edge = -2; s.charsetstring = charsetstring; s.next1 = e; np.start = s; np.end = e; np.count += 2; return(np); }
private static int[] deeps; //对应深度节点数 //由字符构建单个自动机 private static NfaPair CreatNfaPair(char c) { NfaPair np = new NfaPair(); NfaNode s = new NfaNode(), e = new NfaNode(); s.edge = c; s.next1 = e; np.start = s; np.end = e; np.count += 2; return(np); }
//作图 public static void DrawNfaPair(Canvas cv, NfaPair root) { nfanodes = new NfaNode[NfaNode.totalNfaNode]; visited = new bool[NfaNode.totalNfaNode]; deeps = new int[NfaNode.totalNfaNode + 1]; for (int i = 0; i < visited.Count(); i++) { visited[i] = false; deeps[i] = 0; } DFS(root.start); for (int i = 0; i < visited.Count(); i++) { visited[i] = false; } _DrawNfaPair(cv, root.start); }
//形成?闭包自动机 private static NfaPair DoQuestCollapse(NfaPair nfaPair) { NfaPair np = new NfaPair(); NfaNode before = new NfaNode(), after = new NfaNode(); np.start = before; np.end = after; np.count += 2; //前面连接 before.next1 = nfaPair.start; before.edge = -1; //后面连接 nfaPair.end.next1 = after; nfaPair.end.edge = -1; //现在头连尾 before.next2 = after; np.count += nfaPair.count; return(np); }