List of usage examples for java.lang Math min
@HotSpotIntrinsicCandidate public static double min(double a, double b)
From source file:fr.inria.atlanmod.instantiator.neoEMF.Launcher.java
public static void main(String[] args) throws GenerationException, IOException { ResourceSetImpl resourceSet = new ResourceSetImpl(); { // initializing the registry resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(EcorePackage.eNS_PREFIX, new EcoreResourceFactoryImpl()); resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap() .put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl()); resourceSet.getResourceFactoryRegistry().getProtocolToFactoryMap().put(NeoEMFURI.NEOEMF_HBASE_SCHEME, NeoEMFResourceFactory.eINSTANCE); }/*from w w w .j a va2 s . c om*/ Options options = new Options(); configureOptions(options); CommandLineParser parser = new GnuParser(); try { CommandLine commandLine = parser.parse(options, args); String epackage_class = commandLine.getOptionValue(E_PACKAGE_CLASS); LOGGER.info("Start loading the package"); Class<?> inClazz = Launcher.class.getClassLoader().loadClass(epackage_class); EPackage _package = (EPackage) inClazz.getMethod("init").invoke(null); Resource metamodelResource = new XMIResourceImpl(URI.createFileURI("dummy")); metamodelResource.getContents().add(_package); LOGGER.info("Finish loading the package"); int size = Launcher.DEFAULT_AVERAGE_MODEL_SIZE; if (commandLine.hasOption(SIZE)) { Number number = (Number) commandLine.getParsedOptionValue(SIZE); size = (int) Math.min(Integer.MAX_VALUE, number.longValue()); } float variation = Launcher.DEFAULT_DEVIATION; if (commandLine.hasOption(VARIATION)) { Number number = (Number) commandLine.getParsedOptionValue(VARIATION); if (number.floatValue() < 0.0f || number.floatValue() > 1.0f) { throw new ParseException(MessageFormat.format("Invalid value for option -{0}: {1}", VARIATION, number.floatValue())); } variation = number.floatValue(); } float propVariation = Launcher.DEFAULT_DEVIATION; if (commandLine.hasOption(PROP_VARIATION)) { Number number = (Number) commandLine.getParsedOptionValue(PROP_VARIATION); if (number.floatValue() < 0.0f || number.floatValue() > 1.0f) { throw new ParseException(MessageFormat.format("Invalid value for option -{0}: {1}", PROP_VARIATION, number.floatValue())); } propVariation = number.floatValue(); } long seed = System.currentTimeMillis(); if (commandLine.hasOption(SEED)) { seed = ((Number) commandLine.getParsedOptionValue(SEED)).longValue(); } Range<Integer> range = Range.between(Math.round(size * (1 - variation)), Math.round(size * (1 + variation))); GenericMetamodelConfig config = new GenericMetamodelConfig(metamodelResource, range, seed); GenericMetamodelGenerator modelGen = new GenericMetamodelGenerator(config); if (commandLine.hasOption(OUTPUT_PATH)) { String outDir = commandLine.getOptionValue(OUTPUT_PATH); //java.net.URI intermediateURI = java.net.URI.create(outDir); modelGen.setSamplesPath(outDir); } int numberOfModels = 1; if (commandLine.hasOption(N_MODELS)) { numberOfModels = ((Number) commandLine.getParsedOptionValue(N_MODELS)).intValue(); } int valuesSize = GenericMetamodelConfig.DEFAULT_AVERAGE_VALUES_LENGTH; if (commandLine.hasOption(VALUES_SIZE)) { Number number = (Number) commandLine.getParsedOptionValue(VALUES_SIZE); valuesSize = (int) Math.min(Integer.MAX_VALUE, number.longValue()); } int referencesSize = GenericMetamodelConfig.DEFAULT_AVERAGE_REFERENCES_SIZE; if (commandLine.hasOption(VALUES_SIZE)) { Number number = (Number) commandLine.getParsedOptionValue(DEGREE); referencesSize = (int) Math.min(Integer.MAX_VALUE, number.longValue()); } config.setValuesRange(Math.round(valuesSize * (1 - propVariation)), Math.round(valuesSize * (1 + propVariation))); config.setReferencesRange(Math.round(referencesSize * (1 - propVariation)), Math.round(referencesSize * (1 + propVariation))); config.setPropertiesRange(Math.round(referencesSize * (1 - propVariation)), Math.round(referencesSize * (1 + propVariation))); long start = System.currentTimeMillis(); modelGen.runGeneration(resourceSet, numberOfModels, size, variation); long end = System.currentTimeMillis(); LOGGER.info( MessageFormat.format("Generation finished after {0} s", Long.toString((end - start) / 1000))); if (commandLine.hasOption(DIAGNOSE)) { for (Resource resource : resourceSet.getResources()) { LOGGER.info( MessageFormat.format("Requested validation for resource ''{0}''", resource.getURI())); BasicDiagnostic diagnosticChain = diagnoseResource(resource); if (!isFailed(diagnosticChain)) { LOGGER.info(MessageFormat.format("Result of the diagnosis of resurce ''{0}'' is ''OK''", resource.getURI())); } else { LOGGER.severe(MessageFormat.format("Found ''{0}'' error(s) in the resource ''{1}''", diagnosticChain.getChildren().size(), resource.getURI())); for (Diagnostic diagnostic : diagnosticChain.getChildren()) { LOGGER.fine(diagnostic.getMessage()); } } } LOGGER.info("Validation finished"); } } catch (ParseException e) { System.err.println(e.getLocalizedMessage()); HelpFormatter formatter = new HelpFormatter(); formatter.setOptionComparator(new OptionComarator<Option>()); try { formatter.setWidth(Math.max(Terminal.getTerminal().getTerminalWidth(), 80)); } catch (Throwable t) { LOGGER.warning("Unable to get console information"); } ; formatter.printHelp("java -jar <this-file.jar>", options, true); System.exit(ERROR); } catch (ClassNotFoundException t) { System.err.println("ERROR: Unable to load class" + t.getLocalizedMessage()); StringWriter stringWriter = new StringWriter(); t.printStackTrace(new PrintWriter(stringWriter)); System.err.println(stringWriter.toString()); } catch (Throwable t) { System.err.println("ERROR: " + t.getLocalizedMessage()); StringWriter stringWriter = new StringWriter(); t.printStackTrace(new PrintWriter(stringWriter)); System.err.println(t); LOGGER.severe(stringWriter.toString()); System.exit(ERROR); } }
From source file:fr.inria.atlanmod.dag.instantiator.Launcher.java
public static void main(String[] args) throws GenerationException, IOException { ResourceSetImpl resourceSet = new ResourceSetImpl(); { // initializing the registry resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(EcorePackage.eNS_PREFIX, new EcoreResourceFactoryImpl()); resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap() .put(Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl()); resourceSet.getResourceFactoryRegistry().getProtocolToFactoryMap().put(NeoEMFURI.NEOEMF_HBASE_SCHEME, NeoEMFResourceFactory.eINSTANCE); }//from w w w . j a va 2 s . c o m Options options = new Options(); configureOptions(options); CommandLineParser parser = new GnuParser(); try { CommandLine commandLine = parser.parse(options, args); // String epackage_class = commandLine.getOptionValue(E_PACKAGE_CLASS); // // LOGGER.info("Start loading the package"); // Class<?> inClazz = Launcher.class.getClassLoader().loadClass(epackage_class); // EPackage _package = (EPackage) inClazz.getMethod("init").invoke(null); // // Resource metamodelResource = new XMIResourceImpl(URI.createFileURI("dummy")); // metamodelResource.getContents().add(_package); // LOGGER.info("Finish loading the package"); int size = Launcher.DEFAULT_AVERAGE_MODEL_SIZE; if (commandLine.hasOption(SIZE)) { Number number = (Number) commandLine.getParsedOptionValue(SIZE); size = (int) Math.min(Integer.MAX_VALUE, number.longValue()); } float variation = Launcher.DEFAULT_DEVIATION; if (commandLine.hasOption(VARIATION)) { Number number = (Number) commandLine.getParsedOptionValue(VARIATION); if (number.floatValue() < 0.0f || number.floatValue() > 1.0f) { throw new ParseException(MessageFormat.format("Invalid value for option -{0}: {1}", VARIATION, number.floatValue())); } variation = number.floatValue(); } float propVariation = Launcher.DEFAULT_DEVIATION; if (commandLine.hasOption(PROP_VARIATION)) { Number number = (Number) commandLine.getParsedOptionValue(PROP_VARIATION); if (number.floatValue() < 0.0f || number.floatValue() > 1.0f) { throw new ParseException(MessageFormat.format("Invalid value for option -{0}: {1}", PROP_VARIATION, number.floatValue())); } propVariation = number.floatValue(); } long seed = System.currentTimeMillis(); if (commandLine.hasOption(SEED)) { seed = ((Number) commandLine.getParsedOptionValue(SEED)).longValue(); } Range<Integer> range = Range.between(Math.round(size * (1 - variation)), Math.round(size * (1 + variation))); ISpecimenConfiguration config = new DagMetamodelConfig(range, seed); IGenerator generator = new DagGenerator(config, config.getSeed()); GenericMetamodelGenerator modelGen = new GenericMetamodelGenerator(generator); if (commandLine.hasOption(OUTPUT_PATH)) { String outDir = commandLine.getOptionValue(OUTPUT_PATH); //java.net.URI intermediateURI = java.net.URI.create(outDir); modelGen.setSamplesPath(outDir); } int numberOfModels = 1; if (commandLine.hasOption(N_MODELS)) { numberOfModels = ((Number) commandLine.getParsedOptionValue(N_MODELS)).intValue(); } int valuesSize = GenericMetamodelConfig.DEFAULT_AVERAGE_VALUES_LENGTH; if (commandLine.hasOption(VALUES_SIZE)) { Number number = (Number) commandLine.getParsedOptionValue(VALUES_SIZE); valuesSize = (int) Math.min(Integer.MAX_VALUE, number.longValue()); } int referencesSize = GenericMetamodelConfig.DEFAULT_AVERAGE_REFERENCES_SIZE; if (commandLine.hasOption(DEGREE)) { Number number = (Number) commandLine.getParsedOptionValue(DEGREE); referencesSize = (int) Math.min(Integer.MAX_VALUE, number.longValue()); } config.setValuesRange(Math.round(valuesSize * (1 - propVariation)), Math.round(valuesSize * (1 + propVariation))); config.setReferencesRange(Math.round(referencesSize * (1 - propVariation)), Math.round(referencesSize * (1 + propVariation))); config.setPropertiesRange(Math.round(referencesSize * (1 - propVariation)), Math.round(referencesSize * (1 + propVariation))); long start = System.currentTimeMillis(); modelGen.runGeneration(resourceSet, numberOfModels, size, variation); long end = System.currentTimeMillis(); LOGGER.info( MessageFormat.format("Generation finished after {0} s", Long.toString((end - start) / 1000))); // for (Resource rsc : resourceSet.getResources()) { // if (rsc.getContents().get(0) instanceof DAG) { // // } // // } if (commandLine.hasOption(DIAGNOSE)) { for (Resource resource : resourceSet.getResources()) { LOGGER.info( MessageFormat.format("Requested validation for resource ''{0}''", resource.getURI())); BasicDiagnostic diagnosticChain = diagnoseResource(resource); if (!isFailed(diagnosticChain)) { LOGGER.info(MessageFormat.format("Result of the diagnosis of resurce ''{0}'' is ''OK''", resource.getURI())); } else { LOGGER.severe(MessageFormat.format("Found ''{0}'' error(s) in the resource ''{1}''", diagnosticChain.getChildren().size(), resource.getURI())); for (Diagnostic diagnostic : diagnosticChain.getChildren()) { LOGGER.fine(diagnostic.getMessage()); } } } LOGGER.info("Validation finished"); } } catch (ParseException e) { System.err.println(e.getLocalizedMessage()); HelpFormatter formatter = new HelpFormatter(); formatter.setOptionComparator(new OptionComarator<Option>()); try { formatter.setWidth(Math.max(Terminal.getTerminal().getTerminalWidth(), 80)); } catch (Throwable t) { LOGGER.warning("Unable to get console information"); } ; formatter.printHelp("java -jar <this-file.jar>", options, true); System.exit(ERROR); } catch (Throwable t) { System.err.println("ERROR: " + t.getLocalizedMessage()); StringWriter stringWriter = new StringWriter(); t.printStackTrace(new PrintWriter(stringWriter)); System.err.println(t); LOGGER.severe(stringWriter.toString()); System.exit(ERROR); } }
From source file:com.dxc.temp.SimpleProducerConsumer.java
public static void main(String[] args) throws InterruptedException { int argIndex = 0; final String accessKey = args[argIndex++]; final String secretKey = args[argIndex++]; final AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey); final String endpoint = args[argIndex++]; final String queueName = args[argIndex++]; final int producerCount = Integer.parseInt(args[argIndex++]); final int consumerCount = Integer.parseInt(args[argIndex++]); final int batchSize = Integer.parseInt(args[argIndex++]); final int messageSizeByte = Integer.parseInt(args[argIndex++]); final int runTimeMinutes = Integer.parseInt(args[argIndex++]); // configure the SQS client with enough connections for all producer and // consumer threads AmazonSQS sqsClient = new AmazonSQSClient(credentials, new ClientConfiguration().withMaxConnections(producerCount + consumerCount)); sqsClient.setEndpoint(endpoint);// w ww . j a va2 s . com String queueUrl = sqsClient.getQueueUrl(new GetQueueUrlRequest(queueName)).getQueueUrl(); // the flag to stop producer, consumer, and monitor threads AtomicBoolean stop = new AtomicBoolean(false); // start the producers final AtomicInteger producedCount = new AtomicInteger(); Thread[] producers = new Thread[producerCount]; for (int i = 0; i < producerCount; i++) { if (batchSize == 1) producers[i] = new Producer(sqsClient, queueUrl, messageSizeByte, producedCount, stop); else producers[i] = new BatchProducer(sqsClient, queueUrl, batchSize, messageSizeByte, producedCount, stop); producers[i].start(); } // start the consumers final AtomicInteger consumedCount = new AtomicInteger(); Thread[] consumers = new Thread[consumerCount]; for (int i = 0; i < consumerCount; i++) { if (batchSize == 1) consumers[i] = new Consumer(sqsClient, queueUrl, consumedCount, stop); else consumers[i] = new BatchConsumer(sqsClient, queueUrl, batchSize, consumedCount, stop); consumers[i].start(); } // start the monitor (thread) Thread monitor = new Monitor(producedCount, consumedCount, stop); monitor.start(); // wait for the specified amount of time then stop Thread.sleep(TimeUnit.MINUTES.toMillis(Math.min(runTimeMinutes, MAX_RUNTIME_MINUTES))); stop.set(true); // join all threads for (int i = 0; i < producerCount; i++) producers[i].join(); for (int i = 0; i < consumerCount; i++) consumers[i].join(); monitor.interrupt(); monitor.join(); }
From source file:edu.cmu.lti.oaqa.knn4qa.apps.ExtractDataAndQueryAsSparseVectors.java
public static void main(String[] args) { String optKeys[] = { CommonParams.MAX_NUM_QUERY_PARAM, MAX_NUM_DATA_PARAM, CommonParams.MEMINDEX_PARAM, IN_QUERIES_PARAM, OUT_QUERIES_PARAM, OUT_DATA_PARAM, TEXT_FIELD_PARAM, TEST_QTY_PARAM, }; String optDescs[] = { CommonParams.MAX_NUM_QUERY_DESC, MAX_NUM_DATA_DESC, CommonParams.MEMINDEX_DESC, IN_QUERIES_DESC, OUT_QUERIES_DESC, OUT_DATA_DESC, TEXT_FIELD_DESC, TEST_QTY_DESC }; boolean hasArg[] = { true, true, true, true, true, true, true, true }; ParamHelper prmHlp = null;//from w ww . j a va2 s . c o m try { prmHlp = new ParamHelper(args, optKeys, optDescs, hasArg); CommandLine cmd = prmHlp.getCommandLine(); Options opt = prmHlp.getOptions(); int maxNumQuery = Integer.MAX_VALUE; String tmpn = cmd.getOptionValue(CommonParams.MAX_NUM_QUERY_PARAM); if (tmpn != null) { try { maxNumQuery = Integer.parseInt(tmpn); } catch (NumberFormatException e) { UsageSpecify(CommonParams.MAX_NUM_QUERY_PARAM, opt); } } int maxNumData = Integer.MAX_VALUE; tmpn = cmd.getOptionValue(MAX_NUM_DATA_PARAM); if (tmpn != null) { try { maxNumData = Integer.parseInt(tmpn); } catch (NumberFormatException e) { UsageSpecify(MAX_NUM_DATA_PARAM, opt); } } String memIndexPref = cmd.getOptionValue(CommonParams.MEMINDEX_PARAM); if (null == memIndexPref) { UsageSpecify(CommonParams.MEMINDEX_PARAM, opt); } String textField = cmd.getOptionValue(TEXT_FIELD_PARAM); if (null == textField) { UsageSpecify(TEXT_FIELD_PARAM, opt); } textField = textField.toLowerCase(); int fieldId = -1; for (int i = 0; i < FeatureExtractor.mFieldNames.length; ++i) if (FeatureExtractor.mFieldNames[i].compareToIgnoreCase(textField) == 0) { fieldId = i; break; } if (-1 == fieldId) { Usage("Wrong field index, should be one of the following: " + String.join(",", FeatureExtractor.mFieldNames), opt); } InMemForwardIndex indx = new InMemForwardIndex( FeatureExtractor.indexFileName(memIndexPref, FeatureExtractor.mFieldNames[fieldId])); BM25SimilarityLucene bm25simil = new BM25SimilarityLucene(FeatureExtractor.BM25_K1, FeatureExtractor.BM25_B, indx); String inQueryFile = cmd.getOptionValue(IN_QUERIES_PARAM); String outQueryFile = cmd.getOptionValue(OUT_QUERIES_PARAM); if ((inQueryFile == null) != (outQueryFile == null)) { Usage("You should either specify both " + IN_QUERIES_PARAM + " and " + OUT_QUERIES_PARAM + " or none of them", opt); } String outDataFile = cmd.getOptionValue(OUT_DATA_PARAM); tmpn = cmd.getOptionValue(TEST_QTY_PARAM); int testQty = 0; if (tmpn != null) { try { testQty = Integer.parseInt(tmpn); } catch (NumberFormatException e) { UsageSpecify(TEST_QTY_PARAM, opt); } } ArrayList<DocEntry> testDocEntries = new ArrayList<DocEntry>(); ArrayList<DocEntry> testQueryEntries = new ArrayList<DocEntry>(); ArrayList<TrulySparseVector> testDocVectors = new ArrayList<TrulySparseVector>(); ArrayList<TrulySparseVector> testQueryVectors = new ArrayList<TrulySparseVector>(); if (outDataFile != null) { BufferedWriter out = new BufferedWriter( new OutputStreamWriter(CompressUtils.createOutputStream(outDataFile))); ArrayList<DocEntryExt> docEntries = indx.getDocEntries(); for (int id = 0; id < Math.min(maxNumData, docEntries.size()); ++id) { DocEntry e = docEntries.get(id).mDocEntry; TrulySparseVector v = bm25simil.getDocSparseVector(e, false); if (id < testQty) { testDocEntries.add(e); testDocVectors.add(v); } outputVector(out, v); } out.close(); } Splitter splitOnSpace = Splitter.on(' ').trimResults().omitEmptyStrings(); if (outQueryFile != null) { BufferedReader inpText = new BufferedReader( new InputStreamReader(CompressUtils.createInputStream(inQueryFile))); BufferedWriter out = new BufferedWriter( new OutputStreamWriter(CompressUtils.createOutputStream(outQueryFile))); String queryText = XmlHelper.readNextXMLIndexEntry(inpText); for (int queryQty = 0; queryText != null && queryQty < maxNumQuery; queryText = XmlHelper .readNextXMLIndexEntry(inpText), queryQty++) { Map<String, String> queryFields = null; // 1. Parse a query try { queryFields = XmlHelper.parseXMLIndexEntry(queryText); } catch (Exception e) { System.err.println("Parsing error, offending QUERY:\n" + queryText); throw new Exception("Parsing error."); } String fieldText = queryFields.get(FeatureExtractor.mFieldsSOLR[fieldId]); if (fieldText == null) { fieldText = ""; } ArrayList<String> tmpa = new ArrayList<String>(); for (String s : splitOnSpace.split(fieldText)) tmpa.add(s); DocEntry e = indx.createDocEntry(tmpa.toArray(new String[tmpa.size()])); TrulySparseVector v = bm25simil.getDocSparseVector(e, true); if (queryQty < testQty) { testQueryEntries.add(e); testQueryVectors.add(v); } outputVector(out, v); } out.close(); } int testedQty = 0, diffQty = 0; // Now let's do some testing for (int iq = 0; iq < testQueryEntries.size(); ++iq) { DocEntry queryEntry = testQueryEntries.get(iq); TrulySparseVector queryVector = testQueryVectors.get(iq); for (int id = 0; id < testDocEntries.size(); ++id) { DocEntry docEntry = testDocEntries.get(id); TrulySparseVector docVector = testDocVectors.get(id); float val1 = bm25simil.compute(queryEntry, docEntry); float val2 = TrulySparseVector.scalarProduct(queryVector, docVector); ++testedQty; if (Math.abs(val1 - val2) > 1e5) { System.err.println( String.format("Potential mismatch BM25=%f <-> scalar product=%f", val1, val2)); ++diffQty; } } } if (testedQty > 0) System.out.println(String.format("Tested %d Mismatched %d", testedQty, diffQty)); } catch (ParseException e) { Usage("Cannot parse arguments: " + e, prmHlp != null ? prmHlp.getOptions() : null); e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); System.err.println("Terminating due to an exception: " + e); System.exit(1); } }
From source file:acmi.l2.clientmod.l2_version_switcher.Main.java
public static void main(String[] args) { if (args.length != 3 && args.length != 4) { System.out.println("USAGE: l2_version_switcher.jar host game version <--splash> <filter>"); System.out.println("EXAMPLE: l2_version_switcher.jar " + L2.NCWEST_HOST + " " + L2.NCWEST_GAME + " 1 \"system\\*\""); System.out.println(//from ww w . ja v a 2s . c o m " l2_version_switcher.jar " + L2.PLAYNC_TEST_HOST + " " + L2.PLAYNC_TEST_GAME + " 48"); System.exit(0); } List<String> argsList = new ArrayList<>(Arrays.asList(args)); String host = argsList.get(0); String game = argsList.get(1); int version = Integer.parseInt(argsList.get(2)); Helper helper = new Helper(host, game, version); boolean available = false; try { available = helper.isAvailable(); } catch (IOException e) { System.err.print(e.getClass().getSimpleName()); if (e.getMessage() != null) { System.err.print(": " + e.getMessage()); } System.err.println(); } System.out.println(String.format("Version %d available: %b", version, available)); if (!available) { System.exit(0); } List<FileInfo> fileInfoList = null; try { fileInfoList = helper.getFileInfoList(); } catch (IOException e) { System.err.println("Couldn\'t get file info map"); System.exit(1); } boolean splash = argsList.remove("--splash"); if (splash) { Optional<FileInfo> splashObj = fileInfoList.stream() .filter(fi -> fi.getPath().contains("sp_32b_01.bmp")).findAny(); if (splashObj.isPresent()) { try (InputStream is = new FilterInputStream( Util.getUnzipStream(helper.getDownloadStream(splashObj.get().getPath()))) { @Override public int read() throws IOException { int b = super.read(); if (b >= 0) b ^= 0x36; return b; } @Override public int read(byte[] b, int off, int len) throws IOException { int r = super.read(b, off, len); if (r >= 0) { for (int i = 0; i < r; i++) b[off + i] ^= 0x36; } return r; } }) { new DataInputStream(is).readFully(new byte[28]); BufferedImage bi = ImageIO.read(is); JFrame frame = new JFrame("Lineage 2 [" + version + "] " + splashObj.get().getPath()); frame.setContentPane(new JComponent() { { setPreferredSize(new Dimension(bi.getWidth(), bi.getHeight())); } @Override protected void paintComponent(Graphics g) { g.drawImage(bi, 0, 0, null); } }); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } catch (IOException e) { e.printStackTrace(); } } else { System.out.println("Splash not found"); } return; } String filter = argsList.size() > 3 ? separatorsToSystem(argsList.get(3)) : null; File l2Folder = new File(System.getProperty("user.dir")); List<FileInfo> toUpdate = fileInfoList.parallelStream().filter(fi -> { String filePath = separatorsToSystem(fi.getPath()); if (filter != null && !wildcardMatch(filePath, filter, IOCase.INSENSITIVE)) return false; File file = new File(l2Folder, filePath); try { if (file.exists() && file.length() == fi.getSize() && Util.hashEquals(file, fi.getHash())) { System.out.println(filePath + ": OK"); return false; } } catch (IOException e) { System.out.println(filePath + ": couldn't check hash: " + e); return true; } System.out.println(filePath + ": need update"); return true; }).collect(Collectors.toList()); List<String> errors = Collections.synchronizedList(new ArrayList<>()); ExecutorService executor = Executors.newFixedThreadPool(16); CompletableFuture[] tasks = toUpdate.stream().map(fi -> CompletableFuture.runAsync(() -> { String filePath = separatorsToSystem(fi.getPath()); File file = new File(l2Folder, filePath); File folder = file.getParentFile(); if (!folder.exists()) { if (!folder.mkdirs()) { errors.add(filePath + ": couldn't create parent dir"); return; } } try (InputStream input = Util .getUnzipStream(new BufferedInputStream(helper.getDownloadStream(fi.getPath()))); OutputStream output = new BufferedOutputStream(new FileOutputStream(file))) { byte[] buffer = new byte[Math.min(fi.getSize(), 1 << 24)]; int pos = 0; int r; while ((r = input.read(buffer, pos, buffer.length - pos)) >= 0) { pos += r; if (pos == buffer.length) { output.write(buffer, 0, pos); pos = 0; } } if (pos != 0) { output.write(buffer, 0, pos); } System.out.println(filePath + ": OK"); } catch (IOException e) { String msg = filePath + ": FAIL: " + e.getClass().getSimpleName(); if (e.getMessage() != null) { msg += ": " + e.getMessage(); } errors.add(msg); } }, executor)).toArray(CompletableFuture[]::new); CompletableFuture.allOf(tasks).thenRun(() -> { for (String err : errors) System.err.println(err); executor.shutdown(); }); }
From source file:my.yelp.populate.java
public static void main(String[] args) throws FileNotFoundException, ParseException, IOException, java.text.ParseException { try {//from w w w.jav a2s.c o m DbConnection A1 = new DbConnection(); Connection con = A1.getConnection(); JSONParser jsonParser; jsonParser = new JSONParser(); Object obj1 = jsonParser.parse(new FileReader("C:\\Users\\Sanjay Desai\\Desktop\\yelp_user.json")); Object obj2 = jsonParser.parse(new FileReader("C:\\Users\\Sanjay Desai\\Desktop\\yelp_business.json")); Object obj3 = jsonParser.parse(new FileReader("C:\\Users\\Sanjay Desai\\Desktop\\yelp_review.json")); Object obj4 = jsonParser.parse(new FileReader("C:\\Users\\Sanjay Desai\\Desktop\\yelp_checkin.json")); JSONArray jsonArray1; jsonArray1 = (JSONArray) obj1; JSONArray jsonArray2; jsonArray2 = (JSONArray) obj2; JSONArray jsonArray3; jsonArray3 = (JSONArray) obj3; JSONArray jsonArray4; jsonArray4 = (JSONArray) obj4; // yelp_user String yelping_since, name1, user_id, type1; Long review_count1, fans; Double average_stars; Statement stmt; stmt = con.createStatement(); stmt.executeUpdate("Delete from N_User"); for (int i = 0; i < (jsonArray1.size()); i++) { JSONObject jsonObject = (JSONObject) jsonArray1.get(i); yelping_since = (String) jsonObject.get("yelping_since") + "-01"; JSONArray friends = (JSONArray) jsonObject.get("friends"); int friends_size = friends.size(); review_count1 = (Long) jsonObject.get("review_count"); name1 = (String) jsonObject.get("name"); user_id = (String) jsonObject.get("user_id"); fans = (Long) jsonObject.get("fans"); average_stars = (Double) jsonObject.get("average_stars"); type1 = (String) jsonObject.get("type"); try (PreparedStatement pstmt1 = con.prepareStatement( "Insert INTO N_User(yelping_since,friends_size,review_count,name,user_id,fans,average_stars,type) VALUES(?,?,?,?,?,?,?,?)")) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); java.util.Date myDate = format.parse(yelping_since); pstmt1.setDate(1, new java.sql.Date(myDate.getTime())); pstmt1.setInt(2, friends_size); pstmt1.setLong(3, review_count1); pstmt1.setString(4, name1); pstmt1.setString(5, user_id); pstmt1.setLong(6, fans); pstmt1.setDouble(7, average_stars); pstmt1.setString(8, type1); pstmt1.executeUpdate(); } catch (java.text.ParseException ex) { Logger.getLogger(populate.class.getName()).log(Level.SEVERE, null, ex); } } //yelp_business String business_id, address, city, state, name, type_business; Double stars; for (int i = 0; i < jsonArray2.size(); i++) { JSONObject jsonObject = (JSONObject) jsonArray2.get(i); business_id = (String) jsonObject.get("business_id"); address = (String) jsonObject.get("full_address"); city = (String) jsonObject.get("city"); state = (String) jsonObject.get("state"); name = (String) jsonObject.get("name"); stars = (Double) jsonObject.get("stars"); type_business = (String) jsonObject.get("type"); try (PreparedStatement pstmt2 = con.prepareStatement( "Insert INTO N_Business(business_id,address,city,state,name,stars,type_business) VALUES(?,?,?,?,?,?,?)")) { pstmt2.setString(1, business_id); pstmt2.setString(2, address); pstmt2.setString(3, city); pstmt2.setString(4, state); pstmt2.setString(5, name); pstmt2.setDouble(6, stars); pstmt2.setString(7, type_business); pstmt2.executeUpdate(); pstmt2.close(); } } //Category Table String[] categories = { "Active Life", "Arts & Entertainment", "Automotive", "Car Rental", "Cafes", "Beauty & Spas", "Convenience Stores", "Dentists", "Doctors", "Drugstores", "Department Stores", "Education", "Event Planning & Services", "Flowers & Gifts", "Food", "Health & Medical", "Home Services", "Home & Garden", "Hospitals", "Hotels & travel", "Hardware stores", "Grocery", "Medical Centers", "Nurseries & Gardening", "Nightlife", "Restaurants", "Shopping", "Transportation" }; JSONArray category; String[] individual_category = new String[100]; int count = 0, flag = 0, m = 0, n = 0; String[] business_category = new String[50]; String[] subcategory = new String[50]; for (int i = 0; i < jsonArray2.size(); i++) { JSONObject jsonObject3 = (JSONObject) jsonArray2.get(i); String business_id2 = (String) jsonObject3.get("business_id"); category = (JSONArray) jsonObject3.get("categories"); for (int j = 0; j < category.size(); j++) { individual_category[j] = (String) category.get(j); count = count + 1; } for (int k = 0; k < count; k++) { for (String categorie : categories) { if (individual_category[k].equals(categorie)) { flag = 1; break; } } if (flag == 1) { business_category[m] = individual_category[k]; m = m + 1; flag = 0; } else { subcategory[n] = individual_category[k]; n = n + 1; } } for (int p = 0; p < m; p++) { for (int q = 0; q < n; q++) { try (PreparedStatement pstmt3 = con.prepareStatement( "INSERT INTO N_Category(business_id,category,subcategory) VALUES(?,?,?)")) { pstmt3.setString(1, business_id2); pstmt3.setString(2, business_category[p]); pstmt3.setString(3, subcategory[q]); pstmt3.executeUpdate(); } } } count = 0; m = 0; n = 0; } //yelp_review String user_id3, review_id, type3, business_id3, text, text1, review_date; Long stars3; int votes = 0; Integer no_votes; JSONObject votes_info; Set<String> keys; for (int i = 0; i < jsonArray3.size(); i++) { JSONObject jsonObject = (JSONObject) jsonArray3.get(i); votes_info = (JSONObject) jsonObject.get("votes"); keys = votes_info.keySet(); for (String r_key : keys) { votes = (int) (votes + (Long) votes_info.get(r_key)); } no_votes = toIntExact(votes); user_id3 = (String) jsonObject.get("user_id"); review_id = (String) jsonObject.get("review_id"); business_id3 = (String) jsonObject.get("business_id"); review_date = (String) jsonObject.get("date"); text1 = (String) jsonObject.get("text"); text = text1.substring(0, Math.min(1000, text1.length())); stars3 = (Long) jsonObject.get("stars"); type3 = (String) jsonObject.get("type"); try (PreparedStatement pstmt4 = con.prepareStatement( "Insert INTO N_Review(no_votes,user_id,review_id,business_id,review_date,text,stars,type) VALUES(?,?,?,?,?,?,?,?)")) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); java.util.Date myDate = format.parse(review_date); pstmt4.setInt(1, no_votes); pstmt4.setString(2, user_id3); pstmt4.setString(3, review_id); pstmt4.setString(4, business_id3); pstmt4.setDate(5, new java.sql.Date(myDate.getTime())); pstmt4.setString(6, text); pstmt4.setLong(7, stars3); pstmt4.setString(8, type3); pstmt4.executeUpdate(); pstmt4.close(); } } //Checkin_Info JSONObject checkin_info; String business_id4; Long check_in_count; Set<String> keys1; String[] timing = new String[10]; int n1 = 0, time, hour; //Inserting into checkin_info for (int i = 0; i < jsonArray4.size(); i++) { JSONObject jsonObject4 = (JSONObject) jsonArray4.get(i); checkin_info = (JSONObject) jsonObject4.get("checkin_info"); business_id4 = (String) jsonObject4.get("business_id"); keys1 = checkin_info.keySet(); for (String key : keys1) { check_in_count = (Long) checkin_info.get(key); for (String x : key.split("-")) { timing[n1] = x; n1 = n1 + 1; } n1 = 0; hour = Integer.parseInt(timing[0]); time = Integer.parseInt(timing[1]); try (PreparedStatement pstmt5 = con.prepareStatement( "INSERT INTO check_info(business_id,hour,day,check_in_count)VALUES(?,?,?,?)")) { pstmt5.setString(1, business_id4); pstmt5.setInt(2, hour); pstmt5.setInt(3, time); pstmt5.setLong(4, check_in_count); pstmt5.executeUpdate(); } } } con.close(); } catch (SQLException ex) { Logger.getLogger(populate.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:amie.keys.CSAKey.java
public static void main(String[] args) throws IOException, InterruptedException { final Triple<MiningAssistant, Float, String> parsedArgs = parseArguments(args); final Set<Rule> output = new LinkedHashSet<>(); // Helper object that contains the implementation for the calculation // of confidence and support // The file with the non-keys, one per line long timea = System.currentTimeMillis(); List<List<String>> inputNonKeys = Utilities.parseNonKeysFile(parsedArgs.third); System.out.println(inputNonKeys.size() + " input non-keys"); final List<List<String>> nonKeys = pruneBySupport(inputNonKeys, parsedArgs.second, parsedArgs.first.getKb());//from ww w .j a v a 2s . co m Collections.sort(nonKeys, new Comparator<List<String>>() { @Override public int compare(List<String> o1, List<String> o2) { int r = Integer.compare(o2.size(), o1.size()); if (r == 0) { return Integer.compare(o2.hashCode(), o1.hashCode()); } return r; } }); System.out.println(nonKeys.size() + " non-keys after pruning"); int totalLoad = computeLoad(nonKeys); System.out.println(totalLoad + " is the total load"); int nThreads = Runtime.getRuntime().availableProcessors(); //int batchSize = Math.max(Math.min(maxBatchSize, totalLoad / nThreads), minBatchSize); int batchSize = Math.max(Math.min(maxLoad, totalLoad / nThreads), minLoad); final Queue<int[]> chunks = new PriorityQueue(50, new Comparator<int[]>() { @Override public int compare(int[] o1, int[] o2) { return Integer.compare(o2[2], o1[2]); } }); final HashSet<HashSet<Integer>> nonKeysInt = new HashSet<>(); final HashMap<String, Integer> property2Id = new HashMap<>(); final HashMap<Integer, String> id2Property = new HashMap<>(); final List<Integer> propertiesList = new ArrayList<>(); int support = (int) parsedArgs.second.floatValue(); KB kb = parsedArgs.first.getKb(); buildDictionaries(nonKeys, nonKeysInt, property2Id, id2Property, propertiesList, support, kb); final List<HashSet<Integer>> nonKeysIntList = new ArrayList<>(nonKeysInt); int start = 0; int[] nextIdx = nextIndex(nonKeysIntList, 0, batchSize); int end = nextIdx[0]; int load = nextIdx[1]; while (start < nonKeysIntList.size()) { int[] chunk = new int[] { start, end, load }; chunks.add(chunk); start = end; nextIdx = nextIndex(nonKeysIntList, end, batchSize); end = nextIdx[0]; load = nextIdx[1]; } Thread[] threads = new Thread[Math.min(Runtime.getRuntime().availableProcessors(), chunks.size())]; for (int i = 0; i < threads.length; ++i) { threads[i] = new Thread(new Runnable() { @Override public void run() { while (true) { int[] chunk = null; synchronized (chunks) { if (!chunks.isEmpty()) { chunk = chunks.poll(); } else { break; } } System.out.println("Processing chunk " + Arrays.toString(chunk)); mine(parsedArgs, nonKeysIntList, property2Id, id2Property, propertiesList, chunk[0], chunk[1], output); } } }); threads[i].start(); } for (int i = 0; i < threads.length; ++i) { threads[i].join(); } long timeb = System.currentTimeMillis(); System.out.println("==== Unique C-keys ====="); for (Rule r : output) { System.out.println(Utilities.formatKey(r)); } System.out.println( "VICKEY found " + output.size() + " unique conditional keys in " + (timeb - timea) + " ms"); }
From source file:at.tuwien.ifs.somtoolbox.models.GrowingSOM.java
/** * Method for stand-alone execution of map training. Options are:<br/> * <ul>/*w ww . j a v a2 s . c o m*/ * <li>-h toggles HTML output</li> * <li>-l name of class implementing the labeling algorithm</li> * <li>-n number of labels to generate</li> * <li>-w name of weight vector file in case of training an already trained map</li> * <li>-m name of map description file in case of training an already trained map</li> * <li>--noDWM switch to not write the data winner mapping file</li> * <li>properties name of properties file, mandatory</li> * </ul> * * @param args the execution arguments as stated above. */ public static void main(String[] args) { InputData data = null; FileProperties fileProps = null; GrowingSOM som = null; SOMProperties somProps = null; String networkModelName = "GrowingSOM"; // register and parse all options JSAPResult config = OptionFactory.parseResults(args, OPTIONS); Logger.getLogger("at.tuwien.ifs.somtoolbox").info("starting" + networkModelName); int cpus = config.getInt("cpus", 1); int systemCPUs = Runtime.getRuntime().availableProcessors(); // We do not use more CPUs than available! if (cpus > systemCPUs) { String msg = "Number of CPUs required exceeds number of CPUs available."; if (cpus > 2 * systemCPUs) { msg += "Limiting to twice the number of available processors: " + 2 * systemCPUs; cpus = 2 * systemCPUs; } Logger.getLogger("at.tuwien.ifs.somtoolbox").warning(msg); } GrowingLayer.setNO_CPUS(cpus); String propFileName = AbstractOptionFactory.getFilePath(config, "properties"); String weightFileName = AbstractOptionFactory.getFilePath(config, "weightVectorFile"); String mapDescFileName = AbstractOptionFactory.getFilePath(config, "mapDescriptionFile"); String labelerName = config.getString("labeling", null); int numLabels = config.getInt("numberLabels", DEFAULT_LABEL_COUNT); boolean skipDataWinnerMapping = config.getBoolean("skipDataWinnerMapping", false); Labeler labeler = null; // TODO: use parameter for max int numWinners = config.getInt("numberWinners", SOMLibDataWinnerMapping.MAX_DATA_WINNERS); if (labelerName != null) { // if labeling then label try { labeler = AbstractLabeler.instantiate(labelerName); Logger.getLogger("at.tuwien.ifs.somtoolbox").info("Instantiated labeler " + labelerName); } catch (Exception e) { Logger.getLogger("at.tuwien.ifs.somtoolbox") .severe("Could not instantiate labeler \"" + labelerName + "\"."); System.exit(-1); } } if (weightFileName == null) { Logger.getLogger("at.tuwien.ifs.somtoolbox").info("Training a new SOM."); } else { Logger.getLogger("at.tuwien.ifs.somtoolbox").info("Further training of an already trained SOM."); } try { fileProps = new FileProperties(propFileName); somProps = new SOMProperties(propFileName); } catch (PropertiesException e) { Logger.getLogger("at.tuwien.ifs.somtoolbox").severe(e.getMessage() + " Aborting."); System.exit(-1); } data = getInputData(fileProps); if (weightFileName == null) { som = new GrowingSOM(data.isNormalizedToUnitLength(), somProps, data); } else { try { som = new GrowingSOM(new SOMLibFormatInputReader(weightFileName, null, mapDescFileName)); } catch (Exception e) { Logger.getLogger("at.tuwien.ifs.somtoolbox").severe(e.getMessage() + " Aborting."); System.exit(-1); } } if (somProps.getDumpEvery() > 0) { IntermediateSOMDumper dumper = som.new IntermediateSOMDumper(fileProps); som.layer.setTrainingInterruptionListener(dumper, somProps.getDumpEvery()); } // setting input data so it is accessible by map output som.setSharedInputObjects(new SharedSOMVisualisationData(null, null, null, null, fileProps.vectorFileName(true), fileProps.templateFileName(true), null)); som.getSharedInputObjects().setData(SOMVisualisationData.INPUT_VECTOR, data); som.train(data, somProps); if (labelerName != null) { // if labeling then label labeler.label(som, data, numLabels); } try { SOMLibMapOutputter.write(som, fileProps.outputDirectory(), fileProps.namePrefix(false), true, somProps, fileProps); } catch (IOException e) { // TODO: create new exception type Logger.getLogger("at.tuwien.ifs.somtoolbox").severe("Could not open or write to output file " + fileProps.namePrefix(false) + ": " + e.getMessage()); System.exit(-1); } if (!skipDataWinnerMapping) { numWinners = Math.min(numWinners, som.getLayer().getXSize() * som.getLayer().getYSize()); try { SOMLibMapOutputter.writeDataWinnerMappingFile(som, data, numWinners, fileProps.outputDirectory(), fileProps.namePrefix(false), true); } catch (IOException e) { Logger.getLogger("at.tuwien.ifs.somtoolbox").severe("Could not open or write to output file " + fileProps.namePrefix(false) + ": " + e.getMessage()); System.exit(-1); } } else { Logger.getLogger("at.tuwien.ifs.somtoolbox").info("Skipping writing data winner mapping file"); } if (config.getBoolean("htmlOutput") == true) { try { new HTMLOutputter().write(som, fileProps.outputDirectory(), fileProps.namePrefix(false)); } catch (IOException e) { // TODO: create new exception type Logger.getLogger("at.tuwien.ifs.somtoolbox").severe("Could not open or write to output file " + fileProps.namePrefix(false) + ": " + e.getMessage()); System.exit(-1); } } Logger.getLogger("at.tuwien.ifs.somtoolbox").info("finished" + networkModelName + "(" + som.getLayer().getGridLayout() + ", " + som.getLayer().getGridTopology() + ")"); }
From source file:Main.java
public static int calculateMaxCacheSize() { return (int) Math.min(Runtime.getRuntime().maxMemory() / 32L, 0x7fffffffL); }
From source file:Main.java
public static void xorArray(byte[] array, byte[] mask) { final int len = Math.min(array.length, mask.length); for (int i = 0; i < len; ++i) { array[i] ^= mask[i];/*from w ww. j av a 2s . co m*/ } }