Articoli

Cartesian Product with Groovy

In what follow a groovy script to perform cartesian product of variables A,B,C,.. each one with differnt values is presented. The script should be compared with the analog Python or C++ programs.

Following the code with all the comments inside.

#!/usr/bin/groovy
/**
 * This is a simple groovy script calculating combinations of variables A,B,C,... written 
 * in a file test.txt in the
 * following format:
 *
 *  ##########################
 *  # Example file test.txt
 *  ##########################
 *
 *  # Variable A
 *  A:1,2,3
 *
 *  # Variable B
 *  B:a,b,c
 *
 *  # Variable C
 *  C:11,12,13,14,15
 *
 *  # Variable D
 *  D:aa,bb,cc
 *
 *  # Variable E
 *  E:a1,b1,c1,d1,d3,d4
 *
 * The variable must be separated from the list of value with a colon ":", while 
 * the list elements must be separated with a coma ","
 *
 * For example
 *   A:1,2,3,4
 * define the variable A that should get the values 1,2,3,4
 *
 * The # char is used to insert comments in the file, where is also possible to 
 * add how many blank lines as you want to improve readability.
 *
 * Programs must be called with:
 *
 *                              groovy ProdCart.groovy -f test.txt
 *
 * The output is printed on standard output and is possible to re-pipe it in file using 
 * standard *ix (or windows) > and >> operators.
 *
 */

// Print out error if input parameters are not correct
// Look at: http://www.javaworld.com/article/2074004/core-java/customizing-groovy-s-clibuilder-usage-statements.html
def cli = new CliBuilder(
        usage: 'ProdCart -f fileName',
        header: '\nAvailable options (use -h for help):\n',
        footer: '\nInformation provided via above options is used to generate printed string.\n')
//import org.apache.commons.cli.Option
cli.with
        {
            h(longOpt: 'help', 'Usage Information', required: false)
            f(longOpt: 'file', 'File Name', args: 1, required: true)
        }
def opt = cli.parse(args)

if (!opt) return
if (opt.h) cli.usage()

// Variable declaration
def inputFile = opt.f
def cont = 0
def arrHeader = []
def arrItems = [ [] , [] ]
def permutazioni = [ [] ]

// Read the string in the input file
File inFile = new File(inputFile)

String[] arrLines = inFile.readLines()
arrLines.each {line ->
    if(!line.startsWith("#") && !line.empty) {
        def arrHeaderItems = line.tokenize(":")
        arrHeader << arrHeaderItems[0]
        arrItems[cont] = arrHeaderItems[1].tokenize(",")
        cont += 1
    }
}

// Load permutation array with arrItems
arrItems.each {item ->
    permutazioni << item
}

// Print combinations with header
println arrHeader.join(",\t")

permutazioni.combinations().each { comb ->
            println comb.toString().replace(" ","").replace("[","").replace("]","").replace(",",",\t")
}

Following the downloadable code and example imput file
Allegati:
FileDescrizioneDimensione del FileDownloadsCreato
Scarica questo file (ProdCart_Groovy.zip)ProdCart with GroovyA simple Groovy script to calculate cartesian product of N variables defined in a file1 kB7102016-06-20 15:12

Aggiungi commento


Codice di sicurezza
Aggiorna

Copyright © 2017 Berta Danilo. Tutti i diritti riservati.
Joomla! è un software libero rilasciato sotto licenza GNU/GPL.