| 1 | package Lab::XPRESS::Sweep::LogBlock; | ||
| 2 | #ABSTRACT: Sweep add-on for matrix logging | ||
| 3 | $Lab::XPRESS::Sweep::LogBlock::VERSION = '3.899'; | ||
| 4 | use v5.20; | ||
| 5 | |||
| 6 | use Role::Tiny; | ||
| 7 | requires qw/LOG write_LOG/; | ||
| 8 | |||
| 9 | |||
| 10 | use Carp; | ||
| 11 | |||
| 12 | use Data::Dumper; | ||
| 13 | |||
| 14 | |||
| 15 | |||
| 16 | sub LogBlock { | ||
| 17 | my $sweep = shift; | ||
| 18 | |||
| 19 | if ( @_ % 2 != 0 ) { | ||
| 20 | croak "expected hash"; | ||
| 21 | } | ||
| 22 | |||
| 23 | my %args = @_; | ||
| 24 | |||
| 25 | my $block = $args{block}; | ||
| 26 | if ( not defined $block ) { | ||
| 27 | croak "missing mandatory parameter 'block'"; | ||
| 28 | } | ||
| 29 | |||
| 30 | my $prefix = $args{prefix}; | ||
| 31 | if ( not defined $prefix ) { | ||
| 32 | $prefix = []; | ||
| 33 | } | ||
| 34 | |||
| 35 | my $prefix_len = @$prefix; | ||
| 36 | |||
| 37 | my $file = $args{datafile}; | ||
| 38 | if ( not defined $file ) { | ||
| 39 | $file = 0; | ||
| 40 | } | ||
| 41 | |||
| 42 | my $num_rows = @$block; | ||
| 43 | my $row_len = @{ $block->[0] }; | ||
| 44 | |||
| 45 | # Extract column header from the DataFile | ||
| 46 | my $datafile = $sweep->{DataFiles}[$file]; | ||
| 47 | my $columns = $datafile->add_column(); | ||
| 48 | my $columns_len = @$columns; | ||
| 49 | |||
| 50 | if ( $row_len + $prefix_len != $columns_len ) { | ||
| 51 | croak "The datafile expects $columns_len columns.\n" | ||
| 52 | . "You only supplied $prefix_len + $row_len columns."; | ||
| 53 | } | ||
| 54 | |||
| 55 | # Write external parameters and block to datafile | ||
| 56 | |||
| 57 | while ( my ( $i, $row ) = each(@$block) ) { | ||
| 58 | my %log; | ||
| 59 | unshift @$row, @$prefix; | ||
| 60 | |||
| 61 | while ( my ( $j, $key ) = each(@$columns) ) { | ||
| 62 | $log{$key} = $row->[$j]; | ||
| 63 | } | ||
| 64 | |||
| 65 | $sweep->LOG( {%log}, $file ); | ||
| 66 | |||
| 67 | if ( $i != $num_rows - 1 ) { | ||
| 68 | |||
| 69 | # the last writeLOG is called in Sweep.pm | ||
| 70 | $sweep->write_LOG(); | ||
| 71 | } | ||
| 72 | |||
| 73 | } | ||
| 74 | |||
| 75 | } | ||
| 76 | |||
| 77 | 1; | ||
| 78 | |||
| 79 | __END__ |