Search in sources :

Example 46 with CommandLine

use of org.apache.commons.cli.CommandLine in project ProPPR by TeamCohen.

the class GradientFinder method main.

public static void main(String[] args) {
    try {
        int inputFiles = Configuration.USE_GROUNDED | Configuration.USE_INIT_PARAMS;
        int outputFiles = Configuration.USE_GRADIENT | Configuration.USE_PARAMS;
        int modules = Configuration.USE_TRAINER | Configuration.USE_SRW | Configuration.USE_SQUASHFUNCTION;
        int constants = Configuration.USE_THREADS | Configuration.USE_EPOCHS | Configuration.USE_FORCE | Configuration.USE_FIXEDWEIGHTS;
        CustomConfiguration c = new CustomConfiguration(args, inputFiles, outputFiles, constants, modules) {

            boolean relax;

            @Override
            protected Option checkOption(Option o) {
                if (PARAMS_FILE_OPTION.equals(o.getLongOpt()) || INIT_PARAMS_FILE_OPTION.equals(o.getLongOpt()))
                    o.setRequired(false);
                return o;
            }

            @Override
            protected void addCustomOptions(Options options, int[] flags) {
                options.addOption(Option.builder().longOpt("relaxFW").desc("Relax fixedWeight rules for gradient computation (used in ProngHorn)").optionalArg(true).build());
            }

            @Override
            protected void retrieveCustomSettings(CommandLine line, int[] flags, Options options) {
                if (groundedFile == null || !groundedFile.exists())
                    usageOptions(options, flags, "Must specify grounded file using --" + Configuration.GROUNDED_FILE_OPTION);
                if (gradientFile == null)
                    usageOptions(options, flags, "Must specify gradient using --" + Configuration.GRADIENT_FILE_OPTION);
                // default to 0 epochs
                if (!options.hasOption("epochs"))
                    this.epochs = 0;
                this.relax = false;
                if (options.hasOption("relaxFW"))
                    this.relax = true;
            }

            @Override
            public Object getCustomSetting(String name) {
                if ("relaxFW".equals(name))
                    return this.relax;
                return null;
            }
        };
        System.out.println(c.toString());
        ParamVector<String, ?> params = null;
        SymbolTable<String> masterFeatures = new SimpleSymbolTable<String>();
        File featureIndex = new File(c.groundedFile.getParent(), c.groundedFile.getName() + Grounder.FEATURE_INDEX_EXTENSION);
        if (featureIndex.exists()) {
            log.info("Reading feature index from " + featureIndex.getName() + "...");
            for (String line : new ParsedFile(featureIndex)) {
                masterFeatures.insert(line.trim());
            }
        }
        if (c.epochs > 0) {
            // train first
            log.info("Training for " + c.epochs + " epochs...");
            params = c.trainer.train(masterFeatures, new ParsedFile(c.groundedFile), new ArrayLearningGraphBuilder(), // create a parameter vector
            c.initParamsFile, c.epochs);
            if (c.paramsFile != null)
                ParamsFile.save(params, c.paramsFile, c);
        } else if (c.initParamsFile != null) {
            params = new SimpleParamVector<String>(Dictionary.load(new ParsedFile(c.initParamsFile)));
        } else if (c.paramsFile != null) {
            params = new SimpleParamVector<String>(Dictionary.load(new ParsedFile(c.paramsFile)));
        } else {
            params = new SimpleParamVector<String>();
        }
        // this lets prongHorn hold external features fixed for training, but still compute their gradient
        if (((Boolean) c.getCustomSetting("relaxFW"))) {
            log.info("Turning off fixedWeight rules");
            c.trainer.setFixedWeightRules(new FixedWeightRules());
        }
        ParamVector<String, ?> batchGradient = c.trainer.findGradient(masterFeatures, new ParsedFile(c.groundedFile), new ArrayLearningGraphBuilder(), params);
        ParamsFile.save(batchGradient, c.gradientFile, c);
    } catch (Throwable t) {
        t.printStackTrace();
        System.exit(-1);
    }
}
Also used : Options(org.apache.commons.cli.Options) CustomConfiguration(edu.cmu.ml.proppr.util.CustomConfiguration) SimpleParamVector(edu.cmu.ml.proppr.util.math.SimpleParamVector) CommandLine(org.apache.commons.cli.CommandLine) SimpleSymbolTable(edu.cmu.ml.proppr.util.SimpleSymbolTable) FixedWeightRules(edu.cmu.ml.proppr.learn.tools.FixedWeightRules) Option(org.apache.commons.cli.Option) ParsedFile(edu.cmu.ml.proppr.util.ParsedFile) File(java.io.File) ParamsFile(edu.cmu.ml.proppr.util.ParamsFile) ParsedFile(edu.cmu.ml.proppr.util.ParsedFile) ArrayLearningGraphBuilder(edu.cmu.ml.proppr.graph.ArrayLearningGraphBuilder)

