-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_csv.ml
More file actions
160 lines (144 loc) · 7.9 KB
/
load_csv.ml
File metadata and controls
160 lines (144 loc) · 7.9 KB
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
open Table2
open Csv
open String
open Csv_conv
exception NoFileFound of string
let csv_filename = try (Array.get Sys.argv 1) with e -> (print_string "ERROR while attempting to read filename \n"; exit 0; "")
let csv_lst = try (Csv.load csv_filename) with e -> (print_string ("ERROR trying to open file " ^ csv_filename ^ "\n"); raise (NoFileFound csv_filename))
let csv_lst = Csv.square csv_lst
let rec print_row r =
match r with
| [] -> ()
| h::t -> match h with
| Table2.Coq_nat_entry n -> print_float n; print_string "\t"; print_row t
| Table2.Coq_nil_entry -> print_string ("[NULL_ENTRY]" ^ "\t"); print_row t
| Table2.Coq_string_entry s -> print_string (CSV_conv.implode(s) ^ "\t"); print_row t
let csv_to_Table2 csv =
let t_header = CSV_conv.string_list_to_char_matrix (CSV_conv.header_from_csv csv) in
let t_entry_rowlist = CSV_conv.entry_rowlist_from_csv_rowlist (CSV_conv.rowlist_from_csv csv) in
let fdb = Table2.add_header t_header (Table2.empty_table) in
let fdb = CSV_conv.add_rowlist_to_table (t_entry_rowlist) (fdb)
in fdb
let select_where (h_iden : string) (target : string) (tbl: Table2.table) =
let h_ident_char_list = CSV_conv.explode (h_iden) in
let filter_function (e : Table2.entry) =
match e with
| Table2.Coq_string_entry s -> String.equal (CSV_conv.implode s) target
| Table2.Coq_nat_entry f -> begin
match (float_of_string_opt target) with
| None -> false
| Some target_float -> f = target_float
end
| _ -> false
in
Table2.filter_table_by_entry filter_function h_ident_char_list tbl
let rec trim_comma_whitespace str =
match (str) with
| [] -> []
| ','::' '::rest -> trim_comma_whitespace (','::rest)
| h::t -> h::trim_comma_whitespace(t)
let add_row (row_string : string) (tbl : Table2.table) =
let csv_list = String.split_on_char ',' (row_string |> CSV_conv.explode |> trim_comma_whitespace |> CSV_conv.implode) in
let e_list = CSV_conv.entry_list_from_csv_row csv_list in
Table2.add_row e_list tbl
let add_table (table_alias : string) (table_path) (tbl_list) : unit =
let added_csv_lst = try (Csv.load table_path)
with e -> (print_string ("ERROR trying to open file " ^ table_path ^ "\n");
raise (NoFileFound table_path)) in
let added_table_2 = (csv_to_Table2 added_csv_lst) in
Hashtbl.add tbl_list table_alias added_table_2
type command =
| Exit
| Select_where of (string * string)
| Select_from_where_eq of (string * string * string)
| Print of string
| Malformed
| Add_row of string
| Load of (string * string)
let parse (cmd) =
match ((String.split_on_char ' ' cmd): string list) with
| [] -> Malformed
| h::[] -> begin
match h with
| "print" -> Print "main"
| "exit" -> Exit
| _ -> Malformed
end
| h::t -> begin
match h with
| "select" when ((List.hd t) = "where") -> begin
match t with
| whr::h_iden::eq::target::[] -> Select_where (h_iden,target)
| _ -> Malformed
end
| "select" when ((List.hd t) = "from") ->begin
match t with
| from::alias::whr::h_iden::eq::target::[] when (whr = "where" && eq = "=") ->
Select_from_where_eq (alias, h_iden, target)
| _ -> Malformed
end
| "add" when ((List.hd t) = "row") -> begin
match t with
| rw::rw_lst -> begin
try Add_row (String.sub cmd 8 ((String.length cmd) - 8))
with e -> Malformed
end
|_ -> Malformed
end
| "load" -> begin
match t with
| filename::_as::alias::[] when _as = "as" ->
begin
Load (alias, filename)
end
|_ -> Malformed
end
| "print" -> begin
match t with
| alias::[] -> Print alias
| _ -> Malformed
end
| _ -> Malformed
end
let rec main_loop (tbl : Table2.table) (tbl_list : ((string, Table2.table) Hashtbl.t))=
match read_line () |> parse with
| Exit -> exit 0;
| Print alias -> begin
match alias with
| "main" -> Table2.print_table (tbl); main_loop tbl tbl_list
| other_alias -> Table2.print_table (Hashtbl.find tbl_list alias); main_loop tbl tbl_list
end
| Malformed -> print_string ("Error: Malformed command\n"); main_loop tbl tbl_list
| Select_where (h_iden, target) -> begin
print_string ("Here is the resultant table: \n\n");
let new_tbl = select_where h_iden target tbl in
Table2.print_table (new_tbl); main_loop (new_tbl) tbl_list
end
| Select_from_where_eq (alias, h_iden, target) -> begin
match alias with
| "main" ->
begin
print_string ("Here is the resultant table: \n\n");
let new_tbl = select_where h_iden target tbl in
Table2.print_table (new_tbl); main_loop (new_tbl) tbl_list
end
| other_alias ->
begin
print_string ("Here is the resultant table: \n\n");
let new_tbl = select_where h_iden target (Hashtbl.find tbl_list alias) in
let () = Hashtbl.add tbl_list alias new_tbl in
Table2.print_table (new_tbl); main_loop (tbl) tbl_list
end
end
| Add_row s -> print_string ("Here is the resultant table: \n\n");
let new_tbl = add_row s tbl in
Table2.print_table (new_tbl); main_loop (new_tbl) tbl_list
| Load (alias, filename) -> print_string ("Loading in table " ^ alias ^ "\n");
let () = add_table alias filename tbl_list in
print_string ("Added table " ^ alias ^ "\n\n");
Table2.print_table (Hashtbl.find tbl_list alias);
main_loop tbl tbl_list
let fdb = csv_to_Table2 csv_lst
let () = print_string "Here is the prettified CSV output loaded into the FormalDB \n\n"
let () = Table2.print_table (fdb)
let () = main_loop fdb (Hashtbl.create 5)