Example usage for org.apache.commons.lang.time StopWatch start

List of usage examples for org.apache.commons.lang.time StopWatch start

Introduction

In this page you can find the example usage for org.apache.commons.lang.time StopWatch start.

Prototype

public void start() 

Source Link

Document

Start the stopwatch.

This method starts a new timing session, clearing any previous values.

Usage

From source file:com.liferay.portal.tools.DBUpgrader.java

public static void main(String[] args) {
    try {//w  w  w.  ja  v  a  2s. c o m
        StopWatch stopWatch = new StopWatch();

        stopWatch.start();

        InitUtil.initWithSpring();

        upgrade();
        verify();

        System.out.println("\nSuccessfully completed upgrade process in " + (stopWatch.getTime() / Time.SECOND)
                + " seconds.");

        System.exit(0);
    } catch (Exception e) {
        e.printStackTrace();

        System.exit(1);
    }
}

From source file:ch.systemsx.cisd.openbis.generic.server.dataaccess.db.IndexCreationUtil.java

public static void main(final String[] args) throws Exception {
    Parameters parameters = null;//from  ww w  .j  a v a 2s .c  o  m
    try {
        parameters = new Parameters(args);
    } catch (IllegalArgumentException e) {
        System.out.println(Parameters.getUsage());
        System.exit(1);
        return; // for Eclipse
    }
    LogInitializer.init();
    String databaseKind = parameters.getDatabaseKind();
    String duplicatedDatabaseKind = parameters.getDuplicatedDatabaseKind();
    String indexFolder = parameters.getIndexFolder();
    if (duplicatedDatabaseKind != null) {

        String databaseName = DATABASE_NAME_PREFIX + databaseKind;
        String duplicatedDatabaseName = DATABASE_NAME_PREFIX + duplicatedDatabaseKind;
        boolean ok = duplicateDatabase(duplicatedDatabaseName, databaseName);
        if (ok == false) {
            System.exit(1);
        }
        File dumpFile = parameters.getDumpFile();
        operationLog.info("Dump '" + duplicatedDatabaseName + "' into '" + dumpFile + "'.");
        DumpPreparator.createDatabaseDump(duplicatedDatabaseName, dumpFile);
        databaseKind = duplicatedDatabaseKind;
        FileUtilities.deleteRecursively(new File(indexFolder));
    }
    System.setProperty("database.kind", databaseKind);
    // Deactivate the indexing in the application context loaded by Spring.
    System.setProperty("hibernate.search.index-mode", "NO_INDEX");
    System.setProperty("hibernate.search.index-base", indexFolder);
    System.setProperty("database.create-from-scratch", "false");
    hibernateSearchContext = createHibernateSearchContext(indexFolder);
    hibernateSearchContext.afterPropertiesSet();
    operationLog.info("=========== Start indexing ===========");
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    performFullTextIndex();
    stopWatch.stop();
    operationLog.info("Index of database '" + DATABASE_NAME_PREFIX + databaseKind + "' successfully built in '"
            + indexFolder + "' after " + ((stopWatch.getTime() + 30000) / 60000) + " minutes.");
    System.exit(0);
}

From source file:MainClass.java

public static void main(String[] args) {
    StopWatch clock = new StopWatch();
    NumberFormat format = NumberFormat.getInstance();

    System.out.println("How long does it take to take the sin of 0.34 ten million times?");
    clock.start();
    for (int i = 0; i < 100000000; i++) {
        Math.sin(0.34);/*w w w  .  java 2s  .  co m*/
    }
    clock.stop();

    System.out.println("It takes " + clock.getTime() + " milliseconds");

    System.out.println("How long does it take to multiply 2 doubles one billion times?");
    clock.reset();
    clock.start();
    for (int i = 0; i < 1000000000; i++) {
        double result = 3423.2234 * 23e-4;
    }
    clock.stop();
    System.out.println("It takes " + clock.getTime() + " milliseconds.");

    System.out.println("How long does it take to add 2 ints one billion times?");
    clock.reset();
    clock.start();
    for (int i = 0; i < 1000000000; i++) {
        int result = 293842923 + 33382922;
    }
    clock.stop();
    System.out.println("It takes " + clock.getTime() + " milliseconds.");

    System.out.println("Testing the split() method.");
    clock.reset();
    clock.start();
    try {
        Thread.sleep(1000);
    } catch (Exception e) {
    }
    clock.split();
    System.out.println("Split Time after 1 sec: " + clock.getTime());
    try {
        Thread.sleep(1000);
    } catch (Exception e) {
    }
    System.out.println("Split Time after 2 sec: " + clock.getTime());
    clock.unsplit();
    try {
        Thread.sleep(1000);
    } catch (Exception e) {
    }
    System.out.println("Time after 3 sec: " + clock.getTime());

}

From source file:br.edu.ufcg.lsd.oursim.ui.CLI.java