Example 47 with CommandLine

use of org.apache.commons.cli.CommandLine in project ProPPR by TeamCohen.

the class PropertiesConfigurationTest method testProperties.

@Test
public void testProperties() throws ParseException {
    Options options = new Options();
    options.addOption(OptionBuilder.withLongOpt("force").withDescription("Ignore errors and run anyway").create());
    DefaultParser parser = new DefaultParser();
    Properties props = new Properties();
    //		props.put("--force", true);
    props.setProperty("--force", "true");
    CommandLine line = parser.parse(options, new String[0], props);
    assertTrue(line.hasOption("force"));
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) Properties(java.util.Properties) DefaultParser(org.apache.commons.cli.DefaultParser) Test(org.junit.Test)

Example 48 with CommandLine

use of org.apache.commons.cli.CommandLine in project zm-mailbox by Zimbra.

the class AttributeManagerUtil method main.

public static void main(String[] args) throws IOException, ServiceException {
    CliUtil.toolSetup();
    CommandLine cl = parseArgs(args);
    if (!cl.hasOption('a'))
        usage("no action specified");
    String actionStr = cl.getOptionValue('a');
    Action action = null;
    try {
        action = Action.valueOf(actionStr);
    } catch (IllegalArgumentException iae) {
        usage("unknown action: " + actionStr);
    }
    AttributeManager am = null;
    if (action != Action.dump && action != Action.listAttrs) {
        if (!cl.hasOption('i'))
            usage("no input attribute xml files specified");
        am = new AttributeManager(cl.getOptionValue('i'));
        if (am.hasErrors()) {
            ZimbraLog.misc.warn(am.getErrors());
            System.exit(1);
        }
    }
    OutputStream os = System.out;
    if (cl.hasOption('o')) {
        os = new FileOutputStream(cl.getOptionValue('o'));
    }
    PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(os, "utf8")));
    AttributeManagerUtil amu = new AttributeManagerUtil(am);
    switch(action) {
        case dump:
            LdapProv.getInst().dumpLdapSchema(pw);
            break;
        case generateDefaultCOSLdif:
            amu.generateDefaultCOSLdif(pw);
            break;
        case generateDefaultExternalCOSLdif:
            amu.generateDefaultExternalCOSLdif(pw);
            break;
        case generateGetters:
            amu.generateGetters(cl.getOptionValue('c'), cl.getOptionValue('r'));
            break;
        case generateGlobalConfigLdif:
            amu.generateGlobalConfigLdif(pw);
            break;
        case generateLdapSchema:
            if (!cl.hasOption('t')) {
                usage("no schema template specified");
            }
            amu.generateLdapSchema(pw, cl.getOptionValue('t'));
            break;
        case generateMessageProperties:
            amu.generateMessageProperties(cl.getOptionValue('r'));
            break;
        case generateProvisioning:
            amu.generateProvisioningConstants(cl.getOptionValue('r'));
            break;
        case generateSchemaLdif:
            amu.generateSchemaLdif(pw);
            break;
        case listAttrs:
            amu.listAttrs(pw, cl.getOptionValues('c'), cl.getOptionValues('n'), cl.getOptionValues('f'));
            break;
    }
    pw.close();
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Example 49 with CommandLine

