-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
103 lines (92 loc) · 3.7 KB
/
app.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
from FiniteAutomata.Nfa import Nfa
from FiniteAutomata.Dfa import Dfa
from flask import Flask, render_template
import os
import sys
import json
import copy
from flask import request
from flask_cors import CORS
def set_default(obj):
if isinstance(obj, set):
return list(obj)
raise TypeError
app = Flask(__name__)
CORS(app)
@app.route("/")
def hello_world():
return render_template('index.html')
@app.route("/minimizedfa", methods=["POST"])
def Minimize():
States = set(json.loads(request.form.get('states')))
Alphabets = set(json.loads(request.form.get('alphabets')))
Data = json.loads(request.form.get('transitions'))
Transitions = dict()
for i, State in enumerate(Data):
if State[0] not in Transitions:
Transitions[State[0]] = dict({State[1]: [State[2]]})
else:
if State[1] not in Transitions[State[0]]:
Transitions[State[0]][State[1]] = [State[2]]
else:
Transitions[State[0]][State[1]].append(State[2])
for State in Transitions:
for Alphabet in Alphabets:
if not Alphabet in Transitions[State]:
Transitions[State][Alphabet] = []
StartState = str(json.loads(request.form.get('startState')))
FinalStates = set(json.loads(request.form.get('finalStates')))
DfaMachinine = Dfa(States, Alphabets, Transitions, StartState, FinalStates)
MinimizedDfa = DfaMachinine.MinimizeDfa()
for State in MinimizedDfa._Transitions:
if MinimizedDfa._Transitions[State] == {}:
MinimizedDfa._Transitions.pop(State)
for Alphabet in MinimizedDfa._Alphabets:
if MinimizedDfa._Transitions[State][Alphabet] == '':
MinimizedDfa._Transitions[State].pop(Alphabet)
return json.dumps(MinimizedDfa.__dict__, default=set_default)
@app.route("/nfatodfa", methods=["POST"])
def NFAToDFA():
StatesNfa = set(json.loads(request.form.get('states')))
AlphabetsNfa = set(json.loads(request.form.get('alphabets')))
for Alphabet in AlphabetsNfa:
if Alphabet == 'epsilon':
AlphabetsNfa.add(Nfa.EPSILON)
AlphabetsNfa.remove('epsilon')
Data = json.loads(request.form.get('transitions'))
for Transition in Data:
if Transition[1] == 'epsilon':
Transition[1] = Nfa.EPSILON
Transitions = dict()
for i, State in enumerate(Data):
if State[0] not in Transitions:
Transitions[State[0]] = dict({State[1]: [State[2]]})
else:
if State[1] not in Transitions[State[0]]:
Transitions[State[0]][State[1]] = [State[2]]
else:
Transitions[State[0]][State[1]].append(State[2])
for State in StatesNfa:
if State in Transitions:
for Alphabet in AlphabetsNfa:
if not Alphabet == Nfa.EPSILON:
if not Alphabet in Transitions[State]:
Transitions[State][Alphabet] = []
else:
Transitions[State] = dict()
for Alphabet in AlphabetsNfa:
if not Alphabet == Nfa.EPSILON:
Transitions[State][Alphabet] = []
TransitionsNfa = Transitions
StartStateNfa = str(json.loads(request.form.get('startState')))
FinalStatesNfa = set(json.loads(request.form.get('finalStates')))
NfaMachine = Nfa(StatesNfa, AlphabetsNfa, TransitionsNfa,
StartStateNfa, FinalStatesNfa)
EqDfa = NfaMachine.ToDfa()
NewEq = copy.deepcopy(EqDfa._Transitions)
for State in EqDfa._Transitions:
if EqDfa._Transitions[State] == {}:
NewEq.pop(State)
continue
EqDfa._Transitions = copy.deepcopy(NewEq)
return json.dumps(EqDfa.__dict__, default=set_default)