1 ! Copyright (c) 2012 Anonymous
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors http.client io io.encodings.ascii io.files
4 io.files.temp kernel math math.parser memoize sequences
6 IN: rosetta-code.text-processing.max-licenses
8 ! http://rosettacode.org/wiki/Text_processing/Max_licenses_in_use
10 ! A company currently pays a fixed sum for the use of a
11 ! particular licensed software package. In determining if it has a
12 ! good deal it decides to calculate its maximum use of the
13 ! software from its license management log file.
15 ! Assume the software's licensing daemon faithfully records a
16 ! checkout event when a copy of the software starts and a checkin
17 ! event when the software finishes to its log file. An example of
18 ! checkout and checkin events are:
20 ! License OUT @ 2008/10/03_23:51:05 for job 4974
22 ! License IN @ 2008/10/04_00:18:22 for job 4974
24 ! Save the 10,000 line log file from here into a local file then
25 ! write a program to scan the file extracting both the maximum
26 ! licenses that were out at any time, and the time(s) at which
29 TUPLE: maxlicense max-count current-count times ;
33 : <maxlicense> ( -- max ) -1 0 V{ } clone \ maxlicense boa ; inline
35 : out? ( line -- ? ) [ "OUT" ] dip subseq? ; inline
37 : line-time ( line -- time ) " " split harvest fourth ; inline
39 : update-max-count ( max -- max' )
40 dup [ current-count>> ] [ max-count>> ] bi >
41 [ dup current-count>> >>max-count V{ } clone >>times ] when ;
43 : (inc-current-count) ( max ? -- max' )
44 [ [ 1 + ] change-current-count ]
45 [ [ 1 - ] change-current-count ]
47 update-max-count ; inline
49 : inc-current-count ( max ? time -- max' time )
50 [ (inc-current-count) ] dip ;
52 : current-max-equal? ( max -- max ? )
53 dup [ current-count>> ] [ max-count>> ] bi = ;
55 : update-time ( max time -- max' )
56 [ current-max-equal? ] dip
58 [ [ suffix ] curry change-times ] [ drop ] if ;
60 : split-line ( line -- ? time ) [ out? ] [ line-time ] bi ;
62 : process ( max line -- max ) split-line inc-current-count update-time ;
64 MEMO: mlijobs ( -- lines )
65 "mlijobs.txt" temp-file dup exists? [
66 URL" http://rosettacode.org/resources/mlijobs.txt"
68 ] unless ascii file-lines ;
72 : find-max-licenses ( -- max )
73 mlijobs <maxlicense> [ process ] reduce ;
75 : print-max-licenses ( max -- )
76 [ times>> ] [ max-count>> ] bi
77 "Maximum simultaneous license use is " write
79 " at the following times: " print