-
Notifications
You must be signed in to change notification settings - Fork 1
/
TableToAutomateConverter.cs
233 lines (224 loc) · 8.19 KB
/
TableToAutomateConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
namespace AutomataConverter
{
class TableToAutomateConverter
{
private List<string> startStates;
private List<string> finalStates;
private List<List<string>> symbolsSequence;
private List<string> states;
private Type automateType;
private List<TransitionBySequence> transitionsBySequence;
private List<Transition> transitions;
/// <summary>
/// Считывание автомата из файла
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public NamedAutomaton ReadAutomate(string path)
{
startStates = new List<string>();
finalStates = new List<string>();
symbolsSequence = new List<List<string>>();
states = new List<string>();
transitionsBySequence = new List<TransitionBySequence>();
transitions = new List<Transition>();
List<string> text = File.ReadAllLines(path).ToList();
List<List<string>> table = new List<List<string>>();
foreach (var line in text)
{
table.Add(new List<string>(line.Split('\t').Where(item => item != "\t" && item != "")));
}
ParseTable(table);
EvalType();
if (automateType == typeof(NondeterminedFiniteAutomaton))
{
return TableToNFA(table);
}
else
{
return TableToDFA(table);
}
}
/// <summary>
/// Определение типа автомата, таблица которого считана из файла
/// </summary>
private void EvalType()
{
if (startStates.Count > 1 ||
symbolsSequence.Count(i=>i.Contains("eps"))!=0 ||
transitions.Count(item => item.Destinations.Count > 1) != 0)
{
automateType = typeof(NondeterminedFiniteAutomaton);
}
else
{
automateType = typeof(DeterminedFiniteAutomaton);
}
}
/// <summary>
/// Разбор таблицы
/// </summary>
/// <param name="table">Таблица</param>
private void ParseTable(List<List<string>> table)
{
string s;
string[] symbolSequence;
List<string> splited;
for (int i = 0; i < table[0].Count; i++)
{
symbolSequence = table[0][i].Split(
new char[] { ','}, StringSplitOptions.RemoveEmptyEntries);
symbolsSequence.Add(new List<string>(symbolSequence));
}
for (int i = 1; i < table.Count; i++)
{
s = table[i][0];
splited = s.Split(' ').Where(item => item != " " && item != "").ToList();
states.Add(splited[0]);
if (splited.Contains("(f)"))
{
finalStates.Add(splited[0]);
}
if (splited.Contains("(s)"))
{
startStates.Add(splited[0]);
}
}
for (int i = 1; i < table.Count; i++)
{
for (int j = 1; j < table[i].Count; j++)
{
splited = table[i][j].Split(new char[] { '-', ',' }, StringSplitOptions.RemoveEmptyEntries).
Where(item => item != " " && item != ",").ToList();
if (splited.Count == 0)
{
continue;
}
if (symbolsSequence[j - 1].Count > 1)
{
transitionsBySequence.Add(
new TransitionBySequence(states[i - 1], symbolsSequence[j - 1], splited));
}
else
{
transitions.Add(new Transition(states[i - 1], symbolsSequence[j - 1][0], splited));
}
}
}
}
/// <summary>
/// Создание автомата по таблице
/// </summary>
/// <param name="table"></param>
/// <returns></returns>
private NondeterminedFiniteAutomaton TableToNFA(List<List<string>> table)
{
NondeterminedFiniteAutomaton nfa = new NondeterminedFiniteAutomaton();
foreach(var sequence in symbolsSequence)
{
foreach (var symbol in sequence)
{
if (!nfa.IsSymbolExist(symbol))
{
nfa.AddSymbol(symbol);
}
}
}
for (int i = 0; i < states.Count; i++)
{
nfa.AddState(states[i]);
}
for (int j = 0; j < transitionsBySequence.Count; j++)
{
for (int i = 0; i < transitionsBySequence[j].Destinations.Count; i++)
{
nfa.AddTransitionBySymbolsSequence(
transitionsBySequence[j].Source, transitionsBySequence[j].Destinations[i],
transitionsBySequence[j].Symbols);
}
}
for (int j = 0; j < transitions.Count; j++)
{
for (int i = 0; i < transitions[j].Destinations.Count; i++)
{
nfa.AddTransition(transitions[j].Source, transitions[j].Destinations[i],
transitions[j].Symbol);
}
}
nfa.SetStartStates(new HashSet<string>(startStates));
nfa.SetFinalStates(new HashSet<string>(finalStates));
return nfa;
}
/// <summary>
/// Создание автомата по таблице
/// </summary>
/// <param name="table"></param>
/// <returns></returns>
private DeterminedFiniteAutomaton TableToDFA(List<List<string>> table)
{
DeterminedFiniteAutomaton dfa = new DeterminedFiniteAutomaton(startStates[0]);
states.Remove(startStates[0]);
foreach (var sequence in symbolsSequence)
{
foreach (var symbol in sequence)
{
if (!dfa.IsSymbolExist(symbol))
{
dfa.AddSymbol(symbol);
}
}
}
for (int i = 0; i < states.Count; i++)
{
dfa.AddState(states[i]);
}
dfa.SetFinalStates(new HashSet<string>(finalStates));
for (int j = 0; j < transitions.Count; j++)
{
dfa.AddTransition(transitions[j].Source, transitions[j].Destinations[0], transitions[j].Symbol);
}
return dfa;
}
private class TransitionBySequence
{
public string Source { get; set; }
public List<string> Symbols { get; set; }
public List<string> Destinations { get; set; }
public TransitionBySequence()
{
Destinations = new List<string>();
}
public TransitionBySequence(string source, List<string> symbols, List<string> destinations)
{
Source = source;
Symbols = symbols;
Destinations = destinations;
}
}
/// <summary>
/// Переход
/// </summary>
private class Transition
{
public string Source { get; set; }
public string Symbol { get; set; }
public List<string> Destinations { get; set; }
public Transition()
{
Destinations = new List<string>();
}
public Transition(string source, string symbol, List<string> destinations)
{
Source = source;
Symbol = symbol;
Destinations = destinations;
}
}
}
}