/**
 * Exemplo://  w ww.j  a  va 2  s  . c  om
 * 
 * <pre>
 *   java -jar oursim.jar -w resources/trace_filtrado_primeiros_1000_jobs.txt -m resources/hostinfo_sdsc.dat -synthetic_av -o oursim_trace.txt
 *   -w resources/trace_filtrado_primeiros_1000_jobs.txt -s persistent -nr 20 -md resources/hostinfo_sdsc.dat -av resources/disponibilidade.txt -o oursim_trace.txt
 *   -w resources/new_iosup_workload.txt -s persistent -pd resources/iosup_site_description.txt -wt iosup -nr 1 -synthetic_av -o oursim_trace.txt
 *   -w resources/new_workload.txt -s persistent -pd resources/marcus_site_description.txt -wt marcus -nr 20 -d -o oursim_trace.txt
 *   1 ms + 1 dia = 2678400 segundos
 * </pre>
 * 
 * @param args
 * @throws FileNotFoundException
 */
public static void main(String[] args) throws IOException {

    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    List<Closeable> closeables = new ArrayList<Closeable>();

    CommandLine cmd = parseCommandLine(args, prepareOptions(), HELP, USAGE, EXECUTION_LINE);

    File outputFile = (File) cmd.getOptionObject(OUTPUT);
    PrintOutput printOutput = new PrintOutput(outputFile, false);
    JobEventDispatcher.getInstance().addListener(printOutput);
    closeables.add(printOutput);
    if (cmd.hasOption(EXTRACT_REMOTE_WORKLOAD)) {
        File remoteWorkloadFile = (File) cmd.getOptionObject(EXTRACT_REMOTE_WORKLOAD);
        Output remoteWorkloadExtractor = new RemoteTasksExtractorOutput(remoteWorkloadFile);
        closeables.add(remoteWorkloadExtractor);
        JobEventDispatcher.getInstance().addListener(remoteWorkloadExtractor);
    }
    Grid grid = prepareGrid(cmd);

    ComputingElementEventCounter computingElementEventCounter = prepareOutputAccounting(cmd,
            cmd.hasOption(VERBOSE));

    Input<? extends AvailabilityRecord> availability = defineAvailability(cmd, grid.getMapOfPeers());

    prepareOptionalOutputFiles(cmd, grid, (SyntheticAvailabilityCharacterizationAbstract) availability,
            closeables);

    long timeOfFirstSubmission = cmd.getOptionValue(WORKLOAD_TYPE).equals("gwa")
            ? GWAFormat.extractSubmissionTimeFromFirstJob(cmd.getOptionValue(WORKLOAD))
            : 0;
    Workload workload = defineWorkloadType(cmd, cmd.getOptionValue(WORKLOAD), grid.getMapOfPeers(),
            timeOfFirstSubmission);

    JobSchedulerPolicy jobScheduler = defineScheduler(cmd, grid.getListOfPeers());

    OurSim oursim = new OurSim(EventQueue.getInstance(), grid, jobScheduler, workload, availability);

    oursim.setActiveEntity(new ActiveEntityImp());

    if (cmd.hasOption(HALT_SIMULATION)) {
        oursim.addHaltEvent(((Number) cmd.getOptionObject(HALT_SIMULATION)).longValue());
    }

    oursim.start();

    for (Closeable c : closeables) {
        c.close();
    }

    EventQueue.getInstance().clear();

    // adiciona mtricas-resumo ao fim do arquivo
    FileWriter fw = new FileWriter(cmd.getOptionValue(OUTPUT), true);
    closeables.add(fw);
    stopWatch.stop();
    fw.write("# Simulation                  duration:" + stopWatch + ".\n");

    double utilization = grid.getUtilization();
    double realUtilization = grid.getTrueUtilization();

    int numberOfResourcesByPeer = Integer.parseInt(cmd.getOptionValue(NUM_RESOURCES_BY_PEER, "0"));
    fw.write(formatSummaryStatistics(computingElementEventCounter, "NA", "NA", false, grid.getPeers().size(),
            numberOfResourcesByPeer, utilization, realUtilization, stopWatch.getTime()) + "\n");
    fw.close();

    System.out.println(
            getSummaryStatistics(computingElementEventCounter, "NA", "NA", false, grid.getPeers().size(),
                    numberOfResourcesByPeer, utilization, realUtilization, stopWatch.getTime()));

}

From source file:de.unisb.cs.st.javalanche.mutation.util.CsvWriter.java

public static void main(String[] args) throws IOException {
    // Set<Long> covered = MutationCoverageFile.getCoveredMutations();
    // List<Long> mutationIds = QueryManager.getMutationsWithoutResult(
    // covered, 0);

    Session session = HibernateUtil.getSessionFactory().openSession();
    List<Mutation> mutations = QueryManager.getMutationsForProject(
            ConfigurationLocator.getJavalancheConfiguration().getProjectPrefix(), session);

    logger.info("Got " + mutations.size() + " mutation ids.");
    List<String> lines = new ArrayList<String>();
    lines.add(Mutation.getCsvHead() + ",DETECTED");
    int counter = 0;
    int flushs = 0;
    StopWatch stp = new StopWatch();
    for (Mutation mutation : mutations) {
        // Mutation mutation = QueryManager.getMutationByID(id, session);
        lines.add(mutation.getCsvString() + "," + mutation.isKilled());
        counter++;/*from   ww  w. j  a  va2 s  .c o m*/
        if (counter > 20) {
            counter = 0;
            // 20, same as the JDBC batch size
            // flush a batch of inserts and release memory:
            // see
            // http://www.hibernate.org/hib_docs/reference/en/html/batch.html
            stp.reset();
            stp.start();
            flushs++;
            session.flush();
            // session.clear();
            logger.info("Did flush. It took: " + DurationFormatUtils.formatDurationHMS(stp.getTime()));
        }
    }
    session.close();
    logger.info("Starting to write file with " + lines.size() + " entries.");
    FileUtils.writeLines(new File("mutations.csv"), lines);
}

