List of usage examples for java.io File exists
public boolean exists()
From source file:gash.router.app.DemoApp.java
/** * sample application (client) use of our messaging service * * @param args//w w w.j av a2 s. c om */ public static void main(String[] args) { if (args.length == 0) { System.out.println("usage: <ip address> <port no>"); System.exit(1); } String ipAddress = args[0]; int port = Integer.parseInt(args[1]); Scanner s = new Scanner(System.in); boolean isExit = false; try { MessageClient mc = new MessageClient(ipAddress, port); DemoApp da = new DemoApp(mc); int choice = 0; while (true) { System.out.println( "Enter your option \n1. WRITE a file. \n2. READ a file. \n3. Update a File. \n4. Delete a File\n 5 Ping(Global)\n 6 Exit"); choice = s.nextInt(); switch (choice) { case 1: { System.out.println("Enter the full pathname of the file to be written "); String currFileName = s.next(); File file = new File(currFileName); if (file.exists()) { ArrayList<ByteString> chunkedFile = da.divideFileChunks(file); String name = file.getName(); int i = 0; String requestId = SupportMessageGenerator.generateRequestID(); for (ByteString string : chunkedFile) { mc.saveFile(name, string, chunkedFile.size(), i++, requestId); } } else { throw new FileNotFoundException("File does not exist in this path "); } } break; case 2: { System.out.println("Enter the file name to be read : "); String currFileName = s.next(); da.sendReadTasks(currFileName); //Thread.sleep(1000 * 100); } break; case 3: { System.out.println("Enter the full pathname of the file to be updated"); String currFileName = s.next(); File file = new File(currFileName); if (file.exists()) { ArrayList<ByteString> chunkedFile = da.divideFileChunks(file); String name = file.getName(); int i = 0; String requestId = SupportMessageGenerator.generateRequestID(); for (ByteString string : chunkedFile) { mc.updateFile(name, string, chunkedFile.size(), i++, requestId); } //Thread.sleep(10 * 1000); } else { throw new FileNotFoundException("File does not exist in this path "); } } break; case 4: System.out.println("Enter the file name to be deleted : "); String currFileName = s.next(); mc.deleteFile(currFileName); //Thread.sleep(1000 * 100); break; case 5: da.ping(1); break; case 6: isExit = true; break; default: break; } if (isExit) break; } } catch (Exception e) { e.printStackTrace(); } finally { CommConnection.getInstance().release(); if (s != null) s.close(); } }
From source file:com.junoyoon.BullsHtml.java
/** * @param args//from w w w. j a va 2s .c o m */ public static void main(String[] args) { final CommandLineParser clp = new DefaultParser(); CommandLine line = null; // parse CLI options try { line = clp.parse(BullsHtml.OPTS, args); } catch (ParseException e) { printMessage("Invalid options"); usage(); return; } String sourceEncoding = BullsHtml.enc; // get encoding option if (line.hasOption("e")) { sourceEncoding = line.getOptionValue("e"); } // print usage if -h if (line.hasOption("h")) { usage(); } if (line.hasOption("v")) { BullsHtml.verbose = true; } String covfile = null; if (line.hasOption("f")) { covfile = line.getOptionValue("f"); if (!new File(covfile).exists()) { printErrorAndExit(covfile + " does not exists"); } } String outputPath = "."; if (line.getArgs().length != 1) { printErrorAndExit("please provide the html output directory"); } outputPath = line.getArgs()[0]; File o = new File(outputPath); if (!o.exists()) { if (!o.mkdirs()) { printErrorAndExit(outputPath + " directory can be not created."); } } else if (!o.isDirectory()) { printErrorAndExit(outputPath + " is not directory."); } else if (!o.canWrite()) { printErrorAndExit(outputPath + " is not writable."); } BullsHtml bullshtml = new BullsHtml(); bullshtml.process(covfile); if (BullsHtml.baseList.isEmpty()) { printErrorAndExit( "No coverage was recorded in cov file. please check if src is compiled with coverage on."); } try { bullshtml.copyResources(outputPath); } catch (Exception e) { printErrorAndExit("The output " + outputPath + " is not writable.", e); } BullsHtml.sourceEncoding = Encoding.getEncoding(sourceEncoding); bullshtml.generateHtml(o); bullshtml.generateCloverXml(o); }
From source file:de.prozesskraft.pkraft.Clone.java
public static void main(String[] args) throws org.apache.commons.cli.ParseException, IOException { /*---------------------------- get options from ini-file//from w w w. ja va 2 s .c o m ----------------------------*/ java.io.File inifile = new java.io.File( WhereAmI.getInstallDirectoryAbsolutePath(Clone.class) + "/" + "../etc/pkraft-clone.ini"); if (inifile.exists()) { try { ini = new Ini(inifile); } catch (InvalidFileFormatException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else { System.err.println("ini file does not exist: " + inifile.getAbsolutePath()); System.exit(1); } /*---------------------------- create boolean options ----------------------------*/ Option ohelp = new Option("help", "print this message"); Option ov = new Option("v", "prints version and build-date"); /*---------------------------- create argument options ----------------------------*/ Option oinstance = OptionBuilder.withArgName("File").hasArg() .withDescription("[mandatory] process you want to clone.") // .isRequired() .create("instance"); Option obasedir = OptionBuilder.withArgName("DIR").hasArg().withDescription( "[optional, default: <basedirOfInstance>] base directory you want to place the root directory of the clone. this directory must exist at call time.") // .isRequired() .create("basedir"); /*---------------------------- create options object ----------------------------*/ Options options = new Options(); options.addOption(ohelp); options.addOption(ov); options.addOption(oinstance); options.addOption(obasedir); /*---------------------------- create the parser ----------------------------*/ CommandLineParser parser = new GnuParser(); // parse the command line arguments commandline = parser.parse(options, args); /*---------------------------- usage/help ----------------------------*/ if (commandline.hasOption("help")) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("clone", options); System.exit(0); } if (commandline.hasOption("v")) { System.out.println("author: alexander.vogel@prozesskraft.de"); System.out.println("version: [% version %]"); System.out.println("date: [% date %]"); System.exit(0); } /*---------------------------- ueberpruefen ob eine schlechte kombination von parametern angegeben wurde ----------------------------*/ if (!(commandline.hasOption("instance"))) { System.err.println("option -instance is mandatory"); exiter(); } /*---------------------------- die lizenz ueberpruefen und ggf abbrechen ----------------------------*/ // check for valid license ArrayList<String> allPortAtHost = new ArrayList<String>(); allPortAtHost.add(ini.get("license-server", "license-server-1")); allPortAtHost.add(ini.get("license-server", "license-server-2")); allPortAtHost.add(ini.get("license-server", "license-server-3")); MyLicense lic = new MyLicense(allPortAtHost, "1", "user-edition", "0.1"); // lizenz-logging ausgeben for (String actLine : (ArrayList<String>) lic.getLog()) { System.err.println(actLine); } // abbruch, wenn lizenz nicht valide if (!lic.isValid()) { System.exit(1); } /*---------------------------- die eigentliche business logic ----------------------------*/ String pathToInstance = commandline.getOptionValue("instance"); java.io.File fileInstance = new java.io.File(pathToInstance); java.io.File fileBaseDir = null; // wenn es nicht vorhanden ist, dann mit fehlermeldung abbrechen if (!fileInstance.exists()) { System.err.println("instance file does not exist."); exiter(); } // testen ob eventuell vorhandene angaben basedir if (commandline.hasOption("basedir")) { fileBaseDir = new java.io.File(commandline.getOptionValue("basedir")); if (!fileBaseDir.exists()) { System.err.println("error: -basedir: directory does not exist"); exiter(); } if (!fileBaseDir.isDirectory()) { System.err.println("error: -basedir: is not a directory"); exiter(); } } // den main-prozess trotzdem nochmal einlesen um subprozesse extrahieren zu koennen Process p1 = new Process(); p1.setInfilebinary(pathToInstance); Process process = p1.readBinary(); // directories setzen, falls angegeben if (fileBaseDir != null) { process.setBaseDir(fileBaseDir.getCanonicalPath()); } // den main-prozess ueber die static function klonen Process clonedProcess = cloneProcess(process, null); // alle steps durchgehen und falls subprocesses existieren auch fuer diese ein cloning durchfuehren for (Step actStep : process.getStep()) { if (actStep.getSubprocess() != null) { Process pDummy = new Process(); pDummy.setInfilebinary(actStep.getAbsdir() + "/process.pmb"); Process processInSubprocess = pDummy.readBinary(); // System.err.println("info: reading process freshly from file: " + actStep.getAbsdir() + "/process.pmb"); if (processInSubprocess != null) { cloneProcess(processInSubprocess, clonedProcess); } } } }
From source file:com.edgenius.wiki.installation.SilenceInstall.java
public static void main(String[] args) throws FileNotFoundException, IOException, NumberFormatException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { if (args.length != 1) { System.out.println("Usage: SilenceInstall silence-install.properites"); System.exit(1);// w w w. j a va 2 s . c o m return; } if (!(new File(args[0]).exists())) { System.out.println("Given silence-install.properites not found: [" + args[0] + "]"); System.exit(1); return; } SilenceInstall silence = new SilenceInstall(); Properties prop = new Properties(); prop.load(new FileInputStream(args[0])); log.info("Silence installation starting... on properties: {}", args[0]); if (Boolean.parseBoolean(getProperty(prop, "data.root.in.system.property"))) { System.setProperty(DataRoot.rootKey, getProperty(prop, "data.root")); log.info("Date root is set to System Properties {}", getProperty(prop, "data.root")); } try { Field[] flds = Class.forName(Server.class.getName()).getDeclaredFields(); for (Field field : flds) { serverFields.add(field.getName()); } flds = Class.forName(GlobalSetting.class.getName()).getDeclaredFields(); for (Field field : flds) { globalFields.add(field.getName()); } flds = Class.forName(Installation.class.getName()).getDeclaredFields(); for (Field field : flds) { installFields.add(field.getName()); } } catch (Exception e) { log.error("Load fields name failed", e); System.exit(1); } boolean succ = silence.createDataRoot(getProperty(prop, "data.root")); if (!succ) { log.error("Unable to complete create data root"); return; } //detect if Install.xml exist and if it is already installed. File installFile = FileUtil.getFile(DataRoot.getDataRoot() + Installation.FILE); if (installFile.exists()) { Installation install = Installation.refreshInstallation(); if (Installation.STATUS_COMPLETED.equals(install.getStatus())) { log.info("GeniusWiki is already installed, exit this installation."); System.exit(0); } } //load Server.properties, Global.xml and Installation.properties Server server = new Server(); Properties serverProp = FileUtil.loadProperties(Server.FILE_DEFAULT); server.syncFrom(serverProp); GlobalSetting global = GlobalSetting .loadGlobalSetting(FileUtil.getFileInputStream(Global.DEFAULT_GLOBAL_XML)); Installation install = Installation.loadDefault(); //sync values from silence-install.properites silence.sync(prop, server, global, install); //install.... succ = silence.setupDataRoot(server, global, install); if (!succ) { log.error("Unable to complete save configuration files to data root"); return; } if (Boolean.parseBoolean(getProperty(prop, "create.database"))) { succ = silence.createDatabase(server, getProperty(prop, "database.root.username"), getProperty(prop, "database.root.password")); if (!succ) { log.error("Unable to complete create database"); return; } } succ = silence.createTable(server); if (!succ) { log.error("Unable to complete create tables"); return; } succ = silence.createAdministrator(server, getProperty(prop, "admin.fullname"), getProperty(prop, "admin.username"), getProperty(prop, "admin.password"), getProperty(prop, "admin.email")); if (!succ) { log.error("Unable to complete create administrator"); return; } log.info("Silence installation completed successfully."); }
From source file:com.termmed.statistics.runner.Runner.java
/** * The main method./*from www. j av a2 s . co m*/ * * @param args the arguments */ public static void main(String[] args) { logger = new ProcessLogger(); if (args.length == 0) { logger.logInfo("Error happened getting params. Params file doesn't exist"); System.exit(0); // }else{ // args=new String[]{"config/complete_nl-edition11320160930.xml"}; } File infoFolder = new File(I_Constants.PROCESS_INFO_FOLDER); if (!infoFolder.exists()) { infoFolder.mkdirs(); } OutputInfoFactory.get().setExecutionId(UUID.randomUUID().toString()); String msg; int posIni; long start = logger.startTime(); File file = new File(args[0]); Config configFile = getConfig(file); OutputInfoFactory.get().setConfig(configFile); System.setProperty("textdb.allow_full_path", "true"); Connection c; try { boolean clean = false; if (args.length >= 2) { for (int i = 1; i < args.length; i++) { logger.logInfo("Arg " + i + ": " + args[i]); if (args[i].toLowerCase().equals("clean")) { clean = true; } } } dataFolder = new File(I_Constants.REPO_FOLDER); if (!dataFolder.exists()) { dataFolder.mkdirs(); } changedDate = true; changedPreviousDate = true; getParams(file); checkDates(); /*******************************/ // changedDate=false; // changedPreviousDate=false; /********************************/ if (clean || changedDate || changedPreviousDate) { logger.logInfo("Removing old data"); removeDBFolder(); removeRepoFolder(); removeReducedFolder(); changedDate = true; changedPreviousDate = true; } Class.forName("org.hsqldb.jdbcDriver"); logger.logInfo("Connecting to DB. This task can take several minutes... wait please."); c = DriverManager.getConnection("jdbc:hsqldb:file:" + I_Constants.DB_FOLDER, "sa", "sa"); initFileProviders(file); // OutputInfoFactory.get().getStatisticProcess().setOutputFolder(I_Constants.STATS_OUTPUT_FOLDER); /*******************************/ // DbSetup dbs=new DbSetup(c); // dbs.recreatePath("org/ihtsdo/statistics/db/setup/storedprocedure"); // dbs=null; /*******************************/ ImportManager impor = new ImportManager(c, file, changedDate, changedPreviousDate); impor.execute(); impor = null; Processor proc = new Processor(c, file); proc.execute(); proc = null; msg = logger.endTime(start); posIni = msg.indexOf("ProcessingTime:") + 16; OutputInfoFactory.get().getStatisticProcess().setTimeTaken(msg.substring(posIni)); // OutputInfoFactory.get().getPatternProcess().setOutputFolder(I_Constants.PATTERN_OUTPUT_FOLDER); long startPattern = logger.startTime(); PatternExecutor pe = new PatternExecutor(file); pe.execute(); pe = null; msg = logger.endTime(startPattern); posIni = msg.indexOf("ProcessingTime:") + 16; OutputInfoFactory.get().getPatternProcess().setTimeTaken(msg.substring(posIni)); OutputInfoFactory.get().setStatus("Complete"); } catch (Exception e) { OutputInfoFactory.get().setStatus("Error: " + e.getMessage() + " - View log for details."); e.printStackTrace(); } msg = logger.endTime(start); posIni = msg.indexOf("ProcessingTime:") + 16; OutputInfoFactory.get().setTimeTaken(msg.substring(posIni)); try { saveInfo(); } catch (IOException e) { e.printStackTrace(); } }
From source file:de.burlov.amazon.s3.dirsync.CLI.java
/** * @param args/*from w w w.ja va2s . c o m*/ */ @SuppressWarnings("static-access") public static void main(String[] args) { Logger.getLogger("").setLevel(Level.OFF); Logger deLogger = Logger.getLogger("de"); deLogger.setLevel(Level.INFO); Handler handler = new ConsoleHandler(); handler.setFormatter(new VerySimpleFormatter()); deLogger.addHandler(handler); deLogger.setUseParentHandlers(false); // if (true) // { // LogFactory.getLog(CLI.class).error("test msg", new Exception("test extception")); // return; // } Options opts = new Options(); OptionGroup gr = new OptionGroup(); /* * Befehlsgruppe initialisieren */ gr = new OptionGroup(); gr.setRequired(true); gr.addOption(OptionBuilder.withArgName("up|down").hasArg() .withDescription("Upload/Download changed or new files").create(CMD_UPDATE)); gr.addOption(OptionBuilder.withArgName("up|down").hasArg() .withDescription("Upload/Download directory snapshot").create(CMD_SNAPSHOT)); gr.addOption(OptionBuilder.withDescription("Delete remote folder").create(CMD_DELETE_DIR)); gr.addOption(OptionBuilder.withDescription("Delete a bucket").create(CMD_DELETE_BUCKET)); gr.addOption(OptionBuilder.create(CMD_HELP)); gr.addOption(OptionBuilder.create(CMD_VERSION)); gr.addOption(OptionBuilder.withDescription("Prints summary for stored data").create(CMD_SUMMARY)); gr.addOption(OptionBuilder.withDescription("Clean up orphaned objekts").create(CMD_CLEANUP)); gr.addOption(OptionBuilder.withDescription("Changes encryption password").withArgName("new password") .hasArg().create(CMD_CHANGE_PASSWORD)); gr.addOption(OptionBuilder.withDescription("Lists all buckets").create(CMD_LIST_BUCKETS)); gr.addOption(OptionBuilder.withDescription("Lists raw objects in a bucket").create(CMD_LIST_BUCKET)); gr.addOption(OptionBuilder.withDescription("Lists files in remote folder").create(CMD_LIST_DIR)); opts.addOptionGroup(gr); /* * Parametergruppe initialisieren */ opts.addOption(OptionBuilder.withArgName("key").isRequired(false).hasArg().withDescription("S3 access key") .create(OPT_S3S_KEY)); opts.addOption(OptionBuilder.withArgName("secret").isRequired(false).hasArg() .withDescription("Secret key for S3 account").create(OPT_S3S_SECRET)); opts.addOption(OptionBuilder.withArgName("bucket").isRequired(false).hasArg().withDescription( "Optional bucket name for storage. If not specified then an unique bucket name will be generated") .create(OPT_BUCKET)); // opts.addOption(OptionBuilder.withArgName("US|EU").hasArg(). // withDescription( // "Where the new bucket should be created. Default US").create( // OPT_LOCATION)); opts.addOption(OptionBuilder.withArgName("path").isRequired(false).hasArg() .withDescription("Local directory path").create(OPT_LOCAL_DIR)); opts.addOption(OptionBuilder.withArgName("name").isRequired(false).hasArg() .withDescription("Remote directory name").create(OPT_REMOTE_DIR)); opts.addOption(OptionBuilder.withArgName("password").isRequired(false).hasArg() .withDescription("Encryption password").create(OPT_ENC_PASSWORD)); opts.addOption(OptionBuilder.withArgName("patterns").hasArgs() .withDescription("Comma separated exclude file patterns like '*.tmp,*/dir/*.tmp'") .create(OPT_EXCLUDE_PATTERNS)); opts.addOption(OptionBuilder.withArgName("patterns").hasArgs().withDescription( "Comma separated include patterns like '*.java'. If not specified, then all files in specified local directory will be included") .create(OPT_INCLUDE_PATTERNS)); if (args.length == 0) { printUsage(opts); return; } CommandLine cmd = null; try { cmd = new GnuParser().parse(opts, args); if (cmd.hasOption(CMD_HELP)) { printUsage(opts); return; } if (cmd.hasOption(CMD_VERSION)) { System.out.println("s3dirsync version " + Version.CURRENT_VERSION); return; } String awsKey = cmd.getOptionValue(OPT_S3S_KEY); String awsSecret = cmd.getOptionValue(OPT_S3S_SECRET); String bucket = cmd.getOptionValue(OPT_BUCKET); String bucketLocation = cmd.getOptionValue(OPT_LOCATION); String localDir = cmd.getOptionValue(OPT_LOCAL_DIR); String remoteDir = cmd.getOptionValue(OPT_REMOTE_DIR); String password = cmd.getOptionValue(OPT_ENC_PASSWORD); String exclude = cmd.getOptionValue(OPT_EXCLUDE_PATTERNS); String include = cmd.getOptionValue(OPT_INCLUDE_PATTERNS); if (StringUtils.isBlank(awsKey) || StringUtils.isBlank(awsSecret)) { System.out.println("S3 account data required"); return; } if (StringUtils.isBlank(bucket)) { bucket = awsKey + ".dirsync"; } if (cmd.hasOption(CMD_DELETE_BUCKET)) { if (StringUtils.isBlank(bucket)) { System.out.println("Bucket name required"); return; } int deleted = S3Utils.deleteBucket(awsKey, awsSecret, bucket); System.out.println("Deleted objects: " + deleted); return; } if (cmd.hasOption(CMD_LIST_BUCKETS)) { for (String str : S3Utils.listBuckets(awsKey, awsSecret)) { System.out.println(str); } return; } if (cmd.hasOption(CMD_LIST_BUCKET)) { if (StringUtils.isBlank(bucket)) { System.out.println("Bucket name required"); return; } for (String str : S3Utils.listObjects(awsKey, awsSecret, bucket)) { System.out.println(str); } return; } if (StringUtils.isBlank(password)) { System.out.println("Encryption password required"); return; } char[] psw = password.toCharArray(); DirSync ds = new DirSync(awsKey, awsSecret, bucket, bucketLocation, psw); ds.setExcludePatterns(parseSubargumenths(exclude)); ds.setIncludePatterns(parseSubargumenths(include)); if (cmd.hasOption(CMD_SUMMARY)) { ds.printStorageSummary(); return; } if (StringUtils.isBlank(remoteDir)) { System.out.println("Remote directory name required"); return; } if (cmd.hasOption(CMD_DELETE_DIR)) { ds.deleteFolder(remoteDir); return; } if (cmd.hasOption(CMD_LIST_DIR)) { Folder folder = ds.getFolder(remoteDir); if (folder == null) { System.out.println("No such folder found: " + remoteDir); return; } for (Map.Entry<String, FileInfo> entry : folder.getIndexData().entrySet()) { System.out.println(entry.getKey() + " (" + FileUtils.byteCountToDisplaySize(entry.getValue().getLength()) + ")"); } return; } if (cmd.hasOption(CMD_CLEANUP)) { ds.cleanUp(); return; } if (cmd.hasOption(CMD_CHANGE_PASSWORD)) { String newPassword = cmd.getOptionValue(CMD_CHANGE_PASSWORD); if (StringUtils.isBlank(newPassword)) { System.out.println("new password required"); return; } char[] chars = newPassword.toCharArray(); ds.changePassword(chars); newPassword = null; Arrays.fill(chars, ' '); return; } if (StringUtils.isBlank(localDir)) { System.out.println(OPT_LOCAL_DIR + " argument required"); return; } String direction = ""; boolean up = false; boolean snapshot = false; if (StringUtils.isNotBlank(cmd.getOptionValue(CMD_UPDATE))) { direction = cmd.getOptionValue(CMD_UPDATE); } else if (StringUtils.isNotBlank(cmd.getOptionValue(CMD_SNAPSHOT))) { direction = cmd.getOptionValue(CMD_SNAPSHOT); snapshot = true; } if (StringUtils.isBlank(direction)) { System.out.println("Operation direction required"); return; } up = StringUtils.equalsIgnoreCase(OPT_UP, direction); File baseDir = new File(localDir); if (!baseDir.exists() && !baseDir.mkdirs()) { System.out.println("Invalid local directory: " + baseDir.getAbsolutePath()); return; } ds.syncFolder(baseDir, remoteDir, up, snapshot); } catch (DirSyncException e) { System.out.println(e.getMessage()); e.printStackTrace(); } catch (ParseException e) { System.out.println(e.getMessage()); printUsage(opts); } catch (Exception e) { e.printStackTrace(System.err); } }
From source file:net.minecraftforge.fml.common.asm.transformers.AccessTransformer.java
public static void main(String[] args) { if (args.length < 2) { System.out.println("Usage: AccessTransformer <JarPath> <MapFile> [MapFile2]... "); System.exit(1);//w w w.ja va 2 s . c o m } boolean hasTransformer = false; AccessTransformer[] trans = new AccessTransformer[args.length - 1]; for (int x = 1; x < args.length; x++) { try { trans[x - 1] = new AccessTransformer(args[x]); hasTransformer = true; } catch (IOException e) { System.out.println("Could not read Transformer Map: " + args[x]); e.printStackTrace(); } } if (!hasTransformer) { System.out.println("Could not find a valid transformer to perform"); System.exit(1); } File orig = new File(args[0]); File temp = new File(args[0] + ".ATBack"); if (!orig.exists() && !temp.exists()) { System.out.println("Could not find target jar: " + orig); System.exit(1); } if (!orig.renameTo(temp)) { System.out.println("Could not rename file: " + orig + " -> " + temp); System.exit(1); } try { processJar(temp, orig, trans); } catch (IOException e) { e.printStackTrace(); System.exit(1); } if (!temp.delete()) { System.out.println("Could not delete temp file: " + temp); } }
From source file:ca.mcgill.networkdynamics.geoinference.evaluation.CrossValidationScorer.java
public static void main(String[] args) throws Exception { if (args.length != 4) { System.out.println("java CVS predictions-dir/ " + "cv-gold-dir/ results.txt error-sample.tsv"); return;/* ww w. j a v a 2 s. c om*/ } File predDir = new File(args[0]); File cvDir = new File(args[1]); TDoubleList errors = new TDoubleArrayList(10_000_000); TLongSet locatedUsers = new TLongHashSet(10_000_000); TLongSet allUsers = new TLongHashSet(10_000_000); TLongObjectMap<TDoubleList> userToErrors = new TLongObjectHashMap<TDoubleList>(); TLongDoubleMap tweetIdToError = new TLongDoubleHashMap(10_000_000); TLongObjectMap<double[]> idToPredLoc = new TLongObjectHashMap<double[]>(); int tweetsSeen = 0; int tweetsLocated = 0; BufferedReader cvBr = new BufferedReader(new FileReader(new File(cvDir, "folds.info.tsv"))); for (String foldLine = null; (foldLine = cvBr.readLine()) != null;) { String[] cols = foldLine.split("\t"); String foldName = cols[0]; System.out.printf("Scoring results for fold %s%n", foldName); File foldPredictionsFile = new File(predDir, foldName + ".results.tsv.gz"); File goldLocFile = new File(cvDir, foldName + ".gold-locations.tsv"); if (foldPredictionsFile.exists()) { BufferedReader br = Files.openGz(foldPredictionsFile); for (String line = null; (line = br.readLine()) != null;) { String[] arr = line.split("\t"); long id = Long.parseLong(arr[0]); idToPredLoc.put(id, new double[] { Double.parseDouble(arr[1]), Double.parseDouble(arr[2]) }); } br.close(); } System.out.printf("loaded predictions for %d tweets; " + "scoring predictions%n", idToPredLoc.size()); BufferedReader br = new BufferedReader(new FileReader(goldLocFile)); for (String line = null; (line = br.readLine()) != null;) { String[] arr = line.split("\t"); long id = Long.parseLong(arr[0]); long userId = Long.parseLong(arr[1]); allUsers.add(userId); tweetsSeen++; double[] predLoc = idToPredLoc.get(id); if (predLoc == null) continue; tweetsLocated++; locatedUsers.add(userId); double[] goldLoc = new double[] { Double.parseDouble(arr[2]), Double.parseDouble(arr[3]) }; double dist = Geometry.getDistance(predLoc, goldLoc); errors.add(dist); tweetIdToError.put(id, dist); TDoubleList userErrors = userToErrors.get(userId); if (userErrors == null) { userErrors = new TDoubleArrayList(); userToErrors.put(userId, userErrors); } userErrors.add(dist); } br.close(); } errors.sort(); System.out.println("Num errors to score: " + errors.size()); double auc = 0; double userCoverage = 0; double tweetCoverage = tweetsLocated / (double) tweetsSeen; double medianMaxUserError = Double.NaN; double medianMedianUserError = Double.NaN; if (errors.size() > 0) { auc = computeAuc(errors); userCoverage = locatedUsers.size() / ((double) allUsers.size()); TDoubleList maxUserErrors = new TDoubleArrayList(locatedUsers.size()); TDoubleList medianUserErrors = new TDoubleArrayList(locatedUsers.size()); for (TDoubleList userErrors : userToErrors.valueCollection()) { userErrors.sort(); maxUserErrors.add(userErrors.get(userErrors.size() - 1)); medianUserErrors.add(userErrors.get(userErrors.size() / 2)); } maxUserErrors.sort(); medianMaxUserError = maxUserErrors.get(maxUserErrors.size() / 2); medianUserErrors.sort(); medianMedianUserError = medianUserErrors.get(medianUserErrors.size() / 2); // Compute CDF int[] errorsPerKm = new int[MAX_KM]; for (int i = 0; i < errors.size(); ++i) { int error = (int) (Math.round(errors.get(i))); errorsPerKm[error]++; } // The accumulated sum of errors per km int[] errorsBelowEachKm = new int[errorsPerKm.length]; for (int i = 0; i < errorsBelowEachKm.length; ++i) { errorsBelowEachKm[i] = errorsPerKm[i]; if (i > 0) errorsBelowEachKm[i] += errorsBelowEachKm[i - 1]; } final double[] cdf = new double[errorsBelowEachKm.length]; double dSize = errors.size(); // to avoid casting all the time for (int i = 0; i < cdf.length; ++i) cdf[i] = errorsBelowEachKm[i] / dSize; } PrintWriter pw = new PrintWriter(new File(args[2])); pw.println("AUC\t" + auc); pw.println("user coverage\t" + userCoverage); pw.println("tweet coverage\t" + tweetCoverage); pw.println("median-max error\t" + medianMaxUserError); pw.close(); // Choose a random sampling of 10K tweets to pass on to the authors // here. PrintWriter errorsPw = new PrintWriter(args[3]); TLongList idsWithErrors = new TLongArrayList(tweetIdToError.keySet()); idsWithErrors.shuffle(new Random()); // Choose the first 10K for (int i = 0, chosen = 0; i < idsWithErrors.size() && chosen < 10_000; ++i) { long id = idsWithErrors.get(i); double[] prediction = idToPredLoc.get(id); double error = tweetIdToError.get(id); errorsPw.println(id + "\t" + error + "\t" + prediction[0] + "\t" + prediction[1]); ++chosen; } errorsPw.close(); }
From source file:de.tudarmstadt.ukp.experiments.dip.wp1.documents.Step7CollectMTurkResults.java
public static void main(String[] args) throws Exception { // input dir - list of xml query containers // /home/user-ukp/research/data/dip/wp1-documents/step4-boiler-plate/ File inputDir = new File(args[0] + "/"); // MTurk result file // output dir File outputDir = new File(args[2]); if (!outputDir.exists()) { outputDir.mkdirs();// w ww . ja va 2s.co m } // Folder with success files File mturkSuccessDir = new File(args[1]); Collection<File> files = FileUtils.listFiles(mturkSuccessDir, new String[] { "result" }, false); if (files.isEmpty()) { throw new IllegalArgumentException("Input folder is empty. " + mturkSuccessDir); } HashMap<String, List<MTurkAnnotation>> mturkAnnotations = new HashMap<>(); // parsing all CSV files for (File mturkCSVResultFile : files) { System.out.println("Parsing " + mturkCSVResultFile.getName()); MTurkOutputReader outputReader = new MTurkOutputReader( new HashSet<>(Arrays.asList("annotation", "workerid")), mturkCSVResultFile); // for fixing broken data input: for each hit, collect all sentence IDs Map<String, SortedSet<String>> hitSentences = new HashMap<>(); // first iteration: collect the sentences for (Map<String, String> record : outputReader) { String hitID = record.get("hitid"); if (!hitSentences.containsKey(hitID)) { hitSentences.put(hitID, new TreeSet<>()); } String relevantSentences = record.get("Answer.relevant_sentences"); String irrelevantSentences = record.get("Answer.irrelevant_sentences"); if (relevantSentences != null) { hitSentences.get(hitID).addAll(Arrays.asList(relevantSentences.split(","))); } if (irrelevantSentences != null) { hitSentences.get(hitID).addAll(Arrays.asList(irrelevantSentences.split(","))); } } // and now second iteration for (Map<String, String> record : outputReader) { String hitID = record.get("hitid"); String annotatorID = record.get("workerid"); String acceptTime = record.get("assignmentaccepttime"); String submitTime = record.get("assignmentsubmittime"); String relevantSentences = record.get("Answer.relevant_sentences"); String irrelevantSentences = record.get("Answer.irrelevant_sentences"); String reject = record.get("reject"); String filename[]; String comment; String clueWeb; String[] relevant = {}; String[] irrelevant = {}; filename = record.get("annotation").split("_"); String fileXml = filename[0]; clueWeb = filename[1].trim(); comment = record.get("Answer.comment"); if (relevantSentences != null) { relevant = relevantSentences.split(","); } if (irrelevantSentences != null) { irrelevant = irrelevantSentences.split(","); } // sanitizing data: if both relevant and irrelevant are empty, that's a bug // we're gonna look up all sentences from this HIT and treat this assignment // as if there were only irrelevant ones if (relevant.length == 0 && irrelevant.length == 0) { SortedSet<String> strings = hitSentences.get(hitID); irrelevant = new String[strings.size()]; strings.toArray(irrelevant); } if (reject != null) { System.out.println(" HIT " + hitID + " annotated by " + annotatorID + " was rejected "); } else { /* // relevant sentences is a comma-delimited string, // this regular expression is rather strange // it must contain digits, it might be that there is only one space or a comma or some other char // digits are the sentence ids. if relevant sentences do not contain digits then it is wrong if (relevantSentences.matches("^\\D*$") && irrelevantSentences.matches("^\\D*$")) { try { throw new IllegalStateException( "No annotations found for HIT " + hitID + " in " + fileXml + " for document " + clueWeb); } catch (IllegalStateException ex) { ex.printStackTrace(); } } */ MTurkAnnotation mturkAnnotation; try { mturkAnnotation = new MTurkAnnotation(hitID, annotatorID, acceptTime, submitTime, comment, clueWeb, relevant, irrelevant); } catch (IllegalArgumentException ex) { throw new IllegalArgumentException("Record: " + record, ex); } List<MTurkAnnotation> listOfAnnotations = mturkAnnotations.get(fileXml); if (listOfAnnotations == null) { listOfAnnotations = new ArrayList<>(); } listOfAnnotations.add(mturkAnnotation); mturkAnnotations.put(fileXml, listOfAnnotations); } } // parser.close(); } // Debugging: output number of HITs of a query System.out.println("Accepted HITs for a query:"); for (Map.Entry e : mturkAnnotations.entrySet()) { ArrayList<MTurkAnnotation> a = (ArrayList<MTurkAnnotation>) e.getValue(); System.out.println(e.getKey() + " " + a.size()); } for (File f : FileUtils.listFiles(inputDir, new String[] { "xml" }, false)) { QueryResultContainer queryResultContainer = QueryResultContainer .fromXML(FileUtils.readFileToString(f, "utf-8")); String fileName = f.getName(); List<MTurkAnnotation> listOfAnnotations = mturkAnnotations.get(fileName); if (listOfAnnotations == null || listOfAnnotations.isEmpty()) { throw new IllegalStateException("No annotations for " + f.getName()); } for (QueryResultContainer.SingleRankedResult rankedResults : queryResultContainer.rankedResults) { for (MTurkAnnotation mtAnnotation : listOfAnnotations) { String clueWeb = mtAnnotation.clueWeb; if (rankedResults.clueWebID.equals(clueWeb)) { List<QueryResultContainer.MTurkRelevanceVote> mTurkRelevanceVotes = rankedResults.mTurkRelevanceVotes; QueryResultContainer.MTurkRelevanceVote relevanceVote = new QueryResultContainer.MTurkRelevanceVote(); String annotatorID = mtAnnotation.annotatorID; String hitID = mtAnnotation.hitID; String acceptTime = mtAnnotation.acceptTime; String submitTime = mtAnnotation.submitTime; String comment = mtAnnotation.comment; String[] relevant = mtAnnotation.relevant; String[] irrelevant = mtAnnotation.irrelevant; relevanceVote.turkID = annotatorID.trim(); relevanceVote.hitID = hitID.trim(); relevanceVote.acceptTime = acceptTime.trim(); relevanceVote.submitTime = submitTime.trim(); relevanceVote.comment = comment != null ? comment.trim() : null; if (relevant.length == 0 && irrelevant.length == 0) { try { throw new IllegalStateException("the length of the annotations is 0" + rankedResults.clueWebID + " for HIT " + relevanceVote.hitID); } catch (IllegalStateException e) { e.printStackTrace(); } } for (String r : relevant) { String sentenceId = r.trim(); if (!sentenceId.isEmpty() && sentenceId.matches("\\d+")) { QueryResultContainer.SingleSentenceRelevanceVote singleSentenceVote = new QueryResultContainer.SingleSentenceRelevanceVote(); singleSentenceVote.sentenceID = sentenceId; singleSentenceVote.relevant = "true"; relevanceVote.singleSentenceRelevanceVotes.add(singleSentenceVote); } } for (String r : irrelevant) { String sentenceId = r.trim(); if (!sentenceId.isEmpty() && sentenceId.matches("\\d+")) { QueryResultContainer.SingleSentenceRelevanceVote singleSentenceVote = new QueryResultContainer.SingleSentenceRelevanceVote(); singleSentenceVote.sentenceID = sentenceId; singleSentenceVote.relevant = "false"; relevanceVote.singleSentenceRelevanceVotes.add(singleSentenceVote); } } mTurkRelevanceVotes.add(relevanceVote); } } } File outputFile = new File(outputDir, f.getName()); FileUtils.writeStringToFile(outputFile, queryResultContainer.toXML(), "utf-8"); System.out.println("Finished " + outputFile); } }
From source file:fr.jayasoft.ivy.Main.java
public static void main(String[] args) throws Exception { Options options = getOptions();/*from w w w .j a v a 2 s . co m*/ CommandLineParser parser = new GnuParser(); try { // parse the command line arguments CommandLine line = parser.parse(options, args); if (line.hasOption("?")) { usage(options); return; } if (line.hasOption("debug")) { Message.init(new DefaultMessageImpl(Message.MSG_DEBUG)); } else if (line.hasOption("verbose")) { Message.init(new DefaultMessageImpl(Message.MSG_VERBOSE)); } else if (line.hasOption("warn")) { Message.init(new DefaultMessageImpl(Message.MSG_WARN)); } else if (line.hasOption("error")) { Message.init(new DefaultMessageImpl(Message.MSG_ERR)); } else { Message.init(new DefaultMessageImpl(Message.MSG_INFO)); } boolean validate = line.hasOption("novalidate") ? false : true; Ivy ivy = new Ivy(); ivy.addAllVariables(System.getProperties()); if (line.hasOption("m2compatible")) { ivy.setVariable("ivy.default.configuration.m2compatible", "true"); } configureURLHandler(line.getOptionValue("realm", null), line.getOptionValue("host", null), line.getOptionValue("username", null), line.getOptionValue("passwd", null)); String confPath = line.getOptionValue("conf", ""); if ("".equals(confPath)) { ivy.configureDefault(); } else { File conffile = new File(confPath); if (!conffile.exists()) { error(options, "ivy configuration file not found: " + conffile); } else if (conffile.isDirectory()) { error(options, "ivy configuration file is not a file: " + conffile); } ivy.configure(conffile); } File cache = new File( ivy.substitute(line.getOptionValue("cache", ivy.getDefaultCache().getAbsolutePath()))); if (!cache.exists()) { cache.mkdirs(); } else if (!cache.isDirectory()) { error(options, cache + " is not a directory"); } String[] confs; if (line.hasOption("confs")) { confs = line.getOptionValues("confs"); } else { confs = new String[] { "*" }; } File ivyfile; if (line.hasOption("dependency")) { String[] dep = line.getOptionValues("dependency"); if (dep.length != 3) { error(options, "dependency should be expressed with exactly 3 arguments: organisation module revision"); } ivyfile = File.createTempFile("ivy", ".xml"); ivyfile.deleteOnExit(); DefaultModuleDescriptor md = DefaultModuleDescriptor .newDefaultInstance(ModuleRevisionId.newInstance(dep[0], dep[1] + "-caller", "working")); DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(md, ModuleRevisionId.newInstance(dep[0], dep[1], dep[2]), false, false, true); for (int i = 0; i < confs.length; i++) { dd.addDependencyConfiguration("default", confs[i]); } md.addDependency(dd); XmlModuleDescriptorWriter.write(md, ivyfile); confs = new String[] { "default" }; } else { ivyfile = new File(ivy.substitute(line.getOptionValue("ivy", "ivy.xml"))); if (!ivyfile.exists()) { error(options, "ivy file not found: " + ivyfile); } else if (ivyfile.isDirectory()) { error(options, "ivy file is not a file: " + ivyfile); } } ResolveReport report = ivy.resolve(ivyfile.toURL(), null, confs, cache, null, validate, false, true, line.hasOption("useOrigin"), null); if (report.hasError()) { System.exit(1); } ModuleDescriptor md = report.getModuleDescriptor(); if (confs.length == 1 && "*".equals(confs[0])) { confs = md.getConfigurationsNames(); } if (line.hasOption("retrieve")) { String retrievePattern = ivy.substitute(line.getOptionValue("retrieve")); if (retrievePattern.indexOf("[") == -1) { retrievePattern = retrievePattern + "/lib/[conf]/[artifact].[ext]"; } ivy.retrieve(md.getModuleRevisionId().getModuleId(), confs, cache, retrievePattern, null, null, line.hasOption("sync"), line.hasOption("useOrigin")); } if (line.hasOption("cachepath")) { outputCachePath(ivy, cache, md, confs, line.getOptionValue("cachepath", "ivycachepath.txt")); } if (line.hasOption("revision")) { ivy.deliver(md.getResolvedModuleRevisionId(), ivy.substitute(line.getOptionValue("revision")), cache, ivy.substitute(line.getOptionValue("deliverto", "ivy-[revision].xml")), ivy.substitute(line.getOptionValue("status", "release")), null, new DefaultPublishingDRResolver(), validate); if (line.hasOption("publish")) { ivy.publish(md.getResolvedModuleRevisionId(), ivy.substitute(line.getOptionValue("revision")), cache, ivy.substitute(line.getOptionValue("publishpattern", "distrib/[type]s/[artifact]-[revision].[ext]")), line.getOptionValue("publish"), ivy.substitute(line.getOptionValue("deliverto", "ivy-[revision].xml")), validate); } } if (line.hasOption("main")) { invoke(ivy, cache, md, confs, line.getOptionValue("main"), line.getOptionValues("args")); } } catch (ParseException exp) { // oops, something went wrong System.err.println("Parsing failed. Reason: " + exp.getMessage()); usage(options); } }