/**
   * Load in the configuration files
   *
   * @throws SimulatorException
   */
  public void loadConfigFiles(JSAPResult parserConfig) throws SimulatorException {
    // load in the simulator config file
    String configFile =
        parserConfig.getString("configPath") + parserConfig.getString("simulatorConfigFile");

    XStream xstream = new XStream();
    xstream.alias("SpaceSettlersConfig", SpaceSettlersConfig.class);
    xstream.alias("HighLevelTeamConfig", HighLevelTeamConfig.class);
    xstream.alias("BaseConfig", BaseConfig.class);
    xstream.alias("AsteroidConfig", AsteroidConfig.class);

    try {
      simConfig = (SpaceSettlersConfig) xstream.fromXML(new File(configFile));
    } catch (Exception e) {
      throw new SimulatorException("Error parsing config file at string " + e.getMessage());
    }

    // load in the ladder config file
    configFile = parserConfig.getString("configPath") + parserConfig.getString("ladderConfigFile");

    xstream = new XStream();
    xstream.alias("LadderConfig", LadderConfig.class);
    xstream.alias("HighLevelTeamConfig", HighLevelTeamConfig.class);

    try {
      ladderConfig = (LadderConfig) xstream.fromXML(new File(configFile));

      ladderConfig.makePlayerNamesUnique();

    } catch (Exception e) {
      throw new SimulatorException("Error parsing config file at string " + e.getMessage());
    }
  }
예제 #2
0
  @Override
  public void interpretArguments(
      final JSAP jsap, final JSAPResult result, final DAVOptions options) {
    timeService = new TimeLoggingService("execute-splits");
    timeService.start();

    super.interpretArguments(jsap, result, options);
    optionalModelIds = parseOptionalModelIdProperties(configurationProperties);

    evaluateStatistics = result.getBoolean("evaluate-statistics");
    if (!evaluateStatistics) {
      System.out.println(
          "Will not evaluate statistics as run proceed. Run restat on results directory to obtain statistics.");
    } else {
      System.out.println("Will evaluate statistics as run proceed.");
    }
    try {
      final String filename = result.getString("splits");
      splitPlanFilename = filename;
      splitPlan.load(filename);
    } catch (Exception e) {
      LOGGER.error("An error occurred reading splits file. " + result.getString("splits"), e);
    }
    // collect keys for parameters that are not required (have default values)
    paramKeysNotRequired = new ObjectArrayList<String>();
    if (!result.userSpecified("seed")) {
      paramKeysNotRequired.add("--seed");
      paramKeysNotRequired.add(Integer.toString(options.randomSeed));
    }

    modelId = ShortHash.shortHash(getOriginalArgs());
    options.modelId = modelId;
    timeService.setModelId(options.modelId);

    final Map<String, String> additionalConditionsMap = new HashMap<String, String>();
    additionalConditionsMap.put("model-id", modelId);

    for (final OptionalModelId optionalModelId : optionalModelIds) {
      final String[] originalArgs1 = expandShortArgs(getOriginalArgs(), jsap);
      final String[] filteredArgs = filterArgs(originalArgs1, optionalModelId);
      final String optionalModelIdValue = ShortHash.shortHash(filteredArgs);

      additionalConditionsMap.put(optionalModelId.columnIdentifier, optionalModelIdValue);
    }

    final String modelConditionsFilename = "model-conditions.txt";
    final Set<String> skipJsapConditions = new HashSet<String>();
    skipJsapConditions.add("model-id");
    skipJsapConditions.add("mode");
    try {
      writeConditions(
          modelConditionsFilename, jsap, result, additionalConditionsMap, skipJsapConditions);
    } catch (IOException e) {
      LOGGER.error("Error writing " + modelConditionsFilename + " file", e);
    }
  }
예제 #3
0
 public Demo(String[] args) throws Exception {
   commandLineOptions = parseParameters(args);
   String nodeType = commandLineOptions.getString("nodeType");
   isMaster = nodeType != null && nodeType.equals("master");
   cfgFile = commandLineOptions.getString("configFile");
   if (cfgFile == null) {
     System.err.println(jsap.getHelp());
     System.exit(1);
   }
 }
예제 #4
0
  protected JSAPResult parseParameters(String[] args) throws Exception {
    jsap = buildCommandLineOptions();

    JSAPResult config = jsap.parse(args);
    if (!config.success() || jsap.messagePrinted()) {
      Iterator<?> messageIterator = config.getErrorMessageIterator();
      while (messageIterator.hasNext()) System.err.println(messageIterator.next());
      System.err.println(jsap.getHelp());
      System.exit(1);
    }

    return config;
  }
  /** Saves out the results to HTML (as specified in the config file) */
  public void printResultsToHTML() {
    String ladderName = parserConfig.getString("configPath") + ladderConfig.getOutputFileName();
    try {
      FileWriter writer = new FileWriter(ladderName, false);

      // write the top of the table and page
      String str = getHTMLHeader();
      writer.write(str);

      // write the results
      str = getHTMLTableResults();
      writer.write(str);

      str = getHTMLStringResults();
      writer.write(str);

      // end the table and page
      str = getHTMLFooter();
      writer.write(str);

      writer.close();
    } catch (IOException e) {
      System.err.println("Error writing ladder.");
      e.printStackTrace();
    }
  }
예제 #6
0
  private static void applyParameters(JSAPResult args, Object o, Class<?> clazz)
      throws ReflectiveOperationException {
    for (Field field : clazz.getFields()) {
      if (Modifier.isPublic(field.getModifiers())) {
        String fieldName = field.getName().toLowerCase();
        if (args.contains(fieldName)) {
          Object parameterValue = args.getObject(fieldName);
          ReflectionUtils.setFieldFromObject(o, field, parameterValue);

          LOGGER.info("Setting " + fieldName + " to '" + parameterValue + "'.");
        } else {
          LOGGER.warn("The field " + fieldName + " has not been set by command line arguments.");
        }
      }
    }
  }
예제 #7
0
  public WakeOnLan(JSAPResult cmdConfig) throws JSAPException, IOException {
    super();

    if (!cmdConfig.success()) {
      printHelpAndExit();
    }

    if (cmdConfig.getBoolean(CMD_HELP)) {
      printHelpAndExit();
    }

    if (cmdConfig.getBoolean(CMD_VERSION)) {
      printVersionAndExit();
    }

    String[] machines = cmdConfig.getStringArray(CMD_ADDRESSES);
    String configPath = cmdConfig.getString(CMD_CONFIG_FILE);
    Configuration config = new Configuration(configPath);

    if (null == machines || 0 == machines.length) {
      showGUI(config);
    } else {
      InetAddress host = cmdConfig.getInetAddress(CMD_INET_ADDRESS);
      int port = cmdConfig.getInt(CMD_PORT);

      wakeup(machines, config, host, port);
    }
  }
예제 #8
0
  public static void main(String[] args) throws IOException, SOMToolboxException {
    // register and parse all options for the
    JSAPResult config =
        OptionFactory.parseResults(
            args,
            OptionFactory.getOptInputVectorFile(true),
            OptionFactory.getOptClassInformationFile(true),
            OptionFactory.getOptOutputFileName(true),
            OptionFactory.getOptOutputDirectory(false));

    String vectorFileName = config.getString("inputVectorFile");
    String classInfoFile = config.getString("classInformationFile");
    String outputDir = config.getString("outputDirectory", ".");
    String outputFileName = config.getString("output");

    SOMLibSparseInputData inputData = new SOMLibSparseInputData(vectorFileName);
    SOMLibClassInformation classInfo = new SOMLibClassInformation(classInfoFile);
    classInfo.removeNotPresentElements(inputData);
    InputDataWriter.writeAsSOMLib(classInfo, outputDir + File.separator + outputFileName);
  }
