annotate Implab/Automaton/DFATable.cs @ 181:b2b6a6640aa3 ref20160224

minor fixes and debug
author cin
date Thu, 24 Mar 2016 03:54:46 +0300
parents c32688129f14
children 76e8f2ba12b8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
176
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
1 using Implab;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
2 using System;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
3 using System.Collections.Generic;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
4 using System.Linq;
181
b2b6a6640aa3 minor fixes and debug
cin
parents: 180
diff changeset
5 using System.Diagnostics;
176
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
6
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
7 namespace Implab.Automaton {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
8 public class DFATable : IDFATableBuilder {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
9 int m_stateCount;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
10 int m_symbolCount;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
11 int m_initialState;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
12
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
13 readonly HashSet<int> m_finalStates = new HashSet<int>();
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
14 readonly HashSet<AutomatonTransition> m_transitions = new HashSet<AutomatonTransition>();
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
15
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
16
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
17 #region IDFADefinition implementation
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
18
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
19 public bool IsFinalState(int s) {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
20 Safe.ArgumentInRange(s, 0, m_stateCount, "s");
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
21
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
22 return m_finalStates.Contains(s);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
23 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
24
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
25 public IEnumerable<int> FinalStates {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
26 get {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
27 return m_finalStates;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
28 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
29 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
30
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
31 public int StateCount {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
32 get { return m_stateCount; }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
33 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
34
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
35 public int AlphabetSize {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
36 get { return m_symbolCount; }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
37 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
38
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
39 public int InitialState {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
40 get { return m_initialState; }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
41 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
42
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
43 #endregion
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
44
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
45 public void SetInitialState(int s) {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
46 Safe.ArgumentAssert(s >= 0, "s");
181
b2b6a6640aa3 minor fixes and debug
cin
parents: 180
diff changeset
47 m_stateCount = Math.Max(m_stateCount, s + 1);
176
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
48 m_initialState = s;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
49 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
50
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
51 public void MarkFinalState(int state) {
181
b2b6a6640aa3 minor fixes and debug
cin
parents: 180
diff changeset
52 m_stateCount = Math.Max(m_stateCount, state + 1);
176
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
53 m_finalStates.Add(state);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
54 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
55
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
56 public void Add(AutomatonTransition item) {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
57 Safe.ArgumentAssert(item.s1 >= 0, "item");
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
58 Safe.ArgumentAssert(item.s2 >= 0, "item");
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
59 Safe.ArgumentAssert(item.edge >= 0, "item");
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
60
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
61 m_stateCount = Math.Max(m_stateCount, Math.Max(item.s1, item.s2) + 1);
181
b2b6a6640aa3 minor fixes and debug
cin
parents: 180
diff changeset
62 m_symbolCount = Math.Max(m_symbolCount, item.edge + 1);
176
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
63
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
64 m_transitions.Add(item);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
65 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
66
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
67 public void Clear() {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
68 m_stateCount = 0;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
69 m_symbolCount = 0;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
70 m_finalStates.Clear();
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
71 m_transitions.Clear();
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
72 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
73
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
74 public bool Contains(AutomatonTransition item) {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
75 return m_transitions.Contains(item);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
76 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
77
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
78 public void CopyTo(AutomatonTransition[] array, int arrayIndex) {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
79 m_transitions.CopyTo(array, arrayIndex);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
80 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
81
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
82 public bool Remove(AutomatonTransition item) {
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
83 return m_transitions.Remove(item);
176
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
84 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
85
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
86 public int Count {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
87 get {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
88 return m_transitions.Count;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
89 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
90 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
91
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
92 public bool IsReadOnly {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
93 get {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
94 return false;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
95 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
96 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
97
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
98 public IEnumerator<AutomatonTransition> GetEnumerator() {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
99 return m_transitions.GetEnumerator();
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
100 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
101
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
102 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
103 return GetEnumerator();
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
104 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
105
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
106 public int[,] CreateTransitionTable() {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
107 var table = new int[StateCount,AlphabetSize];
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
108
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
109 for (int i = 0; i < StateCount; i++)
181
b2b6a6640aa3 minor fixes and debug
cin
parents: 180
diff changeset
110 for (int j = 0; j < AlphabetSize; j++)
178
d5c5db0335ee working on JSON parser
cin
parents: 176
diff changeset
111 table[i, j] = AutomatonConst.UNREACHABLE_STATE;
176
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
112
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
113 foreach (var t in this)
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
114 table[t.s1,t.edge] = t.s2;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
115
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
116 return table;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
117 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
118
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
119 public bool[] CreateFinalStateTable() {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
120 var table = new bool[StateCount];
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
121
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
122 foreach (var s in FinalStates)
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
123 table[s] = true;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
124
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
125 return table;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
126 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
127
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
128 /// <summary>Формирует множества конечных состояний перед началом работы алгоритма минимизации.</summary>
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
129 /// <remarks>
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
130 /// В процессе построения минимального автомата требуется разделить множество состояний,
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
131 /// на два подмножества - конечные состояния и все остальные, после чего эти подмножества
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
132 /// будут резделены на более мелкие. Иногда требуется гарантировать различия конечных сосотяний,
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
133 /// для этого необходимо переопределить даннцю фукнцию, для получения множеств конечных состояний.
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
134 /// </remarks>
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
135 /// <returns>The final states.</returns>
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
136 protected virtual IEnumerable<HashSet<int>> GroupFinalStates() {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
137 return new HashSet<int>[] { m_finalStates };
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
138 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
139
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
140 protected void Optimize(
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
141 IDFATableBuilder optimalDFA,
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
142 IDictionary<int,int> alphabetMap,
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
143 IDictionary<int,int> stateMap
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
144 ) {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
145 Safe.ArgumentNotNull(optimalDFA, "dfa");
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
146 Safe.ArgumentNotNull(alphabetMap, "alphabetMap");
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
147 Safe.ArgumentNotNull(stateMap, "stateMap");
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
148
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
149
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
150 var setComparer = new CustomEqualityComparer<HashSet<int>>(
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
151 (x, y) => x.SetEquals(y),
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
152 s => s.Sum(x => x.GetHashCode())
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
153 );
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
154
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
155 var optimalStates = new HashSet<HashSet<int>>(setComparer);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
156 var queue = new HashSet<HashSet<int>>(setComparer);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
157
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
158 // получаем конечные состояния, сгруппированные по маркерам
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
159 optimalStates.UnionWith(
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
160 GroupFinalStates()
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
161 );
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
162
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
163 var state = new HashSet<int>(
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
164 Enumerable
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
165 .Range(0, m_stateCount - 1)
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
166 .Where(i => !m_finalStates.Contains(i))
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
167 );
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
168
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
169 optimalStates.Add(state);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
170 queue.Add(state);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
171
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
172 var rmap = m_transitions
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
173 .GroupBy(t => t.s2)
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
174 .ToDictionary(
176
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
175 g => g.Key, // s2
181
b2b6a6640aa3 minor fixes and debug
cin
parents: 180
diff changeset
176 g => g.ToLookup(t => t.edge, t => t.s1)//.ToDictionary(p => p.Key)
176
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
177 );
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
178
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
179 while (queue.Count > 0) {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
180 var stateA = queue.First();
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
181 queue.Remove(stateA);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
182
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
183 for (int c = 0; c < m_symbolCount; c++) {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
184 var stateX = new HashSet<int>();
181
b2b6a6640aa3 minor fixes and debug
cin
parents: 180
diff changeset
185 foreach(var a in stateA.Where(rmap.ContainsKey))
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
186 stateX.UnionWith(rmap[a][c]); // all states from wich the symbol 'c' leads to the state 'a'
176
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
187
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
188 foreach (var stateY in optimalStates.ToArray()) {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
189 if (stateX.Overlaps(stateY) && !stateY.IsSubsetOf(stateX)) {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
190 var stateR1 = new HashSet<int>(stateY);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
191 var stateR2 = new HashSet<int>(stateY);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
192
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
193 stateR1.IntersectWith(stateX);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
194 stateR2.ExceptWith(stateX);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
195
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
196 optimalStates.Remove(stateY);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
197 optimalStates.Add(stateR1);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
198 optimalStates.Add(stateR2);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
199
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
200 if (queue.Contains(stateY)) {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
201 queue.Remove(stateY);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
202 queue.Add(stateR1);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
203 queue.Add(stateR2);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
204 } else {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
205 queue.Add(stateR1.Count <= stateR2.Count ? stateR1 : stateR2);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
206 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
207 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
208 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
209 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
210 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
211
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
212 // карта получения оптимального состояния по соотвествующему ему простому состоянию
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
213 var nextState = 0;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
214 foreach (var item in optimalStates) {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
215 var id = nextState++;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
216 foreach (var s in item)
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
217 stateMap[s] = id;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
218 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
219
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
220 // получаем минимальный алфавит
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
221 // входные символы не различимы, если Move(s,a1) == Move(s,a2), для любого s
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
222 // для этого используем алгоритм кластеризации, сначала
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
223 // считаем, что все символы не различимы
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
224
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
225 var minClasses = new HashSet<HashSet<int>>(setComparer);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
226 var alphaQueue = new Queue<HashSet<int>>();
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
227 alphaQueue.Enqueue(new HashSet<int>(Enumerable.Range(0,AlphabetSize)));
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
228
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
229 // для всех состояний, будем проверять каждый класс на различимость,
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
230 // т.е. символы различимы, если они приводят к разным состояниям
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
231 for (int s = 0 ; s < optimalStates.Count; s++) {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
232 var newQueue = new Queue<HashSet<int>>();
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
233
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
234 foreach (var A in alphaQueue) {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
235 // классы из одного символа делить бесполезно, переводим их сразу в
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
236 // результирующий алфавит
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
237 if (A.Count == 1) {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
238 minClasses.Add(A);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
239 continue;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
240 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
241
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
242 // различаем классы символов, которые переводят в различные оптимальные состояния
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
243 // optimalState -> alphaClass
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
244 var classes = new Dictionary<int, HashSet<int>>();
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
245
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
246 foreach (var term in A) {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
247 // ищем все переходы класса по символу term
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
248 var res = m_transitions.Where(t => stateMap[t.s1] == s && t.edge == term).Select(t => stateMap[t.s2]).ToArray();
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
249
181
b2b6a6640aa3 minor fixes and debug
cin
parents: 180
diff changeset
250 Debug.Assert(res.Length <= 1);
b2b6a6640aa3 minor fixes and debug
cin
parents: 180
diff changeset
251
176
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
252 var s2 = res.Length > 0 ? res[0] : -1;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
253
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
254 HashSet<int> a2;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
255 if (!classes.TryGetValue(s2, out a2)) {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
256 a2 = new HashSet<int>();
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
257 newQueue.Enqueue(a2);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
258 classes[s2] = a2;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
259 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
260 a2.Add(term);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
261 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
262 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
263
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
264 if (newQueue.Count == 0)
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
265 break;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
266 alphaQueue = newQueue;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
267 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
268
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
269 // после окончания работы алгоритма в очереди останутся минимальные различимые классы
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
270 // входных символов
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
271 foreach (var A in alphaQueue)
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
272 minClasses.Add(A);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
273
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
274 // построение отображения алфавитов входных символов.
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
275 // поскольку символ DFAConst.UNCLASSIFIED_INPUT может иметь
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
276 // специальное значение, тогда сохраним минимальный класс,
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
277 // содержащий этот символ на томже месте.
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
278
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
279 var nextCls = 0;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
280 foreach (var item in minClasses) {
178
d5c5db0335ee working on JSON parser
cin
parents: 176
diff changeset
281 if (nextCls == AutomatonConst.UNCLASSIFIED_INPUT)
176
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
282 nextCls++;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
283
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
284 // сохраняем DFAConst.UNCLASSIFIED_INPUT
181
b2b6a6640aa3 minor fixes and debug
cin
parents: 180
diff changeset
285 var cls = item.Contains(AutomatonConst.UNCLASSIFIED_INPUT) ? AutomatonConst.UNCLASSIFIED_INPUT : nextCls++;
176
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
286
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
287 foreach (var a in item)
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
288 alphabetMap[a] = cls;
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
289 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
290
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
291 // построение автомата
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
292 optimalDFA.SetInitialState(stateMap[m_initialState]);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
293
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
294 foreach (var sf in m_finalStates.Select(s => stateMap[s]).Distinct())
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
295 optimalDFA.MarkFinalState(sf);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
296
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
297 foreach (var t in m_transitions.Select(t => new AutomatonTransition(stateMap[t.s1],stateMap[t.s2],alphabetMap[t.edge])).Distinct())
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
298 optimalDFA.Add(t);
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
299 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
300
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
301 protected void PrintDFA<TInput, TState>(IAlphabet<TInput> inputAlphabet, IAlphabet<TState> stateAlphabet) {
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
302 Safe.ArgumentNotNull(inputAlphabet, "inputAlphabet");
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
303 Safe.ArgumentNotNull(stateAlphabet, "stateAlphabet");
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
304
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
305 foreach(var t in m_transitions)
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
306 Console.WriteLine(
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
307 "[{0}] -{{{1}}}-> [{2}]{3}",
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
308 String.Join(",", stateAlphabet.GetSymbols(t.s1)),
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
309 String.Join("", inputAlphabet.GetSymbols(t.edge)),
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
310 String.Join(",", stateAlphabet.GetSymbols(t.s2)),
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
311 m_finalStates.Contains(t.s2) ? "$" : ""
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
312 );
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
313 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
314
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
315 }
0c3c69fe225b rewritten the text scanner
cin
parents: 172
diff changeset
316 }