From source file:fr.inria.edelweiss.kgdqp.core.CentralizedInferrencing.java

public static void main(String args[])
        throws ParseException, EngineException, InterruptedException, IOException {

    List<String> endpoints = new ArrayList<String>();
    String queryPath = null;//ww w. ja  v  a 2 s.c om
    boolean rulesSelection = false;
    File rulesDir = null;
    File ontDir = null;

    /////////////////
    Graph graph = Graph.create();
    QueryProcess exec = QueryProcess.create(graph);

    Options options = new Options();
    Option helpOpt = new Option("h", "help", false, "print this message");
    //        Option queryOpt = new Option("q", "query", true, "specify the sparql query file");
    //        Option endpointOpt = new Option("e", "endpoint", true, "a federated sparql endpoint URL");
    Option versionOpt = new Option("v", "version", false, "print the version information and exit");
    Option rulesOpt = new Option("r", "rulesDir", true, "directory containing the inference rules");
    Option ontOpt = new Option("o", "ontologiesDir", true,
            "directory containing the ontologies for rules selection");
    //        Option locOpt = new Option("c", "centralized", false, "performs centralized inferences");
    Option dataOpt = new Option("l", "load", true, "data file or directory to be loaded");
    //        Option selOpt = new Option("s", "rulesSelection", false, "if set to true, only the applicable rules are run");
    //        options.addOption(queryOpt);
    //        options.addOption(endpointOpt);
    options.addOption(helpOpt);
    options.addOption(versionOpt);
    options.addOption(rulesOpt);
    options.addOption(ontOpt);
    //        options.addOption(selOpt);
    //        options.addOption(locOpt);
    options.addOption(dataOpt);

    String header = "Corese/KGRAM rule engine experiment command line interface";
    String footer = "\nPlease report any issue to alban.gaignard@cnrs.fr, olivier.corby@inria.fr";

    CommandLineParser parser = new BasicParser();
    CommandLine cmd = parser.parse(options, args);
    if (cmd.hasOption("h")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("kgdqp", header, options, footer, true);
        System.exit(0);
    }
    if (cmd.hasOption("o")) {
        rulesSelection = true;
        String ontDirPath = cmd.getOptionValue("o");
        ontDir = new File(ontDirPath);
        if (!ontDir.isDirectory()) {
            logger.warn(ontDirPath + " is not a valid directory path.");
            System.exit(0);
        }
    }
    if (!cmd.hasOption("r")) {
        logger.info("You must specify a path for inference rules directory !");
        System.exit(0);
    }

    if (cmd.hasOption("l")) {
        String[] dataPaths = cmd.getOptionValues("l");
        for (String path : dataPaths) {
            Load ld = Load.create(graph);
            ld.load(path);
            logger.info("Loaded " + path);
        }
    }

    if (cmd.hasOption("v")) {
        logger.info("version 3.0.4-SNAPSHOT");
        System.exit(0);
    }

    String rulesDirPath = cmd.getOptionValue("r");
    rulesDir = new File(rulesDirPath);
    if (!rulesDir.isDirectory()) {
        logger.warn(rulesDirPath + " is not a valid directory path.");
        System.exit(0);
    }

    // Local rules graph initialization
    Graph rulesG = Graph.create();
    Load ld = Load.create(rulesG);

    if (rulesSelection) {
        // Ontology loading
        if (ontDir.isDirectory()) {
            for (File o : ontDir.listFiles()) {
                logger.info("Loading " + o.getAbsolutePath());
                ld.load(o.getAbsolutePath());
            }
        }
    }

    // Rules loading
    if (rulesDir.isDirectory()) {
        for (File r : rulesDir.listFiles()) {
            logger.info("Loading " + r.getAbsolutePath());
            ld.load(r.getAbsolutePath());
        }
    }

    // Rule engine initialization
    RuleEngine ruleEngine = RuleEngine.create(graph);
    ruleEngine.set(exec);
    ruleEngine.setOptimize(true);
    ruleEngine.setConstructResult(true);
    ruleEngine.setTrace(true);

    StopWatch sw = new StopWatch();
    logger.info("Federated graph size : " + graph.size());
    logger.info("Rules graph size : " + rulesG.size());

    // Rule selection
    logger.info("Rules selection");
    QueryProcess localKgram = QueryProcess.create(rulesG);
    ArrayList<String> applicableRules = new ArrayList<String>();
    sw.start();
    String rulesSelQuery = "";
    if (rulesSelection) {
        rulesSelQuery = pertinentRulesQuery;
    } else {
        rulesSelQuery = allRulesQuery;
    }
    Mappings maps = localKgram.query(rulesSelQuery);
    logger.info("Rules selected in " + sw.getTime() + " ms");
    logger.info("Applicable rules : " + maps.size());

    // Selected rule loading
    for (Mapping map : maps) {
        IDatatype dt = (IDatatype) map.getValue("?res");
        String rule = dt.getLabel();
        //loading rule in the rule engine
        //            logger.info("Adding rule : ");
        //            System.out.println("-------");
        //            System.out.println(rule);
        //            System.out.println("");
        //            if (! rule.toLowerCase().contains("sameas")) {
        applicableRules.add(rule);
        ruleEngine.addRule(rule);
        //            }
    }

    // Rules application on distributed sparql endpoints
    logger.info("Rules application (" + applicableRules.size() + " rules)");
    ExecutorService threadPool = Executors.newCachedThreadPool();
    RuleEngineThread ruleThread = new RuleEngineThread(ruleEngine);
    sw.reset();
    sw.start();

    //        ruleEngine.process();
    threadPool.execute(ruleThread);
    threadPool.shutdown();

    //monitoring loop
    while (!threadPool.isTerminated()) {
        //            System.out.println("******************************");
        //            System.out.println(Util.jsonDqpCost(QueryProcessDQP.queryCounter, QueryProcessDQP.queryVolumeCounter, QueryProcessDQP.sourceCounter, QueryProcessDQP.sourceVolumeCounter));
        //            System.out.println("Rule engine running for " + sw.getTime() + " ms");
        //            System.out.println("Federated graph size : " + graph.size());
        System.out.println(sw.getTime() + " , " + graph.size());
        Thread.sleep(5000);
    }

    logger.info("Federated graph size : " + graph.size());
    //        logger.info(Util.jsonDqpCost(QueryProcessDQP.queryCounter, QueryProcessDQP.queryVolumeCounter, QueryProcessDQP.sourceCounter, QueryProcessDQP.sourceVolumeCounter));

    //        TripleFormat f = TripleFormat.create(graph, true);
    //        f.write("/tmp/gAll.ttl");

}