예제 #9
0
  public Object execute(List<String> argList) {
    JSAPResult argv = parse(argList);
    if (argv == null) {
      return false;
    }

    String modelID = argv.getString(SmOption.MODEL_OPTION);

    ModelDB model = SmOption.getModel(modelID);

    DBJobParam dBJobParam = new DBJobParam();
    dBJobParam.setDstModel(model);

    for (String portName : argv.getStringArray(SmOption.PORT_OPTION)) {
      dBJobParam.setDstPortName(portName);

      new PortDeleteDBJob(dBJobParam).launch();
    }

    return true;
  }
예제 #10
0
  public static void main(String[] args) throws Exception {
    int port = 8077;
    boolean useSsl = false;
    boolean requirePeerAuthentication = false;

    // TODO: add more options for SSL to configure CRT etc.
    SimpleJSAP commandLineOptions =
        new SimpleJSAP(
            "NettyServer",
            "Runs a simple SSL or non-SSL netty server",
            new Parameter[] {
              new FlaggedOption(
                  "port",
                  JSAP.INTEGER_PARSER,
                  "8077",
                  JSAP.NOT_REQUIRED,
                  'p',
                  "port",
                  "listener port"),
              new Switch("ssl", 's', "ssl", "enforces SSL connection"),
              new Switch(
                  "peer", 'p', "peer", "enforces peer certification authentication (in SSL mode)")
            });
    JSAPResult cmd = commandLineOptions.parse(args);
    if (commandLineOptions.messagePrinted()) {
      System.err.println("(use option --help for usage)");
      System.exit(1);
    }

    port = cmd.getInt("port");
    useSsl = cmd.getBoolean("ssl");
    requirePeerAuthentication = cmd.getBoolean("peer");

    if (args.length > 0) {
      port = Integer.parseInt(args[0]);
    }
    System.out.println("Starting an SSL test server on port " + port);
    new SslServer(port, useSsl, requirePeerAuthentication).run();
  }
예제 #11
0
  @Override
  protected void doCommand(JSAPResult parsedArgs)
      throws CommandArgumentsException, CommandOperationException {
    String appName = parsedArgs.getString("app-name");
    String disclaimer = "/*\n* " + parsedArgs.getString("disclaimer") + "\n*/\n\n";
    App app = brjs.app(appName);

    if (!app.dirExists())
      throw new CommandArgumentsException("Could not find application '" + appName + "'", this);

    File destinationZipLocation =
        new File(brjs.storageDir("exported-app").getAbsolutePath() + "/" + appName + ".zip");

    try {
      File temporaryExportDir = FileUtility.createTemporaryDirectory(appName);

      IOFileFilter excludeUserLibraryTestsFilter = createExcludeUserLibsTestsFilter(appName);
      NotFileFilter brjsJarFilter =
          new NotFileFilter(
              new AndFileFilter(new PrefixFileFilter("brjs-"), new SuffixFileFilter(".jar")));
      IOFileFilter combinedFilter =
          new AndFileFilter(new ExcludeDirFileFilter("js-test-driver", "bundles"), brjsJarFilter);

      combinedFilter = new AndFileFilter(combinedFilter, excludeUserLibraryTestsFilter);

      createResourcesFromSdkTemplate(app.dir(), temporaryExportDir, combinedFilter);
      includeDisclaimerInDirectoryClasses(new File(temporaryExportDir, "libs"), disclaimer);
      FileUtility.zipFolder(temporaryExportDir, destinationZipLocation, false);
    } catch (Exception e) {
      throw new CommandOperationException(
          "Could not create application zip for application '" + appName + "'", e);
    }

    logger.info("Successfully exported application '" + appName + "'");
    logger.info(" " + destinationZipLocation.getAbsolutePath());
  }
예제 #12
0
  @Override
  public void process(JSAPResult config) {
    String opt_command = config.getString("command");

    if (opt_command != null && !opt_command.isEmpty()) {
      // Specific command <help <command>>

      System.out.println("Help about '" + opt_command + "'");
      System.out.println();

      MacProtectionCommand entry = MacProtectionCui.getCommands().get(opt_command);
      try {
        JSAP jsap = entry.initCall();

        System.out.println("  NAME");
        System.out.println("\t" + opt_command + " - " + entry.getDescription());
        System.out.println();

        System.out.println("  SYNOPSIS");
        System.out.println("\t" + jsap.getUsage());
        System.out.println();

        System.out.println("  OPTIONS");
        System.out.println(this.implode("\n\t", jsap.getHelp().split("\n"), true));
      } catch (JSAPException ex) {
        System.err.println(ex);
      }
    } else {
      // General command <help>

      for (Map.Entry<String, MacProtectionCommand> entry :
          MacProtectionCui.getCommands().entrySet()) {
        try {
          JSAP jsap = entry.getValue().initCall();
          System.out.print("\t");
          System.out.print(entry.getKey());
          System.out.print("\t");
          System.out.println(entry.getValue().getDescription());
          System.out.print("\t\t");
          System.out.print(jsap.getUsage());
          System.out.println("\n");
        } catch (JSAPException ex) {
          Logger.getLogger(HelpCommand.class.getName()).log(Level.SEVERE, null, ex);
        }
      }
    }
  }
예제 #13
0
  @SuppressWarnings("unchecked")
  private static void writeConditions(
      final String conditionsFilename,
      final JSAP jsap,
      final JSAPResult jsapResult,
      final Map<String, String> additionalConditionsMap,
      final Set<String> skipJsapConditions)
      throws IOException {
    PrintWriter modelConditionsWriter = null;
    try {
      modelConditionsWriter = new PrintWriter(new FileWriter(conditionsFilename, true));
      boolean firstItem = true;

      // Write the additional conditions
      for (final String conditionKey : additionalConditionsMap.keySet()) {
        final String value = additionalConditionsMap.get(conditionKey);
        if (firstItem) {
          firstItem = false;
        } else {
          modelConditionsWriter.print("\t");
        }
        modelConditionsWriter.printf("%s=%s", conditionKey, value);
      }

      // Write the JSAP configuration, as configured for ExecuteSplitsMode
      for (final String id : new IteratorIterable<String>(jsap.getIDMap().idIterator())) {
        if (skipJsapConditions.contains(id)) {
          // Skip some of the conditions
          continue;
        }
        final Parameter paramObj = jsap.getByID(id);
        if (paramObj instanceof Switch) {
          if (jsapResult.getBoolean(id)) {
            if (firstItem) {
              firstItem = false;
            } else {
              modelConditionsWriter.print("\t");
            }
            modelConditionsWriter.printf("%s=enabled", id);
          }
        } else if (paramObj instanceof FlaggedOption) {
          // A flag switch exists. Pass it along.
          final FlaggedOption flagOpt = (FlaggedOption) paramObj;
          if (jsapResult.contains(id)) {
            if (firstItem) {
              firstItem = false;
            } else {
              modelConditionsWriter.print("\t");
            }
            final String stringVal =
                SequenceMode.jsapOptionToConcatenatedString(jsapResult, flagOpt, ',');
            modelConditionsWriter.printf("%s=%s", id, stringVal);
          }
        }
      }
      modelConditionsWriter.println();
    } finally {
      IOUtils.closeQuietly(modelConditionsWriter);
      modelConditionsWriter = null;
    }
  }
