-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathws.pl
187 lines (165 loc) · 5.09 KB
/
ws.pl
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
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/html_write)).
:- use_module(library(http/http_parameters)).
:- use_module(library(uri)).
:- use_module(fd_parser).
:- use_module(fd).
:- use_module(timeout).
:- use_module(library(http/json)).
:- use_module(library(http/json_convert)).
:- use_module(library(http/http_json)).
:- json_object
analysis(
nf,
keys,
primaryattributes,
secondaryattributes,
fmin,
d3nf,
bcnf
).
:- json_object error(message).
:- http_handler(root(.), httpIndex, []).
:- http_handler(root(nf), httpNFTimeout, []).
:- http_handler(root(keys), httpKeysTimeout, []).
:- http_handler(root(primaryattributes), httpPrimaryAttributesTimeout, []).
:- http_handler(root(secondaryattributes), httpSecondaryAttributesTimeout, []).
:- http_handler(root(fmin), httpFMinTimeout, []).
:- http_handler(root(bcnfs), httpBCNFsTimeout, []).
:- http_handler(root(d3nfs), http3NFsTimeout, []).
:- http_handler(root(json), httpJSONTimeout, []).
httpIndex(Request) :- http_reply_file('index.html', [], Request).
httpNFTimeout(Request) :- reply_with_timeout(Request, httpNF).
httpKeysTimeout(Request) :- reply_with_timeout(Request, httpKeys).
httpPrimaryAttributesTimeout(Request) :- reply_with_timeout(Request, httpPrimaryAttributes).
httpSecondaryAttributesTimeout(Request) :- reply_with_timeout(Request, httpSecondaryAttributes).
httpFMinTimeout(Request) :- reply_with_timeout(Request, httpFMin).
httpBCNFsTimeout(Request) :- reply_with_timeout(Request, httpBCNFs).
http3NFsTimeout(Request) :- reply_with_timeout(Request, http3NFs).
% port number
port(5000).
start :-
port(Port),
server(Port).
stop :-
port(Port),
http_stop_server(Port, []).
restart :-
stop, [ws], start.
server(Port) :-
http_server(http_dispatch, [port(Port)]).
null_to_empty([], '') :- !.
null_to_empty(PrimaryAttributes, PrimaryAttributes).
reply(Text) :-
format('Content-type: text/plain~n'),
format('Access-Control-Allow-Origin: *~n'),
format('Access-Control-Allow-Method: GET~n~n'),
format(Text).
% nf
httpNF(Request, Reply) :-
http_parameters(Request,
[
r(R, []),
f(F, [])
]),
parse_fds(F, F0),
nf(R, F0, N),
Reply = N.
% fmin
httpFMin(Request, Reply) :-
http_parameters(Request,
[
f(F, [])
]),
parse_fds(F, F0),
fmins(F0, FMins0),
atomic_list_concat(FMins0, '~n', FMins1),
Reply = FMins1.
% list all fmins with timeout
fmins(F, FMins) :-
call_with_timeout(F, FMins, fmins_all, fmin, fd_parser:fds_to_string).
% keys
httpKeys(Request, Reply) :-
http_parameters(Request,
[
r(R, []),
f(F, [])
]),
parse_fds(F, F0),
keys(R, F0, Keys),
atomic_list_concat(Keys, ', ', S),
Reply = S.
% primary attributes
httpPrimaryAttributes(Request, Reply) :-
http_parameters(Request,
[
r(R, []),
f(F, [])
]),
parse_fds(F, F0),
primaryattributes(R, F0, PrimaryAttributes),
null_to_empty(PrimaryAttributes, PrimaryAttributes0),
Reply = PrimaryAttributes0.
% secondary attributes
httpSecondaryAttributes(Request, Reply) :-
http_parameters(Request,
[
r(R, []),
f(F, [])
]),
parse_fds(F, F0),
secondaryattributes(R, F0, SecondaryAttributes),
null_to_empty(SecondaryAttributes, SecondaryAttributes0),
Reply = SecondaryAttributes0.
% BCNF decompositions
httpBCNFs(Request, Reply) :-
http_parameters(Request,
[
r(R, []),
f(F, [])
]),
parse_fds(F, F0),
bcnfs(R, F0, Rhos0),
atomic_list_concat(Rhos0, '~n', Rhos1),
Reply = Rhos1.
% list all BCNF decompositions with timeout
bcnfs(R, F, Rhos) :-
call_with_timeout(R, F, Rhos, bcnfs_all, bcnf, fd:decomposition_to_text).
% 3NF decompositions
http3NFs(Request, Reply) :-
http_parameters(Request,
[
r(R, []),
f(F, [])
]),
parse_fds(F, F0),
d3nfs(R, F0, Rhos0),
atomic_list_concat(Rhos0, '~n', Rhos1),
Reply = Rhos1.
% list all 3NF decompositions with timeout
d3nfs(R, F, Rhos) :-
call_with_timeout(R, F, Rhos, d3nfs_all, d3nf, fd:decomposition_to_text).
httpJSONTimeout(Request) :-
timeout(T),
catch(
call_with_time_limit(T, httpJSON(Request)),
time_limit_exceeded, % exception type
reply('timeout') % catch branch
).
httpJSON(Request) :-
http_parameters(Request,
[
r(R, []),
f(F, [])
]),
parse_fds(F, F0),
nf(R, F0, N),
keys(R, F0, Keys),
primaryattributes(R, F0, PrimaryAttributes),
secondaryattributes(R, F0, SecondaryAttributes),
fmins(F0, FMins),
d3nfs(R, F0, D3NFs),
bcnfs(R, F0, BCNFs),
prolog_to_json(analysis(N, Keys, PrimaryAttributes, SecondaryAttributes, FMins, D3NFs, BCNFs), Result),
reply_json(Result).