-
Notifications
You must be signed in to change notification settings - Fork 0
/
Automaton.py
156 lines (126 loc) · 4.75 KB
/
Automaton.py
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
import Data as dt
class Automaton:
def __init__(self, startState=None, finalStates=None, states=None, alphabet=None, transitions=None):
"""
Input:
-startState: start state (s0) ''
-finalStartes: a list final state (F) []
-states: a lists containing the states (S) []
-alphabet: a lists alphabet (Σ - sigma) []
-transitions: state transition function (δ -delta) []
"""
self.startState = startState
self.finalStates = finalStates
self.states = states
self.alphabet = alphabet
self.transitions = transitions
# automation data frame
def automatonForm(self):
automaton = {
'startState': self.startState,
'finalStates': self.finalStates,
'states': self.states,
'alphabet': self.alphabet,
'transitions': self.transitions
}
return automaton
def printTran(self):
print('\t{:{width}}'.format('S\\Σ', width=20), end='')
for i in self.alphabet:
print('{:{width}}'.format(i, width=20,), end='')
print()
s_states = getStartTranStates(self.transitions)
for i in s_states:
print('\t{:{width}}'.format('{}'.format(i), width=20), end='')
for al in self.alphabet:
j = 0
check_tran = True
while check_tran == True and j < len(self.transitions):
# for j in self.transitions:
if i == self.transitions[j][0] and al == self.transitions[j][1]:
if len(self.transitions[j]) == 2:
print('{:{width}}'.format('-', width=20,), end='')
elif len(self.transitions[j]) > 3:
p = []
for k in range(2, len(self.transitions[j])):
p.append(self.transitions[j][k])
print('{:{width}}'.format('{}'.format(p), width=20,), end='')
else:
print('{:{width}}'.format('{}'.format(self.transitions[j][2]), width=20,), end='')
check_tran = False
else: j += 1
print()
# print data automation
def printAutomation(self):
automaton = self.automatonForm()
# print
for i in automaton:
if i == 'startState' and isinstance(automaton[i], list):
print('+ {}: {}'.format(i, automaton[i][0]))
elif i == 'transitions':
print('+ transitions (delta):')
# In dạng bảng
self.printTran()
# In dạng hàm
# for j in range(len(automaton[i])):
# print('\tdelta {}: {}'.format(j, automaton[i][j]))
else:
print('+ {}: {}'.format(i, automaton[i]))
# Input data in otomat
def automatonData(filename):
fileData = dt.opendFileInput(filename)
ndftData = dt.ndfInput(fileData)
automaton = Automaton()
for i in ndftData:
if i == 'startState':
automaton.startState = ndftData[i]
elif i == 'finalStates':
automaton.finalStates = ndftData[i]
elif i == 'states':
automaton.states = ndftData[i]
elif i == 'alphabet':
automaton.alphabet = ndftData[i]
elif i == 'transitions':
automaton.transitions = ndftData[i]
return automaton
# find transition of state x
# Tìm hàm chuyển của trạng thái x
def findTransitions(x, transitions, alphabet):
listFind = []
check = True
length = 0
t = 0
while check == True and length < len(transitions):
i = transitions[length]
dataFind = []
if x == i[0]:
for j in range(1, len(i)):
dataFind.append(i[j])
listFind.append(tuple(dataFind))
t += 1
if t == len(alphabet):
check = False
else: length += 1
return listFind
# Lay trang thai bat dau
def getStartTranStates(trans):
start_state_trans = []
for i in trans:
if i[0] not in start_state_trans:
start_state_trans.append(i[0])
return start_state_trans
# Lay trạng thái chuyển tiếp
def getNextTransStates(trans):
next_states_trans = []
for i in trans:
if len(i) <= 2:
pass
elif len(i) == 3:
next_states_trans.append[[i[2]]]
else:
states = []
for j in range(2, len(i)):
states.append(i[j])
if states not in next_states_trans:
next_states_trans.append(states)
return next_states_trans