small update
[mc1516pa.git] / modelchecker / main.c
1 #include <stdio.h>
2 #include <stdbool.h>
3 #include <ctype.h>
4 #include <time.h>
5 #include <unistd.h>
6
7 #include <sylvan.h>
8
9 #include "sokoban.h"
10 #include "coord.h"
11 #include "object.h"
12
13 #define ERRPRINT(fmt, as...) fprintf(stderr, fmt, ## as);
14 #define DPRINT(fmt, as...) if(DEBUG) ERRPRINT(fmt, ## as);
15
16 typedef enum {OBJECT, COORD, HYBRID} strategy;
17
18 //Global variables
19 bool DEBUG = false;
20 strategy strat = HYBRID;
21
22 void usage(char *prg)
23 {
24 ERRPRINT("Usage:\n"
25 "\t%s [opts] [FILE [FILE [...]]]\n"
26 "\n"
27 "Options:\n"
28 "\tAll strategies are mutually exclusive\n"
29 "\t-c coordinate based strategy\n"
30 "\t-o object based strategy\n"
31 "\t-y hybrid strategy\n"
32 // "\t-l LURD lURD verification strategy\n"
33 // "\t-r show all positions that are a valid solution\n"
34 "\n"
35 "\t-d enable verbose debug output\n"
36 "\t-h show this help\n"
37 "\n"
38 "Positional arguments:\n"
39 "\tFILE zero or more sokoban screens\n"
40 "\t when no file is specified stdin will be used\n", prg);
41 }
42
43 void solve(FILE *inputstream)
44 {
45 clock_t time_start_read, time_end_read;
46 clock_t time_start_encode, time_end_encode;
47
48 time_start_read = clock();
49 sokoban_screen *screen = parse_screen(inputstream);
50 if (screen == NULL) printf("Something went wrong...\n");
51 //sokoban_print(screen);
52 time_end_read = clock();
53
54 time_start_encode = clock();
55
56 lace_init(0, 1000000);
57 lace_startup(0, NULL, NULL);
58 LACE_ME;
59 sylvan_init_package(1LL<<21, 1LL<<27, 1LL<<20, 1LL<<26);
60 sylvan_init_bdd(6);
61 switch(strat){
62 case COORD:
63 DPRINT("Encoding coordinate based\n");
64 state *s = encode_screen(screen);
65 //test_relprod();
66 rels *rls = encode_rel(screen);
67 test_trans(s, rls->relr);
68 break;
69 case OBJECT:
70 DPRINT("Encoding object based\n");
71 solve_object(screen);
72 break;
73 case HYBRID:
74 DPRINT("Encoding hybrid based\n");
75 DPRINT("Not implemented yet...\n");
76 break;
77 default:
78 ERRPRINT("Huh?");
79 exit(2);
80 }
81 sokoban_free(screen);
82 time_end_encode = clock();
83
84 //SOLVE???
85
86 ERRPRINT("Reading: %fs\n",
87 ((double) (time_end_read-time_start_read))/CLOCKS_PER_SEC);
88 ERRPRINT("Encoding: %fs\n",
89 ((double) (time_end_encode-time_start_encode))/CLOCKS_PER_SEC);
90 }
91
92 int main(int argc, char **argv)
93 {
94 int optchar;
95
96 while((optchar = getopt(argc, argv, "cdhoy")) != -1){
97 switch(optchar){
98 case 'c':
99 strat = COORD;
100 DPRINT("Strategy changed to Coordinate based\n");
101 break;
102 case 'd':
103 DEBUG = true;
104 DPRINT("Debug enabled\n");
105 break;
106 case 'h':
107 usage(argv[0]);
108 return 0;
109 case 'o':
110 strat = OBJECT;
111 DPRINT("Strategy changed to Object based\n");
112 break;
113 case 'y':
114 strat = HYBRID;
115 DPRINT("Strategy changed to Hybrid\n");
116 break;
117 case '?':
118 if(isprint(optopt)){
119 ERRPRINT("Unknown option `-%c'.\n", optopt);
120 } else {
121 ERRPRINT("Unknown option char `-\\x%x'.\n", optopt);
122 }
123 return 2;
124 default:
125 break;
126 }
127 }
128
129 if(optind == argc){
130 ERRPRINT("You have not specified a file, reading from stdin\n");
131 solve(stdin);
132 }
133
134 for(int filepathindex = optind; filepathindex < argc; filepathindex++){
135 char *currentfilepath = argv[filepathindex];
136 ERRPRINT("Processing: %s\n", currentfilepath);
137 FILE *currentfile = fopen(currentfilepath, "r");
138 DPRINT("Opening file\n");
139 solve(currentfile);
140 DPRINT("Closing file\n");
141 fclose(currentfile);
142 }
143 return 0;
144 }