List of usage examples for java.nio.file Files readAllLines
public static List<String> readAllLines(Path path) throws IOException
From source file:com.sumzerotrading.intraday.trading.strategy.ReportGeneratorTest.java
@Test public void testReportGeneratorEndToEnd() throws Exception { StockTicker longTicker = new StockTicker("ABC"); StockTicker shortTicker = new StockTicker("XYZ"); ZonedDateTime entryOrderTime = ZonedDateTime.of(2016, 3, 25, 6, 18, 35, 0, ZoneId.systemDefault()); ZonedDateTime exitOrderTime = ZonedDateTime.of(2016, 3, 25, 6, 19, 35, 0, ZoneId.systemDefault()); String directory = System.getProperty("java.io.tmpdir"); if (!directory.endsWith("/")) { directory += "/"; }/* w ww .ja v a2s .c o m*/ Path reportPath = Paths.get(directory + "report.csv"); Files.deleteIfExists(reportPath); System.out.println("Creating directory at: " + directory); ReportGenerator generator = new ReportGenerator(directory); TradeOrder longEntryOrder = new TradeOrder("123", longTicker, 100, TradeDirection.BUY); longEntryOrder.setFilledPrice(100.00); longEntryOrder.setReference("Intraday-Strategy:guid-123:Entry:Long*"); longEntryOrder.setCurrentStatus(OrderStatus.Status.FILLED); longEntryOrder.setOrderFilledTime(entryOrderTime); generator.orderEvent(new OrderEvent(longEntryOrder, null)); assertFalse(Files.exists(reportPath)); TradeOrder longExitOrder = new TradeOrder("1234", longTicker, 100, TradeDirection.SELL); longExitOrder.setFilledPrice(105.00); longExitOrder.setReference("Intraday-Strategy:guid-123:Exit:Long*"); longExitOrder.setCurrentStatus(OrderStatus.Status.FILLED); longExitOrder.setOrderFilledTime(exitOrderTime); generator.orderEvent(new OrderEvent(longExitOrder, null)); assertTrue(Files.exists(reportPath)); List<String> lines = Files.readAllLines(reportPath); assertEquals(1, lines.size()); String line = lines.get(0); String expected = "2016-03-25T06:18:35,LONG,ABC,100,100.0,0,2016-03-25T06:19:35,105.0,0"; assertEquals(expected, line); generator.orderEvent(new OrderEvent(longEntryOrder, null)); generator.orderEvent(new OrderEvent(longExitOrder, null)); lines = Files.readAllLines(reportPath); assertEquals(2, lines.size()); assertEquals(expected, lines.get(0)); assertEquals(expected, lines.get(1)); }
From source file:org.languagetool.rules.spelling.suggestions.SuggestionChangesTest.java
public void testChanges() throws IOException, InterruptedException { File configFile = new File(System.getProperty("config", "SuggestionChangesTestConfig.json")); ObjectMapper mapper = new ObjectMapper(new JsonFactory().enable(JsonParser.Feature.ALLOW_COMMENTS)); SuggestionChangesTestConfig config = mapper.readValue(configFile, SuggestionChangesTestConfig.class); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss"); String timestamp = dateFormat.format(new Date()); Path loggingFile = Paths.get(config.logDir, String.format("suggestionChangesExperiment_%s.log", timestamp)); Path datasetFile = Paths.get(config.logDir, String.format("suggestionChangesExperiment_%s.csv", timestamp)); BufferedWriter writer = Files.newBufferedWriter(loggingFile); CSVPrinter datasetWriter = new CSVPrinter(Files.newBufferedWriter(datasetFile), CSVFormat.DEFAULT.withEscape('\\')); List<String> datasetHeader = new ArrayList<>( Arrays.asList("sentence", "correction", "covered", "replacement", "dataset_id")); SuggestionsChanges.init(config, writer); writer.write("Evaluation configuration: \n"); String configContent = String.join("\n", Files.readAllLines(configFile.toPath())); writer.write(configContent);/*from w ww .ja v a 2 s .co m*/ writer.write("\nRunning experiments: \n"); int experimentId = 0; for (SuggestionChangesExperiment experiment : SuggestionsChanges.getInstance().getExperiments()) { experimentId++; writer.write(String.format("#%d: %s%n", experimentId, experiment)); datasetHeader.add(String.format("experiment_%d_suggestions", experimentId)); datasetHeader.add(String.format("experiment_%d_metadata", experimentId)); datasetHeader.add(String.format("experiment_%d_suggestions_metadata", experimentId)); } writer.newLine(); datasetWriter.printRecord(datasetHeader); BlockingQueue<SuggestionTestData> tasks = new LinkedBlockingQueue<>(1000); ConcurrentLinkedQueue<Pair<SuggestionTestResultData, String>> results = new ConcurrentLinkedQueue<>(); List<SuggestionTestThread> threads = new ArrayList<>(); for (int i = 0; i < Runtime.getRuntime().availableProcessors(); i++) { SuggestionTestThread worker = new SuggestionTestThread(tasks, results); worker.start(); threads.add(worker); } // Thread for writing results from worker threads into CSV Thread logger = new Thread(() -> { try { long messages = 0; //noinspection InfiniteLoopStatement while (true) { Pair<SuggestionTestResultData, String> message = results.poll(); if (message != null) { writer.write(message.getRight()); SuggestionTestResultData result = message.getLeft(); int datasetId = 1 + config.datasets.indexOf(result.getInput().getDataset()); if (result != null && result.getSuggestions() != null && !result.getSuggestions().isEmpty() && result.getSuggestions().stream() .noneMatch(m -> m.getSuggestedReplacements() == null || m.getSuggestedReplacements().isEmpty())) { List<Object> record = new ArrayList<>(Arrays.asList(result.getInput().getSentence(), result.getInput().getCorrection(), result.getInput().getCovered(), result.getInput().getReplacement(), datasetId)); for (RuleMatch match : result.getSuggestions()) { List<String> suggestions = match.getSuggestedReplacements(); record.add(mapper.writeValueAsString(suggestions)); // features extracted by SuggestionsOrdererFeatureExtractor record.add(mapper.writeValueAsString(match.getFeatures())); List<SortedMap<String, Float>> suggestionsMetadata = new ArrayList<>(); for (SuggestedReplacement replacement : match.getSuggestedReplacementObjects()) { suggestionsMetadata.add(replacement.getFeatures()); } record.add(mapper.writeValueAsString(suggestionsMetadata)); } datasetWriter.printRecord(record); } if (++messages % 1000 == 0) { writer.flush(); System.out.printf("Evaluated %d corrections.%n", messages); } } } } catch (IOException e) { throw new RuntimeException(e); } }); logger.setDaemon(true); logger.start(); // format straight from database dump String[] header = { "id", "sentence", "correction", "language", "rule_id", "suggestion_pos", "accept_language", "country", "region", "created_at", "updated_at", "covered", "replacement", "text_session_id", "client" }; int datasetId = 0; // read data, send to worker threads via queue for (SuggestionChangesDataset dataset : config.datasets) { writer.write(String.format("Evaluating dataset #%d: %s.%n", ++datasetId, dataset)); CSVFormat format = CSVFormat.DEFAULT; if (dataset.type.equals("dump")) { format = format.withEscape('\\').withNullString("\\N").withHeader(header); } else if (dataset.type.equals("artificial")) { format = format.withEscape('\\').withFirstRecordAsHeader(); } try (CSVParser parser = new CSVParser(new FileReader(dataset.path), format)) { for (CSVRecord record : parser) { String lang = record.get("language"); String rule = dataset.type.equals("dump") ? record.get("rule_id") : ""; String covered = record.get("covered"); String replacement = record.get("replacement"); String sentence = record.get("sentence"); String correction = record.isSet("correction") ? record.get("correction") : ""; String acceptLanguage = dataset.type.equals("dump") ? record.get("accept_language") : ""; if (sentence == null || sentence.trim().isEmpty()) { continue; } if (!config.language.equals(lang)) { continue; // TODO handle auto maybe? } if (dataset.type.equals("dump") && !config.rule.equals(rule)) { continue; } // correction column missing in export from doccano; workaround if (dataset.enforceCorrect && !record.isSet("correction")) { throw new IllegalStateException("enforceCorrect in dataset configuration enabled," + " but column 'correction' is not set for entry " + record); } if (dataset.type.equals("dump") && dataset.enforceAcceptLanguage) { if (acceptLanguage != null) { String[] entries = acceptLanguage.split(",", 2); if (entries.length == 2) { String userLanguage = entries[0]; // TODO: what to do with e.g. de-AT,de-DE;... if (!config.language.equals(userLanguage)) { continue; } } } } tasks.put(new SuggestionTestData(lang, sentence, covered, replacement, correction, dataset)); } } } for (Thread t : threads) { t.join(); } logger.join(10000L); logger.interrupt(); datasetWriter.close(); }
From source file:org.apdplat.superword.extract.ChineseSynonymAntonymExtractor.java
/** * ??/*www .j av a 2s . co m*/ * ?? * ?? * ??? * @param path */ private static void filterSameRecord(Path path) { try { AtomicInteger i = new AtomicInteger(); Set<String> set = new HashSet<>(); List<String> list = Files.readAllLines(path).stream().filter(line -> { String[] attr = line.split("\\s+"); String words = Arrays.asList(attr).stream().sorted().collect(Collectors.toList()).toString(); if (set.contains(words)) { i.incrementAndGet(); LOGGER.info("??" + line); return false; } set.add(words); return true; }).sorted().collect(Collectors.toList()); Files.write(path, list); LOGGER.info("??" + i.get()); } catch (Exception e) { LOGGER.error("??", e); } }
From source file:com.sumzerotrading.eod.trading.strategy.ReportGeneratorTest.java
@Test public void testReportGeneratorEndToEnd() throws Exception { StockTicker longTicker = new StockTicker("ABC"); StockTicker shortTicker = new StockTicker("XYZ"); ZonedDateTime entryOrderTime = ZonedDateTime.of(2016, 3, 25, 6, 18, 35, 0, ZoneId.systemDefault()); ZonedDateTime exitOrderTime = ZonedDateTime.of(2016, 3, 25, 6, 19, 35, 0, ZoneId.systemDefault()); String directory = System.getProperty("java.io.tmpdir"); if (!directory.endsWith("/")) { directory += "/"; }//from w ww. ja va 2s .com Path reportPath = Paths.get(directory + "report.csv"); Files.deleteIfExists(reportPath); System.out.println("Creating directory at: " + directory); ReportGenerator generator = new ReportGenerator(directory); TradeOrder longEntryOrder = new TradeOrder("123", longTicker, 100, TradeDirection.BUY); longEntryOrder.setFilledPrice(100.00); longEntryOrder.setReference("EOD-Pair-Strategy:guid-123:Entry:Long*"); longEntryOrder.setCurrentStatus(OrderStatus.Status.FILLED); longEntryOrder.setOrderFilledTime(entryOrderTime); TradeOrder shortEntryOrder = new TradeOrder("234", shortTicker, 50, TradeDirection.SELL); shortEntryOrder.setFilledPrice(50.00); shortEntryOrder.setReference("EOD-Pair-Strategy:guid-123:Entry:Short*"); shortEntryOrder.setCurrentStatus(OrderStatus.Status.FILLED); shortEntryOrder.setOrderFilledTime(entryOrderTime); generator.orderEvent(new OrderEvent(longEntryOrder, null)); assertFalse(Files.exists(reportPath)); generator.orderEvent(new OrderEvent(shortEntryOrder, null)); assertFalse(Files.exists(reportPath)); TradeOrder longExitOrder = new TradeOrder("1234", longTicker, 100, TradeDirection.SELL); longExitOrder.setFilledPrice(105.00); longExitOrder.setReference("EOD-Pair-Strategy:guid-123:Exit:Long*"); longExitOrder.setCurrentStatus(OrderStatus.Status.FILLED); longExitOrder.setOrderFilledTime(exitOrderTime); TradeOrder shortExitOrder = new TradeOrder("2345", shortTicker, 50, TradeDirection.BUY); shortExitOrder.setFilledPrice(40.00); shortExitOrder.setReference("EOD-Pair-Strategy:guid-123:Exit:Short*"); shortExitOrder.setCurrentStatus(OrderStatus.Status.FILLED); shortExitOrder.setOrderFilledTime(exitOrderTime); generator.orderEvent(new OrderEvent(longExitOrder, null)); assertFalse(Files.exists(reportPath)); generator.orderEvent(new OrderEvent(shortExitOrder, null)); assertTrue(Files.exists(reportPath)); List<String> lines = Files.readAllLines(reportPath); assertEquals(1, lines.size()); String line = lines.get(0); String expected = "2016-03-25T06:18:35,Long,ABC,100,100.0,0,2016-03-25T06:19:35,105.0,0,Short,XYZ,50,50.0,0,40.0,0"; assertEquals(expected, line); generator.orderEvent(new OrderEvent(longEntryOrder, null)); generator.orderEvent(new OrderEvent(longExitOrder, null)); generator.orderEvent(new OrderEvent(shortEntryOrder, null)); generator.orderEvent(new OrderEvent(shortExitOrder, null)); lines = Files.readAllLines(reportPath); assertEquals(2, lines.size()); assertEquals(expected, lines.get(0)); assertEquals(expected, lines.get(1)); }
From source file:com.oneops.inductor.Config.java
/** * Helper method to read inductor ${@link #env} and returns an env vars map. * * @param env env can be a file location or a string containing multiple ENV_NAME=VALUE entries. * Entries are separated by newline (file) or ',' (string). * @return env var map./*from w ww . j a v a 2 s .com*/ */ private Map<String, String> readEnvVars(String env) { Path path = Paths.get(env); List<String> kvList; if (path.toFile().exists()) { try { kvList = Files.readAllLines(path); } catch (IOException ioe) { logger.warn("Error reading env var file: " + path, ioe); kvList = Collections.emptyList(); } } else { kvList = Arrays.asList(env.trim().split(",")); } return kvList.stream().map(s -> s.split("=")).filter(p -> p.length == 2) .collect(Collectors.toMap(p -> p[0].trim(), p -> p[1].trim())); }
From source file:org.mda.bcb.tcgagsdata.create.ProcessFile.java
protected Double getPointOnePercentile(File theDir) throws IOException { Double pointOnePercentile = null; File annotFile = new File(theDir, "annotations.tsv"); if (annotFile.exists()) { List<String> lines = Files.readAllLines(Paths.get(annotFile.getAbsolutePath())); for (String line : lines) { if (line.startsWith("POINT_ONE_PERCENTILE")) { line = line.replace("POINT_ONE_PERCENTILE\t", ""); pointOnePercentile = Double.valueOf(line); }// ww w. j av a2s. c o m } } if (null != pointOnePercentile) { TcgaGSData.printWithFlag("ProcessFile::getPointOnePercentile - found point one percentile for " + theDir.getAbsolutePath()); TcgaGSData.printWithFlag( "ProcessFile::getPointOnePercentile - point one percentile " + pointOnePercentile); } return pointOnePercentile; }
From source file:com.sumzerotrading.reporting.csv.ReportGeneratorTest.java
@Test public void testReportGeneratorEndToEnd() throws Exception { StockTicker longTicker = new StockTicker("ABC"); StockTicker shortTicker = new StockTicker("XYZ"); ZonedDateTime entryOrderTime = ZonedDateTime.of(2016, 3, 25, 6, 18, 35, 0, ZoneId.systemDefault()); ZonedDateTime exitOrderTime = ZonedDateTime.of(2016, 3, 25, 6, 19, 35, 0, ZoneId.systemDefault()); String directory = System.getProperty("java.io.tmpdir"); if (!directory.endsWith("/")) { directory += "/"; }// ww w . j a v a 2 s .com Path reportPath = Paths.get(directory + "report.csv"); Files.deleteIfExists(reportPath); System.out.println("Creating directory at: " + directory); ReportGenerator generator = new ReportGenerator("EOD-Pair-Strategy", directory, pairRoundtripBuilder); TradeOrder longEntryOrder = new TradeOrder("123", longTicker, 100, TradeDirection.BUY); longEntryOrder.setFilledPrice(100.00); longEntryOrder.setReference("EOD-Pair-Strategy:guid-123:Entry:Long*"); longEntryOrder.setCurrentStatus(OrderStatus.Status.FILLED); longEntryOrder.setOrderFilledTime(entryOrderTime); TradeOrder shortEntryOrder = new TradeOrder("234", shortTicker, 50, TradeDirection.SELL); shortEntryOrder.setFilledPrice(50.00); shortEntryOrder.setReference("EOD-Pair-Strategy:guid-123:Entry:Short*"); shortEntryOrder.setCurrentStatus(OrderStatus.Status.FILLED); shortEntryOrder.setOrderFilledTime(entryOrderTime); generator.orderEvent(new OrderEvent(longEntryOrder, null)); assertFalse(Files.exists(reportPath)); generator.orderEvent(new OrderEvent(shortEntryOrder, null)); assertFalse(Files.exists(reportPath)); TradeOrder longExitOrder = new TradeOrder("1234", longTicker, 100, TradeDirection.SELL); longExitOrder.setFilledPrice(105.00); longExitOrder.setReference("EOD-Pair-Strategy:guid-123:Exit:Long*"); longExitOrder.setCurrentStatus(OrderStatus.Status.FILLED); longExitOrder.setOrderFilledTime(exitOrderTime); TradeOrder shortExitOrder = new TradeOrder("2345", shortTicker, 50, TradeDirection.BUY); shortExitOrder.setFilledPrice(40.00); shortExitOrder.setReference("EOD-Pair-Strategy:guid-123:Exit:Short*"); shortExitOrder.setCurrentStatus(OrderStatus.Status.FILLED); shortExitOrder.setOrderFilledTime(exitOrderTime); generator.orderEvent(new OrderEvent(longExitOrder, null)); assertFalse(Files.exists(reportPath)); generator.orderEvent(new OrderEvent(shortExitOrder, null)); assertTrue(Files.exists(reportPath)); List<String> lines = Files.readAllLines(reportPath); assertEquals(1, lines.size()); String line = lines.get(0); String expected = "2016-03-25T06:18:35,Long,ABC,100,100.0,0,2016-03-25T06:19:35,105.0,0,Short,XYZ,50,50.0,0,40.0,0"; assertEquals(expected, line); generator.orderEvent(new OrderEvent(longEntryOrder, null)); generator.orderEvent(new OrderEvent(longExitOrder, null)); generator.orderEvent(new OrderEvent(shortEntryOrder, null)); generator.orderEvent(new OrderEvent(shortExitOrder, null)); lines = Files.readAllLines(reportPath); assertEquals(2, lines.size()); assertEquals(expected, lines.get(0)); assertEquals(expected, lines.get(1)); }
From source file:ro.cs.products.Executor.java
private static int execute(CommandLine commandLine) throws Exception { int retCode = ReturnCode.OK; CommandLineParser parser = new DefaultParser(); String logFile = props.getProperty("master.log.file"); String folder;//from w ww .ja va 2s . c o m boolean debugMode = commandLine.hasOption(Constants.PARAM_VERBOSE); Logger.CustomLogger logger; SensorType sensorType = commandLine.hasOption(Constants.SENSOR) ? Enum.valueOf(SensorType.class, commandLine.getOptionValue(Constants.SENSOR)) : SensorType.S2; if (commandLine.hasOption(Constants.PARAM_INPUT_FOLDER)) { folder = commandLine.getOptionValue(Constants.PARAM_INPUT_FOLDER); Utilities.ensureExists(Paths.get(folder)); Logger.initialize(Paths.get(folder, logFile).toAbsolutePath().toString(), debugMode); logger = Logger.getRootLogger(); if (commandLine.hasOption(Constants.PARAM_VERBOSE)) { printCommandLine(commandLine); } if (sensorType == SensorType.L8) { logger.warn("Argument --input will be ignored for Landsat8"); } else { String rootFolder = commandLine.getOptionValue(Constants.PARAM_INPUT_FOLDER); FillAnglesMethod fillAnglesMethod = Enum.valueOf(FillAnglesMethod.class, commandLine.hasOption(Constants.PARAM_FILL_ANGLES) ? commandLine.getOptionValue(Constants.PARAM_FILL_ANGLES).toUpperCase() : FillAnglesMethod.NONE.name()); if (!FillAnglesMethod.NONE.equals(fillAnglesMethod)) { try { Set<String> products = null; if (commandLine.hasOption(Constants.PARAM_PRODUCT_LIST)) { products = new HashSet<>(); for (String product : commandLine.getOptionValues(Constants.PARAM_PRODUCT_LIST)) { if (!product.endsWith(".SAFE")) { products.add(product + ".SAFE"); } else { products.add(product); } } } ProductInspector inspector = new ProductInspector(rootFolder, fillAnglesMethod, products); inspector.traverse(); } catch (IOException e) { logger.error(e.getMessage()); retCode = ReturnCode.DOWNLOAD_ERROR; } } } } else { folder = commandLine.getOptionValue(Constants.PARAM_OUT_FOLDER); Utilities.ensureExists(Paths.get(folder)); Logger.initialize(Paths.get(folder, logFile).toAbsolutePath().toString(), debugMode); logger = Logger.getRootLogger(); printCommandLine(commandLine); String proxyType = commandLine.hasOption(Constants.PARAM_PROXY_TYPE) ? commandLine.getOptionValue(Constants.PARAM_PROXY_TYPE) : nullIfEmpty(props.getProperty("proxy.type", null)); String proxyHost = commandLine.hasOption(Constants.PARAM_PROXY_HOST) ? commandLine.getOptionValue(Constants.PARAM_PROXY_HOST) : nullIfEmpty(props.getProperty("proxy.host", null)); String proxyPort = commandLine.hasOption(Constants.PARAM_PROXY_PORT) ? commandLine.getOptionValue(Constants.PARAM_PROXY_PORT) : nullIfEmpty(props.getProperty("proxy.port", null)); String proxyUser = commandLine.hasOption(Constants.PARAM_PROXY_USER) ? commandLine.getOptionValue(Constants.PARAM_PROXY_USER) : nullIfEmpty(props.getProperty("proxy.user", null)); String proxyPwd = commandLine.hasOption(Constants.PARAM_PROXY_PASSWORD) ? commandLine.getOptionValue(Constants.PARAM_PROXY_PASSWORD) : nullIfEmpty(props.getProperty("proxy.pwd", null)); NetUtils.setProxy(proxyType, proxyHost, proxyPort == null ? 0 : Integer.parseInt(proxyPort), proxyUser, proxyPwd); List<ProductDescriptor> products = new ArrayList<>(); Set<String> tiles = new HashSet<>(); Polygon2D areaOfInterest = new Polygon2D(); ProductStore source = Enum.valueOf(ProductStore.class, commandLine.getOptionValue(Constants.PARAM_DOWNLOAD_STORE, ProductStore.SCIHUB.toString())); if (sensorType == SensorType.S2 && !commandLine.hasOption(Constants.PARAM_FLAG_SEARCH_AWS) && !commandLine.hasOption(Constants.PARAM_USER)) { throw new MissingOptionException("Missing SciHub credentials"); } String user = commandLine.getOptionValue(Constants.PARAM_USER); String pwd = commandLine.getOptionValue(Constants.PARAM_PASSWORD); if (user != null && pwd != null && !user.isEmpty() && !pwd.isEmpty()) { String authToken = "Basic " + new String(Base64.getEncoder().encode((user + ":" + pwd).getBytes())); NetUtils.setAuthToken(authToken); } ProductDownloader downloader = sensorType.equals(SensorType.S2) ? new SentinelProductDownloader(source, commandLine.getOptionValue(Constants.PARAM_OUT_FOLDER), props) : new LandsatProductDownloader(commandLine.getOptionValue(Constants.PARAM_OUT_FOLDER), props); TileMap tileMap = sensorType == SensorType.S2 ? SentinelTilesMap.getInstance() : LandsatTilesMap.getInstance(); if (commandLine.hasOption(Constants.PARAM_AREA)) { String[] points = commandLine.getOptionValues(Constants.PARAM_AREA); for (String point : points) { areaOfInterest.append(Double.parseDouble(point.substring(0, point.indexOf(","))), Double.parseDouble(point.substring(point.indexOf(",") + 1))); } } else if (commandLine.hasOption(Constants.PARAM_AREA_FILE)) { areaOfInterest = Polygon2D.fromWKT(new String( Files.readAllBytes(Paths.get(commandLine.getOptionValue(Constants.PARAM_AREA_FILE))), StandardCharsets.UTF_8)); } else if (commandLine.hasOption(Constants.PARAM_TILE_SHAPE_FILE)) { String tileShapeFile = commandLine.getOptionValue(Constants.PARAM_TILE_SHAPE_FILE); if (Files.exists(Paths.get(tileShapeFile))) { logger.info(String.format("Reading %s tiles extents", sensorType)); tileMap.fromKmlFile(tileShapeFile); logger.info(String.valueOf(tileMap.getCount() + " tiles found")); } } else { if (tileMap.getCount() == 0) { logger.info(String.format("Loading %s tiles extents", sensorType)); tileMap.read(Executor.class.getResourceAsStream(sensorType + "tilemap.dat")); logger.info(String.valueOf(tileMap.getCount() + " tile extents loaded")); } } if (commandLine.hasOption(Constants.PARAM_TILE_LIST)) { Collections.addAll(tiles, commandLine.getOptionValues(Constants.PARAM_TILE_LIST)); } else if (commandLine.hasOption(Constants.PARAM_TILE_LIST_FILE)) { tiles.addAll( Files.readAllLines(Paths.get(commandLine.getOptionValue(Constants.PARAM_TILE_LIST_FILE)))); } if (commandLine.hasOption(Constants.PARAM_PRODUCT_LIST)) { String[] uuids = commandLine.getOptionValues(Constants.PARAM_PRODUCT_UUID_LIST); String[] productNames = commandLine.getOptionValues(Constants.PARAM_PRODUCT_LIST); if (sensorType == SensorType.S2 && (!commandLine.hasOption(Constants.PARAM_DOWNLOAD_STORE) || ProductStore.SCIHUB.toString() .equals(commandLine.getOptionValue(Constants.PARAM_DOWNLOAD_STORE))) && (uuids == null || uuids.length != productNames.length)) { logger.error("For the list of product names a corresponding list of UUIDs has to be given!"); return -1; } for (int i = 0; i < productNames.length; i++) { ProductDescriptor productDescriptor = sensorType == SensorType.S2 ? new SentinelProductDescriptor(productNames[i]) : new LandsatProductDescriptor(productNames[i]); if (uuids != null) { productDescriptor.setId(uuids[i]); } products.add(productDescriptor); } } else if (commandLine.hasOption(Constants.PARAM_PRODUCT_LIST_FILE)) { for (String line : Files .readAllLines(Paths.get(commandLine.getOptionValue(Constants.PARAM_PRODUCT_LIST_FILE)))) { products.add(sensorType == SensorType.S2 ? new SentinelProductDescriptor(line) : new LandsatProductDescriptor(line)); } } double clouds; if (commandLine.hasOption(Constants.PARAM_CLOUD_PERCENTAGE)) { clouds = Double.parseDouble(commandLine.getOptionValue(Constants.PARAM_CLOUD_PERCENTAGE)); } else { clouds = Constants.DEFAULT_CLOUD_PERCENTAGE; } String sensingStart; if (commandLine.hasOption(Constants.PARAM_START_DATE)) { String dateString = commandLine.getOptionValue(Constants.PARAM_START_DATE); LocalDate startDate = LocalDate.parse(dateString, DateTimeFormatter.ISO_DATE); long days = ChronoUnit.DAYS.between(startDate, LocalDate.now()); sensingStart = String.format(Constants.PATTERN_START_DATE, days); } else { sensingStart = Constants.DEFAULT_START_DATE; } String sensingEnd; if (commandLine.hasOption(Constants.PARAM_END_DATE)) { String dateString = commandLine.getOptionValue(Constants.PARAM_END_DATE); LocalDate endDate = LocalDate.parse(dateString, DateTimeFormatter.ISO_DATE); long days = ChronoUnit.DAYS.between(endDate, LocalDate.now()); sensingEnd = String.format(Constants.PATTERN_START_DATE, days); } else { sensingEnd = Constants.DEFAULT_END_DATE; } int limit; if (commandLine.hasOption(Constants.PARAM_RESULTS_LIMIT)) { limit = Integer.parseInt(commandLine.getOptionValue(Constants.PARAM_RESULTS_LIMIT)); } else { limit = Constants.DEFAULT_RESULTS_LIMIT; } if (commandLine.hasOption(Constants.PARAM_DOWNLOAD_STORE)) { String value = commandLine.getOptionValue(Constants.PARAM_DOWNLOAD_STORE); if (downloader instanceof SentinelProductDownloader) { ((SentinelProductDownloader) downloader) .setDownloadStore(Enum.valueOf(ProductStore.class, value)); logger.info("Products will be downloaded from %s", value); } else { logger.warn("Argument --store will be ignored for Landsat8"); } } downloader.shouldCompress(commandLine.hasOption(Constants.PARAM_FLAG_COMPRESS)); downloader.shouldDeleteAfterCompression(commandLine.hasOption(Constants.PARAM_FLAG_DELETE)); if (commandLine.hasOption(Constants.PARAM_FILL_ANGLES)) { if (downloader instanceof SentinelProductDownloader) { ((SentinelProductDownloader) downloader) .setFillMissingAnglesMethod(Enum.valueOf(FillAnglesMethod.class, commandLine.hasOption(Constants.PARAM_FILL_ANGLES) ? commandLine.getOptionValue(Constants.PARAM_FILL_ANGLES).toUpperCase() : FillAnglesMethod.NONE.name())); } else { logger.warn("Argument --ma will be ignored for Landsat8"); } } int numPoints = areaOfInterest.getNumPoints(); tiles = tiles.stream().map(t -> t.startsWith("T") ? t.substring(1) : t).collect(Collectors.toSet()); if (products.size() == 0 && numPoints == 0 && tileMap.getCount() > 0) { Rectangle2D rectangle2D = tileMap.boundingBox(tiles); areaOfInterest.append(rectangle2D.getX(), rectangle2D.getY()); areaOfInterest.append(rectangle2D.getMaxX(), rectangle2D.getY()); areaOfInterest.append(rectangle2D.getMaxX(), rectangle2D.getMaxY()); areaOfInterest.append(rectangle2D.getX(), rectangle2D.getMaxY()); areaOfInterest.append(rectangle2D.getX(), rectangle2D.getY()); } numPoints = areaOfInterest.getNumPoints(); if (products.size() == 0 && numPoints > 0) { String searchUrl; AbstractSearch searchProvider; logger.debug("No product provided, searching on the AOI"); if (sensorType == SensorType.L8) { logger.debug("Search will be done for Landsat"); searchUrl = props.getProperty(Constants.PROPERTY_NAME_LANDSAT_SEARCH_URL, Constants.PROPERTY_NAME_DEFAULT_LANDSAT_SEARCH_URL); if (!NetUtils.isAvailable(searchUrl)) { logger.warn(searchUrl + " is not available!"); } searchProvider = new LandsatSearch(searchUrl); if (commandLine.hasOption(Constants.PARAM_START_DATE)) { searchProvider.setSensingStart(commandLine.getOptionValue(Constants.PARAM_START_DATE)); } if (commandLine.hasOption(Constants.PARAM_END_DATE)) { searchProvider.setSensingEnd(commandLine.getOptionValue(Constants.PARAM_END_DATE)); } if (commandLine.hasOption(Constants.PARAM_TILE_LIST)) { searchProvider.setTiles(tiles); } ((LandsatSearch) searchProvider).limit(limit); } else if (!commandLine.hasOption(Constants.PARAM_FLAG_SEARCH_AWS)) { logger.debug("Search will be done on SciHub"); searchUrl = props.getProperty(Constants.PROPERTY_NAME_SEARCH_URL, Constants.PROPERTY_DEFAULT_SEARCH_URL); if (!NetUtils.isAvailable(searchUrl)) { logger.warn(searchUrl + " is not available!"); searchUrl = props.getProperty(Constants.PROPERTY_NAME_SEARCH_URL_SECONDARY, Constants.PROPERTY_DEFAULT_SEARCH_URL_SECONDARY); } searchProvider = new SciHubSearch(searchUrl); SciHubSearch search = (SciHubSearch) searchProvider; if (user != null && !user.isEmpty() && pwd != null && !pwd.isEmpty()) { search = search.auth(user, pwd); } String interval = "[" + sensingStart + " TO " + sensingEnd + "]"; search.filter(Constants.SEARCH_PARAM_INTERVAL, interval).limit(limit); if (commandLine.hasOption(Constants.PARAM_RELATIVE_ORBIT)) { search.filter(Constants.SEARCH_PARAM_RELATIVE_ORBIT_NUMBER, commandLine.getOptionValue(Constants.PARAM_RELATIVE_ORBIT)); } } else { logger.debug("Search will be done on AWS"); searchUrl = props.getProperty(Constants.PROPERTY_NAME_AWS_SEARCH_URL, Constants.PROPERTY_DEFAULT_AWS_SEARCH_URL); searchProvider = new AmazonSearch(searchUrl); searchProvider.setTiles(tiles); Calendar calendar = Calendar.getInstance(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); calendar.add(Calendar.DAY_OF_MONTH, Integer.parseInt(sensingStart.replace("NOW", "").replace("DAY", ""))); searchProvider.setSensingStart(dateFormat.format(calendar.getTime())); calendar = Calendar.getInstance(); String endOffset = sensingEnd.replace("NOW", "").replace("DAY", ""); int offset = endOffset.isEmpty() ? 0 : Integer.parseInt(endOffset); calendar.add(Calendar.DAY_OF_MONTH, offset); searchProvider.setSensingEnd(dateFormat.format(calendar.getTime())); if (commandLine.hasOption(Constants.PARAM_RELATIVE_ORBIT)) { searchProvider.setOrbit( Integer.parseInt(commandLine.getOptionValue(Constants.PARAM_RELATIVE_ORBIT))); } } if (searchProvider.getTiles() == null || searchProvider.getTiles().size() == 0) { searchProvider.setAreaOfInterest(areaOfInterest); } searchProvider.setClouds(clouds); products = searchProvider.execute(); } else { logger.debug("Product name(s) present, no additional search will be performed."); } if (downloader instanceof SentinelProductDownloader) { ((SentinelProductDownloader) downloader).setFilteredTiles(tiles, commandLine.hasOption(Constants.PARAM_FLAG_UNPACKED)); } downloader.setProgressListener(batchProgressListener); downloader.setFileProgressListener(fileProgressListener); retCode = downloader.downloadProducts(products); } return retCode; }
From source file:org.languagetool.server.TextChecker.java
void checkText(AnnotatedText aText, HttpExchange httpExchange, Map<String, String> parameters, ErrorRequestLimiter errorRequestLimiter, String remoteAddress) throws Exception { checkParams(parameters);/*w w w .ja v a 2 s . c om*/ long timeStart = System.currentTimeMillis(); UserLimits limits = ServerTools.getUserLimits(parameters, config); // logging information String agent = parameters.get("useragent") != null ? parameters.get("useragent") : "-"; Long agentId = null, userId = null; if (logger.isLogging()) { DatabaseAccess db = DatabaseAccess.getInstance(); agentId = db.getOrCreateClientId(parameters.get("useragent")); userId = limits.getPremiumUid(); } String referrer = httpExchange.getRequestHeaders().getFirst("Referer"); String userAgent = httpExchange.getRequestHeaders().getFirst("User-Agent"); if (aText.getPlainText().length() > limits.getMaxTextLength()) { String msg = "limit: " + limits.getMaxTextLength() + ", size: " + aText.getPlainText().length(); logger.log(new DatabaseAccessLimitLogEntry("MaxCharacterSizeExceeded", logServerId, agentId, userId, msg, referrer, userAgent)); ServerMetricsCollector.getInstance() .logRequestError(ServerMetricsCollector.RequestErrorType.MAX_TEXT_SIZE); throw new TextTooLongException( "Your text exceeds the limit of " + limits.getMaxTextLength() + " characters (it's " + aText.getPlainText().length() + " characters). Please submit a shorter text."); } UserConfig userConfig = new UserConfig( limits.getPremiumUid() != null ? getUserDictWords(limits.getPremiumUid()) : Collections.emptyList(), new HashMap<>(), config.getMaxSpellingSuggestions()); // NOTE: at the moment, feedback for A/B-Tests is only delivered from this client, so only run tests there if (agent != null && agent.equals("ltorg")) { userConfig.setAbTest(config.getAbTest()); } //print("Check start: " + text.length() + " chars, " + langParam); boolean autoDetectLanguage = getLanguageAutoDetect(parameters); List<String> preferredVariants = getPreferredVariants(parameters); if (parameters.get("noopLanguages") != null && !autoDetectLanguage) { ServerMetricsCollector.getInstance() .logRequestError(ServerMetricsCollector.RequestErrorType.INVALID_REQUEST); throw new IllegalArgumentException( "You can specify 'noopLanguages' only when also using 'language=auto'"); } List<String> noopLangs = parameters.get("noopLanguages") != null ? Arrays.asList(parameters.get("noopLanguages").split(",")) : Collections.emptyList(); List<String> preferredLangs = parameters.get("preferredLanguages") != null ? Arrays.asList(parameters.get("preferredLanguages").split(",")) : Collections.emptyList(); DetectedLanguage detLang = getLanguage(aText.getPlainText(), parameters, preferredVariants, noopLangs, preferredLangs); Language lang = detLang.getGivenLanguage(); Integer count = languageCheckCounts.get(lang.getShortCodeWithCountryAndVariant()); if (count == null) { count = 1; } else { count++; } //print("Starting check: " + aText.getPlainText().length() + " chars, #" + count); String motherTongueParam = parameters.get("motherTongue"); Language motherTongue = motherTongueParam != null ? Languages.getLanguageForShortCode(motherTongueParam) : null; boolean useEnabledOnly = "yes".equals(parameters.get("enabledOnly")) || "true".equals(parameters.get("enabledOnly")); List<Language> altLanguages = new ArrayList<>(); if (parameters.get("altLanguages") != null) { String[] altLangParams = parameters.get("altLanguages").split(",\\s*"); for (String langCode : altLangParams) { Language altLang = Languages.getLanguageForShortCode(langCode); altLanguages.add(altLang); if (altLang.hasVariant() && !altLang.isVariant()) { ServerMetricsCollector.getInstance() .logRequestError(ServerMetricsCollector.RequestErrorType.INVALID_REQUEST); throw new IllegalArgumentException("You specified altLanguage '" + langCode + "', but for this language you need to specify a variant, e.g. 'en-GB' instead of just 'en'"); } } } List<String> enabledRules = getEnabledRuleIds(parameters); List<String> disabledRules = getDisabledRuleIds(parameters); List<CategoryId> enabledCategories = getCategoryIds("enabledCategories", parameters); List<CategoryId> disabledCategories = getCategoryIds("disabledCategories", parameters); if ((disabledRules.size() > 0 || disabledCategories.size() > 0) && useEnabledOnly) { ServerMetricsCollector.getInstance() .logRequestError(ServerMetricsCollector.RequestErrorType.INVALID_REQUEST); throw new IllegalArgumentException( "You cannot specify disabled rules or categories using enabledOnly=true"); } if (enabledRules.isEmpty() && enabledCategories.isEmpty() && useEnabledOnly) { ServerMetricsCollector.getInstance() .logRequestError(ServerMetricsCollector.RequestErrorType.INVALID_REQUEST); throw new IllegalArgumentException( "You must specify enabled rules or categories when using enabledOnly=true"); } boolean useQuerySettings = enabledRules.size() > 0 || disabledRules.size() > 0 || enabledCategories.size() > 0 || disabledCategories.size() > 0; boolean allowIncompleteResults = "true".equals(parameters.get("allowIncompleteResults")); boolean enableHiddenRules = "true".equals(parameters.get("enableHiddenRules")); JLanguageTool.Mode mode = ServerTools.getMode(parameters); String callback = parameters.get("callback"); QueryParams params = new QueryParams(altLanguages, enabledRules, disabledRules, enabledCategories, disabledCategories, useEnabledOnly, useQuerySettings, allowIncompleteResults, enableHiddenRules, mode, callback); Long textSessionId = null; try { if (parameters.containsKey("textSessionId")) { String textSessionIdStr = parameters.get("textSessionId"); if (textSessionIdStr.contains(":")) { // transitioning to new format used in chrome addon // format: "{random number in 0..99999}:{unix time}" long random, timestamp; int sepPos = textSessionIdStr.indexOf(':'); random = Long.valueOf(textSessionIdStr.substring(0, sepPos)); timestamp = Long.valueOf(textSessionIdStr.substring(sepPos + 1)); // use random number to choose a slice in possible range of values // then choose position in slice by timestamp long maxRandom = 100000; long randomSegmentSize = (Long.MAX_VALUE - maxRandom) / maxRandom; long segmentOffset = random * randomSegmentSize; if (timestamp > randomSegmentSize) { print(String.format("Could not transform textSessionId '%s'", textSessionIdStr)); } textSessionId = segmentOffset + timestamp; } else { textSessionId = Long.valueOf(textSessionIdStr); } userConfig.setTextSessionId(textSessionId); } } catch (NumberFormatException ex) { print("Could not parse textSessionId '" + parameters.get("textSessionId") + "' as long: " + ex.getMessage()); } int textSize = aText.getPlainText().length(); List<RuleMatch> ruleMatchesSoFar = Collections.synchronizedList(new ArrayList<>()); Future<List<RuleMatch>> future = executorService.submit(new Callable<List<RuleMatch>>() { @Override public List<RuleMatch> call() throws Exception { // use to fake OOM in thread for testing: /*if (Math.random() < 0.1) { throw new OutOfMemoryError(); }*/ return getRuleMatches(aText, lang, motherTongue, parameters, params, userConfig, f -> ruleMatchesSoFar.add(f)); } }); String incompleteResultReason = null; List<RuleMatch> matches; try { if (limits.getMaxCheckTimeMillis() < 0) { matches = future.get(); } else { matches = future.get(limits.getMaxCheckTimeMillis(), TimeUnit.MILLISECONDS); } } catch (ExecutionException e) { future.cancel(true); if (ExceptionUtils.getRootCause(e) instanceof ErrorRateTooHighException) { ServerMetricsCollector.getInstance() .logRequestError(ServerMetricsCollector.RequestErrorType.TOO_MANY_ERRORS); logger.log(new DatabaseCheckErrorLogEntry("ErrorRateTooHigh", logServerId, agentId, userId, lang, detLang.getDetectedLanguage(), textSize, "matches: " + ruleMatchesSoFar.size())); } if (params.allowIncompleteResults && ExceptionUtils.getRootCause(e) instanceof ErrorRateTooHighException) { print(e.getMessage() + " - returning " + ruleMatchesSoFar.size() + " matches found so far. Detected language: " + detLang); matches = new ArrayList<>(ruleMatchesSoFar); // threads might still be running, so make a copy incompleteResultReason = "Results are incomplete: " + ExceptionUtils.getRootCause(e).getMessage(); } else if (e.getCause() != null && e.getCause() instanceof OutOfMemoryError) { throw (OutOfMemoryError) e.getCause(); } else { throw new RuntimeException(e.getMessage() + ", detected: " + detLang, e); } } catch (TimeoutException e) { boolean cancelled = future.cancel(true); Path loadFile = Paths.get("/proc/loadavg"); // works in Linux only(?) String loadInfo = loadFile.toFile().exists() ? Files.readAllLines(loadFile).toString() : "(unknown)"; if (errorRequestLimiter != null) { errorRequestLimiter.logAccess(remoteAddress, httpExchange.getRequestHeaders(), parameters); } String message = "Text checking took longer than allowed maximum of " + limits.getMaxCheckTimeMillis() + " milliseconds (cancelled: " + cancelled + ", lang: " + lang.getShortCodeWithCountryAndVariant() + ", detected: " + detLang + ", #" + count + ", " + aText.getPlainText().length() + " characters of text" + ", mode: " + mode.toString().toLowerCase() + ", h: " + reqCounter.getHandleCount() + ", r: " + reqCounter.getRequestCount() + ", system load: " + loadInfo + ")"; if (params.allowIncompleteResults) { print(message + " - returning " + ruleMatchesSoFar.size() + " matches found so far"); matches = new ArrayList<>(ruleMatchesSoFar); // threads might still be running, so make a copy incompleteResultReason = "Results are incomplete: text checking took longer than allowed maximum of " + String.format(Locale.ENGLISH, "%.2f", limits.getMaxCheckTimeMillis() / 1000.0) + " seconds"; } else { ServerMetricsCollector.getInstance() .logRequestError(ServerMetricsCollector.RequestErrorType.MAX_CHECK_TIME); logger.log(new DatabaseCheckErrorLogEntry("MaxCheckTimeExceeded", logServerId, agentId, limits.getPremiumUid(), lang, detLang.getDetectedLanguage(), textSize, "load: " + loadInfo)); throw new RuntimeException(message, e); } } setHeaders(httpExchange); List<RuleMatch> hiddenMatches = new ArrayList<>(); if (config.getHiddenMatchesServer() != null && params.enableHiddenRules && config.getHiddenMatchesLanguages().contains(lang)) { if (config.getHiddenMatchesServerFailTimeout() > 0 && lastHiddenMatchesServerTimeout != -1 && System.currentTimeMillis() - lastHiddenMatchesServerTimeout < config .getHiddenMatchesServerFailTimeout()) { ServerMetricsCollector.getInstance().logHiddenServerStatus(false); print("Warn: Skipped querying hidden matches server at " + config.getHiddenMatchesServer() + " because of recent error/timeout (timeout=" + config.getHiddenMatchesServerFailTimeout() + "ms)."); } else { ResultExtender resultExtender = new ResultExtender(config.getHiddenMatchesServer(), config.getHiddenMatchesServerTimeout()); try { long start = System.currentTimeMillis(); List<RemoteRuleMatch> extensionMatches = resultExtender .getExtensionMatches(aText.getPlainText(), parameters); hiddenMatches = resultExtender.getFilteredExtensionMatches(matches, extensionMatches); long end = System.currentTimeMillis(); print("Hidden matches: " + extensionMatches.size() + " -> " + hiddenMatches.size() + " in " + (end - start) + "ms for " + lang.getShortCodeWithCountryAndVariant()); ServerMetricsCollector.getInstance().logHiddenServerStatus(true); lastHiddenMatchesServerTimeout = -1; } catch (Exception e) { ServerMetricsCollector.getInstance().logHiddenServerStatus(false); print("Warn: Failed to query hidden matches server at " + config.getHiddenMatchesServer() + ": " + e.getClass() + ": " + e.getMessage()); lastHiddenMatchesServerTimeout = System.currentTimeMillis(); } } } int compactMode = Integer.parseInt(parameters.getOrDefault("c", "0")); String response = getResponse(aText, detLang, motherTongue, matches, hiddenMatches, incompleteResultReason, compactMode); if (params.callback != null) { // JSONP - still needed today for the special case of hosting your own on-premise LT without SSL // and using it from a local MS Word (not Online Word) - issue #89 in the add-in repo: response = params.callback + "(" + response + ");"; } String messageSent = "sent"; String languageMessage = lang.getShortCodeWithCountryAndVariant(); try { httpExchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.getBytes(ENCODING).length); httpExchange.getResponseBody().write(response.getBytes(ENCODING)); ServerMetricsCollector.getInstance().logResponse(HttpURLConnection.HTTP_OK); } catch (IOException exception) { // the client is disconnected messageSent = "notSent: " + exception.getMessage(); } if (motherTongue != null) { languageMessage += " (mother tongue: " + motherTongue.getShortCodeWithCountryAndVariant() + ")"; } if (autoDetectLanguage) { languageMessage += "[auto]"; } languageCheckCounts.put(lang.getShortCodeWithCountryAndVariant(), count); int computationTime = (int) (System.currentTimeMillis() - timeStart); String version = parameters.get("v") != null ? ", v:" + parameters.get("v") : ""; print("Check done: " + aText.getPlainText().length() + " chars, " + languageMessage + ", #" + count + ", " + referrer + ", " + matches.size() + " matches, " + computationTime + "ms, agent:" + agent + version + ", " + messageSent + ", q:" + (workQueue != null ? workQueue.size() : "?") + ", h:" + reqCounter.getHandleCount() + ", dH:" + reqCounter.getDistinctIps() + ", m:" + mode.toString().toLowerCase()); int matchCount = matches.size(); Map<String, Integer> ruleMatchCount = new HashMap<>(); for (RuleMatch match : matches) { String ruleId = match.getRule().getId(); ruleMatchCount.put(ruleId, ruleMatchCount.getOrDefault(ruleId, 0) + 1); } ServerMetricsCollector.getInstance().logCheck(lang, computationTime, textSize, matchCount, mode, agent, ruleMatchCount); if (!config.isSkipLoggingChecks()) { DatabaseCheckLogEntry logEntry = new DatabaseCheckLogEntry(userId, agentId, logServerId, textSize, matchCount, lang, detLang.getDetectedLanguage(), computationTime, textSessionId, mode.toString()); logEntry.setRuleMatches(new DatabaseRuleMatchLogEntry( config.isSkipLoggingRuleMatches() ? Collections.emptyMap() : ruleMatchCount)); logger.log(logEntry); } }
From source file:ai.grakn.graql.GraqlShell.java
/** * load the user's preferred editor to edit a query * @return the string written to the editor *//*from w ww .jav a2 s . co m*/ private String runEditor() throws IOException { // Get preferred editor Map<String, String> env = System.getenv(); String editor = Optional.ofNullable(env.get("EDITOR")).orElse(DEFAULT_EDITOR); // Run the editor, pipe input into and out of tty so we can provide the input/output to the editor via Graql ProcessBuilder builder = new ProcessBuilder("/bin/bash", "-c", editor + " </dev/tty >/dev/tty " + tempFile.getAbsolutePath()); // Wait for user to finish editing try { builder.start().waitFor(); } catch (InterruptedException e) { throw new RuntimeException(e); } return String.join("\n", Files.readAllLines(tempFile.toPath())); }