예제 #14
0
  @Override
  public JSAPResult parse(String[] args) {
    JSAPResult config;
    config = super.parse(args);

    try {
      /* parameter checking */
      this.messagePrinted = super.messagePrinted();

      boolean existsEnabledRatingAlgorithm =
          config.contains(OPTION_DYNAMIC_LINEAR_REGRESSION)
              || config.contains(OPTION_CONSTANT_LINEAR_REGRESSION)
              || config.getBoolean(OPTION_DIRECT_SCORES);
      /* ****************** ADD NEW RATING SYSTEMS HERE ****************** */

      if (!existsEnabledRatingAlgorithm) {
        messagePrinted = true;
        if (!config.getBoolean(OPTION_HELP)) {
          System.err.println("Error: At least one rating algorithm must be enabled.");
        }
      }

      boolean existsEnabledOutputAlgorithm =
          config.getBoolean(OPTION_CSV_OUTPUT)
              || config.getBoolean(OPTION_GNUPLOT_OUTPUT)
              || config.getBoolean(OPTION_HTML_OUTPUT);
      /* ****************** ADD NEW OUTPUT METHODS HERE ****************** */

      if (!existsEnabledOutputAlgorithm) {
        messagePrinted = true;
        if (!config.getBoolean(OPTION_HELP)) {
          System.err.println("Error: At least one output algorithm must be enabled.");
        }
      }

      if (messagePrinted) {
        // if user hasn't asked for help, "beat him with a clue stick", as the JSAP manual says
        if (!config.getBoolean(OPTION_HELP)) {
          System.err.println();
          System.err.println("Type " + APPLICATION_CALL + " --" + OPTION_HELP + " for help.");
        }
        config.addException(null, new JSAPException("Help message printed."));
      }
    } catch (UnspecifiedParameterException e) {
      if (!config.getBoolean(OPTION_HELP)) {
        System.err.println();
        System.err.println("Type " + APPLICATION_CALL + " --" + OPTION_HELP + " for help.");
      }
      throw e;
    }
    return config;
  }
  protected static Options parseArguments(String[] args) throws JSAPException, IOException {
    JSAP jsap = new JSAP();

    FlaggedOption rosNameSpace =
        new FlaggedOption("namespace")
            .setLongFlag("namespace")
            .setShortFlag(JSAP.NO_SHORTFLAG)
            .setRequired(false)
            .setStringParser(JSAP.STRING_PARSER);
    rosNameSpace.setDefault(DEFAULT_PREFIX);

    FlaggedOption tfPrefix =
        new FlaggedOption("tfPrefix")
            .setLongFlag("tfPrefix")
            .setShortFlag(JSAP.NO_SHORTFLAG)
            .setRequired(false)
            .setStringParser(JSAP.STRING_PARSER);
    tfPrefix.setDefault(DEFAULT_TF_PREFIX);

    FlaggedOption model =
        new FlaggedOption("robotModel")
            .setLongFlag("model")
            .setShortFlag('m')
            .setRequired(false)
            .setStringParser(JSAP.STRING_PARSER);
    model.setDefault(DEFAULT_STRING);

    FlaggedOption location =
        new FlaggedOption("startingLocation")
            .setLongFlag("location")
            .setShortFlag('s')
            .setRequired(false)
            .setStringParser(JSAP.STRING_PARSER);
    location.setDefault(DEFAULT_STRING);

    Switch visualizeSCSSwitch =
        new Switch("disable-visualize").setShortFlag('d').setLongFlag("disable-visualize");
    visualizeSCSSwitch.setHelp("Disable rendering/visualization of Simulation Construction Set");

    Switch requestAutomaticDiagnostic =
        new Switch("requestAutomaticDiagnostic")
            .setLongFlag("requestAutomaticDiagnostic")
            .setShortFlag(JSAP.NO_SHORTFLAG);
    requestAutomaticDiagnostic.setHelp("enable automatic diagnostic routine");

    jsap.registerParameter(model);
    jsap.registerParameter(location);
    jsap.registerParameter(rosNameSpace);
    jsap.registerParameter(tfPrefix);
    jsap.registerParameter(requestAutomaticDiagnostic);
    jsap.registerParameter(visualizeSCSSwitch);
    JSAPResult config = jsap.parse(args);

    Options options = new Options();
    options.robotModel = config.getString(model.getID());
    options.disableViz = config.getBoolean(visualizeSCSSwitch.getID());
    options.startingLocation = config.getString(location.getID());
    options.tfPrefix = config.getString(tfPrefix.getID());
    options.nameSpace = config.getString(rosNameSpace.getID());
    options.runAutomaticDiagnosticRoutine = config.getBoolean(requestAutomaticDiagnostic.getID());
    return options;
  }
