Grid-Request

 view release on metacpan or  search on metacpan

lib/Grid/Request/JobFormulator.pm  view on Meta::CPAN

package Grid::Request::JobFormulator;

use strict;
use Grid::Request::HTC;
use Grid::Request::Param;
use Grid::Request::Exceptions;
use Log::Log4perl qw(get_logger);

my $logger = get_logger(__PACKAGE__);

our $VERSION = '0.12';
if ($^W) {
    $VERSION = $VERSION;
}

# The constructor.
sub new {
    my ($class, %args) = @_;
    my $self = bless {}, $class || ref($class);
    return $self;
}

sub formulate {
    $logger->debug("In formulate.");

    # The first argument is the blocksize, or how many invocations each worker
    # will be processing. The second argument is the executable to invoke to
    # invoke, and the remaining arguments are the MW parameters.
    my ($self, $blocksize, $executable, @mw_params) = @_; 

    if (! defined $blocksize || $blocksize <= 0) {
        Grid::Request::Exception->throw("Bad blocksize. Must be >= 1.");
    } else {
        $logger->debug("Blocksize: $blocksize");
    }

    $logger->debug("Number of MW parameters: " . scalar(@mw_params));

    # An array of arrays for all the parameters to invoke
    my @arg_groups = ();
    foreach my $param (@mw_params) {
        $logger->debug(qq|Processing parameter "$param"|);
        my $arg_ref = _get_arguments_for_param($param);
        push (@arg_groups, $arg_ref);
    }

    # Determine the largest list of parameters, then backfill the "PARAM"
    # arguments with that number of values.
    my $min;
    if (scalar @arg_groups) {
        for (my $column = 0; $column < scalar @arg_groups; $column++) {
            if ( ref($arg_groups[$column]) eq "ARRAY") {
                my $column_count =  scalar @{ $arg_groups[$column] }; 
                if (defined $min) {
                    if ( $column_count < $min) {
                        $min = $column_count;
                    }
                } else {
                    $min = $column_count;
                }
            }
        }
    }

    # Now replace the PARAM columns with $min copies of the value.
    if (scalar @arg_groups) {
        for (my $column = 0; $column < scalar @arg_groups; $column++) {
            my $arg = $arg_groups[$column];
            if ( ref(\$arg) eq "SCALAR") {
                my $param = Grid::Request::Param->new($arg);
                my $value = $param->value();
                my @args = ($value) x $min;
                $arg_groups[$column] = \@args;
            }
        }
    }
    
    # Chop all the arg_groups according to the group with the smallest size
    @arg_groups = _limit_arguments(\@arg_groups);

    my @invocations =  _assemble_invocations($blocksize, $executable, \@arg_groups);

    return wantarray ? @invocations : \@invocations;
}



( run in 0.735 second using v1.01-cache-2.11-cpan-39bf76dae61 )