List of usage examples for java.lang Boolean parseBoolean
public static boolean parseBoolean(String s)
From source file:uk.dsxt.voting.tests.TestDataGenerator.java
public static void main(String[] args) { try {/*from w w w .j a va 2s . co m*/ if (args.length == 1) { generateCredentialsJSON(); return; } if (args.length > 0 && args.length < 10) { System.out.println( "<name> <totalParticipant> <holdersCount> <vmCount> <levelsCount> <minutes> <generateVotes> <victimsCount>"); throw new IllegalArgumentException("Invalid arguments count exception."); } int argId = 0; String name = args.length == 0 ? "ss_10_100000_30" : args[argId++]; int totalParticipant = args.length == 0 ? 100000 : Integer.parseInt(args[argId++]); int holdersCount = args.length == 0 ? 10 : Integer.parseInt(args[argId++]); int vmCount = args.length == 0 ? 1 : Integer.parseInt(args[argId++]); int levelsCount = args.length == 0 ? 3 : Integer.parseInt(args[argId++]); int minutes = args.length == 0 ? 30 : Integer.parseInt(args[argId++]); boolean generateVotes = args.length == 0 ? true : Boolean.parseBoolean(args[argId++]); int victimsCount = args.length == 0 ? 0 : Integer.parseInt(args[argId++]); boolean generateDisconnect = args.length == 0 ? false : Boolean.parseBoolean(args[argId++]); int disconnectNodes = args.length == 0 ? 0 : Integer.parseInt(args[argId]); TestDataGenerator generator = new TestDataGenerator(); generator.generate(name, totalParticipant, holdersCount, vmCount, levelsCount, minutes, generateVotes, victimsCount, generateDisconnect, disconnectNodes); } catch (Exception e) { log.error("Test generation was failed.", e); } }
From source file:FileSplitter.java
public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Split? [true/false]: "); boolean split = Boolean.parseBoolean(scan.next()); if (split) {//from w ww .j a v a2s.c om File file = new File("T:/Java/com/power/nb/visual/dist/visual.zip"); FileSplitter splitter = new FileSplitter(file); System.out.println("splitter.split(3): " + splitter.split(3)); } else { File file = new File("T:/Java/com/power/nb/visual/dist/visual.zip.part.0"); FileSplitter splitter = new FileSplitter(file); System.out.println("splitter.unsplit(): " + splitter.unsplit()); } }
From source file:de.tudarmstadt.lt.lm.app.GenerateNgrams.java
@SuppressWarnings("static-access") public static void main(String[] args) { Options opts = new Options(); opts.addOption(OptionBuilder.withLongOpt("help").withDescription("Display help message.").create("?")); opts.addOption(OptionBuilder.withLongOpt("ptype").withArgName("class").hasArg().withDescription( "specify the instance of the language model provider that you want to use: {LtSegProvider, BreakIteratorStringProvider, UimaStringProvider, PreTokenizedStringProvider} (default: LtSegProvider)") .create("p")); opts.addOption(OptionBuilder.withLongOpt("cardinality").withArgName("ngram-order").hasArg().withDescription( "Specify the cardinality of the ngrams (min. 1). Specify a range using 'from-to'. (Examples: 5 = extract 5grams; 1-5 = extract 1grams, 2grams, ..., 5grams; default: 1-5).") .create("n")); opts.addOption(OptionBuilder.withLongOpt("dir").withArgName("directory").isRequired().hasArg() .withDescription(/* w ww . j a v a2 s . c o m*/ "specify the directory that contains '.txt' files that are used as source for generating ngrams.") .create("d")); opts.addOption(OptionBuilder.withLongOpt("overwrite").withDescription("Overwrite existing ngram file.") .create("w")); CommandLine cli = null; try { cli = new GnuParser().parse(opts, args); } catch (Exception e) { print_usage(opts, e.getMessage()); } if (cli.hasOption("?")) print_usage(opts, null); AbstractStringProvider prvdr = null; try { prvdr = StartLM .getStringProviderInstance(cli.getOptionValue("ptype", LtSegProvider.class.getSimpleName())); } catch (Exception e) { print_usage(opts, String.format("Could not instantiate LmProvider '%s': %s", cli.getOptionValue("ptype", LtSegProvider.class.getSimpleName()), e.getMessage())); } String n_ = cli.getOptionValue("cardinality", "1-5"); int dash_index = n_.indexOf('-'); int n_e = Integer.parseInt(n_.substring(dash_index + 1, n_.length()).trim()); int n_b = n_e; if (dash_index == 0) n_b = 1; if (dash_index > 0) n_b = Math.max(1, Integer.parseInt(n_.substring(0, dash_index).trim())); final File src_dir = new File(cli.getOptionValue("dir")); boolean overwrite = Boolean.parseBoolean(cli.getOptionValue("overwrite", "false")); generateNgrams(src_dir, prvdr, n_b, n_e, overwrite); }
From source file:com.searchcode.app.App.java
public static void main(String[] args) { injector = Guice.createInjector(new InjectorConfig()); int server_port = Helpers.tryParseInt( Properties.getProperties().getProperty(Values.SERVERPORT, Values.DEFAULTSERVERPORT), Values.DEFAULTSERVERPORT);/*from w w w.j av a2s.com*/ boolean onlyLocalhost = Boolean .parseBoolean(Properties.getProperties().getProperty("only_localhost", "false")); // Database migrations happen before we start databaseMigrations(); LOGGER.info("Starting searchcode server on port " + server_port); Spark.port(server_port); JobService js = injector.getInstance(JobService.class); Repo repo = injector.getInstance(Repo.class); Data data = injector.getInstance(Data.class); Api api = injector.getInstance(Api.class); ApiService apiService = injector.getInstance(ApiService.class); StatsService statsService = new StatsService(); scl = Singleton.getSearchcodeLib(data); js.initialJobs(); Gson gson = new Gson(); Spark.staticFileLocation("/public"); before((request, response) -> { if (onlyLocalhost) { if (!request.ip().equals("127.0.0.1")) { halt(204); } } }); get("/", (req, res) -> { Map<String, Object> map = new HashMap<>(); map.put("repoCount", repo.getRepoCount()); if (req.queryParams().contains("q") && !req.queryParams("q").trim().equals("")) { String query = req.queryParams("q").trim(); int page = 0; if (req.queryParams().contains("p")) { try { page = Integer.parseInt(req.queryParams("p")); page = page > 19 ? 19 : page; } catch (NumberFormatException ex) { page = 0; } } List<String> reposList = new ArrayList<>(); List<String> langsList = new ArrayList<>(); List<String> ownsList = new ArrayList<>(); if (req.queryParams().contains("repo")) { String[] repos = new String[0]; repos = req.queryParamsValues("repo"); if (repos.length != 0) { reposList = Arrays.asList(repos); } } if (req.queryParams().contains("lan")) { String[] langs = new String[0]; langs = req.queryParamsValues("lan"); if (langs.length != 0) { langsList = Arrays.asList(langs); } } if (req.queryParams().contains("own")) { String[] owns = new String[0]; owns = req.queryParamsValues("own"); if (owns.length != 0) { ownsList = Arrays.asList(owns); } } map.put("searchValue", query); map.put("searchResultJson", gson.toJson(new CodePreload(query, page, langsList, reposList, ownsList))); map.put("logoImage", getLogo()); map.put("isCommunity", ISCOMMUNITY); return new ModelAndView(map, "search_test.ftl"); } // Totally pointless vanity but lets rotate the image every week int photoId = getWeekOfMonth(); if (photoId <= 0) { photoId = 3; } if (photoId > 4) { photoId = 2; } CodeSearcher cs = new CodeSearcher(); map.put("photoId", photoId); map.put("numDocs", cs.getTotalNumberDocumentsIndexed()); map.put("logoImage", getLogo()); map.put("isCommunity", ISCOMMUNITY); return new ModelAndView(map, "index.ftl"); }, new FreeMarkerEngine()); get("/html/", (req, res) -> { CodeSearcher cs = new CodeSearcher(); CodeMatcher cm = new CodeMatcher(data); Map<String, Object> map = new HashMap<>(); map.put("repoCount", repo.getRepoCount()); if (req.queryParams().contains("q")) { String query = req.queryParams("q").trim(); String altquery = query.replaceAll("[^A-Za-z0-9 ]", " ").trim().replaceAll(" +", " "); int page = 0; if (req.queryParams().contains("p")) { try { page = Integer.parseInt(req.queryParams("p")); page = page > 19 ? 19 : page; } catch (NumberFormatException ex) { page = 0; } } String[] repos = new String[0]; String[] langs = new String[0]; String reposFilter = ""; String langsFilter = ""; String reposQueryString = ""; String langsQueryString = ""; if (req.queryParams().contains("repo")) { repos = req.queryParamsValues("repo"); if (repos.length != 0) { List<String> reposList = Arrays.asList(repos).stream() .map((s) -> "reponame:" + QueryParser.escape(s)).collect(Collectors.toList()); reposFilter = " && (" + StringUtils.join(reposList, " || ") + ")"; List<String> reposQueryList = Arrays.asList(repos).stream() .map((s) -> "&repo=" + URLEncoder.encode(s)).collect(Collectors.toList()); reposQueryString = StringUtils.join(reposQueryList, ""); } } if (req.queryParams().contains("lan")) { langs = req.queryParamsValues("lan"); if (langs.length != 0) { List<String> langsList = Arrays.asList(langs).stream() .map((s) -> "languagename:" + QueryParser.escape(s)).collect(Collectors.toList()); langsFilter = " && (" + StringUtils.join(langsList, " || ") + ")"; List<String> langsQueryList = Arrays.asList(langs).stream() .map((s) -> "&lan=" + URLEncoder.encode(s)).collect(Collectors.toList()); langsQueryString = StringUtils.join(langsQueryList, ""); } } // split the query escape it and and it together String cleanQueryString = scl.formatQueryString(query); SearchResult searchResult = cs.search(cleanQueryString + reposFilter + langsFilter, page); searchResult.setCodeResultList(cm.formatResults(searchResult.getCodeResultList(), query, true)); for (CodeFacetRepo f : searchResult.getRepoFacetResults()) { if (Arrays.asList(repos).contains(f.getRepoName())) { f.setSelected(true); } } for (CodeFacetLanguage f : searchResult.getLanguageFacetResults()) { if (Arrays.asList(langs).contains(f.getLanguageName())) { f.setSelected(true); } } map.put("searchValue", query); map.put("searchResult", searchResult); map.put("reposQueryString", reposQueryString); map.put("langsQueryString", langsQueryString); map.put("altQuery", altquery); map.put("isHtml", true); map.put("logoImage", getLogo()); map.put("isCommunity", ISCOMMUNITY); return new ModelAndView(map, "searchresults.ftl"); } // Totally pointless vanity but lets rotate the image every week int photoId = getWeekOfMonth(); if (photoId <= 0) { photoId = 3; } if (photoId > 4) { photoId = 2; } map.put("photoId", photoId); map.put("numDocs", cs.getTotalNumberDocumentsIndexed()); map.put("logoImage", getLogo()); map.put("isCommunity", ISCOMMUNITY); return new ModelAndView(map, "index.ftl"); }, new FreeMarkerEngine()); /** * Allows one to write literal lucene search queries against the index * TODO This is still very much WIP */ get("/literal/", (req, res) -> { CodeSearcher cs = new CodeSearcher(); CodeMatcher cm = new CodeMatcher(data); Map<String, Object> map = new HashMap<>(); map.put("repoCount", repo.getRepoCount()); if (req.queryParams().contains("q")) { String query = req.queryParams("q").trim(); int page = 0; if (req.queryParams().contains("p")) { try { page = Integer.parseInt(req.queryParams("p")); page = page > 19 ? 19 : page; } catch (NumberFormatException ex) { page = 0; } } String altquery = query.replaceAll("[^A-Za-z0-9 ]", " ").trim().replaceAll(" +", " "); SearchResult searchResult = cs.search(query, page); searchResult.setCodeResultList(cm.formatResults(searchResult.getCodeResultList(), altquery, false)); map.put("searchValue", query); map.put("searchResult", searchResult); map.put("reposQueryString", ""); map.put("langsQueryString", ""); map.put("altQuery", ""); map.put("logoImage", getLogo()); map.put("isCommunity", ISCOMMUNITY); return new ModelAndView(map, "searchresults.ftl"); } map.put("numDocs", cs.getTotalNumberDocumentsIndexed()); map.put("logoImage", getLogo()); map.put("isCommunity", ISCOMMUNITY); return new ModelAndView(map, "index.ftl"); }, new FreeMarkerEngine()); /** * This is the endpoint used by the frontend. */ get("/api/codesearch/", (req, res) -> { CodeSearcher cs = new CodeSearcher(); CodeMatcher cm = new CodeMatcher(data); if (req.queryParams().contains("q") && req.queryParams("q").trim() != "") { String query = req.queryParams("q").trim(); int page = 0; if (req.queryParams().contains("p")) { try { page = Integer.parseInt(req.queryParams("p")); page = page > 19 ? 19 : page; } catch (NumberFormatException ex) { page = 0; } } String[] repos = new String[0]; String[] langs = new String[0]; String[] owners = new String[0]; String reposFilter = ""; String langsFilter = ""; String ownersFilter = ""; if (req.queryParams().contains("repo")) { repos = req.queryParamsValues("repo"); if (repos.length != 0) { List<String> reposList = Arrays.asList(repos).stream() .map((s) -> "reponame:" + QueryParser.escape(s)).collect(Collectors.toList()); reposFilter = " && (" + StringUtils.join(reposList, " || ") + ")"; } } if (req.queryParams().contains("lan")) { langs = req.queryParamsValues("lan"); if (langs.length != 0) { List<String> langsList = Arrays.asList(langs).stream() .map((s) -> "languagename:" + QueryParser.escape(s)).collect(Collectors.toList()); langsFilter = " && (" + StringUtils.join(langsList, " || ") + ")"; } } if (req.queryParams().contains("own")) { owners = req.queryParamsValues("own"); if (owners.length != 0) { List<String> ownersList = Arrays.asList(owners).stream() .map((s) -> "codeowner:" + QueryParser.escape(s)).collect(Collectors.toList()); ownersFilter = " && (" + StringUtils.join(ownersList, " || ") + ")"; } } // Need to pass in the filters into this query String cacheKey = query + page + reposFilter + langsFilter + ownersFilter; if (cache.containsKey(cacheKey)) { return cache.get(cacheKey); } // split the query escape it and and it together String cleanQueryString = scl.formatQueryString(query); SearchResult searchResult = cs.search(cleanQueryString + reposFilter + langsFilter + ownersFilter, page); searchResult.setCodeResultList(cm.formatResults(searchResult.getCodeResultList(), query, true)); searchResult.setQuery(query); for (String altQuery : scl.generateAltQueries(query)) { searchResult.addAltQuery(altQuery); } // Null out code as it isnt required and there is no point in bloating our ajax requests for (CodeResult codeSearchResult : searchResult.getCodeResultList()) { codeSearchResult.setCode(null); } cache.put(cacheKey, searchResult); return searchResult; } return null; }, new JsonTransformer()); get("/api/repo/add/", "application/json", (request, response) -> { boolean apiEnabled = Boolean .parseBoolean(Properties.getProperties().getProperty("api_enabled", "false")); boolean apiAuth = Boolean .parseBoolean(Properties.getProperties().getProperty("api_key_authentication", "true")); if (!apiEnabled) { return new ApiResponse(false, "API not enabled"); } String publicKey = request.queryParams("pub"); String signedKey = request.queryParams("sig"); String reponames = request.queryParams("reponame"); String repourls = request.queryParams("repourl"); String repotype = request.queryParams("repotype"); String repousername = request.queryParams("repousername"); String repopassword = request.queryParams("repopassword"); String reposource = request.queryParams("reposource"); String repobranch = request.queryParams("repobranch"); if (reponames == null || reponames.trim().equals(Values.EMPTYSTRING)) { return new ApiResponse(false, "reponame is a required parameter"); } if (repourls == null || repourls.trim().equals(Values.EMPTYSTRING)) { return new ApiResponse(false, "repourl is a required parameter"); } if (repotype == null) { return new ApiResponse(false, "repotype is a required parameter"); } if (repousername == null) { return new ApiResponse(false, "repousername is a required parameter"); } if (repopassword == null) { return new ApiResponse(false, "repopassword is a required parameter"); } if (reposource == null) { return new ApiResponse(false, "reposource is a required parameter"); } if (repobranch == null) { return new ApiResponse(false, "repobranch is a required parameter"); } if (apiAuth) { if (publicKey == null || publicKey.trim().equals(Values.EMPTYSTRING)) { return new ApiResponse(false, "pub is a required parameter"); } if (signedKey == null || signedKey.trim().equals(Values.EMPTYSTRING)) { return new ApiResponse(false, "sig is a required parameter"); } String toValidate = String.format( "pub=%s&reponame=%s&repourl=%s&repotype=%s&repousername=%s&repopassword=%s&reposource=%s&repobranch=%s", URLEncoder.encode(publicKey), URLEncoder.encode(reponames), URLEncoder.encode(repourls), URLEncoder.encode(repotype), URLEncoder.encode(repousername), URLEncoder.encode(repopassword), URLEncoder.encode(reposource), URLEncoder.encode(repobranch)); boolean validRequest = apiService.validateRequest(publicKey, signedKey, toValidate); if (!validRequest) { return new ApiResponse(false, "invalid signed url"); } } // Clean if (repobranch == null || repobranch.trim().equals(Values.EMPTYSTRING)) { repobranch = "master"; } repotype = repotype.trim().toLowerCase(); if (!"git".equals(repotype) && !"svn".equals(repotype)) { repotype = "git"; } RepoResult repoResult = repo.getRepoByName(reponames); if (repoResult != null) { return new ApiResponse(false, "repository name already exists"); } repo.saveRepo(new RepoResult(-1, reponames, repotype, repourls, repousername, repopassword, reposource, repobranch)); return new ApiResponse(true, "added repository successfully"); }, new JsonTransformer()); get("/api/repo/delete/", "application/json", (request, response) -> { boolean apiEnabled = Boolean .parseBoolean(Properties.getProperties().getProperty("api_enabled", "false")); boolean apiAuth = Boolean .parseBoolean(Properties.getProperties().getProperty("api_key_authentication", "true")); if (!apiEnabled) { return new ApiResponse(false, "API not enabled"); } String publicKey = request.queryParams("pub"); String signedKey = request.queryParams("sig"); String reponames = request.queryParams("reponame"); if (reponames == null || reponames.trim().equals(Values.EMPTYSTRING)) { return new ApiResponse(false, "reponame is a required parameter"); } if (apiAuth) { if (publicKey == null || publicKey.trim().equals(Values.EMPTYSTRING)) { return new ApiResponse(false, "pub is a required parameter"); } if (signedKey == null || signedKey.trim().equals(Values.EMPTYSTRING)) { return new ApiResponse(false, "sig is a required parameter"); } String toValidate = String.format("pub=%s&reponame=%s", URLEncoder.encode(publicKey), URLEncoder.encode(reponames)); boolean validRequest = apiService.validateRequest(publicKey, signedKey, toValidate); if (!validRequest) { return new ApiResponse(false, "invalid signed url"); } } RepoResult rr = repo.getRepoByName(reponames); if (rr == null) { return new ApiResponse(false, "repository already deleted"); } Singleton.getUniqueDeleteRepoQueue().add(rr); return new ApiResponse(true, "repository queued for deletion"); }, new JsonTransformer()); get("/api/repo/list/", "application/json", (request, response) -> { boolean apiEnabled = Boolean .parseBoolean(Properties.getProperties().getProperty("api_enabled", "false")); boolean apiAuth = Boolean .parseBoolean(Properties.getProperties().getProperty("api_key_authentication", "true")); if (!apiEnabled) { return new ApiResponse(false, "API not enabled"); } String publicKey = request.queryParams("pub"); String signedKey = request.queryParams("sig"); if (apiAuth) { if (publicKey == null || publicKey.trim().equals(Values.EMPTYSTRING)) { return new ApiResponse(false, "pub is a required parameter"); } if (signedKey == null || signedKey.trim().equals(Values.EMPTYSTRING)) { return new ApiResponse(false, "sig is a required parameter"); } String toValidate = String.format("pub=%s", URLEncoder.encode(publicKey)); boolean validRequest = apiService.validateRequest(publicKey, signedKey, toValidate); if (!validRequest) { return new ApiResponse(false, "invalid signed url"); } } List<RepoResult> repoResultList = repo.getAllRepo(); return new RepoResultApiResponse(true, Values.EMPTYSTRING, repoResultList); }, new JsonTransformer()); get("/admin/", (request, response) -> { if (getAuthenticatedUser(request) == null) { response.redirect("/login/"); halt(); return null; } CodeSearcher cs = new CodeSearcher(); Map<String, Object> map = new HashMap<>(); map.put("repoCount", repo.getRepoCount()); map.put("numDocs", cs.getTotalNumberDocumentsIndexed()); map.put("numSearches", statsService.getSearchCount()); map.put("uptime", statsService.getUptime()); // Put all properties here map.put(Values.SQLITEFILE, Properties.getProperties().getProperty(Values.SQLITEFILE, Values.DEFAULTSQLITEFILE)); map.put(Values.SERVERPORT, Properties.getProperties().getProperty(Values.SERVERPORT, Values.DEFAULTSERVERPORT)); map.put(Values.REPOSITORYLOCATION, Properties.getProperties().getProperty(Values.REPOSITORYLOCATION, Values.DEFAULTREPOSITORYLOCATION)); map.put(Values.INDEXLOCATION, Properties.getProperties().getProperty(Values.INDEXLOCATION, Values.DEFAULTINDEXLOCATION)); map.put(Values.FACETSLOCATION, Properties.getProperties().getProperty(Values.FACETSLOCATION, Values.DEFAULTFACETSLOCATION)); map.put(Values.CHECKREPOCHANGES, Properties.getProperties().getProperty(Values.CHECKREPOCHANGES, Values.DEFAULTCHECKREPOCHANGES)); map.put(Values.ONLYLOCALHOST, Properties.getProperties().getProperty(Values.ONLYLOCALHOST, Values.DEFAULTONLYLOCALHOST)); map.put(Values.LOWMEMORY, Properties.getProperties().getProperty(Values.LOWMEMORY, Values.DEFAULTLOWMEMORY)); map.put(Values.SPELLINGCORRECTORSIZE, Properties.getProperties() .getProperty(Values.SPELLINGCORRECTORSIZE, Values.DEFAULTSPELLINGCORRECTORSIZE)); map.put(Values.USESYSTEMGIT, Properties.getProperties().getProperty(Values.USESYSTEMGIT, Values.DEFAULTUSESYSTEMGIT)); map.put(Values.GITBINARYPATH, Properties.getProperties().getProperty(Values.GITBINARYPATH, Values.DEFAULTGITBINARYPATH)); map.put(Values.APIENABLED, Properties.getProperties().getProperty(Values.APIENABLED, Values.DEFAULTAPIENABLED)); map.put(Values.APIKEYAUTH, Properties.getProperties().getProperty(Values.APIKEYAUTH, Values.DEFAULTAPIKEYAUTH)); map.put(Values.SVNBINARYPATH, Properties.getProperties().getProperty(Values.SVNBINARYPATH, Values.DEFAULTSVNBINARYPATH)); map.put(Values.SVNENABLED, Properties.getProperties().getProperty(Values.SVNENABLED, Values.DEFAULTSVNENABLED)); map.put(Values.MAXDOCUMENTQUEUESIZE, Properties.getProperties().getProperty(Values.MAXDOCUMENTQUEUESIZE, Values.DEFAULTMAXDOCUMENTQUEUESIZE)); map.put(Values.MAXDOCUMENTQUEUELINESIZE, Properties.getProperties() .getProperty(Values.MAXDOCUMENTQUEUELINESIZE, Values.DEFAULTMAXDOCUMENTQUEUELINESIZE)); map.put(Values.MAXFILELINEDEPTH, Properties.getProperties().getProperty(Values.MAXFILELINEDEPTH, Values.DEFAULTMAXFILELINEDEPTH)); map.put("deletionQueue", Singleton.getUniqueDeleteRepoQueue().size()); map.put("version", VERSION); map.put("logoImage", getLogo()); map.put("isCommunity", ISCOMMUNITY); return new ModelAndView(map, "admin.ftl"); }, new FreeMarkerEngine()); get("/admin/repo/", (request, response) -> { if (getAuthenticatedUser(request) == null) { response.redirect("/login/"); halt(); return null; } int repoCount = repo.getRepoCount(); String offSet = request.queryParams("offset"); String searchQuery = request.queryParams("q"); int indexOffset = 0; Map<String, Object> map = new HashMap<>(); if (offSet != null) { try { indexOffset = Integer.parseInt(offSet); if (indexOffset > repoCount || indexOffset < 0) { indexOffset = 0; } } catch (NumberFormatException ex) { indexOffset = 0; } } if (searchQuery != null) { map.put("repoResults", repo.searchRepo(searchQuery)); } else { map.put("repoResults", repo.getPagedRepo(indexOffset, 100)); } map.put("searchQuery", searchQuery); map.put("hasPrevious", indexOffset > 0); map.put("hasNext", (indexOffset + 100) < repoCount); map.put("previousOffset", "" + (indexOffset - 100)); map.put("nextOffset", "" + (indexOffset + 100)); map.put("logoImage", getLogo()); map.put("isCommunity", ISCOMMUNITY); return new ModelAndView(map, "admin_repo.ftl"); }, new FreeMarkerEngine()); get("/admin/bulk/", (request, response) -> { if (getAuthenticatedUser(request) == null) { response.redirect("/login/"); halt(); return null; } Map<String, Object> map = new HashMap<>(); map.put("logoImage", getLogo()); map.put("isCommunity", ISCOMMUNITY); return new ModelAndView(map, "admin_bulk.ftl"); }, new FreeMarkerEngine()); get("/admin/api/", (request, response) -> { if (getAuthenticatedUser(request) == null) { response.redirect("/login/"); halt(); return null; } Map<String, Object> map = new HashMap<>(); map.put("apiKeys", api.getAllApi()); boolean apiEnabled = Boolean .parseBoolean(Properties.getProperties().getProperty("api_enabled", "false")); boolean apiAuth = Boolean .parseBoolean(Properties.getProperties().getProperty("api_key_authentication", "true")); map.put("apiAuthentication", apiEnabled && apiAuth); map.put("logoImage", getLogo()); map.put("isCommunity", ISCOMMUNITY); return new ModelAndView(map, "admin_api.ftl"); }, new FreeMarkerEngine()); post("/admin/api/", (request, response) -> { if (getAuthenticatedUser(request) == null) { response.redirect("/login/"); halt(); return null; } apiService.createKeys(); response.redirect("/admin/api/"); halt(); return null; }, new FreeMarkerEngine()); get("/admin/api/delete/", "application/json", (request, response) -> { if (getAuthenticatedUser(request) == null || !request.queryParams().contains("publicKey")) { response.redirect("/login/"); halt(); return false; } String publicKey = request.queryParams("publicKey"); apiService.deleteKey(publicKey); return true; }, new JsonTransformer()); get("/admin/settings/", (request, response) -> { if (getAuthenticatedUser(request) == null) { response.redirect("/login/"); halt(); return null; } String[] highlighters = "agate,androidstudio,arta,ascetic,atelier-cave.dark,atelier-cave.light,atelier-dune.dark,atelier-dune.light,atelier-estuary.dark,atelier-estuary.light,atelier-forest.dark,atelier-forest.light,atelier-heath.dark,atelier-heath.light,atelier-lakeside.dark,atelier-lakeside.light,atelier-plateau.dark,atelier-plateau.light,atelier-savanna.dark,atelier-savanna.light,atelier-seaside.dark,atelier-seaside.light,atelier-sulphurpool.dark,atelier-sulphurpool.light,brown_paper,codepen-embed,color-brewer,dark,darkula,default,docco,far,foundation,github-gist,github,googlecode,grayscale,hopscotch,hybrid,idea,ir_black,kimbie.dark,kimbie.light,magula,mono-blue,monokai,monokai_sublime,obsidian,paraiso.dark,paraiso.light,pojoaque,railscasts,rainbow,school_book,solarized_dark,solarized_light,sunburst,tomorrow-night-blue,tomorrow-night-bright,tomorrow-night-eighties,tomorrow-night,tomorrow,vs,xcode,zenburn" .split(","); Map<String, Object> map = new HashMap<>(); map.put("logoImage", getLogo()); map.put("syntaxHighlighter", getSyntaxHighlighter()); map.put("highlighters", highlighters); map.put("averageSalary", "" + (int) getAverageSalary()); map.put("matchLines", "" + (int) getMatchLines()); map.put("maxLineDepth", "" + (int) getMaxLineDepth()); map.put("minifiedLength", "" + (int) getMinifiedLength()); map.put("isCommunity", ISCOMMUNITY); return new ModelAndView(map, "admin_settings.ftl"); }, new FreeMarkerEngine()); get("/admin/reports/", (request, response) -> { if (getAuthenticatedUser(request) == null) { response.redirect("/login/"); halt(); return null; } Map<String, Object> map = new HashMap<>(); map.put("logoImage", getLogo()); map.put("isCommunity", ISCOMMUNITY); return new ModelAndView(map, "admin_reports.ftl"); }, new FreeMarkerEngine()); post("/admin/settings/", (request, response) -> { if (getAuthenticatedUser(request) == null) { response.redirect("/login/"); halt(); return null; } if (ISCOMMUNITY) { response.redirect("/admin/settings/"); halt(); } String logo = request.queryParams("logo").trim(); String syntaxHighlighter = request.queryParams("syntaxhighligher"); try { double averageSalary = Double.parseDouble(request.queryParams("averagesalary")); data.saveData(Values.AVERAGESALARY, "" + (int) averageSalary); } catch (NumberFormatException ex) { data.saveData(Values.AVERAGESALARY, Values.DEFAULTAVERAGESALARY); } try { double averageSalary = Double.parseDouble(request.queryParams("matchlines")); data.saveData(Values.MATCHLINES, "" + (int) averageSalary); } catch (NumberFormatException ex) { data.saveData(Values.MATCHLINES, Values.DEFAULTMATCHLINES); } try { double averageSalary = Double.parseDouble(request.queryParams("maxlinedepth")); data.saveData(Values.MAXLINEDEPTH, "" + (int) averageSalary); } catch (NumberFormatException ex) { data.saveData(Values.MAXLINEDEPTH, Values.DEFAULTMAXLINEDEPTH); } try { double minifiedlength = Double.parseDouble(request.queryParams("minifiedlength")); data.saveData(Values.MINIFIEDLENGTH, "" + (int) minifiedlength); } catch (NumberFormatException ex) { data.saveData(Values.MINIFIEDLENGTH, Values.DEFAULTMINIFIEDLENGTH); } data.saveData(Values.LOGO, logo); data.saveData(Values.SYNTAXHIGHLIGHTER, syntaxHighlighter); // Redo anything that requires updates at this point scl = Singleton.getSearchcodeLib(data); response.redirect("/admin/settings/"); halt(); return null; }, new FreeMarkerEngine()); post("/admin/bulk/", (request, response) -> { if (getAuthenticatedUser(request) == null) { response.redirect("/login/"); halt(); return null; } String repos = request.queryParams("repos"); String repolines[] = repos.split("\\r?\\n"); for (String line : repolines) { String[] repoparams = line.split(",", -1); if (repoparams.length == 7) { String branch = repoparams[6].trim(); if (branch.equals(Values.EMPTYSTRING)) { branch = "master"; } String scm = repoparams[1].trim().toLowerCase(); if (scm.equals(Values.EMPTYSTRING)) { scm = "git"; } RepoResult rr = repo.getRepoByName(repoparams[0]); if (rr == null) { repo.saveRepo(new RepoResult(-1, repoparams[0], scm, repoparams[2], repoparams[3], repoparams[4], repoparams[5], branch)); } } } response.redirect("/admin/bulk/"); halt(); return null; }, new FreeMarkerEngine()); post("/admin/repo/", (request, response) -> { if (getAuthenticatedUser(request) == null) { response.redirect("/login/"); halt(); return null; } String[] reponames = request.queryParamsValues("reponame"); String[] reposcms = request.queryParamsValues("reposcm"); String[] repourls = request.queryParamsValues("repourl"); String[] repousername = request.queryParamsValues("repousername"); String[] repopassword = request.queryParamsValues("repopassword"); String[] reposource = request.queryParamsValues("reposource"); String[] repobranch = request.queryParamsValues("repobranch"); for (int i = 0; i < reponames.length; i++) { if (reponames[i].trim().length() != 0) { String branch = repobranch[i].trim(); if (branch.equals(Values.EMPTYSTRING)) { branch = "master"; } repo.saveRepo(new RepoResult(-1, reponames[i], reposcms[i], repourls[i], repousername[i], repopassword[i], reposource[i], branch)); } } response.redirect("/admin/repo/"); halt(); return null; }, new FreeMarkerEngine()); get("/login/", (request, response) -> { if (getAuthenticatedUser(request) != null) { response.redirect("/admin/"); halt(); return null; } Map<String, Object> map = new HashMap<>(); map.put("logoImage", getLogo()); map.put("isCommunity", ISCOMMUNITY); return new ModelAndView(map, "login.ftl"); }, new FreeMarkerEngine()); post("/login/", (req, res) -> { if (req.queryParams().contains("password") && req.queryParams("password") .equals(com.searchcode.app.util.Properties.getProperties().getProperty("password"))) { addAuthenticatedUser(req); res.redirect("/admin/"); halt(); } Map<String, Object> map = new HashMap<>(); map.put("logoImage", getLogo()); map.put("isCommunity", ISCOMMUNITY); return new ModelAndView(map, "login.ftl"); }, new FreeMarkerEngine()); get("/logout/", (req, res) -> { removeAuthenticatedUser(req); res.redirect("/"); return null; }); get("/admin/delete/", "application/json", (request, response) -> { if (getAuthenticatedUser(request) == null || !request.queryParams().contains("repoName")) { response.redirect("/login/"); halt(); return false; } String repoName = request.queryParams("repoName"); RepoResult rr = repo.getRepoByName(repoName); if (rr != null) { Singleton.getUniqueDeleteRepoQueue().add(rr); } return true; }, new JsonTransformer()); get("/admin/checkversion/", "application/json", (request, response) -> { if (getAuthenticatedUser(request) == null) { response.redirect("/login/"); halt(); return false; } String version; try { version = IOUtils.toString(new URL("https://searchcode.com/product/version/")).replace("\"", Values.EMPTYSTRING); } catch (IOException ex) { return "Unable to determine if running the latest version. Check https://searchcode.com/product/download/ for the latest release."; } if (App.VERSION.equals(version)) { return "Your searchcode server version " + version + " is the latest."; } else { return "Your searchcode server version " + App.VERSION + " instance is out of date. The latest version is " + version + "."; } }, new JsonTransformer()); get("/file/:codeid/:reponame/*", (request, response) -> { Map<String, Object> map = new HashMap<>(); CodeSearcher cs = new CodeSearcher(); Cocomo2 coco = new Cocomo2(); StringBuilder code = new StringBuilder(); String fileName = Values.EMPTYSTRING; if (request.splat().length != 0) { fileName = request.splat()[0]; } CodeResult codeResult = cs.getByRepoFileName(request.params(":reponame"), fileName); if (codeResult == null) { int codeid = Integer.parseInt(request.params(":codeid")); codeResult = cs.getById(codeid); } if (codeResult == null) { response.redirect("/404/"); halt(); } List<String> codeLines = codeResult.code; for (int i = 0; i < codeLines.size(); i++) { code.append("<span id=\"" + (i + 1) + "\"></span>"); code.append(StringEscapeUtils.escapeHtml4(codeLines.get(i))); code.append("\n"); } boolean highlight = true; if (Integer.parseInt(codeResult.codeLines) > 1000) { highlight = false; } RepoResult repoResult = repo.getRepoByName(codeResult.repoName); if (repoResult != null) { map.put("source", repoResult.getSource()); } map.put("fileName", codeResult.fileName); map.put("codePath", codeResult.codePath); map.put("codeLength", codeResult.codeLines); map.put("languageName", codeResult.languageName); map.put("md5Hash", codeResult.md5hash); map.put("repoName", codeResult.repoName); map.put("highlight", highlight); map.put("repoLocation", codeResult.getRepoLocation()); map.put("codeValue", code.toString()); map.put("highligher", getSyntaxHighlighter()); map.put("codeOwner", codeResult.getCodeOwner()); double estimatedEffort = coco.estimateEffort(scl.countFilteredLines(codeResult.getCode())); int estimatedCost = (int) coco.estimateCost(estimatedEffort, getAverageSalary()); if (estimatedCost != 0 && !scl.languageCostIgnore(codeResult.getLanguageName())) { map.put("estimatedCost", estimatedCost); } map.put("logoImage", getLogo()); map.put("isCommunity", ISCOMMUNITY); return new ModelAndView(map, "coderesult.ftl"); }, new FreeMarkerEngine()); /** * Deprecated should not be used * TODO delete this method */ get("/codesearch/view/:codeid", (request, response) -> { Map<String, Object> map = new HashMap<>(); int codeid = Integer.parseInt(request.params(":codeid")); CodeSearcher cs = new CodeSearcher(); Cocomo2 coco = new Cocomo2(); StringBuilder code = new StringBuilder(); // escape all the lines and include deeplink for line number CodeResult codeResult = cs.getById(codeid); if (codeResult == null) { response.redirect("/404/"); halt(); } List<String> codeLines = codeResult.code; for (int i = 0; i < codeLines.size(); i++) { code.append("<span id=\"" + (i + 1) + "\"></span>"); code.append(StringEscapeUtils.escapeHtml4(codeLines.get(i))); code.append("\n"); } boolean highlight = true; if (Integer.parseInt(codeResult.codeLines) > 1000) { highlight = false; } RepoResult repoResult = repo.getRepoByName(codeResult.repoName); if (repoResult != null) { map.put("source", repoResult.getSource()); } map.put("fileName", codeResult.fileName); map.put("codePath", codeResult.codePath); map.put("codeLength", codeResult.codeLines); map.put("languageName", codeResult.languageName); map.put("md5Hash", codeResult.md5hash); map.put("repoName", codeResult.repoName); map.put("highlight", highlight); map.put("repoLocation", codeResult.getRepoLocation()); map.put("codeValue", code.toString()); map.put("highligher", getSyntaxHighlighter()); map.put("codeOwner", codeResult.getCodeOwner()); double estimatedEffort = coco.estimateEffort(scl.countFilteredLines(codeResult.getCode())); int estimatedCost = (int) coco.estimateCost(estimatedEffort, getAverageSalary()); if (estimatedCost != 0 && !scl.languageCostIgnore(codeResult.getLanguageName())) { map.put("estimatedCost", estimatedCost); } map.put("logoImage", getLogo()); map.put("isCommunity", ISCOMMUNITY); return new ModelAndView(map, "coderesult.ftl"); }, new FreeMarkerEngine()); get("/documentation/", (request, response) -> { Map<String, Object> map = new HashMap<>(); map.put("logoImage", getLogo()); map.put("isCommunity", ISCOMMUNITY); return new ModelAndView(map, "documentation.ftl"); }, new FreeMarkerEngine()); get("/search_test/", (request, response) -> { Map<String, Object> map = new HashMap<>(); map.put("logoImage", getLogo()); map.put("isCommunity", ISCOMMUNITY); return new ModelAndView(map, "search_test.ftl"); }, new FreeMarkerEngine()); get("/404/", (request, response) -> { Map<String, Object> map = new HashMap<>(); map.put("logoImage", getLogo()); map.put("isCommunity", ISCOMMUNITY); return new ModelAndView(map, "404.ftl"); }, new FreeMarkerEngine()); /** * Test that was being used to display blame information */ // get("/test/:reponame/*", (request, response) -> { // User user = injector.getInstance(User.class); // user.Blame(request.params(":reponame"), request.splat()[0]); // return ""; // }, new JsonTransformer()); }
From source file:gov.lanl.adore.djatoka.DjatokaCompress.java
/** * Uses apache commons cli to parse input args. Passes parsed * parameters to ICompress implementation. * @param args command line parameters to defined input,output,etc. */// www . j a va 2s .c om public static void main(String[] args) { // create the command line parser CommandLineParser parser = new PosixParser(); // create the Options Options options = new Options(); options.addOption("i", "input", true, "Filepath of the input file or dir."); options.addOption("o", "output", true, "Filepath of the output file or dir."); options.addOption("r", "rate", true, "Absolute Compression Ratio"); options.addOption("s", "slope", true, "Used to generate relative compression ratio based on content characteristics."); options.addOption("y", "Clayers", true, "Number of quality levels."); options.addOption("l", "Clevels", true, "Number of DWT levels (reolution levels)."); options.addOption("v", "Creversible", true, "Use Reversible Wavelet"); options.addOption("c", "Cprecincts", true, "Precinct dimensions"); options.addOption("p", "props", true, "Compression Properties File"); options.addOption("d", "Corder", true, "Progression order"); options.addOption("g", "ORGgen_plt", true, "Enables insertion of packet length information in the header"); options.addOption("t", "ORGtparts", true, "Division of each tile's packets into tile-parts"); options.addOption("b", "Cblk", true, "Codeblock Size"); options.addOption("a", "AltImpl", true, "Alternate ICompress Implemenation"); try { if (args.length == 0) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("gov.lanl.adore.djatoka.DjatokaCompress", options); System.exit(0); } // parse the command line arguments CommandLine line = parser.parse(options, args); String input = line.getOptionValue("i"); String output = line.getOptionValue("o"); String propsFile = line.getOptionValue("p"); DjatokaEncodeParam p; if (propsFile != null) { Properties props = IOUtils.loadConfigByPath(propsFile); p = new DjatokaEncodeParam(props); } else p = new DjatokaEncodeParam(); String rate = line.getOptionValue("r"); if (rate != null) p.setRate(rate); String slope = line.getOptionValue("s"); if (slope != null) p.setSlope(slope); String Clayers = line.getOptionValue("y"); if (Clayers != null) p.setLayers(Integer.parseInt(Clayers)); String Clevels = line.getOptionValue("l"); if (Clevels != null) p.setLevels(Integer.parseInt(Clevels)); String Creversible = line.getOptionValue("v"); if (Creversible != null) p.setUseReversible(Boolean.parseBoolean(Creversible)); String Cprecincts = line.getOptionValue("c"); if (Cprecincts != null) p.setPrecincts(Cprecincts); String Corder = line.getOptionValue("d"); if (Corder != null) p.setProgressionOrder(Corder); String ORGgen_plt = line.getOptionValue("g"); if (ORGgen_plt != null) p.setInsertPLT(Boolean.parseBoolean(ORGgen_plt)); String Cblk = line.getOptionValue("b"); if (Cblk != null) p.setCodeBlockSize(Cblk); String alt = line.getOptionValue("a"); ICompress jp2 = new KduCompressExe(); if (alt != null) jp2 = (ICompress) Class.forName(alt).newInstance(); if (new File(input).isDirectory() && new File(output).isDirectory()) { ArrayList<File> files = IOUtils.getFileList(input, new SourceImageFileFilter(), false); for (File f : files) { long x = System.currentTimeMillis(); File outFile = new File(output, f.getName().substring(0, f.getName().indexOf(".")) + ".jp2"); compress(jp2, f.getAbsolutePath(), outFile.getAbsolutePath(), p); report(f.getAbsolutePath(), x); } } else { long x = System.currentTimeMillis(); File f = new File(input); if (output == null) output = f.getName().substring(0, f.getName().indexOf(".")) + ".jp2"; if (new File(output).isDirectory()) output = output + f.getName().substring(0, f.getName().indexOf(".")) + ".jp2"; compress(jp2, input, output, p); report(input, x); } } catch (ParseException e) { logger.error("Parse exception:" + e.getMessage(), e); } catch (DjatokaException e) { logger.error("djatoka Compression exception:" + e.getMessage(), e); } catch (InstantiationException e) { logger.error("Unable to initialize alternate implemenation:" + e.getMessage(), e); } catch (Exception e) { logger.error("An exception occured:" + e.getMessage(), e); } }
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);/*from www. ja v a 2s . co 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:eu.amidst.core.inference.ImportanceSamplingExperiments.java
/** * The class constructor.//from w w w. j a va 2 s. c o m * @param args Array of options: "filename variable a b N useVMP" if variable is continuous or "filename variable w N useVMP" for discrete */ public static void main(String[] args) throws Exception { //if (Main.VERBOSE) System.out.println("CONTINUOUS VARIABLE"); boolean discrete = false; String filename = ""; //Filename with the Bayesian Network String varname = ""; // Variable of interest in the BN double a = 0; // Lower endpoint of the interval double b = 0; // Upper endpoint of the interval int N = 0; // Sample size boolean useVMP = false; // Boolean indicating whether use VMP or not // FOR A CONTINUOUS VARIABLE OF INTEREST if (args.length == 6) { filename = args[0]; //Filename with the Bayesian Network varname = args[1]; // Variable of interest in the BN String aa = args[2]; // Lower endpoint of the interval String bb = args[3]; // Upper endpoint of the interval String NN = args[4]; // Sample size String useVMParg = args[5]; // Boolean indicating whether use VMP or not try { a = Double.parseDouble(aa); b = Double.parseDouble(bb); N = Integer.parseInt(NN); useVMP = Boolean.parseBoolean(useVMParg); } catch (NumberFormatException e) { if (Main.VERBOSE) System.out.println(e.toString()); } } // FOR A DISCRETE VARIABLE OF INTEREST else if (args.length == 5) { //if (Main.VERBOSE) System.out.println("DISCRETE VARIABLE"); discrete = true; if (Main.VERBOSE) System.out.println("Not available yet"); System.exit(1); } else if (args.length == 0) { filename = "networks/simulated/Bayesian10Vars15Links.bn"; //Filename with the Bayesian Network //filename="networks/Bayesian2Vars1Link.bn"; varname = "GaussianVar1"; // Variable of interest in the BN a = 0; // Lower endpoint of the interval b = 1; // Upper endpoint of the interval N = 10000; // Sample size useVMP = false; // Boolean indicating whether use VMP or not } else { if (Main.VERBOSE) System.out.println("Invalid number of arguments. See comments in main"); System.exit(1); } BayesianNetwork bn; VMP vmp = new VMP(); ImportanceSampling importanceSampling = new ImportanceSampling(); try { bn = BayesianNetworkLoader.loadFromFile(filename); if (Main.VERBOSE) System.out.println(bn.toString()); Variable varInterest = bn.getVariables().getVariableByName(varname); vmp.setModel(bn); vmp.runInference(); importanceSampling.setModel(bn); //importanceSampling.setSamplingModel(vmp.getSamplingModel()); importanceSampling.setSamplingModel(bn); importanceSampling.setParallelMode(true); importanceSampling.setKeepDataOnMemory(true); importanceSampling.setSampleSize(N); // Including evidence: Assignment assignment = randomEvidence(1823716125, 0.05, bn, varInterest); importanceSampling.setEvidence(assignment); //importanceSampling.setSamplingModel(vmp.getSamplingModel()); //importanceSampling.runInference(vmp); //if (Main.VERBOSE) System.out.println("Posterior of " + varInterest.getName() + " (IS w. Evidence VMP) :" + importanceSampling.getPosterior(varInterest).toString()); //importanceSampling.setSamplingModel(bn); importanceSampling.runInference(); if (Main.VERBOSE) System.out.println("Posterior of " + varInterest.getName() + " (IS w. Evidence) :" + importanceSampling.getPosterior(varInterest).toString()); if (Main.VERBOSE) System.out.println("Posterior of " + varInterest.getName() + " (VMP) :" + vmp.getPosterior(varInterest).toString()); if (Main.VERBOSE) System.out.println(); if (Main.VERBOSE) System.out.println("Variable of interest: " + varInterest.getName()); if (Main.VERBOSE) System.out.println(); a = 1.5; // Lower endpoint of the interval b = 10000; // Upper endpoint of the interval final double finalA = a; final double finalB = b; double result = importanceSampling.getExpectedValue(varInterest, v -> (finalA < v && v < finalB) ? 1.0 : 0.0); if (Main.VERBOSE) System.out.println("Query: P(" + Double.toString(a) + " < " + varInterest.getName() + " < " + Double.toString(b) + ")"); if (Main.VERBOSE) System.out.println("Probability result: " + result); /* if (Main.VERBOSE) System.out.println(); varname = "DiscreteVar2"; if (Main.VERBOSE) System.out.println(); Variable discreteVarInterest = bn.getVariables().getVariableByName(varname); if (Main.VERBOSE) System.out.println("Variable of interest: " + discreteVarInterest.getName()); importanceSampling.runInference(); int w=1; // Value of interest double result2 = importanceSampling.runQuery(discreteVarInterest, w); if (Main.VERBOSE) System.out.println("Query: P(" + discreteVarInterest.getName() + " = " + Integer.toString(w) + ")"); if (Main.VERBOSE) System.out.println("Probability result: " + result2);*/ } catch (Exception e) { if (Main.VERBOSE) System.out.println("Error loading Bayesian Network from file"); if (Main.VERBOSE) System.out.println(e.toString()); } }
From source file:eu.amidst.flinklink.examples.misc.DynamicParallelVMPExtended.java
/** * * ./bin/flink run -m yarn-cluster -yn 8 -ys 4 -yjm 1024 -ytm 9000 * -c eu.amidst.flinklink.examples.misc.DynamicParallelVMPExtended ../flinklink.jar 0 10 1000000 100 100 100 3 0 * * * @param args command line arguments//from w ww .j a v a2 s .com * @throws Exception */ public static void main(String[] args) throws Exception { int nCVars = Integer.parseInt(args[0]); int nMVars = Integer.parseInt(args[1]); int nSamples = Integer.parseInt(args[2]); int windowSize = Integer.parseInt(args[3]); int globalIter = Integer.parseInt(args[4]); int localIter = Integer.parseInt(args[5]); int nsets = Integer.parseInt(args[6]); int seed = Integer.parseInt(args[7]); boolean generateData = Boolean.parseBoolean(args[8]); /* * Generate dynamic datasets */ if (generateData) { String[] argsDatasets = ArrayUtils.addAll(ArrayUtils.subarray(args, 0, 4), ArrayUtils.subarray(args, 6, 8)); DynamicDataSets dynamicDataSets = new DynamicDataSets(); dynamicDataSets.generateDynamicDataset(argsDatasets); } /* * Logging */ //PropertyConfigurator.configure(args[6]); BasicConfigurator.configure(); logger.info("Starting DynamicVMPExtended experiments"); //String fileName = "hdfs:///tmp"+nCVars+"_"+nMVars+"_"+nSamples+"_"+nsets+"_"+seed; String fileName = "./datasets/tmp" + nCVars + "_" + nMVars + "_" + nSamples + "_" + nsets + "_" + seed; final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment(); DataFlink<DynamicDataInstance> data0 = DataFlinkLoader.loadDynamicDataFromFolder(env, fileName + "_iter_" + 0 + ".arff", false); DynamicDAG hiddenNB = getHiddenDynamicNaiveBayesStructure(data0.getAttributes(), false); System.out.println(hiddenNB.toString()); //Structure learning is excluded from the test, i.e., we use directly the initial Asia network structure // and just learn then test the parameter learning long start = System.nanoTime(); //Parameter Learning DynamicParallelVB parallelVB = new DynamicParallelVB(); parallelVB.setGlobalThreshold(0.1); parallelVB.setMaximumGlobalIterations(globalIter); parallelVB.setLocalThreshold(0.1); parallelVB.setMaximumLocalIterations(localIter); parallelVB.setSeed(5); parallelVB.setOutput(true); //Set the window size parallelVB.setBatchSize(windowSize); parallelVB.setDAG(hiddenNB); parallelVB.initLearning(); System.out.println("--------------- DATA " + 0 + " --------------------------"); parallelVB.updateModelWithNewTimeSlice(0, data0); for (int i = 1; i < nsets; i++) { logger.info("--------------- DATA " + i + " --------------------------"); DataFlink<DynamicDataInstance> dataNew = DataFlinkLoader.loadDynamicDataFromFolder(env, fileName + "_iter_" + i + ".arff", false); parallelVB.updateModelWithNewTimeSlice(i, dataNew); } logger.info(parallelVB.getLearntDynamicBayesianNetwork().toString()); long duration = (System.nanoTime() - start) / 1; double seconds = duration / 1000000000.0; logger.info("Running time: {} seconds.", seconds); }
From source file:TestBufferStreamGenomicsDBImporter.java
/** * Sample driver code for testing Java VariantContext write API for GenomicsDB * The code shows two ways of using the API * (a) Iterator<VariantContext>//from w ww .j av a 2 s . c o m * (b) Directly adding VariantContext objects * If "-iterators" is passed as the second argument, method (a) is used. */ public static void main(final String[] args) throws IOException, GenomicsDBException, ParseException { if (args.length < 2) { System.err.println("For loading: [-iterators] <loader.json> " + "<stream_name_to_file.json> [bufferCapacity rank lbRowIdx ubRowIdx useMultiChromosomeIterator]"); System.exit(-1); } int argsLoaderFileIdx = 0; if (args[0].equals("-iterators")) argsLoaderFileIdx = 1; //Buffer capacity long bufferCapacity = (args.length >= argsLoaderFileIdx + 3) ? Integer.parseInt(args[argsLoaderFileIdx + 2]) : 1024; //Specify rank (or partition idx) of this process int rank = (args.length >= argsLoaderFileIdx + 4) ? Integer.parseInt(args[argsLoaderFileIdx + 3]) : 0; //Specify smallest row idx from which to start loading. // This is useful for incremental loading into existing array long lbRowIdx = (args.length >= argsLoaderFileIdx + 5) ? Long.parseLong(args[argsLoaderFileIdx + 4]) : 0; //Specify largest row idx up to which loading should be performed - for completeness long ubRowIdx = (args.length >= argsLoaderFileIdx + 6) ? Long.parseLong(args[argsLoaderFileIdx + 5]) : Long.MAX_VALUE - 1; //Boolean to use MultipleChromosomeIterator boolean useMultiChromosomeIterator = (args.length >= argsLoaderFileIdx + 7) ? Boolean.parseBoolean(args[argsLoaderFileIdx + 6]) : false; //<loader.json> first arg String loaderJSONFile = args[argsLoaderFileIdx]; GenomicsDBImporter loader = new GenomicsDBImporter(loaderJSONFile, rank, lbRowIdx, ubRowIdx); //<stream_name_to_file.json> - useful for the driver only //JSON file that contains "stream_name": "vcf_file_path" entries FileReader mappingReader = new FileReader(args[argsLoaderFileIdx + 1]); JSONParser parser = new JSONParser(); LinkedHashMap streamNameToFileName = (LinkedHashMap) parser.parse(mappingReader, new LinkedHashFactory()); ArrayList<VCFFileStreamInfo> streamInfoVec = new ArrayList<VCFFileStreamInfo>(); long rowIdx = 0; for (Object currObj : streamNameToFileName.entrySet()) { Map.Entry<String, String> entry = (Map.Entry<String, String>) currObj; VCFFileStreamInfo currInfo = new VCFFileStreamInfo(entry.getValue(), loaderJSONFile, rank, useMultiChromosomeIterator); /** The following 2 lines are not mandatory - use initializeSampleInfoMapFromHeader() * iff you know for sure that sample names in the VCF header are globally unique * across all streams/files. If not, you have 2 options: * (a) specify your own mapping from sample index in the header to SampleInfo object * (unique_name, rowIdx) OR * (b) specify the mapping in the callset_mapping_file (JSON) and pass null to * addSortedVariantContextIterator() */ LinkedHashMap<Integer, GenomicsDBImporter.SampleInfo> sampleIndexToInfo = new LinkedHashMap<Integer, GenomicsDBImporter.SampleInfo>(); rowIdx = GenomicsDBImporter.initializeSampleInfoMapFromHeader(sampleIndexToInfo, currInfo.mVCFHeader, rowIdx); int streamIdx = -1; if (args[0].equals("-iterators")) streamIdx = loader.addSortedVariantContextIterator(entry.getKey(), currInfo.mVCFHeader, currInfo.mIterator, bufferCapacity, VariantContextWriterBuilder.OutputType.BCF_STREAM, sampleIndexToInfo); //pass sorted VC iterators else //use buffers - VCs will be provided by caller streamIdx = loader.addBufferStream(entry.getKey(), currInfo.mVCFHeader, bufferCapacity, VariantContextWriterBuilder.OutputType.BCF_STREAM, sampleIndexToInfo); currInfo.mStreamIdx = streamIdx; streamInfoVec.add(currInfo); } if (args[0].equals("-iterators")) { //Much simpler interface if using Iterator<VariantContext> loader.importBatch(); assert loader.isDone(); } else { //Must be called after all iterators/streams added - no more iterators/streams // can be added once this function is called loader.setupGenomicsDBImporter(); //Counts and tracks buffer streams for which new data must be supplied //Initialized to all the buffer streams int numExhaustedBufferStreams = streamInfoVec.size(); int[] exhaustedBufferStreamIdxs = new int[numExhaustedBufferStreams]; for (int i = 0; i < numExhaustedBufferStreams; ++i) exhaustedBufferStreamIdxs[i] = i; while (!loader.isDone()) { //Add data for streams that were exhausted in the previous round for (int i = 0; i < numExhaustedBufferStreams; ++i) { VCFFileStreamInfo currInfo = streamInfoVec.get(exhaustedBufferStreamIdxs[i]); boolean added = true; while (added && (currInfo.mIterator.hasNext() || currInfo.mNextVC != null)) { if (currInfo.mNextVC != null) added = loader.add(currInfo.mNextVC, currInfo.mStreamIdx); if (added) if (currInfo.mIterator.hasNext()) currInfo.mNextVC = currInfo.mIterator.next(); else currInfo.mNextVC = null; } } loader.importBatch(); numExhaustedBufferStreams = (int) loader.getNumExhaustedBufferStreams(); for (int i = 0; i < numExhaustedBufferStreams; ++i) exhaustedBufferStreamIdxs[i] = loader.getExhaustedBufferStreamIndex(i); } } }
From source file:de.urszeidler.ethereum.licencemanager1.deployer.LicenseManagerDeployer.java
/** * @param args//w w w . j ava 2 s . c o m */ public static void main(String[] args) { Options options = createOptions(); CommandLineParser parser = new DefaultParser(); int returnValue = 0; boolean dontExit = false; try { CommandLine commandLine = parser.parse(options, args); if (commandLine.hasOption("h")) { printHelp(options); return; } if (commandLine.hasOption("de")) dontExit = true; String senderKey = null; String senderPass = null; if (commandLine.hasOption("sk")) senderKey = commandLine.getOptionValue("sk"); if (commandLine.hasOption("sp")) senderPass = commandLine.getOptionValue("sp"); LicenseManagerDeployer manager = new LicenseManagerDeployer(); try { manager.init(senderKey, senderPass); long currentMili = 0; EthValue balance = null; if (commandLine.hasOption("calcDeploymendCost")) { currentMili = System.currentTimeMillis(); balance = manager.ethereum.getBalance(manager.sender); } if (commandLine.hasOption("observeBlock")) { manager.ethereum.events().observeBlocks() .subscribe(b -> System.out.println("Block: " + b.blockNumber + " " + b.receipts)); } if (commandLine.hasOption("f")) { String[] values = commandLine.getOptionValues("f"); String filename = values[0]; String isCompiled = values[1]; manager.deployer.setContractSource(filename, Boolean.parseBoolean(isCompiled)); } if (commandLine.hasOption("millis")) { manager.setMillis(Long.parseLong(commandLine.getOptionValue("millis"))); } if (commandLine.hasOption("c")) { String[] values = commandLine.getOptionValues("c"); if (values == null || values.length != 2) { System.out.println("Error. Need 2 parameters: paymentAddress,name"); System.out.println(""); printHelp(options); return; } String paymentAddress = values[0]; String name = values[1]; manager.deployLicenseManager(EthAddress.of(paymentAddress), name); } else if (commandLine.hasOption("l")) { String contractAddress = commandLine.getOptionValue("l"); if (contractAddress == null) { System.out.println("Error. Need 1 parameters: contract address"); System.out.println(""); printHelp(options); return; } manager.setManager(EthAddress.of(contractAddress)); manager.listContractData(EthAddress.of(contractAddress)); } else if (commandLine.hasOption("cic")) { String[] values = commandLine.getOptionValues("cic"); if (values == null || values.length != 6) { System.out.println("Error. Need 6 itemName, textHash, url, lifeTime, price"); System.out.println(""); printHelp(options); return; } String contractAddress = values[0]; String itemName = values[1]; String textHash = values[2]; String url = values[3]; String lifeTime = values[4]; String price = values[5]; manager.setManager(EthAddress.of(contractAddress)); manager.createIssuerContract(itemName, textHash, url, Integer.parseInt(lifeTime), Integer.parseInt(price)); } else if (commandLine.hasOption("bli")) { String[] values = commandLine.getOptionValues("bli"); if (values == null || values.length < 2 || values.length > 3) { System.out.println( "Error. Need 2-3 issuerAddress, name, optional an address when not use the sender."); System.out.println(""); printHelp(options); return; } String issuerAddress = values[0]; String name = values[1]; String address = values.length > 2 ? values[2] : null; manager.buyLicense(issuerAddress, name, address); } else if (commandLine.hasOption("v")) { String[] values = commandLine.getOptionValues("v"); String issuerAddress = values[0]; String message = values[1]; String signature = values[2]; String publicKey = values[3]; manager.verifyLicense(issuerAddress, message, signature, publicKey); } else if (commandLine.hasOption("cs")) { String message = commandLine.getOptionValue("cs"); if (message == null) { System.out.println("Error. Need 1 parameter: message"); System.out.println(""); printHelp(options); return; } String signature = createSignature(manager.sender, message); String publicKeyString = toPublicKeyString(manager.sender); System.out.println("The signature for the message is:"); System.out.println(signature); System.out.println("The public key is:"); System.out.println(publicKeyString); } else if (commandLine.hasOption("co")) { String[] values = commandLine.getOptionValues("co"); if (values == null || values.length != 2) { System.out.println("Error. Need 2 parameters: contractAddress, newOwnerAddress"); System.out.println(""); printHelp(options); return; } String contractAddress = values[0]; String newOwner = values[1]; manager.changeOwner(EthAddress.of(contractAddress), EthAddress.of(newOwner)); } else if (commandLine.hasOption("si")) { String contractAddress = commandLine.getOptionValue("si"); if (contractAddress == null) { System.out.println("Error. Need 1 parameters: contract address"); System.out.println(""); printHelp(options); return; } manager.setManager(EthAddress.of(contractAddress)); manager.stopIssue(contractAddress); } else if (commandLine.hasOption("ppuk")) { System.out.println("Public key: " + toPublicKeyString(manager.sender)); } if (manager.licenseManager != null && commandLine.hasOption("wca")) { String[] values = commandLine.getOptionValues("wca"); String filename = values[0]; File file = new File(filename); IOUtils.write( !commandLine.hasOption("cic") ? manager.licenseManager.contractAddress.withLeading0x() : manager.licenseManager.contractInstance .contracts(manager.licenseManager.contractInstance.contractCount() - 1) .withLeading0x(), new FileOutputStream(file), "UTF-8"); } if (commandLine.hasOption("calcDeploymendCost")) { balance = balance.minus(manager.ethereum.getBalance(manager.sender)); BigDecimal divide = new BigDecimal(balance.inWei()) .divide(BigDecimal.valueOf(1_000_000_000_000_000_000L)); System.out.println("Deployment cost: " + (divide) + " in wei:" + balance.inWei() + " time need: " + (System.currentTimeMillis() - currentMili)); } } catch (Exception e) { System.out.println(e.getMessage()); printHelp(options); returnValue = 10; } } catch (ParseException e1) { System.out.println(e1.getMessage()); printHelp(options); returnValue = 10; } if (!dontExit) System.exit(returnValue); }