예제 #16
0
  public static void execCommandArgs(String[] args) {
    JSAP jsap = new JSAP();
    final String update = "up";
    final String help = "help";
    final String prozess = "tex";
    final String outout = "out";
    final String src = "src";

    try {

      Switch swHelp = new Switch(help);
      swHelp.setShortFlag('h');
      swHelp.setLongFlag("help");
      swHelp.setHelp("Show this.");

      jsap.registerParameter(swHelp);

      Switch swUpdate = new Switch(update);
      swUpdate.setShortFlag('u');
      swUpdate.setLongFlag("update");
      swUpdate.setHelp("Updates from Remote");

      jsap.registerParameter(swUpdate);

      Switch swTex = new Switch(prozess);
      swTex.setShortFlag('t');
      swTex.setLongFlag("transform");
      swTex.setHelp("starts transform to tex");

      jsap.registerParameter(swTex);

      FlaggedOption optOut = new FlaggedOption(outout);
      optOut.setStringParser(JSAP.STRING_PARSER);
      optOut.setDefault(DEFAULT_OUTPUT_FOLDER + "/Songbook.tex");
      optOut.setRequired(false);
      optOut.setShortFlag('o');
      optOut.setLongFlag("output");
      optOut.setHelp("Filename for output");

      jsap.registerParameter(optOut);

      FlaggedOption optSrc = new FlaggedOption(src);
      optSrc.setStringParser(JSAP.STRING_PARSER);
      optSrc.setDefault(DEFAULT_DATA_FOLDER);
      optSrc.setRequired(false);
      optSrc.setShortFlag('s');
      optSrc.setLongFlag("src");
      optSrc.setHelp("input folder");

      jsap.registerParameter(optSrc);

      if (false) {
        final String style = "style";
        FlaggedOption optStyle = new FlaggedOption(style);
        optStyle.setStringParser(JSAP.STRING_PARSER);
        optStyle.setDefault(getDefault());
        optStyle.setRequired(false);
        optStyle.setShortFlag('y');
        optStyle.setLongFlag("style");

        jsap.registerParameter(optStyle);
      }

    } catch (JSAPException e) {
      throw new IllegalStateException(e);
    }
    JSAPResult config = jsap.parse(args);
    if (config.getBoolean("help") || !config.success() || !config.getBoolean(prozess)) {
      // TODO jar name/ package
      System.out.println("Usage: java -jar " + "??????.jar " + jsap.getUsage());

      System.out.println();
      System.out.println(jsap.getHelp());
      System.out.println();
    }

    if (config.getBoolean(update)) {
      updater.update();
    }
    if (config.getBoolean(prozess)) {
      File sourceDir = new File(config.getString(src));
      if (!sourceDir.isDirectory()) {
        System.out.println("No songs. Please update; " + sourceDir.getAbsolutePath());
      } else {
        processSongs(config.getString(src), config.getString(outout));
      }
    }
  }
  public static void main(final String[] arg)
      throws IOException, JSAPException, ConfigurationException, ClassNotFoundException {

    SimpleJSAP jsap =
        new SimpleJSAP(
            WeightedPageRankPowerMethod.class.getName(),
            "Computes PageRank of a graph with given graphBasename using the power method."
                + "The resulting doubles are stored in binary form in rankFile."
                + "\n[STOPPING CRITERION] The computation is stopped as soon as two successive iterates have"
                + "an L2-distance smaller than a given threshold (-t option); in any case no more than a fixed"
                + "number of iterations (-i option) is performed.",
            new Parameter[] {
              new FlaggedOption(
                  "alpha",
                  JSAP.DOUBLE_PARSER,
                  Double.toString(WeightedPageRank.DEFAULT_ALPHA),
                  JSAP.NOT_REQUIRED,
                  'a',
                  "alpha",
                  "Damping factor."),
              new FlaggedOption(
                  "maxIter",
                  JSAP.INTEGER_PARSER,
                  Integer.toString(WeightedPageRank.DEFAULT_MAX_ITER),
                  JSAP.NOT_REQUIRED,
                  'i',
                  "max-iter",
                  "Maximum number of iterations."),
              new FlaggedOption(
                  "threshold",
                  JSAP.DOUBLE_PARSER,
                  Double.toString(WeightedPageRank.DEFAULT_THRESHOLD),
                  JSAP.NOT_REQUIRED,
                  't',
                  "threshold",
                  "Threshold to determine whether to stop."),
              new FlaggedOption(
                  "coeff",
                  JSAP.STRING_PARSER,
                  JSAP.NO_DEFAULT,
                  JSAP.NOT_REQUIRED,
                  'c',
                  "coeff",
                  "Save the k-th coefficient of the Taylor polynomial using this basename."),
              new FlaggedOption(
                      "derivative",
                      JSAP.INTEGER_PARSER,
                      JSAP.NO_DEFAULT,
                      JSAP.NOT_REQUIRED,
                      'd',
                      "derivative",
                      "The order(s) of the the derivative(s) to be computed (>0).")
                  .setAllowMultipleDeclarations(true),
              new FlaggedOption(
                  "preferenceVector",
                  JSAP.STRING_PARSER,
                  JSAP.NO_DEFAULT,
                  JSAP.NOT_REQUIRED,
                  'p',
                  "preference-vector",
                  "A preference vector stored as a vector of binary doubles."),
              new FlaggedOption(
                  "preferenceObject",
                  JSAP.STRING_PARSER,
                  JSAP.NO_DEFAULT,
                  JSAP.NOT_REQUIRED,
                  'P',
                  "preference-object",
                  "A preference vector stored as a serialised DoubleList."),
              new FlaggedOption(
                  "startFilename",
                  JSAP.STRING_PARSER,
                  JSAP.NO_DEFAULT,
                  JSAP.NOT_REQUIRED,
                  '1',
                  "start",
                  "Start vector filename."),
              new FlaggedOption(
                  "buckets",
                  JSAP.STRING_PARSER,
                  JSAP.NO_DEFAULT,
                  JSAP.NOT_REQUIRED,
                  'b',
                  "buckets",
                  "The buckets of the graph; if supplied, buckets will be treated as dangling nodes."),
              new Switch("offline", 'o', "offline", "use loadOffline() to load the graph"),
              new Switch(
                  "strongly",
                  'S',
                  "strongly",
                  "use the preference vector to redistribute the dangling rank."),
              new Switch(
                  "sortedRank",
                  's',
                  "sorted-ranks",
                  "Store the ranks (from highest to lowest) into <rankBasename>-sorted.ranks."),
              new FlaggedOption(
                  "norm",
                  JSAP.STRING_PARSER,
                  WeightedPageRank.Norm.INFTY.toString(),
                  JSAP.NOT_REQUIRED,
                  'n',
                  "norm",
                  "Norm type. Possible values: " + Arrays.toString(WeightedPageRank.Norm.values())),
              new UnflaggedOption(
                  "graphBasename",
                  JSAP.STRING_PARSER,
                  JSAP.NO_DEFAULT,
                  JSAP.REQUIRED,
                  JSAP.NOT_GREEDY,
                  "The basename of the graph."),
              new UnflaggedOption(
                  "rankBasename",
                  JSAP.STRING_PARSER,
                  JSAP.NO_DEFAULT,
                  JSAP.REQUIRED,
                  JSAP.NOT_GREEDY,
                  "The filename where the resulting rank (doubles in binary form) are stored.")
            });

    JSAPResult jsapResult = jsap.parse(arg);
    if (jsap.messagePrinted()) return;

    final boolean offline = jsapResult.getBoolean("offline", false);
    final boolean strongly = jsapResult.getBoolean("strongly", false);
    final boolean sorted = jsapResult.getBoolean("sortedRank", false);
    final int[] order = jsapResult.getIntArray("derivative");
    final String graphBasename = jsapResult.getString("graphBasename");
    final String rankBasename = jsapResult.getString("rankBasename");
    final String buckets = jsapResult.getString("buckets");
    final String startFilename = jsapResult.getString("startFilename", null);
    final String coeffBasename = jsapResult.getString("coeff");
    final String norm = jsapResult.getString("norm");
    final ProgressLogger progressLogger = new ProgressLogger(LOGGER, "nodes");

    ArcLabelledImmutableGraph graph =
        offline
            ? ArcLabelledImmutableGraph.loadOffline(graphBasename, progressLogger)
            : ArcLabelledImmutableGraph.loadSequential(graphBasename, progressLogger);

    DoubleList preference = null;
    String preferenceFilename = null;
    if (jsapResult.userSpecified("preferenceVector"))
      preference =
          DoubleArrayList.wrap(
              BinIO.loadDoubles(preferenceFilename = jsapResult.getString("preferenceVector")));

    if (jsapResult.userSpecified("preferenceObject")) {
      if (jsapResult.userSpecified("preferenceVector"))
        throw new IllegalArgumentException("You cannot specify twice the preference vector");
      preference =
          (DoubleList)
              BinIO.loadObject(preferenceFilename = jsapResult.getString("preferenceObject"));
    }

    if (strongly && preference == null)
      throw new IllegalArgumentException("The 'strongly' option requires a preference vector");

    DoubleList start = null;
    if (startFilename != null) {
      LOGGER.debug("Loading start vector \"" + startFilename + "\"...");
      start = DoubleArrayList.wrap(BinIO.loadDoubles(startFilename));
      LOGGER.debug("done.");
    }

    WeightedPageRankPowerMethod pr = new WeightedPageRankPowerMethod(graph);
    pr.alpha = jsapResult.getDouble("alpha");
    pr.preference = preference;
    pr.buckets = (BitSet) (buckets == null ? null : BinIO.loadObject(buckets));
    pr.stronglyPreferential = strongly;
    pr.start = start;
    pr.norm = WeightedPageRank.Norm.valueOf(norm);
    pr.order = order != null ? order : null;
    pr.coeffBasename = coeffBasename;

    // cycle until we reach maxIter interations or the norm is less than the given threshold
    // (whichever comes first)
    pr.stepUntil(
        or(
            new WeightedPageRank.NormDeltaStoppingCriterion(jsapResult.getDouble("threshold")),
            new WeightedPageRank.IterationNumberStoppingCriterion(jsapResult.getInt("maxIter"))));

    System.err.print("Saving ranks...");
    BinIO.storeDoubles(pr.rank, rankBasename + ".ranks");
    if (pr.numNodes < 100) {
      for (int i = 0; i < pr.rank.length; i++) {
        System.err.println("PageRank[" + i + "]=" + pr.rank[i]);
      }
    }
    Properties prop = pr.buildProperties(graphBasename, preferenceFilename, startFilename);
    prop.save(rankBasename + ".properties");

    if (order != null) {
      System.err.print("Saving derivatives...");
      for (int i = 0; i < order.length; i++)
        BinIO.storeDoubles(pr.derivative[i], rankBasename + ".der-" + order[i]);
    }

    final double[] rank = pr.rank;
    pr = null; // Let us free some memory...
    graph = null;

    if (sorted) {
      System.err.print("Sorting ranks...");
      Arrays.sort(rank);
      final int n = rank.length;
      int i = n / 2;
      double t;
      // Since we need the ranks from highest to lowest, we invert their order.
      while (i-- != 0) {
        t = rank[i];
        rank[i] = rank[n - i - 1];
        rank[n - i - 1] = t;
      }
      System.err.print(" saving sorted ranks...");
      BinIO.storeDoubles(rank, rankBasename + "-sorted.ranks");
      System.err.println(" done.");
    }
  }
