1 |
|
---|
2 |
|
---|
3 |
|
---|
4 |
|
---|
5 |
|
---|
6 |
|
---|
7 |
|
---|
8 |
|
---|
9 |
|
---|
10 |
package logformat.slog2.input; |
---|
11 |
|
---|
12 |
import java.util.StringTokenizer; |
---|
13 |
import java.io.InputStreamReader; |
---|
14 |
import java.io.BufferedReader; |
---|
15 |
|
---|
16 |
import base.drawable.*; |
---|
17 |
import logformat.slog2.*; |
---|
18 |
import logformat.slog2.input.InputLog; |
---|
19 |
|
---|
20 |
public class Navigator |
---|
21 |
{ |
---|
22 |
private static boolean printAll = false; |
---|
23 |
private static boolean changedPrintAll = false; |
---|
24 |
private static boolean isVerbose = false; |
---|
25 |
|
---|
26 |
private static InputStreamReader sys_insrdr |
---|
27 |
= new InputStreamReader( System.in ); |
---|
28 |
private static BufferedReader sys_bufrdr |
---|
29 |
= new BufferedReader( sys_insrdr ); |
---|
30 |
|
---|
31 |
private static String in_filename; |
---|
32 |
private static short depth_max, depth; |
---|
33 |
|
---|
34 |
public static final void main( String[] args ) |
---|
35 |
{ |
---|
36 |
InputLog slog_ins; |
---|
37 |
CategoryMap objdefs; |
---|
38 |
TreeTrunk treetrunk; |
---|
39 |
TreeNode treeroot; |
---|
40 |
TimeBoundingBox timeframe_root, timeframe_old, timeframe; |
---|
41 |
String err_msg; |
---|
42 |
|
---|
43 |
parseCmdLineArgs( args ); |
---|
44 |
|
---|
45 |
slog_ins = new InputLog( in_filename ); |
---|
46 |
if ( slog_ins == null ) { |
---|
47 |
System.err.println( "Null input logfile!" ); |
---|
48 |
System.exit( 1 ); |
---|
49 |
} |
---|
50 |
if ( ! slog_ins.isSLOG2() ) { |
---|
51 |
System.err.println( in_filename + " is NOT SLOG-2 file!." ); |
---|
52 |
System.exit( 1 ); |
---|
53 |
} |
---|
54 |
if ( (err_msg = slog_ins.getCompatibleHeader()) != null ) { |
---|
55 |
System.err.print( err_msg ); |
---|
56 |
InputLog.stdoutConfirmation(); |
---|
57 |
} |
---|
58 |
slog_ins.initialize(); |
---|
59 |
|
---|
60 |
|
---|
61 |
|
---|
62 |
treetrunk = new TreeTrunk( slog_ins, Drawable.INCRE_STARTTIME_ORDER ); |
---|
63 |
treetrunk.setDebuggingEnabled( isVerbose ); |
---|
64 |
treetrunk.initFromTreeTop(); |
---|
65 |
treeroot = treetrunk.getTreeRoot(); |
---|
66 |
if ( treeroot == null ) { |
---|
67 |
System.out.println( "SLOG-2 file, " + in_filename + " " |
---|
68 |
+ "contains no drawables" ); |
---|
69 |
slog_ins.close(); |
---|
70 |
System.exit( 0 ); |
---|
71 |
} |
---|
72 |
timeframe_root = new TimeBoundingBox( treeroot ); |
---|
73 |
depth_max = treeroot.getTreeNodeID().depth; |
---|
74 |
System.out.println( "TimeWindow = " + timeframe_root |
---|
75 |
+ " @ dmax = " + depth_max ); |
---|
76 |
timeframe_old = new TimeBoundingBox( timeframe_root ); |
---|
77 |
|
---|
78 |
|
---|
79 |
|
---|
80 |
depth = depth_max; |
---|
81 |
timeframe = getTimeWindowFromStdin( timeframe_old ); |
---|
82 |
System.out.println( "TimeWindow = " + timeframe |
---|
83 |
+ " @ d = " + depth ); |
---|
84 |
treetrunk.growInTreeWindow( treeroot, depth, timeframe ); |
---|
85 |
if ( printAll ) |
---|
86 |
System.out.println( treetrunk.toString( timeframe ) ); |
---|
87 |
else |
---|
88 |
System.out.println( treetrunk.toStubString() ); |
---|
89 |
timeframe_old = timeframe; |
---|
90 |
|
---|
91 |
|
---|
92 |
while ( ( timeframe = getTimeWindowFromStdin( timeframe_old ) ) |
---|
93 |
!= null ) { |
---|
94 |
System.out.println( "TimeWindow = " + timeframe |
---|
95 |
+ " @ d = " + depth ); |
---|
96 |
if ( changedPrintAll |
---|
97 |
|| treetrunk.updateTimeWindow( timeframe_old, timeframe ) |
---|
98 |
> TreeTrunk.TIMEBOX_EQUAL ) { |
---|
99 |
if ( printAll ) |
---|
100 |
|
---|
101 |
System.out.println( treetrunk.toString( timeframe ) ); |
---|
102 |
|
---|
103 |
else |
---|
104 |
System.out.println( treetrunk.toStubString() ); |
---|
105 |
timeframe_old = timeframe; |
---|
106 |
changedPrintAll = false; |
---|
107 |
} |
---|
108 |
} |
---|
109 |
|
---|
110 |
slog_ins.close(); |
---|
111 |
} |
---|
112 |
|
---|
113 |
private static String format_msg = "[s|a] [d=I] [[ts=D] [tf=D]] " |
---|
114 |
+ "[-[=D]] [+[=D]] [<[=F]] [>[=F]]"; |
---|
115 |
|
---|
116 |
private static String input_msg = "Interactive Input Options: \n" |
---|
117 |
+ "\t Specification of the Time Frame : \n" |
---|
118 |
+ "\t \t " + format_msg + "\n" |
---|
119 |
+ "\t [s|a], print details " |
---|
120 |
+ " s for stub, a for all drawables.\n" |
---|
121 |
+ "\t [d=I], depth=Integer " |
---|
122 |
+ " Needed only when program starts.\n" |
---|
123 |
+ "\t [ts=D], timeframe_start=Double " |
---|
124 |
+ " Specify Start of TimeFrame.\n" |
---|
125 |
+ "\t [tf=D], timeframe_final=Double " |
---|
126 |
+ " Specify Final of TimeFrame.\n" |
---|
127 |
+ "\t [-[=D]], zoom-out[=Double] " |
---|
128 |
+ " Specify Center of Zoom-Out Frame.\n" |
---|
129 |
+ "\t [+[=D]], zoom-in[=Double] " |
---|
130 |
+ " Specify Center of Zoom-In Frame.\n" |
---|
131 |
+ "\t [<[=F]], scroll-backward[=Frames] " |
---|
132 |
+ " Specify Frames to Scroll Backward.\n" |
---|
133 |
+ "\t [>[=F]], scroll-forward[=Frames] " |
---|
134 |
+ " Specify Frames to Scroll Forward.\n"; |
---|
135 |
|
---|
136 |
private static double zoom_ftr = 2.0d; |
---|
137 |
|
---|
138 |
private static TimeBoundingBox getTimeWindow( |
---|
139 |
final TimeBoundingBox timeframe_old, |
---|
140 |
String argv[] ) |
---|
141 |
{ |
---|
142 |
StringBuffer err_msg = new StringBuffer(); |
---|
143 |
TimeBoundingBox timeframe = new TimeBoundingBox( timeframe_old ); |
---|
144 |
String str; |
---|
145 |
int idx = 0; |
---|
146 |
|
---|
147 |
try { |
---|
148 |
while ( idx < argv.length ) { |
---|
149 |
if ( argv[ idx ].indexOf( '=' ) != -1 ) { |
---|
150 |
if ( argv[ idx ].startsWith( "d=" ) ) { |
---|
151 |
str = argv[ idx ].trim().substring( 2 ); |
---|
152 |
depth = Short.parseShort( str ); |
---|
153 |
err_msg.append( "\n lowest_depth = " + str ); |
---|
154 |
idx++; |
---|
155 |
} |
---|
156 |
else if ( argv[ idx ].startsWith( "ts=" ) ) { |
---|
157 |
str = argv[ idx ].trim().substring( 3 ); |
---|
158 |
timeframe.setEarliestTime( Double.parseDouble( str ) ); |
---|
159 |
err_msg.append( "\n time_frame_start = " + str ); |
---|
160 |
idx++; |
---|
161 |
} |
---|
162 |
else if ( argv[ idx ].startsWith( "tf=" ) ) { |
---|
163 |
str = argv[ idx ].trim().substring( 3 ); |
---|
164 |
timeframe.setLatestTime( Double.parseDouble( str ) ); |
---|
165 |
err_msg.append( "\n time_frame_final = " + str ); |
---|
166 |
idx++; |
---|
167 |
} |
---|
168 |
else if ( argv[ idx ].startsWith( "+=" ) ) { |
---|
169 |
str = argv[ idx ].trim().substring( 2 ); |
---|
170 |
double zoom_ctr = Double.parseDouble( str ); |
---|
171 |
double ctr_span = timeframe_old.getDuration() / 2.0d |
---|
172 |
/ zoom_ftr; |
---|
173 |
timeframe.setEarliestTime( zoom_ctr - ctr_span ); |
---|
174 |
timeframe.setLatestTime( zoom_ctr + ctr_span ); |
---|
175 |
err_msg.append( "\n zoom_in_center = " + str ); |
---|
176 |
idx++; |
---|
177 |
} |
---|
178 |
else if ( argv[ idx ].startsWith( "-=" ) ) { |
---|
179 |
str = argv[ idx ].trim().substring( 2 ); |
---|
180 |
double zoom_ctr = Double.parseDouble( str ); |
---|
181 |
double ctr_span = timeframe_old.getDuration() / 2.0d |
---|
182 |
* zoom_ftr; |
---|
183 |
timeframe.setEarliestTime( zoom_ctr - ctr_span ); |
---|
184 |
timeframe.setLatestTime( zoom_ctr + ctr_span ); |
---|
185 |
err_msg.append( "\n zoom_out_center = " + str ); |
---|
186 |
idx++; |
---|
187 |
} |
---|
188 |
else if ( argv[ idx ].startsWith( ">=" ) ) { |
---|
189 |
str = argv[ idx ].trim().substring( 2 ); |
---|
190 |
double Nwins = Double.parseDouble( str ); |
---|
191 |
double win_span = timeframe_old.getDuration(); |
---|
192 |
timeframe.setEarliestTime( Nwins * win_span |
---|
193 |
+ timeframe_old.getEarliestTime() ); |
---|
194 |
timeframe.setLatestTime( win_span |
---|
195 |
+ timeframe.getEarliestTime() ); |
---|
196 |
err_msg.append( "\n scroll_forward = " + str |
---|
197 |
+ " frames" ); |
---|
198 |
idx++; |
---|
199 |
} |
---|
200 |
else if ( argv[ idx ].startsWith( "<=" ) ) { |
---|
201 |
str = argv[ idx ].trim().substring( 2 ); |
---|
202 |
double Nwins = Double.parseDouble( str ); |
---|
203 |
double win_span = timeframe_old.getDuration(); |
---|
204 |
timeframe.setEarliestTime( - Nwins * win_span |
---|
205 |
+ timeframe_old.getEarliestTime() ); |
---|
206 |
timeframe.setLatestTime( win_span |
---|
207 |
+ timeframe.getEarliestTime() ); |
---|
208 |
err_msg.append( "\n scroll_forward = " + str |
---|
209 |
+ " frames" ); |
---|
210 |
idx++; |
---|
211 |
} |
---|
212 |
else { |
---|
213 |
System.err.println( "Unrecognized option, " |
---|
214 |
+ argv[ idx ] + ", at " |
---|
215 |
+ indexOrderStr( idx+1 ) |
---|
216 |
+ " input argument" ); |
---|
217 |
System.err.flush(); |
---|
218 |
return null; |
---|
219 |
} |
---|
220 |
} |
---|
221 |
else { |
---|
222 |
if ( argv[ idx ].startsWith( "+" ) ) { |
---|
223 |
double zoom_ctr = ( timeframe_old.getEarliestTime() |
---|
224 |
+ timeframe_old.getLatestTime() ) |
---|
225 |
/ 2.0d; |
---|
226 |
double ctr_span = timeframe_old.getDuration() / 2.0d |
---|
227 |
/ zoom_ftr; |
---|
228 |
timeframe.setEarliestTime( zoom_ctr - ctr_span ); |
---|
229 |
timeframe.setLatestTime( zoom_ctr + ctr_span ); |
---|
230 |
err_msg.append( "\n zoom_in_center = " + zoom_ctr ); |
---|
231 |
idx++; |
---|
232 |
} |
---|
233 |
else if ( argv[ idx ].startsWith( "-" ) ) { |
---|
234 |
double zoom_ctr = ( timeframe_old.getEarliestTime() |
---|
235 |
+ timeframe_old.getLatestTime() ) |
---|
236 |
/ 2.0d; |
---|
237 |
double ctr_span = timeframe_old.getDuration() / 2.0d |
---|
238 |
* zoom_ftr; |
---|
239 |
timeframe.setEarliestTime( zoom_ctr - ctr_span ); |
---|
240 |
timeframe.setLatestTime( zoom_ctr + ctr_span ); |
---|
241 |
err_msg.append( "\n zoom_out_center = " + zoom_ctr ); |
---|
242 |
idx++; |
---|
243 |
} |
---|
244 |
else if ( argv[ idx ].startsWith( ">" ) ) { |
---|
245 |
double Nwins = 1.0d; |
---|
246 |
double win_span = timeframe_old.getDuration(); |
---|
247 |
timeframe.setEarliestTime( Nwins * win_span |
---|
248 |
+ timeframe_old.getEarliestTime() ); |
---|
249 |
timeframe.setLatestTime( win_span |
---|
250 |
+ timeframe.getEarliestTime() ); |
---|
251 |
err_msg.append( "\n scroll_forward = " + Nwins |
---|
252 |
+ " frames" ); |
---|
253 |
idx++; |
---|
254 |
} |
---|
255 |
else if ( argv[ idx ].startsWith( "<" ) ) { |
---|
256 |
double Nwins = 1.0d; |
---|
257 |
double win_span = timeframe_old.getDuration(); |
---|
258 |
timeframe.setEarliestTime( - Nwins * win_span |
---|
259 |
+ timeframe_old.getEarliestTime() ); |
---|
260 |
timeframe.setLatestTime( win_span |
---|
261 |
+ timeframe.getEarliestTime() ); |
---|
262 |
err_msg.append( "\n scroll_forward = " + Nwins |
---|
263 |
+ " frames" ); |
---|
264 |
idx++; |
---|
265 |
} |
---|
266 |
else if ( argv[ idx ].startsWith( "s" ) ) { |
---|
267 |
printAll = false; |
---|
268 |
changedPrintAll = true; |
---|
269 |
err_msg.append( "\n print_details = " + printAll ); |
---|
270 |
idx++; |
---|
271 |
} |
---|
272 |
else if ( argv[ idx ].startsWith( "a" ) ) { |
---|
273 |
printAll = true; |
---|
274 |
changedPrintAll = true; |
---|
275 |
err_msg.append( "\n print_details = " + printAll ); |
---|
276 |
idx++; |
---|
277 |
} |
---|
278 |
else { |
---|
279 |
System.err.println( "Unrecognized option, " |
---|
280 |
+ argv[ idx ] + ", at " |
---|
281 |
+ indexOrderStr( idx+1 ) |
---|
282 |
+ " input argument" ); |
---|
283 |
System.err.flush(); |
---|
284 |
return null; |
---|
285 |
} |
---|
286 |
} |
---|
287 |
} |
---|
288 |
} catch ( NumberFormatException numerr ) { |
---|
289 |
if ( err_msg.length() > 0 ) |
---|
290 |
System.err.println( err_msg.toString() ); |
---|
291 |
String idx_order_str = indexOrderStr( idx ); |
---|
292 |
System.err.println( "Error occurs after option " |
---|
293 |
+ argv[ idx-1 ] + ", " + indexOrderStr( idx ) |
---|
294 |
+ " input argument. It needs a number." ); |
---|
295 |
|
---|
296 |
numerr.printStackTrace(); |
---|
297 |
return null; |
---|
298 |
} |
---|
299 |
|
---|
300 |
|
---|
301 |
if ( depth >= 0 && depth <= depth_max ) |
---|
302 |
return timeframe; |
---|
303 |
else { |
---|
304 |
System.err.println( "Invalid TimeWindow!" ); |
---|
305 |
return null; |
---|
306 |
} |
---|
307 |
} |
---|
308 |
|
---|
309 |
private static TimeBoundingBox getTimeWindowFromStdin( |
---|
310 |
final TimeBoundingBox timeframe_old ) |
---|
311 |
{ |
---|
312 |
TimeBoundingBox timeframe; |
---|
313 |
String[] input_args; |
---|
314 |
StringTokenizer tokens; |
---|
315 |
|
---|
316 |
do { |
---|
317 |
System.out.print( "Enter TimeWindow: " + format_msg + " ?\n" ); |
---|
318 |
String input_str; |
---|
319 |
try { |
---|
320 |
input_str = sys_bufrdr.readLine(); |
---|
321 |
} catch ( java.io.IOException ioerr ) { |
---|
322 |
ioerr.printStackTrace(); |
---|
323 |
return null; |
---|
324 |
} |
---|
325 |
tokens = new StringTokenizer( input_str ); |
---|
326 |
input_args = new String[ tokens.countTokens() ]; |
---|
327 |
for ( int idx = 0; tokens.hasMoreTokens(); idx++ ) |
---|
328 |
input_args[ idx ] = tokens.nextToken(); |
---|
329 |
} while ( ( timeframe = getTimeWindow( timeframe_old, input_args ) ) |
---|
330 |
== null ); |
---|
331 |
return timeframe; |
---|
332 |
} |
---|
333 |
|
---|
334 |
private static String stub_msg = "Format of the TreeNodeStub: \n " + |
---|
335 |
"{ TreeNodeID, TreeNodeEndtimes, BlockSize + FilePointer, NumOfDrawables .. }\n" |
---|
336 |
+ "\t TreeNodeID : ID( d, i ) " |
---|
337 |
+ " d is the depth of the TreeNode.\n" |
---|
338 |
+ "\t " |
---|
339 |
+ " d = 0, for LeafNode; \n" |
---|
340 |
+ "\t " |
---|
341 |
+ " d = d_max, for RootNode.\n" |
---|
342 |
+ "\t " |
---|
343 |
+ " i is TreeNode's index(same depth).\n" |
---|
344 |
+ "\t TreeNodeEndtimes " |
---|
345 |
+ " TreeNode's Start and End times.\n"; |
---|
346 |
|
---|
347 |
private static String help_msg = "Usage: java slog2.input.Navigator " |
---|
348 |
+ "[options] slog2_filename.\n" |
---|
349 |
+ "Options: \n" |
---|
350 |
+ "\t [-h|-help|--help] " |
---|
351 |
+ " Display this message.\n" |
---|
352 |
+ "\t [-s|-stub] " |
---|
353 |
+ " Print TreeNode's stub (Default).\n" |
---|
354 |
+ "\t [-a|-all] " |
---|
355 |
+ " Print TreeNode's drawable content.\n" |
---|
356 |
+ "\t [-v|-verbose] " |
---|
357 |
+ " Print detailed diagnostic message.\n" |
---|
358 |
+ "\n" + input_msg |
---|
359 |
+ "\n" + stub_msg; |
---|
360 |
|
---|
361 |
private static void parseCmdLineArgs( String argv[] ) |
---|
362 |
{ |
---|
363 |
String arg_str; |
---|
364 |
StringBuffer err_msg = new StringBuffer(); |
---|
365 |
int idx = 0; |
---|
366 |
|
---|
367 |
while ( idx < argv.length ) { |
---|
368 |
if ( argv[ idx ].startsWith( "-" ) ) { |
---|
369 |
if ( argv[ idx ].equals( "-h" ) |
---|
370 |
|| argv[ idx ].equals( "-help" ) |
---|
371 |
|| argv[ idx ].equals( "--help" ) ) { |
---|
372 |
System.out.println( help_msg ); |
---|
373 |
System.out.flush(); |
---|
374 |
System.exit( 0 ); |
---|
375 |
} |
---|
376 |
else if ( argv[ idx ].equals( "-s" ) |
---|
377 |
|| argv[ idx ].equals( "-stub" ) ) { |
---|
378 |
printAll = false; |
---|
379 |
idx++; |
---|
380 |
} |
---|
381 |
else if ( argv[ idx ].equals( "-a" ) |
---|
382 |
|| argv[ idx ].equals( "-all" ) ) { |
---|
383 |
printAll = true; |
---|
384 |
idx++; |
---|
385 |
} |
---|
386 |
else if ( argv[ idx ].equals( "-v" ) |
---|
387 |
|| argv[ idx ].equals( "-verbose" ) ) { |
---|
388 |
isVerbose = true; |
---|
389 |
idx++; |
---|
390 |
} |
---|
391 |
else { |
---|
392 |
System.err.println( "Unrecognized option, " |
---|
393 |
+ argv[ idx ] + ", at " |
---|
394 |
+ indexOrderStr( idx+1 ) |
---|
395 |
+ " command line argument" ); |
---|
396 |
System.out.flush(); |
---|
397 |
System.exit( 1 ); |
---|
398 |
} |
---|
399 |
} |
---|
400 |
else { |
---|
401 |
in_filename = argv[ idx ]; |
---|
402 |
idx++; |
---|
403 |
} |
---|
404 |
} |
---|
405 |
|
---|
406 |
if ( in_filename == null ) { |
---|
407 |
System.err.println( "The Program needs a SLOG-2 filename as " |
---|
408 |
+ "a command line argument." ); |
---|
409 |
System.err.println( help_msg ); |
---|
410 |
System.exit( 1 ); |
---|
411 |
} |
---|
412 |
} |
---|
413 |
|
---|
414 |
private static String indexOrderStr( int idx ) |
---|
415 |
{ |
---|
416 |
switch (idx) { |
---|
417 |
case 1 : return Integer.toString( idx ) + "st"; |
---|
418 |
case 2 : return Integer.toString( idx ) + "nd"; |
---|
419 |
case 3 : return Integer.toString( idx ) + "rd"; |
---|
420 |
default : return Integer.toString( idx ) + "th"; |
---|
421 |
} |
---|
422 |
} |
---|
423 |
} |
---|