From source file:fr.inria.edelweiss.kgimport.RdfSplitter.java

/**
 * The application entrypoint, configured through the command line input
 * arguments./*from  w  w w . ja v a 2 s . c  om*/
 *
 * @param args the input command line arguments.
 */
public static void main(String args[]) {

    RdfSplitter rdfSplitter = new RdfSplitter();

    Options options = new Options();
    Option helpOpt = new Option("h", "help", false, "Print usage information.");
    Option inDirOpt = new Option("i", "input-dir", true, "The directory containing RDF files to be loaded.");
    Option outDirOpt = new Option("o", "output-dir", true,
            "The directory containing the generated RDF fragments");
    Option predFiltOpt = new Option("p", "predicate-filter", true,
            "Predicate filter used to segment the dataset. "
                    + "You can use multiple filters, typically one per fragment.");
    Option fragNbOpt = new Option("n", "number-of-fragments", true,
            "Number of fragments generated for the whole input dataset.");
    Option fragRepOpt = new Option("f", "fractionning-percentage", true,
            "Percentage of the whole input dataset for this fragment.");
    Option tdbOpt = new Option("tdb", "tdb-storage", false,
            "RDF fragments are persisted into a Jena TDB backend.");
    Option versionOpt = new Option("v", "version", false, "Print the version information and exit.");
    options.addOption(inDirOpt);
    options.addOption(outDirOpt);
    options.addOption(predFiltOpt);
    options.addOption(helpOpt);
    options.addOption(versionOpt);
    options.addOption(fragNbOpt);
    options.addOption(fragRepOpt);
    options.addOption(tdbOpt);

    String header = "RDF data fragmentation tool command line interface";
    String footer = "\nPlease report any issue to alban.gaignard@cnrs.fr";

    CommandLineParser parser = new BasicParser();
    CommandLine cmd = null;
    try {
        cmd = parser.parse(options, args);

        if (cmd.hasOption("h")) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("java -jar [].jar", header, options, footer, true);
            System.exit(0);
        }

        if (!cmd.hasOption("i")) {
            logger.warn("You must specify a valid input directory !");
            System.exit(-1);
        } else {
            rdfSplitter.setInputDirPath(cmd.getOptionValue("i"));
        }
        if (!cmd.hasOption("o")) {
            logger.warn("You must specify a valid output directory !");
            System.exit(-1);
        } else {
            rdfSplitter.setOutputDirPath(cmd.getOptionValue("o"));
        }
        if (cmd.hasOption("p")) {
            rdfSplitter.setInputPredicates(new ArrayList<String>(Arrays.asList(cmd.getOptionValues("p"))));
        }
        if (cmd.hasOption("f")) {
            ArrayList<String> opts = new ArrayList<String>(Arrays.asList(cmd.getOptionValues("f")));
            for (String opt : opts) {
                try {
                    rdfSplitter.getFragList().add(Integer.parseInt(opt));
                } catch (NumberFormatException e) {
                    logger.error(opt + " cannot be pased as an percentage value.");
                    System.exit(-1);
                }
            }
        }
        if (cmd.hasOption("n")) {
            try {
                rdfSplitter.setFragNb(Integer.parseInt(cmd.getOptionValue("n")));
            } catch (NumberFormatException e) {
                logger.error(cmd.getOptionValue("n") + " cannot be pased as an integer value.");
                System.exit(-1);
            }
        }

        File oDir = new File(rdfSplitter.getOutputDirPath());
        if (oDir.exists()) {
            logger.warn(rdfSplitter.getOutputDirPath() + " already exists !");
            oDir = Files.createTempDir();
            logger.warn(oDir.getAbsolutePath() + " created.");
            rdfSplitter.setOutputDirPath(oDir.getAbsolutePath());
        } else {
            if (oDir.mkdir()) {
                logger.info(rdfSplitter.getOutputDirPath() + " created.");
            }
        }

        if (!cmd.hasOption("n") && !cmd.hasOption("f") && !cmd.hasOption("p")) {
            logger.error("You must specify just one fragmentation type through '-n', '-f', or 'p' options");
            for (String arg : args) {
                logger.trace(arg);
            }
            System.exit(-1);
        }

        String fragName = rdfSplitter.getInputDirPath()
                .substring(rdfSplitter.getInputDirPath().lastIndexOf("/") + 1);

        //Input data loading
        Model model = ModelFactory.createDefaultModel();
        File inputDir = new File(rdfSplitter.getInputDirPath());
        if (inputDir.isDirectory()) {
            for (File f : inputDir.listFiles()) {
                logger.info("Loading " + f.getAbsolutePath());
                if (f.isDirectory()) {
                    String directory = f.getAbsolutePath();
                    Dataset dataset = TDBFactory.createDataset(directory);
                    dataset.begin(ReadWrite.READ);
                    // Get model inside the transaction
                    model.add(dataset.getDefaultModel());
                    dataset.end();
                } else {
                    InputStream iS;
                    try {
                        iS = new FileInputStream(f);
                        if (f.getAbsolutePath().endsWith(".n3")) {
                            model.read(iS, null, "N3");
                        } else if (f.getAbsolutePath().endsWith(".nt")) {
                            model.read(iS, null, "N-TRIPLES");
                        } else if (f.getAbsolutePath().endsWith(".rdf")) {
                            model.read(iS, null);
                        }
                    } catch (FileNotFoundException ex) {
                        LogManager.getLogger(RdfSplitter.class.getName()).log(Level.ERROR, "", ex);
                    }
                }
            }
            logger.info("Loaded " + model.size() + " triples");
        } else {
            System.exit(0);
        }

        StopWatch sw = new StopWatch();
        if (cmd.hasOption("n")) {
            sw.start();
            if (cmd.hasOption("tdb")) {
                rdfSplitter.saveFragmentsTDB(rdfSplitter.getFragHoriz(model, rdfSplitter.getFragNb()),
                        "Homog-" + fragName);
            } else {
                rdfSplitter.saveFragmentsRDF(rdfSplitter.getFragHoriz(model, rdfSplitter.getFragNb()),
                        "Homog-" + fragName);
            }
            logger.info("Homog horiz frag in " + sw.getTime() + "ms");
            sw.reset();
        } else if (cmd.hasOption("f")) {
            sw.start();
            if (cmd.hasOption("tdb")) {
                rdfSplitter.saveFragmentsTDB(rdfSplitter.getFragHoriz(model, rdfSplitter.getFragList()),
                        "Inhomog-" + fragName);
            } else {
                rdfSplitter.saveFragmentsRDF(rdfSplitter.getFragHoriz(model, rdfSplitter.getFragList()),
                        "Inhomog-" + fragName);
            }
            logger.info("Inhomog horiz frag in " + sw.getTime() + "ms");
            sw.reset();
        } else if (cmd.hasOption("p")) {
            sw.start();
            if (cmd.hasOption("tdb")) {
                rdfSplitter.saveFragmentsTDB(rdfSplitter.getFragVert(model, rdfSplitter.getInputPredicates()));
            } else {
                rdfSplitter.saveFragmentsRDF(rdfSplitter.getFragVert(model, rdfSplitter.getInputPredicates()));
            }
            logger.info("Vert frag in " + sw.getTime() + "ms");
            sw.reset();
        }

    } catch (ParseException ex) {
        logger.error("Impossible to parse the input command line " + cmd.toString());
    }
}