예제 #18
0
  public static void main(String[] args) throws JSAPException, IOException, RatingException {
    CommandLineInterface commandLineInterface = new CommandLineInterface();

    JSAPResult jsap = commandLineInterface.parse(args);
    if (!jsap.success()) {
      throw new JSAPException("Command line parsing failed.");
    }

    //////////////////////////////////////////////////////
    //             now configure everything             //
    //////////////////////////////////////////////////////
    Configuration configuration = new Configuration();

    /* configure debug level */
    String debugLevel = jsap.getString(OPTION_DEBUG_LEVEL).toUpperCase();
    Level level = Level.parse(debugLevel);
    configuration.setDebugLevel(level);

    /* configure input dir */
    MatchSetReader matchSetReader =
        new FileMatchSetReader(jsap.getFile(OPTION_INPUT_DIR), configuration);
    configuration.setMatchReader(matchSetReader);

    /* configure output dir */
    Configuration.setOutputDir(jsap.getFile(OPTION_OUTPUT_DIR));

    /* configure previous ratings file */
    configuration.setPreviousRatings(jsap.getFile(OPTION_PREVIOUS_RATINGS));

    /* configure rating algorithms */
    if (jsap.contains(OPTION_DYNAMIC_LINEAR_REGRESSION)) {
      int maxMatchSets = jsap.getInt(OPTION_DYNAMIC_LINEAR_REGRESSION);
      configuration.addRatingSystem(new DynamicLinearRegressionStrategy(maxMatchSets));
    }
    if (jsap.contains(OPTION_CONSTANT_LINEAR_REGRESSION)) {
      double learningRate = jsap.getDouble(OPTION_CONSTANT_LINEAR_REGRESSION);
      configuration.addRatingSystem(new ConstantLinearRegressionStrategy(learningRate));
    }
    if (jsap.getBoolean(OPTION_DIRECT_SCORES)) {
      configuration.addRatingSystem(new DirectScoresStrategy());
    }
    /* ****************** ADD NEW RATING SYSTEMS HERE ****************** */

    /* make ignore list */
    Set<Player> ignorePlayers =
        new IgnorePlayerSet(jsap.getFile(OPTION_INPUT_DIR), configuration.getPlayerSet());

    /* configure output methods */
    for (RatingSystemType type : configuration.getEnabledRatingSystems()) {
      if (jsap.getBoolean(OPTION_CSV_OUTPUT)) {
        configuration.addCSVOutputBuilder(type, ignorePlayers);
      }
      if (jsap.getBoolean(OPTION_GNUPLOT_OUTPUT)) {
        configuration.addGnuplotOutputBuilder(type, ignorePlayers);
      }
      if (jsap.getBoolean(OPTION_HTML_OUTPUT)) {
        configuration.addHtmlOutputBuilder(type, ignorePlayers);
      }
      /* ****************** ADD NEW OUTPUT METHODS HERE ****************** */
    }

    configuration.run();
  }
