-
Notifications
You must be signed in to change notification settings - Fork 32
/
tester.c
170 lines (146 loc) · 5.78 KB
/
tester.c
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
/* Copyright 2013 Bliksem Labs. See the LICENSE file at the top-level directory of this distribution and at https://github.com/bliksemlabs/rrrr/. */
/* tester.c : single-theaded test of router for unit tests and debugging */
#include <syslog.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <getopt.h>
#include <time.h>
#include <string.h>
#include "config.h"
#include "rrrr.h"
#include "tdata.h"
#include "router.h"
#include "parse.h"
#include "json.h"
// consider changing to --arrive TIME or --depart TIME --date DATE
// also look up stop ids
#define OUTPUT_LEN 8000
static struct option long_options[] = {
{ "arrive", no_argument, NULL, 'a' },
{ "depart", no_argument, NULL, 'd' },
{ "random", no_argument, NULL, 'r' },
{ "help", no_argument, NULL, 'h' },
{ "date", required_argument, NULL, 'D' },
{ "walk-slack", required_argument, NULL, 's' },
{ "walk-speed", required_argument, NULL, 'S' },
{ "optimise", required_argument, NULL, 'o' },
{ "from-idx", required_argument, NULL, 'f' },
{ "to-idx", required_argument, NULL, 't' },
{ "via-idx", required_argument, NULL, 'V' },
{ "mode", required_argument, NULL, 'm' },
{ "start-trip-idx", required_argument, NULL, 'Q' },
{ "banned-routes-idx", required_argument, NULL, 'x' },
{ "banned-stops-idx", required_argument, NULL, 'y' },
{ "banned-trips-idx", required_argument, NULL, 'z' },
{ "banned-stops-hard-idx", required_argument, NULL, 'w' },
{ "trip-attributes", required_argument, NULL, 'A' },
{ "gtfsrt", required_argument, NULL, 'g' },
{ "gtfsrt-alerts", required_argument, NULL, 'G' },
{ "timetable", required_argument, NULL, 'T' },
{ "verbose", no_argument, NULL, 'v' },
{ NULL, 0, 0, 0 } /* end */
};
int main(int argc, char **argv) {
router_request_t req;
router_request_initialize (&req);
char *tdata_file = RRRR_INPUT_FILE;
char *gtfsrt_file = NULL;
char *gtfsrt_alerts_file = NULL;
bool verbose = false;
int opt = 0;
while (opt >= 0) {
opt = getopt_long(argc, argv, "adrhD:s:S:o:f:t:V:m:Q:x:y:z:w:A:g:G:T:v", long_options, NULL);
if (opt < 0) continue;
switch (opt) {
case 'T':
tdata_file = optarg;
break;
case 'g':
gtfsrt_file = optarg;
break;
case 'G':
gtfsrt_alerts_file = optarg;
break;
case 'v':
verbose = true;
break;
case 'h':
goto usage;
}
}
/* SETUP */
// load transit data from disk
tdata_t tdata;
tdata_load(tdata_file, &tdata);
optind = 0;
opt = 0;
while (opt >= 0) {
opt = getopt_long(argc, argv, "adrhD:s:S:o:f:t:V:m:Q:x:y:z:w:A:g:G:T:v", long_options, NULL);
parse_request(&req, &tdata, NULL, opt, optarg);
}
if (req.from == NONE || req.to == NONE) goto usage;
if (req.from == req.to) {
fprintf(stderr, "Dude, you are already there.\n");
exit(-1);
}
if (req.from >= tdata.n_stops || req.to >= tdata.n_stops) {
fprintf(stderr, "Invalid stopids in from and/or to.\n");
exit(-1);
}
if (req.time_rounded && !(req.arrive_by)) {
req.time++; // Round time upwards when departing after a requested time
}
req.time_rounded = false;
// load gtfs-rt file from disk
if (gtfsrt_file != NULL || gtfsrt_alerts_file != NULL) {
RadixTree *tripid_index = rxt_load_strings_from_tdata (tdata.trip_ids, tdata.trip_id_width, tdata.n_trips);
if (gtfsrt_file != NULL) {
tdata_clear_gtfsrt (&tdata);
tdata_apply_gtfsrt_file (&tdata, tripid_index, gtfsrt_file);
}
if (gtfsrt_alerts_file != NULL) {
RadixTree *routeid_index = rxt_load_strings_from_tdata (tdata.route_ids, tdata.route_id_width, tdata.n_routes);
RadixTree *stopid_index = rxt_load_strings_from_tdata (tdata.stop_ids, tdata.stop_id_width, tdata.n_stops);
tdata_clear_gtfsrt_alerts(&tdata);
tdata_apply_gtfsrt_alerts_file (&tdata, routeid_index, stopid_index, tripid_index, gtfsrt_alerts_file);
}
}
// initialize router
router_t router;
router_setup(&router, &tdata);
//tdata_dump(&tdata); // debug timetable file format
char result_buf[OUTPUT_LEN];
router_route (&router, &req);
if (verbose) {
router_request_dump (&router, &req);
router_result_dump(&router, &req, result_buf, OUTPUT_LEN);
printf("%s", result_buf);
}
// repeat search in reverse to compact transfers
uint32_t n_reversals = req.arrive_by ? 1 : 2;
// but do not reverse requests starting on board (they cannot be compressed, earliest arrival is good enough)
if (req.start_trip_trip != NONE) n_reversals = 0;
// n_reversals = 0; // DEBUG turn off reversals
for (uint32_t i = 0; i < n_reversals; ++i) {
router_request_reverse (&router, &req); // handle case where route is not reversed
router_route (&router, &req);
if (verbose) {
printf ("Repeated search with reversed request: \n");
router_request_dump (&router, &req);
router_result_dump(&router, &req, result_buf, OUTPUT_LEN);
printf("%s", result_buf);
}
}
/* Output only final result in non-verbose mode */
if (!verbose) {
router_result_dump(&router, &req, result_buf, OUTPUT_LEN);
printf("%s", result_buf);
}
router_teardown(&router);
tdata_close(&tdata);
exit(EXIT_SUCCESS);
usage:
printf("Usage:\n%s [-r(andomize)] [--from-idx from_stop] [--to-idx to_stop] [-a(rrive)] [-d(epart)] [-D YYYY-MM-DDThh:mm:ss] [-g gtfsrt.pb] [-T timetable.dat]\n", argv[0]);
exit(-2);
}