From source file:fr.inria.edelweiss.kgdqp.core.CentralizedInferrencingNoSpin.java

public static void main(String args[])
        throws ParseException, EngineException, InterruptedException, IOException, LoadException {

    List<String> endpoints = new ArrayList<String>();
    String queryPath = null;//w  w w.j  a va 2s  .  c  o m
    boolean rulesSelection = false;
    File rulesDir = null;
    File ontDir = null;

    /////////////////
    Graph graph = Graph.create();
    QueryProcess exec = QueryProcess.create(graph);

    Options options = new Options();
    Option helpOpt = new Option("h", "help", false, "print this message");
    //        Option queryOpt = new Option("q", "query", true, "specify the sparql query file");
    //        Option endpointOpt = new Option("e", "endpoint", true, "a federated sparql endpoint URL");
    Option versionOpt = new Option("v", "version", false, "print the version information and exit");
    Option rulesOpt = new Option("r", "rulesDir", true, "directory containing the inference rules");
    Option ontOpt = new Option("o", "ontologiesDir", true,
            "directory containing the ontologies for rules selection");
    //        Option locOpt = new Option("c", "centralized", false, "performs centralized inferences");
    Option dataOpt = new Option("l", "load", true, "data file or directory to be loaded");
    //        Option selOpt = new Option("s", "rulesSelection", false, "if set to true, only the applicable rules are run");
    //        options.addOption(queryOpt);
    //        options.addOption(endpointOpt);
    options.addOption(helpOpt);
    options.addOption(versionOpt);
    options.addOption(rulesOpt);
    options.addOption(ontOpt);
    //        options.addOption(selOpt);
    //        options.addOption(locOpt);
    options.addOption(dataOpt);

    String header = "Corese/KGRAM rule engine experiment command line interface";
    String footer = "\nPlease report any issue to alban.gaignard@cnrs.fr, olivier.corby@inria.fr";

    CommandLineParser parser = new BasicParser();
    CommandLine cmd = parser.parse(options, args);
    if (cmd.hasOption("h")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("kgdqp", header, options, footer, true);
        System.exit(0);
    }
    if (cmd.hasOption("o")) {
        rulesSelection = true;
        String ontDirPath = cmd.getOptionValue("o");
        ontDir = new File(ontDirPath);
        if (!ontDir.isDirectory()) {
            logger.warn(ontDirPath + " is not a valid directory path.");
            System.exit(0);
        }
    }
    if (!cmd.hasOption("r")) {
        logger.info("You must specify a path for inference rules directory !");
        System.exit(0);
    }

    if (cmd.hasOption("l")) {
        String[] dataPaths = cmd.getOptionValues("l");
        for (String path : dataPaths) {
            Load ld = Load.create(graph);
            ld.load(path);
            logger.info("Loaded " + path);
        }
    }

    if (cmd.hasOption("v")) {
        logger.info("version 3.0.4-SNAPSHOT");
        System.exit(0);
    }

    String rulesDirPath = cmd.getOptionValue("r");
    rulesDir = new File(rulesDirPath);
    if (!rulesDir.isDirectory()) {
        logger.warn(rulesDirPath + " is not a valid directory path.");
        System.exit(0);
    }

    // Local rules graph initialization
    Graph rulesG = Graph.create();
    Load ld = Load.create(rulesG);

    if (rulesSelection) {
        // Ontology loading
        if (ontDir.isDirectory()) {
            for (File o : ontDir.listFiles()) {
                logger.info("Loading " + o.getAbsolutePath());
                ld.load(o.getAbsolutePath());
            }
        }
    }

    // Rules loading
    if (rulesDir.isDirectory()) {
        for (File r : rulesDir.listFiles()) {
            if (r.getAbsolutePath().endsWith(".rq")) {
                logger.info("Loading " + r.getAbsolutePath());
                //                ld.load(r.getAbsolutePath());

                //                    byte[] encoded = Files.readAllBytes(Paths.get(r.getAbsolutePath()));
                //                    String construct = new String(encoded, "UTF-8"); //StandardCharsets.UTF_8);

                FileInputStream f = new FileInputStream(r);
                QueryLoad ql = QueryLoad.create();
                String construct = ql.read(f);
                f.close();

                SPINProcess sp = SPINProcess.create();
                String spinConstruct = sp.toSpin(construct);

                ld.load(new ByteArrayInputStream(spinConstruct.getBytes()), Load.TURTLE_FORMAT);
                logger.info("Rules graph size : " + rulesG.size());

            }
        }
    }

    // Rule engine initialization
    RuleEngine ruleEngine = RuleEngine.create(graph);
    ruleEngine.set(exec);
    ruleEngine.setOptimize(true);
    ruleEngine.setConstructResult(true);
    ruleEngine.setTrace(true);

    StopWatch sw = new StopWatch();
    logger.info("Federated graph size : " + graph.size());
    logger.info("Rules graph size : " + rulesG.size());

    // Rule selection
    logger.info("Rules selection");
    QueryProcess localKgram = QueryProcess.create(rulesG);
    ArrayList<String> applicableRules = new ArrayList<String>();
    sw.start();
    String rulesSelQuery = "";
    if (rulesSelection) {
        rulesSelQuery = pertinentRulesQuery;
    } else {
        rulesSelQuery = allRulesQuery;
    }
    Mappings maps = localKgram.query(rulesSelQuery);
    logger.info("Rules selected in " + sw.getTime() + " ms");
    logger.info("Applicable rules : " + maps.size());

    // Selected rule loading
    for (Mapping map : maps) {
        IDatatype dt = (IDatatype) map.getValue("?res");
        String rule = dt.getLabel();
        //loading rule in the rule engine
        //            logger.info("Adding rule : ");
        //            System.out.println("-------");
        //            System.out.println(rule);
        //            System.out.println("");
        //            if (! rule.toLowerCase().contains("sameas")) {
        applicableRules.add(rule);
        ruleEngine.addRule(rule);
        //            }
    }

    // Rules application on distributed sparql endpoints
    logger.info("Rules application (" + applicableRules.size() + " rules)");
    ExecutorService threadPool = Executors.newCachedThreadPool();
    RuleEngineThread ruleThread = new RuleEngineThread(ruleEngine);
    sw.reset();
    sw.start();

    //        ruleEngine.process();
    threadPool.execute(ruleThread);
    threadPool.shutdown();

    //monitoring loop
    while (!threadPool.isTerminated()) {
        //            System.out.println("******************************");
        //            System.out.println(Util.jsonDqpCost(QueryProcessDQP.queryCounter, QueryProcessDQP.queryVolumeCounter, QueryProcessDQP.sourceCounter, QueryProcessDQP.sourceVolumeCounter));
        //            System.out.println("Rule engine running for " + sw.getTime() + " ms");
        //            System.out.println("Federated graph size : " + graph.size());
        System.out.println(sw.getTime() + " , " + graph.size());
        Thread.sleep(5000);
    }

    logger.info("Federated graph size : " + graph.size());
    //        logger.info(Util.jsonDqpCost(QueryProcessDQP.queryCounter, QueryProcessDQP.queryVolumeCounter, QueryProcessDQP.sourceCounter, QueryProcessDQP.sourceVolumeCounter));

    //        TripleFormat f = TripleFormat.create(graph, true);
    //        f.write("/tmp/gAll.ttl");
}