예제 #19
0
  /**
   * Sample program params for a class 52 with some specified modules Report3 -c:52
   * -f:u:/formality/Greenfield2010/CyczReport3-GeoProbGraph.csv
   * -m:237,246,247,250,252,259,261,265,268
   *
   * @param args
   */
  public static void main(String[] args) {
    Report3 r = new Report3();
    try {
      SimpleJSAP jsap =
          new SimpleJSAP(
              "Report3",
              "Report 3",
              new Parameter[] {
                new QualifiedSwitch(
                    "beginUserId",
                    JSAP.INTEGER_PARSER,
                    JSAP.NO_DEFAULT,
                    JSAP.NOT_REQUIRED,
                    'b',
                    "buid",
                    "Takes a begin Id.  Must be combined with -e --euid"),
                new QualifiedSwitch(
                    "endUserId",
                    JSAP.INTEGER_PARSER,
                    JSAP.NO_DEFAULT,
                    JSAP.NOT_REQUIRED,
                    'e',
                    "euid",
                    "Takes a end Id (must be combined with -b --buid"),
                new QualifiedSwitch(
                    "classId",
                    JSAP.INTEGER_PARSER,
                    JSAP.NO_DEFAULT,
                    JSAP.NOT_REQUIRED,
                    'c',
                    "classId",
                    "Takes the course id."),
                new QualifiedSwitch(
                    "outfile",
                    FileStringParser.getParser(),
                    JSAP.NO_DEFAULT,
                    JSAP.REQUIRED,
                    'f',
                    "file",
                    "The output file (full path) to a comma separated value file."),
                new QualifiedSwitch(
                        "modules",
                        JSAP.INTEGER_PARSER,
                        JSAP.NO_DEFAULT,
                        JSAP.NOT_REQUIRED,
                        'm',
                        "modules",
                        "Module IDs to report on")
                    .setList(true)
                    .setListSeparator(',')
              });
      JSAPResult config = jsap.parse(args);
      if (jsap.messagePrinted()) System.exit(1);

      File f = config.getFile("outfile");
      FileWriter fw = new FileWriter(f);
      Connection conn = null;
      conn = r.getConnection();
      StringBuffer sb = new StringBuffer();
      int beginId = 0, endId = 0, courseId;
      List<List<String>> rows = null;
      int[] modules = config.getIntArray("modules");
      r.modIds = modules;
      if (config.getBoolean("beginUserId") && config.getBoolean("endUserId")) {
        beginId = config.getInt("beginUserId");
        endId = config.getInt("endUserId");
        rows = r.writeUsers(beginId, endId, conn);
      } else if (config.getBoolean("classId")) {
        courseId = config.getInt("classId");
        r.courseName = r.getCourseName(conn, courseId);
        rows = r.writeCourse(courseId, conn);
      }
      r.toCSV(rows, sb);
      System.out.println(sb.toString());
      fw.write(sb.toString());
      fw.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 /**
  * Create a config object based on parsed JSAP.
  *
  * @param jsapResult the parsed JSAP
  */
 public SplitTranscriptsConfig(final JSAPResult jsapResult) {
   inputFile = jsapResult.getString("input");
   outputBase = jsapResult.getString("output");
   maxEntriesPerFile = jsapResult.getInt("max-entries-per-file");
 }
  public Object execute(List<String> argList) throws TclShellException {
    JSAPResult argv = parse(argList);
    if (argv == null) {
      return false;
    }

    if (argv.getBoolean(SmOptionPM.LIST_ATTR_OPTION)) {
      for (PWR_SRC_ATTR e : PWR_SRC_ATTR.values())
        System.out.println(
            " =>  "
                + e.arg
                + " :\n         "
                + e.help
                + (e.allowed == null ? "" : " = " + StringUtilities.join(", ", e.allowed) + ")"));

      return true;
    }

    String modelID = argv.getString(SmOption.MODEL_OPTION);
    ModelDB model =
        (modelID != null) ? ToolsDesign.getModel(modelID) : UnwCore.getDBProject().model();

    if (model == null) {
      Logger.error(ErrorList.DB3.errorName(), ErrorList.DB3.getMessage(modelID));
      throw new TclShellException("");
    }

    PMConfig cfg = PMResourceManager.instance.getPmconfig(model, null);

    String pwrsrcName = argv.getString(SmOptionPM.PWRSRC_OPTION);

    PowerSourceInstance powerSource = PMFindVDomainTools.findPowerSource(cfg, pwrsrcName);

    if (powerSource == null) {
      Logger.error(
          ErrorListPM.PM22.errorName(),
          ErrorListPM.PM22.getMessage(pwrsrcName + "in model " + model.getModelName()));
      throw new TclShellException("");
    }

    if (!argv.contains(SmOption.NAME_VALUE_OPTION)) {
      Logger.error("TCL", "no attribute specified");
      throw new TclShellException("");
    }

    String[] tokens = argv.getStringArray(SmOption.NAME_VALUE_OPTION);
    if (tokens.length % 2 != 0) {
      Logger.error(ErrorListPM.PM_TCL1.errorName(), ErrorListPM.PM_TCL1.getMessage());
      throw new TclShellException("");
    }

    String attrName;
    String attrValue;
    boolean error = false;

    for (int i = 0; i < tokens.length; i += 2) {
      attrName = tokens[i];
      attrValue = tokens[i + 1];

      PWR_SRC_ATTR id = PWR_SRC_ATTR.findAttr(attrName);

      if (id == null) {
        Logger.error(ErrorListPM.PM_TCL3.errorName(), ErrorListPM.PM_TCL3.getMessage(attrName));
        throw new TclShellException("");

      } else {

        CONNECT_DIRECTIVE direct = null;

        switch (id) {
          case CONNECT_IVOLT:
            try {
              direct = CONNECT_DIRECTIVE.valueOf(attrValue);
              powerSource.setConnectInputVoltage(direct);
            } catch (Exception e) {
              error = true;
            }
            break;

          case CONNECT_CTRL:
            try {
              direct = CONNECT_DIRECTIVE.valueOf(attrValue);
              powerSource.setConnectControl(direct);
            } catch (Exception e) {
              error = true;
            }
            break;

          case NAME:
            if (FormatValidation.isAlphaNumeric(attrValue, false)) {
              powerSource.setName(attrValue);
            } else error = true;

          default:
            break;
        }

        if (error) {
          Logger.error(
              ErrorListPM.PM_TCL2.errorName(),
              ErrorListPM.PM_TCL2.getMessage(attrValue + " for attribute " + attrName));
          throw new TclShellException("");
        }
      }
    }

    return true;
  }
예제 #22
0
  public static void main(final String[] arg)
      throws IOException, JSAPException, NoSuchMethodException {

    final SimpleJSAP jsap =
        new SimpleJSAP(
            BloomFilter.class.getName(),
            "Creates a Bloom filter reading from standard input a newline-separated list of terms.",
            new Parameter[] {
              new FlaggedOption(
                  "bufferSize",
                  IntSizeStringParser.getParser(),
                  "64Ki",
                  JSAP.NOT_REQUIRED,
                  'b',
                  "buffer-size",
                  "The size of the I/O buffer used to read terms."),
              new FlaggedOption(
                  "encoding",
                  ForNameStringParser.getParser(Charset.class),
                  "UTF-8",
                  JSAP.NOT_REQUIRED,
                  'e',
                  "encoding",
                  "The term file encoding."),
              new UnflaggedOption(
                  "bloomFilter",
                  JSAP.STRING_PARSER,
                  JSAP.NO_DEFAULT,
                  JSAP.REQUIRED,
                  JSAP.NOT_GREEDY,
                  "The filename for the serialised front-coded list."),
              new UnflaggedOption(
                  "size",
                  JSAP.INTSIZE_PARSER,
                  JSAP.NO_DEFAULT,
                  JSAP.REQUIRED,
                  JSAP.NOT_GREEDY,
                  "The size of the filter (i.e., the expected number of elements in the filter; usually, the number of terms)."),
              new UnflaggedOption(
                  "precision",
                  JSAP.INTEGER_PARSER,
                  JSAP.NO_DEFAULT,
                  JSAP.REQUIRED,
                  JSAP.NOT_GREEDY,
                  "The precision of the filter.")
            });

    JSAPResult jsapResult = jsap.parse(arg);
    if (jsap.messagePrinted()) return;

    final int bufferSize = jsapResult.getInt("bufferSize");
    final String filterName = jsapResult.getString("bloomFilter");
    final Charset encoding = (Charset) jsapResult.getObject("encoding");

    BloomFilter filter = new BloomFilter(jsapResult.getInt("size"), jsapResult.getInt("precision"));
    final ProgressLogger pl = new ProgressLogger();
    pl.itemsName = "terms";
    pl.start("Reading terms...");
    MutableString s = new MutableString();
    FastBufferedReader reader =
        new FastBufferedReader(new InputStreamReader(System.in, encoding), bufferSize);
    while (reader.readLine(s) != null) {
      filter.add(s);
      pl.lightUpdate();
    }
    pl.done();

    BinIO.storeObject(filter, filterName);
  }
예제 #23
0
파일: Main.java 프로젝트: IGBB/pgm
  /** @param args the command line arguments */
  public static void main(String[] args) throws FileNotFoundException, IOException, Exception {
    if (args.length == 0) {
      startGui();
    } else {
      JSAPResult config = parseArguments(args);

      if (config.getString("mode").length() == 0 && config.getBoolean("help")) {
        printHelp();
        System.exit(0);
      }

      // which mode?
      Mode m = Mode.valueOf(config.getString("mode"));
      if (m == Mode.SequestDifference) {
        SequestDifference.main(args);
        return;
      }

      String peptideFilePath = config.getString("peptideFilePath");
      String referenceFilePath = config.getString("referenceFilePath");
      String codeFile = config.getString("codeFile");
      String codeName = config.getString("codeName");

      String outputFilename = config.getString("outputFilename");
      String outputFastaFilename = config.getString("outputFastaFilename");
      String outputGff3Filename = config.getString("outputGff3Filename");

      String beginSpliceSitesPath = config.getString("beginSpliceSitesPath");
      String endSpliceSitesPath = config.getString("endSpliceSitesPath");
      boolean isEukaryote = config.getBoolean("isEukaryote");
      int codons = config.getInt("codons");
      String geneSplicerOutputPath = config.getString("geneSplicerOutputPath");
      boolean tabbedFile = config.getBoolean("tabbedFile");

      GeneSplicerParser geneSplicerParser = null;
      File f = new File(geneSplicerOutputPath);
      if (f.exists()) {
        geneSplicerParser = new GeneSplicerParser(geneSplicerOutputPath);
      }

      List<BioSequence> peptides;
      if (tabbedFile) {
        peptides = TabbedSequenceReader.readSequences(peptideFilePath);
      } else {
        peptides = FASTASequenceReader.readSequences(peptideFilePath);
      }

      MapperMode mode = Mapper.MapperMode.Prokaryote;
      if (isEukaryote) {
        mode = MapperMode.Eukaryote;
      } else if (codons > 0) {
        mode = MapperMode.Codon;
      } else if (geneSplicerParser != null) {
        mode = MapperMode.GeneSplicer;
      }

      StateMachine sm = new StateMachine(peptides);

      CodeTable table = CodeTable.getCodeTable(codeFile, codeName);

      Mapper mapper =
          new Mapper(
              peptides,
              sm,
              referenceFilePath,
              outputFilename,
              outputFastaFilename,
              outputGff3Filename,
              table,
              mode,
              beginSpliceSitesPath,
              endSpliceSitesPath,
              codons,
              geneSplicerParser);

      // perform the mapping
      mapper.map();
    }
  }
예제 #24
0
  public Object execute(List<String> argList) throws TclShellException {

    JSAPResult argv = parse(argList);
    if (argv == null) {
      return false;
    }
    InstanceDB inst;
    boolean hasError = false;
    String modelID = argv.getString(SmOption.MODEL_OPTION);

    ModelDB model = SmOption.getModel(modelID);
    if (model == null) throw new TclShellException("");

    boolean createPort = argv.getBoolean(SmOption.CREATE_PORT_OPTION);
    /*
     * Set Parameters
     */
    String[] instances = argv.getStringArray(SmOption.INSTANCE_OPTION);

    Collection<InstanceDB> instToConnect = new ArrayList<InstanceDB>();

    if (instances == null || instances.length == 0) instToConnect = model.instances();

    for (String instName : instances) {
      inst = model.instance(instName);

      if (inst == null) {
        Logger.error(
            ErrorList.DB15.errorName(),
            ErrorList.DB15.getMessage(instName + " in model " + model.getModelName()));
        hasError = true;
        continue;
      }
      instToConnect.add(inst);
    }

    DBJobParam param = new DBJobParam();
    param.setSrcData(
        ConnectByNameDBJob.CONNECT_INSTANCES,
        instToConnect.toArray(new InstanceDB[instToConnect.size()]));
    param.setSrcData(ConnectByNameDBJob.CREATE_PORT, createPort);
    param.setSrcData(ConnectByNameDBJob.LEAKAGE_PORT, argv.getBoolean(SmOption.LEAKAGE_OPTION));
    param.setSrcData(
        ConnectByNameDBJob.PROPAGATE_UP,
        SmOption.UP_OPTION.equals(argv.getString(SmOption.PROPAGATE_OPTION)));

    if (argv.contains(SmOption.DIRECTION_OPTION)) {
      String dir = argv.getString(SmOption.DIRECTION_OPTION);

      if ("in".equals(dir)) param.setSrcData(ConnectByNameDBJob.DIRECTION, PortInDB.class);
      else if ("out".equals(dir)) param.setSrcData(ConnectByNameDBJob.DIRECTION, PortOutDB.class);
    }

    if (argv.getBoolean(SmOption.HIER_OPTION))
      param.setSrcData(ConnectByNameDBJob.HIER_OPTION, true);

    param.setSrcData(ConnectByNameDBJob.KEEPCONNECTION, !argv.getBoolean(SmOption.FORCE_OPTION));

    DBJob job = new ConnectByNameDBJob(param);
    job.launch();

    if (hasError || !job.isSuccessful())
      throw new TclShellException("error with " + getCommandText());

    return null;
  }
예제 #25
0
  @SuppressWarnings("unchecked")
  public static <T> JsapResultsWithObject<T> constructObject(
      Class<T> clazz, String[] rawArguments, String mainHelp, Parameter[] otherParameters)
      throws JSAPException, IOException, IllegalArgumentException, ReflectiveOperationException {
    Constructor<T> constructor = null;
    for (Constructor<?> constr : clazz.getConstructors()) {
      boolean annotationPresent = constr.isAnnotationPresent(CommandLine.class);
      if (annotationPresent) {
        constructor = (Constructor<T>) constr;
        break;
      }
    }
    if (constructor == null)
      throw new IllegalAccessError(
          "Class " + clazz + " must have a constructor annotated with @CommandLine");

    String[] argNames = constructor.getAnnotation(CommandLine.class).argNames();
    if (argNames.length != constructor.getParameterCount())
      throw new IllegalAnnotationException(
          "Annotation @CommandLine argNames are out of sync with the constructor arguments.");

    boolean[] isSerializedFile = new boolean[argNames.length];

    SimpleJSAP jsap = new SimpleJSAP(clazz.getName(), mainHelp, otherParameters);
    int i = 0;
    for (java.lang.reflect.Parameter x : constructor.getParameters()) {
      Parameter option;
      if (x.getType().equals(boolean.class)) {
        isSerializedFile[i] = false;
        option =
            new Switch(
                argNames[i],
                JSAP.NO_SHORTFLAG,
                argNames[i],
                "Set the value of " + argNames[i] + " for " + clazz.getSimpleName() + " as true.");
      } else {
        StringParser parser;
        String help;
        try {
          parser = getParserFor(x.getType());
          isSerializedFile[i] = false;
          help = "The " + x.getType().getSimpleName() + " value of " + argNames[i];
        } catch (NoJSAPParserForThisTypeException e) {
          parser = JSAP.STRING_PARSER;
          isSerializedFile[i] = true;
          help =
              "A serialized " + x.getType().getSimpleName() + " file to initialize " + argNames[i];
        }
        option = new UnflaggedOption(argNames[i], parser, JSAP.REQUIRED, help);
      }

      jsap.registerParameter(option);

      i++;
    }

    JSAPResult args = jsap.parse(rawArguments);
    if (jsap.messagePrinted()) System.exit(1);

    LOGGER.info("Initializing...");
    Object[] arguments = new Object[argNames.length];
    i = 0;
    for (String argName : argNames) {
      if (isSerializedFile[i]) arguments[i] = SerializationUtils.read(args.getString(argName));
      else arguments[i] = args.getObject(argName);
      i++;
    }
    T object = constructor.newInstance(arguments);
    LOGGER.info("Ready.");

    return new JsapResultsWithObject<T>(args, object);
  }
예제 #26
0
  public static void main(String[] args) throws IOException {
    // register and parse all options
    JSAPResult config = OptionFactory.parseResults(args, OPTIONS);

    String inputFileName = AbstractOptionFactory.getFilePath(config, "input");
    String mappingFile = AbstractOptionFactory.getFilePath(config, "nameMappingFile");
    String outputFileName = AbstractOptionFactory.getFilePath(config, "output");

    String stripFromLabel = config.getString("stripFromString");
    boolean onlyLastPathSegment = config.getBoolean("onlyLastPathSegment");

    boolean gzip = config.getBoolean("gzip");

    MatchMode matchMode = MatchMode.valueOf(config.getString("matchMode"));

    Hashtable<String, String> mapping = readMappingFile(mappingFile);
    String[] keys = null;
    if (matchMode == MatchMode.endsWith) {
      // defining the keys ones as String[] is still slow, but faster than using Hashmap#keySet()
      // often
      keys = mapping.keySet().toArray(new String[mapping.size()]);
    }

    OutputStream out =
        gzip
            ? new GZIPOutputStream(new FileOutputStream(outputFileName))
            : new FileOutputStream(outputFileName);
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));

    HashMap<String, String> headers =
        FileUtils.readSOMLibFileHeaders(
            FileUtils.openFile("Input vector", inputFileName), "input vector");
    int totalVectorCount = Integer.parseInt(headers.get("$XDIM"));

    BufferedReader br = FileUtils.openFile("Input vector", inputFileName);
    String line = null;
    StdErrProgressWriter progress =
        new StdErrProgressWriter(totalVectorCount, "rewriting vector ", 100);
    int written = 0;
    int skipped = 0;
    while ((line = br.readLine()) != null) {
      if (line.startsWith("$")) {
        bw.write(line);
        bw.newLine();
      } else {
        int lastPos = line.lastIndexOf(" ");
        String label = line.substring(lastPos + 1);

        // System.out.println(stripFromLabel);
        // System.out.print(label);
        label = label.replaceAll(stripFromLabel, "");
        if (onlyLastPathSegment && label.contains("/")) {
          label = label.substring(label.lastIndexOf("/") + 1);
        }
        // System.out.println("=>" + label);

        String target = getReplacement(mapping, keys, label, matchMode);
        if (target != null) {
          bw.write(line.substring(0, lastPos));
          bw.write(" " + target);
          bw.newLine();
          written++;
        } else {
          // System.out.println("No label found for " + label);
          skipped++;
        }

        progress.progress();
      }
    }

    br.close();
    bw.close();

    System.out.println(
        "Wrote "
            + written
            + " vectors, skipped "
            + skipped
            + " because no label found in matching file");
  }