use of org.apache.commons.cli.CommandLine in project zm-mailbox by Zimbra.

the class LdapUpgrade method upgrade.

// public for unittest
public static void upgrade(String[] args) throws ServiceException {
    printer.println("\n\n--------------");
    printer.print(LdapUpgrade.class.getCanonicalName() + " ");
    for (String arg : args) {
        printer.print(arg + " ");
    }
    printer.println();
    printer.println("--------------");
    CliUtil.toolSetup();
    CommandLine cl = null;
    try {
        CommandLineParser parser = new GnuParser();
        Options options = getAllOptions();
        cl = parser.parse(options, args);
        if (cl == null) {
            throw new ParseException("");
        }
    } catch (ParseException e) {
        usage(e, null, null);
        System.exit(1);
    }
    if (cl.hasOption(O_HELP)) {
        usage();
        System.exit(0);
    }
    if (cl.hasOption(O_DESCRIBE)) {
        String bug = cl.getOptionValue(O_DESCRIBE);
        UpgradeOp upgradeOp = getUpgradeOp(bug, printer);
        upgradeOp.describe();
        System.exit(0);
    }
    if (cl.hasOption(O_DESCRIBE_ALL)) {
        for (UpgradeTask task : UpgradeTask.values()) {
            String bug = task.getBug();
            UpgradeOp upgradeOp = getUpgradeOp(bug, printer);
            upgradeOp.describe();
        }
        System.exit(0);
    }
    if (!cl.hasOption(O_BUG)) {
        usage();
        System.exit(1);
    }
    boolean verbose = cl.hasOption(O_VERBOSE);
    String bug = cl.getOptionValue(O_BUG);
    UpgradeOp upgradeOp = getUpgradeOp(bug, printer);
    upgradeOp.setVerbose(verbose);
    if (!upgradeOp.parseCommandLine(cl)) {
        System.exit(1);
    }
    upgradeOp.doUpgrade();
    printer.println("\n\n--------------");
    printer.println("done " + bug);
    printer.println("--------------");
}
Also used : Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) GnuParser(org.apache.commons.cli.GnuParser) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException)

Example 50 with CommandLine

use of org.apache.commons.cli.CommandLine in project zm-mailbox by Zimbra.

the class ZoneInfo2iCalendar method parseArgs.

private static CommandLine parseArgs(String[] args) throws org.apache.commons.cli.ParseException {
    CommandLineParser parser = new GnuParser();
    CommandLine cl = null;
    try {
        cl = parser.parse(sOptions, args);
    } catch (org.apache.commons.cli.ParseException pe) {
        usage(pe.getMessage());
        System.exit(1);
    }
    return cl;
}
Also used : CommandLine(org.apache.commons.cli.CommandLine) GnuParser(org.apache.commons.cli.GnuParser) CommandLineParser(org.apache.commons.cli.CommandLineParser)

Aggregations

CommandLine (org.apache.commons.cli.CommandLine)968 Options (org.apache.commons.cli.Options)560 ParseException (org.apache.commons.cli.ParseException)464 CommandLineParser (org.apache.commons.cli.CommandLineParser)459 HelpFormatter (org.apache.commons.cli.HelpFormatter)259 GnuParser (org.apache.commons.cli.GnuParser)230 DefaultParser (org.apache.commons.cli.DefaultParser)213 PosixParser (org.apache.commons.cli.PosixParser)177 File (java.io.File)165 IOException (java.io.IOException)162 Test (org.junit.Test)144 Option (org.apache.commons.cli.Option)131 ArrayList (java.util.ArrayList)63 Path (org.apache.hadoop.fs.Path)60 BasicParser (org.apache.commons.cli.BasicParser)50 Configuration (org.apache.hadoop.conf.Configuration)41 Configuration (org.apache.flink.configuration.Configuration)37 HashMap (java.util.HashMap)34 List (java.util.List)34 Properties (java.util.Properties)33