From source file:fr.inria.edelweiss.kgdqp.core.FedInferrencingCLI.java

public static void main(String args[]) throws ParseException, EngineException, InterruptedException {

    List<String> endpoints = new ArrayList<String>();
    String queryPath = null;//from w  w  w  . j av  a  2s.c  om
    boolean rulesSelection = false;
    File rulesDir = null;
    File ontDir = null;

    Options options = new Options();
    Option helpOpt = new Option("h", "help", false, "print this message");
    Option queryOpt = new Option("q", "query", true, "specify the sparql query file");
    Option endpointOpt = new Option("e", "endpoint", true, "a federated sparql endpoint URL");
    Option versionOpt = new Option("v", "version", false, "print the version information and exit");
    Option rulesOpt = new Option("r", "rulesDir", true, "directory containing the inference rules");
    Option ontOpt = new Option("o", "ontologiesDir", true,
            "directory containing the ontologies for rules selection");
    //        Option selOpt = new Option("s", "rulesSelection", false, "if set to true, only the applicable rules are run");
    options.addOption(queryOpt);
    options.addOption(endpointOpt);
    options.addOption(helpOpt);
    options.addOption(versionOpt);
    options.addOption(rulesOpt);
    options.addOption(ontOpt);
    //        options.addOption(selOpt);

    String header = "Corese/KGRAM distributed rule engine command line interface";
    String footer = "\nPlease report any issue to alban.gaignard@cnrs.fr, olivier.corby@inria.fr";

    CommandLineParser parser = new BasicParser();
    CommandLine cmd = parser.parse(options, args);
    if (cmd.hasOption("h")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("kgdqp", header, options, footer, true);
        System.exit(0);
    }
    if (!cmd.hasOption("e")) {
        logger.info("You must specify at least the URL of one sparql endpoint !");
        System.exit(0);
    } else {
        endpoints = new ArrayList<String>(Arrays.asList(cmd.getOptionValues("e")));
    }
    if (cmd.hasOption("o")) {
        rulesSelection = true;
        String ontDirPath = cmd.getOptionValue("o");
        ontDir = new File(ontDirPath);
        if (!ontDir.isDirectory()) {
            logger.warn(ontDirPath + " is not a valid directory path.");
            System.exit(0);
        }
    }
    if (!cmd.hasOption("r")) {
        logger.info("You must specify a path for inference rules directory !");
        System.exit(0);
    } else if (rulesSelection) {

    }

    if (cmd.hasOption("v")) {
        logger.info("version 3.0.4-SNAPSHOT");
        System.exit(0);
    }

    String rulesDirPath = cmd.getOptionValue("r");
    rulesDir = new File(rulesDirPath);
    if (!rulesDir.isDirectory()) {
        logger.warn(rulesDirPath + " is not a valid directory path.");
        System.exit(0);
    }

    /////////////////
    Graph graph = Graph.create();
    QueryProcessDQP execDQP = QueryProcessDQP.create(graph);
    for (String url : endpoints) {
        try {
            execDQP.addRemote(new URL(url), WSImplem.REST);
        } catch (MalformedURLException ex) {
            logger.error(url + " is not a well-formed URL");
            System.exit(1);
        }
    }

    // Local rules graph initialization
    Graph rulesG = Graph.create();
    Load ld = Load.create(rulesG);

    if (rulesSelection) {
        // Ontology loading
        if (ontDir.isDirectory()) {
            for (File o : ontDir.listFiles()) {
                logger.info("Loading " + o.getAbsolutePath());
                ld.load(o.getAbsolutePath());
            }
        }
    }

    // Rules loading
    if (rulesDir.isDirectory()) {
        for (File r : rulesDir.listFiles()) {
            logger.info("Loading " + r.getAbsolutePath());
            ld.load(r.getAbsolutePath());
        }
    }

    // Rule engine initialization
    RuleEngine ruleEngine = RuleEngine.create(graph);
    ruleEngine.set(execDQP);

    StopWatch sw = new StopWatch();
    logger.info("Federated graph size : " + graph.size());
    logger.info("Rules graph size : " + rulesG.size());

    // Rule selection
    logger.info("Rules selection");
    QueryProcess localKgram = QueryProcess.create(rulesG);
    ArrayList<String> applicableRules = new ArrayList<String>();
    sw.start();
    String rulesSelQuery = "";
    if (rulesSelection) {
        rulesSelQuery = pertinentRulesQuery;
    } else {
        rulesSelQuery = allRulesQuery;
    }
    Mappings maps = localKgram.query(rulesSelQuery);
    logger.info("Rules selected in " + sw.getTime() + " ms");
    logger.info("Applicable rules : " + maps.size());

    // Selected rule loading
    for (Mapping map : maps) {
        IDatatype dt = (IDatatype) map.getValue("?res");
        String rule = dt.getLabel();
        //loading rule in the rule engine
        //            logger.info("Adding rule : " + rule);
        applicableRules.add(rule);
        ruleEngine.addRule(rule);
    }

    // Rules application on distributed sparql endpoints
    logger.info("Rules application (" + applicableRules.size() + " rules)");
    ExecutorService threadPool = Executors.newCachedThreadPool();
    RuleEngineThread ruleThread = new RuleEngineThread(ruleEngine);
    sw.reset();
    sw.start();

    //        ruleEngine.process();
    threadPool.execute(ruleThread);
    threadPool.shutdown();

    //monitoring loop
    while (!threadPool.isTerminated()) {
        System.out.println("******************************");
        System.out.println(Util.jsonDqpCost(QueryProcessDQP.queryCounter, QueryProcessDQP.queryVolumeCounter,
                QueryProcessDQP.sourceCounter, QueryProcessDQP.sourceVolumeCounter));
        System.out.println("Rule engine running for " + sw.getTime() + " ms");
        System.out.println("Federated graph size : " + graph.size());
        Thread.sleep(10000);
    }

    logger.info("Federated graph size : " + graph.size());
    logger.info(Util.jsonDqpCost(QueryProcessDQP.queryCounter, QueryProcessDQP.queryVolumeCounter,
            QueryProcessDQP.sourceCounter, QueryProcessDQP.sourceVolumeCounter));

    ///////////// Query file processing
    //        StringBuffer fileData = new StringBuffer(1000);
    //        BufferedReader reader = null;
    //        try {
    //            reader = new BufferedReader(new FileReader(queryPath));
    //        } catch (FileNotFoundException ex) {
    //             logger.error("Query file "+queryPath+" not found !");
    //             System.exit(1);
    //        }
    //        char[] buf = new char[1024];
    //        int numRead = 0;
    //        try {
    //            while ((numRead = reader.read(buf)) != -1) {
    //                String readData = String.valueOf(buf, 0, numRead);
    //                fileData.append(readData);
    //                buf = new char[1024];
    //            }
    //            reader.close();
    //        } catch (IOException ex) {
    //           logger.error("Error while reading query file "+queryPath);
    //           System.exit(1);
    //        }
    //
    //        String sparqlQuery = fileData.toString();
    //
    //        Query q = exec.compile(sparqlQuery,null);
    //        System.out.println(q);
    //        
    //        StopWatch sw = new StopWatch();
    //        sw.start();
    //        Mappings map = exec.query(sparqlQuery);
    //        int dqpSize = map.size();
    //        System.out.println("--------");
    //        long time = sw.getTime();
    //        System.out.println(time + " " + dqpSize);
}

From source file:com.icantrap.collections.dawg.Dawg.java

public static void main(String[] args) throws IOException {
    Dawg dawg = Dawg.load(Dawg.class.getResourceAsStream("/twl06.dat"));

    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader reader = new BufferedReader(isr);

    StopWatch stopWatch = new StopWatch();

    while (true) {
        System.out.print("letters:  ");
        String letters = reader.readLine();
        System.out.print("pattern:  ");
        String pattern = reader.readLine();

        stopWatch.reset();/*from   w ww . j a v a 2  s .com*/
        stopWatch.start();
        Result[] results = dawg.subwords(letters.toUpperCase(), pattern.toUpperCase());
        stopWatch.stop();

        if (results != null) {
            System.out.println();

            for (Result result : results) {
                StringBuilder message = new StringBuilder(result.word);
                if (result.wildcardPositions != null) {
                    message.append(" with wildcards at");
                    for (int position : result.wildcardPositions)
                        message.append(" ").append(position);
                }
                System.out.println(message.toString());
                System.out.println();
            }

            System.out.println("Found " + results.length + " matches in " + stopWatch.getTime() + " ms.");
        }

        System.out.println();
    }
}