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