예제 #27
0
  public static void main(String[] args) throws Exception {

    final SimpleJSAP jsap =
        new SimpleJSAP(
            Sparsifier.class.getName(),
            "Estimates and sparsifies a propagation model from a set of observations.",
            new Parameter[] {
              new FlaggedOption(
                  "social-network",
                  JSAP.STRING_PARSER,
                  JSAP.NO_DEFAULT,
                  JSAP.REQUIRED,
                  's',
                  "social-network",
                  "The file containing the social network graph"),
              new FlaggedOption(
                  "probabilities",
                  JSAP.STRING_PARSER,
                  JSAP.NO_DEFAULT,
                  JSAP.REQUIRED,
                  'p',
                  "probabilities",
                  "The file containing the propagation probabilities"),
              new FlaggedOption(
                  "candidate-selection-policy",
                  JSAP.STRING_PARSER,
                  CandidateSelectionPolicy.DEFAULT_CANDIDATE_SELECTION_POLICY
                      .getClass()
                      .getSimpleName(),
                  JSAP.REQUIRED,
                  'c',
                  "candidate-selection-policy",
                  "The name of the candidate selection policy"),
              new FlaggedOption(
                  "auxiliary-basename",
                  JSAP.STRING_PARSER,
                  JSAP.NO_DEFAULT,
                  JSAP.NOT_REQUIRED,
                  JSAP.NO_SHORTFLAG,
                  "auxiliary-basename",
                  "The base name for reading a pre-computed auxiliary structure"),
              new FlaggedOption(
                  "input",
                  JSAP.STRING_PARSER,
                  JSAP.NO_DEFAULT,
                  JSAP.REQUIRED,
                  'i',
                  "input",
                  "The file containing the observations"),
              new FlaggedOption(
                  "sparsifier",
                  JSAP.STRING_PARSER,
                  DEFAULT_SPARSIFIER.getSimpleName(),
                  JSAP.NOT_REQUIRED,
                  'f',
                  "sparsifier",
                  "The sparsifier to run, from this list: "
                      + StringUtils.join(Reflection.subClasses(Sparsifier.class), ',')),
              new FlaggedOption(
                  "sparse-model-size",
                  JSAP.INTEGER_PARSER,
                  JSAP.NO_DEFAULT,
                  JSAP.NOT_REQUIRED,
                  'k',
                  "sparse-model-size",
                  "The size of the sparse model"),
              new FlaggedOption(
                  "number-of-chunks",
                  JSAP.INTEGER_PARSER,
                  JSAP.NO_DEFAULT,
                  JSAP.NOT_REQUIRED,
                  'r',
                  "number-of-chunks",
                  "The number of chunks to be sparsified in parralel"),
              new FlaggedOption(
                  "output",
                  JSAP.STRING_PARSER,
                  JSAP.NO_DEFAULT,
                  JSAP.NOT_REQUIRED,
                  'o',
                  "output",
                  "File to dump sparsified model to"),
              new FlaggedOption(
                  "measures-file",
                  JSAP.STRING_PARSER,
                  JSAP.NO_DEFAULT,
                  JSAP.NOT_REQUIRED,
                  'z',
                  "measures-file",
                  "Save measures of partial models to file"),
              new FlaggedOption(
                  "debug-file",
                  JSAP.STRING_PARSER,
                  JSAP.NO_DEFAULT,
                  JSAP.NOT_REQUIRED,
                  'd',
                  "debug-file",
                  "Save debug information to file"),
              new Switch(
                  "with-fraction",
                  'n',
                  "with-fraction",
                  "Disable the computation of the 'fraction of covered propagations'."),
              new Switch(
                  "incremental-likelihood",
                  JSAP.NO_SHORTFLAG,
                  "incremental-likelihood",
                  "Performs incremental computation of likelihood, for sparsifications methods that support this option (faster, experimental)."),
            });

    final JSAPResult jsapResult = jsap.parse(args);
    if (jsap.messagePrinted()) {
      return;
    }

    // Load social network and input
    String snFilename = jsapResult.getString("social-network");
    SocialNetwork socNet = new SocialNetwork(Utilities.getIterator(snFilename));
    String obsFilename = jsapResult.getString("input");
    ObservationsReader observations = new ObservationsReader(obsFilename);

    // Load original model
    ICModel originalModel =
        new ICModel(socNet, Utilities.getIterator(jsapResult.getString("probabilities")));

    // Load candidate selection policy
    String selectionPolicyName = jsapResult.getString("candidate-selection-policy");
    Class<?> selectionPolicyClass =
        Class.forName(
            CandidateSelectionPolicy.class.getPackage().getName() + "." + selectionPolicyName);
    CandidateSelectionPolicy candidateSelectionPolicy =
        (CandidateSelectionPolicy)
            selectionPolicyClass.getConstructor(new Class[] {}).newInstance(new Object[] {});

    // Create sparsifier
    String sparsifierName = jsapResult.getString("sparsifier");
    Class<?> sparsifierClass =
        Class.forName(Sparsifier.class.getPackage().getName() + "." + sparsifierName);
    Sparsifier sparsifier =
        (Sparsifier)
            sparsifierClass
                .getConstructor(new Class[] {originalModel.getClass()})
                .newInstance(new Object[] {originalModel});
    LOGGER.info("Created a " + sparsifier.getClass().getSimpleName());

    // Set sparsifier options
    if (!jsapResult.getBoolean("with-fraction")) {
      sparsifier.disableComputationOfPartialFractionOfPropagations();
      LOGGER.info("Disabled the computation of fraction of propagations");
    }
    if (jsapResult.getBoolean("incremental-likelihood")) {
      if (sparsifier instanceof GreedySparsifier) {
        ((GreedySparsifier) sparsifier).setIncrementalLikelihoodComputation();
        LOGGER.info("Enabled incremental computation of likelihood (faster, experimental)");
      } else {
        LOGGER.warn(
            "This type of sparsifier does not accept the --incrementa-likelihood switch, ignoring");
      }
    }

    if (jsapResult.userSpecified("auxiliary-basename")) {
      // Use existing auxiliary file
      String auxiliaryBasename = jsapResult.getString("auxiliary-basename");
      ICEstimateAuxiliary auxiliary = new ICEstimateAuxiliary(socNet, observations, null);
      LOGGER.info("Loading pre-computed auxiliary variables");
      auxiliary.read(auxiliaryBasename);
      sparsifier.useAuxiliary(auxiliary);
      if (auxiliary
          .getCandidateSelectionPolicy()
          .toSpec()
          .equals(candidateSelectionPolicy.toSpec())) {
        LOGGER.info(
            "Candidate selection policy: " + auxiliary.getCandidateSelectionPolicy().toSpec());
      } else {
        throw new IllegalArgumentException(
            "The candidate selection policies do not match: auxiliary has '"
                + auxiliary.getCandidateSelectionPolicy().toSpec()
                + "', sparsifier has '"
                + candidateSelectionPolicy.toSpec()
                + "'");
      }
    } else {
      // Compute auxiliary variables
      LOGGER.info("Computing auxiliary variables");
      sparsifier.computeAuxiliary(observations, candidateSelectionPolicy);
    }

    int maxSparseSize;
    if (jsapResult.userSpecified("sparse-model-size")) {
      maxSparseSize = jsapResult.getInt("sparse-model-size");
    } else {
      maxSparseSize = originalModel.getProbs().cardinality();
      LOGGER.info(
          "Setting target number of arcs to number of arcs with non-zero probability in the original model");
    }

    // Open debug file
    if (jsapResult.userSpecified("debug-file")) {
      String debugFilename = jsapResult.getString("debug-file");
      LOGGER.info("Will write debug output to " + debugFilename);
      sparsifier.openDebugFile(debugFilename);
    }

    int numOfChunks = 1;
    if (jsapResult.userSpecified("number-of-chunks")) {
      numOfChunks = jsapResult.getInt("number-of-chunks");
    }

    ICModel sparseModel =
        runSparsifier(
            socNet, observations, originalModel, maxSparseSize, sparsifier, numOfChunks, true);

    // Write partial results to file if necessary
    if (jsapResult.userSpecified("measures-file")) {
      for (Measure m : sparsifier.partialResults.keySet()) {
        String logFilename = jsapResult.getString("measures-file");
        switch (m) {
          case LOG_L:
            logFilename = logFilename + ".logL";
            break;
          case FRACTION_OF_PROPAGATIONS:
            logFilename = logFilename + ".frac";
            break;
          default:
            break;
        }
        PrintWriter report =
            new PrintWriter(new BufferedWriter(new FileWriter(new File(logFilename))));
        LOGGER.info("Writing partial " + m.toString() + " results to " + logFilename);
        report.println("#k\t" + m.toString());
        int[] ks = sparsifier.partialResults.get(m).keySet().toArray(new int[] {});
        Arrays.sort(ks);
        for (int k : ks) {
          report.println(k + "\t" + sparsifier.partialResults.get(m).get(k));
        }
        report.close();
      }
    }

    // Dump probabilities
    if (jsapResult.userSpecified("output")) {
      String probsFilename = jsapResult.getString("output");
      PrintWriter pw = Utilities.getPW(probsFilename);
      LOGGER.info("Dumping probabilities to " + probsFilename);
      sparseModel.dumpProbabilities(pw);
      pw.close();
    }

    sparsifier.closeDebugFile();
  }