Example usage for com.google.common.io Files append

List of usage examples for com.google.common.io Files append

Introduction

In this page you can find the example usage for com.google.common.io Files append.

Prototype

public static void append(CharSequence from, File to, Charset charset) throws IOException 

Source Link

Usage

From source file:org.apache.ctakes.temporal.duration.Utils.java

/**
 * Output label and list of cleartk features to a file for debugging.
 *//*  ww  w . j a v  a  2 s  . co  m*/
public static void writeInstance(String label, List<Feature> features, String fileName) {

    StringBuffer output = new StringBuffer(label);
    for (Feature feature : features) {
        if (feature.getName() == null || feature.getValue() == null) {
            continue;
        }
        String name = feature.getName();
        Object value = feature.getValue();
        String nameValuePair;
        if (value instanceof String) {
            String cleanedUpName = name.replace(",", "COMMA").replace(":", "COLON").replace("\n", "EOL");
            String cleanedUpValue = value.toString().replace(",", "COMMA").replace(":", "COLON").replace("\n",
                    "EOL");
            nameValuePair = String.format(",%s-%s:%s", cleanedUpName, cleanedUpValue, 1);
        } else if (value instanceof Integer) {
            String cleanedUpName = name.replace(",", "COMMA").replace(":", "COLON").replace("\n", "EOL");
            String cleanedUpValue = value.toString().replace(",", "COMMA").replace(":", "COLON").replace("\n",
                    "EOL");
            nameValuePair = String.format(",%s:%s", cleanedUpName, cleanedUpValue);
        } else {
            continue;
        }
        output.append(nameValuePair);
    }
    try {
        Files.append(output + "\n", new File(fileName), Charsets.UTF_8);
    } catch (IOException e) {
        System.err.println("could not write to output file!");
    }
}

From source file:io.mycat.server.handler.ServerLoadDataInfileHandler.java

private void saveDataToFile(LoadData data, String dnName) {
    if (data.getFileName() == null) {
        String dnPath = tempPath + dnName + ".txt";
        data.setFileName(dnPath);//from   w ww.  j a v a  2  s  .  c o m
    }

    File dnFile = new File(data.getFileName());
    try {
        if (!dnFile.exists()) {
            Files.createParentDirs(dnFile);
        }
        Files.append(joinLine(data.getData(), data), dnFile, Charset.forName(loadData.getCharset()));

    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        data.setData(null);

    }

}

From source file:com.google.dart.tools.core.internal.builder.BuildDartParticipant.java

private void log(IFile builderFile, String string) {
    try {/*from w  ww  .ja v a2s .c o m*/
        IFile logFile = builderFile.getParent().getFile(new Path(BUILD_LOG_NAME));
        File file = new File(logFile.getLocationURI().toURL().toURI());
        Files.append(string + "\n", file, Charsets.UTF_8);
    } catch (IOException ioe) {

    } catch (URISyntaxException e) {

    }
}

From source file:org.codehaus.mojo.nbm.CreateWebstartAppMojo.java

/**
 *
 * @throws org.apache.maven.plugin.MojoExecutionException
 * @throws org.apache.maven.plugin.MojoFailureException
 *///  w  ww  . j a  v a  2s.com
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if ("none".equalsIgnoreCase(includeLocales)) {
        includeLocales = "";
    }

    if (signingThreads < 1) {
        signingThreads = Runtime.getRuntime().availableProcessors();
    }

    if ((signingMaximumThreads > 0) && (signingThreads > signingMaximumThreads)) {
        signingThreads = signingMaximumThreads;
    }

    getLog().info("Using " + signingThreads + " signing threads.");

    if (!"nbm-application".equals(project.getPackaging())) {
        throw new MojoExecutionException(
                "This goal only makes sense on project with nbm-application packaging.");
    }

    final Project antProject = antProject();

    getLog().warn(
            "WARNING: Unsigned and self-signed WebStart applications are deprecated from JDK7u21 onwards. To ensure future correct functionality please use trusted certificate.");

    if (keystore != null && keystorealias != null && keystorepassword != null) {
        File ks = new File(keystore);
        if (!ks.exists()) {
            throw new MojoFailureException("Cannot find keystore file at " + ks.getAbsolutePath());
        } else {
            //proceed..
        }
    } else if (keystore != null || keystorepassword != null || keystorealias != null) {
        throw new MojoFailureException(
                "If you want to sign the jnlp application, you need to define all three keystore related parameters.");
    } else {
        File generatedKeystore = new File(outputDirectory, "generated.keystore");
        if (!generatedKeystore.exists()) {
            getLog().warn("Keystore related parameters not set, generating a default keystore.");
            GenerateKey genTask = (GenerateKey) antProject.createTask("genkey");
            genTask.setAlias("jnlp");
            genTask.setStorepass("netbeans");
            genTask.setDname("CN=" + System.getProperty("user.name"));
            genTask.setKeystore(generatedKeystore.getAbsolutePath());
            genTask.execute();
        }
        keystore = generatedKeystore.getAbsolutePath();
        keystorepassword = "netbeans";
        keystorealias = "jnlp";
    }

    Taskdef taskdef = (Taskdef) antProject.createTask("taskdef");
    taskdef.setClassname(MakeJnlp2.class.getName());
    taskdef.setName("makejnlp");
    taskdef.execute();

    taskdef = (Taskdef) antProject.createTask("taskdef");
    taskdef.setClassname(Jar.class.getName());
    taskdef.setName("jar");
    taskdef.execute();

    taskdef = (Taskdef) antProject.createTask("taskdef");
    taskdef.setClassname(VerifyJNLP.class.getName());
    taskdef.setName("verifyjnlp");
    taskdef.execute();
    // +p

    try {
        final File webstartBuildDir = new File(
                outputDirectory + File.separator + "webstart" + File.separator + brandingToken);

        if (webstartBuildDir.exists()) {
            FileUtils.deleteDirectory(webstartBuildDir);
        }

        webstartBuildDir.mkdirs();

        // P: copy webappResources --[

        MavenResourcesExecution mavenResourcesExecution = new MavenResourcesExecution(webappResources,
                webstartBuildDir, project, encoding, Collections.EMPTY_LIST, Collections.EMPTY_LIST, session);
        mavenResourcesExecution.setEscapeWindowsPaths(true);
        mavenResourcesFiltering.filterResources(mavenResourcesExecution);

        // ]--

        final String localCodebase = codebase != null ? codebase : webstartBuildDir.toURI().toString();
        getLog().info("Generating webstartable binaries at " + webstartBuildDir.getAbsolutePath());

        final File nbmBuildDirFile = new File(outputDirectory, brandingToken);

        // +p (needs to be before make jnlp)

        //TODO is it really netbeans/
        if (masterJnlpFileName == null) {
            masterJnlpFileName = brandingToken;
        }

        Properties props = new Properties();
        props.setProperty("jnlp.codebase", localCodebase);
        props.setProperty("app.name", brandingToken);
        props.setProperty("app.title", project.getName());
        if (project.getOrganization() != null) {
            props.setProperty("app.vendor", project.getOrganization().getName());
        } else {
            props.setProperty("app.vendor", "Nobody");
        }
        String description = project.getDescription() != null ? project.getDescription()
                : "No Project Description";
        props.setProperty("app.description", description);
        props.setProperty("branding.token", brandingToken);
        props.setProperty("master.jnlp.file.name", masterJnlpFileName);
        props.setProperty("netbeans.jnlp.fixPolicy", "false");

        StringBuilder stBuilder = new StringBuilder();
        if (additionalArguments != null) {
            StringTokenizer st = new StringTokenizer(additionalArguments);
            while (st.hasMoreTokens()) {
                String arg = st.nextToken();
                if (arg.startsWith("-J")) {
                    if (stBuilder.length() > 0) {
                        stBuilder.append(' ');
                    }
                    stBuilder.append(arg.substring(2));
                }
            }
        }
        props.setProperty("netbeans.run.params", stBuilder.toString());

        final File masterJnlp = new File(webstartBuildDir, masterJnlpFileName + ".jnlp");

        filterCopy(masterJnlpFile, "master.jnlp", masterJnlp, props);

        if (generateJnlpTimestamp) //  \/\/\/\/  bad bad bad  \/\/\/\/
        {
            final File masterJnlpFileTmp = File.createTempFile(masterJnlpFileName + "_", "");

            Files.append(JnlpUtils.getCurrentJnlpTimestamp() + "\n", masterJnlpFileTmp,
                    Charset.forName("UTF-8"));

            ByteSink sink = Files.asByteSink(masterJnlpFileTmp, FileWriteMode.APPEND);

            sink.write(Files.toByteArray(masterJnlp));

            Files.copy(masterJnlpFileTmp, masterJnlp);
        }

        File startup = copyLauncher(outputDirectory, nbmBuildDirFile);

        String masterJnlpStr = FileUtils.fileRead(masterJnlp);

        // P: JNLP-INF/APPLICATION_TEMPLATE.JNLP support --[
        // this can be done better and will
        // ashamed
        if (generateJnlpApplicationTemplate) {
            File jnlpInfDir = new File(outputDirectory, "JNLP-INF");

            getLog().info("Generate JNLP application template under: " + jnlpInfDir);

            jnlpInfDir.mkdirs();

            File jnlpTemplate = new File(jnlpInfDir, "APPLICATION_TEMPLATE.JNLP");

            masterJnlpStr = masterJnlpStr.replaceAll("(<jnlp.*codebase\\ *=\\ *)\"((?!\").)*", "$1\"*")
                    .replaceAll("(<jnlp.*href\\ *=\\ *)\"((?!\").)*", "$1\"*");

            FileUtils.fileWrite(jnlpTemplate, masterJnlpStr);

            File startupMerged = new File(outputDirectory, "startup-jnlpinf.jar");

            Jar jar = (Jar) antProject.createTask("jar");
            jar.setDestFile(startupMerged);
            jar.setFilesetmanifest((FilesetManifestConfig) EnumeratedAttribute
                    .getInstance(FilesetManifestConfig.class, "merge"));

            FileSet jnlpInfDirectoryFileSet = new FileSet();
            jnlpInfDirectoryFileSet.setDir(outputDirectory);
            jnlpInfDirectoryFileSet.setIncludes("JNLP-INF/**");

            jar.addFileset(jnlpInfDirectoryFileSet);

            ZipFileSet startupJar = new ZipFileSet();
            startupJar.setSrc(startup);

            jar.addZipfileset(startupJar);

            jar.execute();

            startup = startupMerged;

            getLog().info("APPLICATION_TEMPLATE.JNLP generated - startup.jar: " + startup);
        }

        final JarsConfig startupConfig = new JarsConfig();

        ManifestEntries startupManifestEntries = new ManifestEntries();

        startupConfig.setManifestEntries(startupManifestEntries);

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        if (!validateJnlpDtd) {
            factory.setValidating(false);
            factory.setNamespaceAware(true);
            factory.setFeature("http://xml.org/sax/features/namespaces", false);
            factory.setFeature("http://xml.org/sax/features/validation", false);
            factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
            factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        }
        DocumentBuilder builder = factory.newDocumentBuilder();

        final BufferedReader masterJnlpStrReader = new BufferedReader(new StringReader(masterJnlpStr));

        if (generateJnlpTimestamp) {
            masterJnlpStrReader.readLine();
        }

        Document doc = builder.parse(new InputSource(masterJnlpStrReader));

        Element jnlpRoot = doc.getDocumentElement();

        jarCodebase = jnlpRoot.getAttribute("codebase");

        if (jarCodebase.isEmpty()) {
            jarCodebase = "*";
        }

        startupManifestEntries.setCodebase(jarCodebase);

        XPath xpath = XPathFactory.newInstance().newXPath();

        Node jnlpSecurityPermission = (Node) xpath.evaluate(
                "(/jnlp/security/all-permissions | /jnlp/security/j2ee-application-client-permissions)[1]", doc,
                XPathConstants.NODE);

        if (jnlpSecurityPermission == null) {
            jarPermissions = "sandbox";
            jnlpSecurity = "";
        } else {
            jarPermissions = "all-permissions";
            jnlpSecurity = "<security><" + jnlpSecurityPermission.getNodeName() + "/></security>";
        }

        startupManifestEntries.setPermissions(jarPermissions);

        if (applicationName == null) {
            String jnlpApplicationTitle = (String) xpath.evaluate("/jnlp/information/title", doc,
                    XPathConstants.STRING);

            applicationName = jnlpApplicationTitle == null ? brandingToken : jnlpApplicationTitle;
        }

        startupManifestEntries.setApplicationName(applicationName);

        // +p

        if (autoManifestSecurityEntries) {
            if (jarsConfigs == null) {
                jarsConfigs = new ArrayList<JarsConfig>();
            }

            jarsConfigs.add(0, startupConfig);
        }

        final List<SignJar.JarsConfig> signJarJarsConfigs = buildSignJarJarsConfigs(jarsConfigs);

        File jnlpDestination = new File(webstartBuildDir.getAbsolutePath() + File.separator + "startup.jar");

        SignJar signTask = (SignJar) antProject.createTask("signjar");
        signTask.setKeystore(keystore);
        signTask.setStorepass(keystorepassword);
        signTask.setAlias(keystorealias);

        if (keystoretype != null) {
            signTask.setStoretype(keystoretype);
        }

        signTask.setForce(signingForce);
        signTask.setTsacert(signingTsaCert);
        signTask.setTsaurl(signingTsaUrl);
        signTask.setMaxmemory(signingMaxMemory);
        signTask.setRetryCount(signingRetryCount);

        signTask.setUnsignFirst(signingRemoveExistingSignatures);

        signTask.setJarsConfigs(buildSignJarJarsConfigs(Collections.singletonList(startupConfig)));

        signTask.setBasedir(nbmBuildDirFile);

        signTask.setSignedjar(jnlpDestination);

        signTask.setJar(startup);

        signTask.setPack200(pack200);
        signTask.setPack200Effort(pack200Effort);

        signTask.execute();
        // <-- all of this will be refactored soon ]--

        //            FileUtils.copyDirectoryStructureIfModified( nbmBuildDirFile, webstartBuildDir );

        MakeJnlp2 jnlpTask = (MakeJnlp2) antProject.createTask("makejnlp");
        jnlpTask.setOptimize(optimize);
        jnlpTask.setIncludelocales(includeLocales);
        jnlpTask.setDir(webstartBuildDir);
        jnlpTask.setCodebase(localCodebase);
        //TODO, how to figure verify excludes..
        jnlpTask.setVerify(false);
        jnlpTask.setPermissions(jnlpSecurity);
        jnlpTask.setSignJars(true);

        jnlpTask.setAlias(keystorealias);
        jnlpTask.setKeystore(keystore);
        jnlpTask.setStorePass(keystorepassword);
        if (keystoretype != null) {
            jnlpTask.setStoreType(keystoretype);
        }

        jnlpTask.setSigningForce(signingForce);
        jnlpTask.setSigningTsaCert(signingTsaCert);
        jnlpTask.setSigningTsaUrl(signingTsaUrl);
        jnlpTask.setUnsignFirst(signingRemoveExistingSignatures);
        jnlpTask.setJarsConfigs(signJarJarsConfigs);
        jnlpTask.setSigningMaxMemory(signingMaxMemory);
        jnlpTask.setSigningRetryCount(signingRetryCount);
        jnlpTask.setBasedir(nbmBuildDirFile);

        jnlpTask.setNbThreads(signingThreads);

        jnlpTask.setProcessJarVersions(processJarVersions);

        jnlpTask.setPack200(pack200);
        jnlpTask.setPack200Effort(pack200Effort);

        FileSet fs = jnlpTask.createModules();
        fs.setDir(nbmBuildDirFile);
        OrSelector or = new OrSelector();
        AndSelector and = new AndSelector();
        FilenameSelector inc = new FilenameSelector();
        inc.setName("*/modules/**/*.jar");
        or.addFilename(inc);
        inc = new FilenameSelector();
        inc.setName("*/lib/**/*.jar");
        or.addFilename(inc);
        inc = new FilenameSelector();
        inc.setName("*/core/**/*.jar");
        or.addFilename(inc);

        ModuleSelector ms = new ModuleSelector();
        Parameter included = new Parameter();
        included.setName("includeClusters");
        included.setValue("");
        Parameter excluded = new Parameter();
        excluded.setName("excludeClusters");
        excluded.setValue("");
        Parameter exModules = new Parameter();
        exModules.setName("excludeModules");
        exModules.setValue("");
        ms.setParameters(new Parameter[] { included, excluded, exModules });
        and.add(or);
        and.add(ms);
        fs.addAnd(and);
        jnlpTask.execute();

        Set<String> locales = jnlpTask.getExecutedLocales();

        String extSnippet = generateExtensions(fs, antProject, ""); // "netbeans/"

        //branding
        DirectoryScanner ds = new DirectoryScanner();
        ds.setBasedir(nbmBuildDirFile);

        final List<String> localeIncludes = new ArrayList<String>();
        final List<String> localeExcludes = new ArrayList<String>();

        localeIncludes.add("**/locale/*.jar");

        if (includeLocales != null) {
            List<String> excludes = Splitter.on(',').trimResults().omitEmptyStrings()
                    .splitToList(includeLocales);

            for (String exclude : (Collection<String>) CollectionUtils.subtract(locales, excludes)) {
                localeExcludes.add("**/locale/*_" + exclude + ".jar");
            }
        }

        ds.setIncludes(localeIncludes.toArray(new String[localeIncludes.size()]));
        ds.setExcludes(localeExcludes.toArray(new String[localeExcludes.size()]));
        ds.scan();
        String[] includes = ds.getIncludedFiles();

        StringBuilder brandRefs = new StringBuilder(
                "<property name=\"jnlp.packEnabled\" value=\"" + String.valueOf(pack200) + "\"/>\n");

        if (includes != null && includes.length > 0) {
            final File brandingDir = new File(webstartBuildDir, "branding");
            brandingDir.mkdirs();
            for (String incBran : includes) {
                File source = new File(nbmBuildDirFile, incBran);
                File dest = new File(brandingDir, source.getName());
                brandRefs.append("    <jar href=\'branding/").append(dest.getName()).append("\'/>\n");
            }

            final ExecutorService executorService = Executors.newFixedThreadPool(signingThreads);

            final List<Exception> threadException = new ArrayList<Exception>();

            for (final String toSign : includes) {
                executorService.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            File toSignFile = new File(nbmBuildDirFile, toSign);

                            SignJar signTask = (SignJar) antProject.createTask("signjar");
                            if (keystoretype != null) {
                                signTask.setStoretype(keystoretype);
                            }
                            signTask.setKeystore(keystore);
                            signTask.setStorepass(keystorepassword);
                            signTask.setAlias(keystorealias);
                            signTask.setForce(signingForce);
                            signTask.setTsacert(signingTsaCert);
                            signTask.setTsaurl(signingTsaUrl);
                            signTask.setMaxmemory(signingMaxMemory);
                            signTask.setRetryCount(signingRetryCount);
                            signTask.setUnsignFirst(signingRemoveExistingSignatures);
                            signTask.setJarsConfigs(signJarJarsConfigs);
                            signTask.setJar(toSignFile);
                            signTask.setDestDir(brandingDir);
                            signTask.setBasedir(nbmBuildDirFile);
                            signTask.setDestFlatten(true);
                            signTask.setPack200(pack200);
                            signTask.setPack200Effort(pack200Effort);
                            signTask.execute();
                        } catch (Exception e) {
                            threadException.add(e);
                        }
                    }
                });
            }

            executorService.shutdown();

            executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);

            if (!threadException.isEmpty()) {
                throw threadException.get(0);
            }
        }

        File modulesJnlp = new File(webstartBuildDir.getAbsolutePath() + File.separator + "modules.jnlp");
        props.setProperty("jnlp.branding.jars", brandRefs.toString());
        props.setProperty("jnlp.resources", extSnippet);
        filterCopy(null, /* filename is historical */"branding.jnlp", modulesJnlp, props);

        if (verifyJnlp) {
            getLog().info("Verifying generated webstartable content.");
            VerifyJNLP verifyTask = (VerifyJNLP) antProject.createTask("verifyjnlp");
            FileSet verify = new FileSet();
            verify.setFile(masterJnlp);
            verifyTask.addConfiguredFileset(verify);
            verifyTask.execute();
        }

        // create zip archive
        if (destinationFile.exists()) {
            destinationFile.delete();
        }
        ZipArchiver archiver = new ZipArchiver();
        if (codebase != null) {
            getLog().warn("Defining <codebase>/${nbm.webstart.codebase} is generally unnecessary");
            archiver.addDirectory(webstartBuildDir);
        } else {
            archiver.addDirectory(webstartBuildDir, null, new String[] { "**/*.jnlp" });
            for (final File jnlp : webstartBuildDir.listFiles()) {
                if (!jnlp.getName().endsWith(".jnlp")) {
                    continue;
                }
                archiver.addResource(new PlexusIoResource() {
                    public @Override InputStream getContents() throws IOException {
                        return new ByteArrayInputStream(FileUtils.fileRead(jnlp, "UTF-8")
                                .replace(localCodebase, "$$codebase").getBytes("UTF-8"));
                    }

                    public @Override long getLastModified() {
                        return jnlp.lastModified();
                    }

                    public @Override boolean isExisting() {
                        return true;
                    }

                    public @Override long getSize() {
                        return UNKNOWN_RESOURCE_SIZE;
                    }

                    public @Override URL getURL() throws IOException {
                        return null;
                    }

                    public @Override String getName() {
                        return jnlp.getAbsolutePath();
                    }

                    public @Override boolean isFile() {
                        return true;
                    }

                    public @Override boolean isDirectory() {
                        return false;
                    }
                }, jnlp.getName(), archiver.getDefaultFileMode());
            }
        }
        File jdkhome = new File(System.getProperty("java.home"));
        File servlet = new File(jdkhome, "sample/jnlp/servlet/jnlp-servlet.jar");
        if (!servlet.exists()) {
            servlet = new File(jdkhome.getParentFile(), "sample/jnlp/servlet/jnlp-servlet.jar");

            if (!servlet.exists()) {
                servlet = File.createTempFile("nbm_", "jnlp-servlet.jar");

                FileUtils.copyURLToFile(
                        Thread.currentThread().getContextClassLoader().getResource("jnlp-servlet.jar"),
                        servlet);
            }
        }
        if (servlet.exists()) {
            File servletDir = new File(webstartBuildDir, "WEB-INF/lib");

            servletDir.mkdirs();

            signTask = (SignJar) antProject.createTask("signjar");
            signTask.setKeystore(keystore);
            signTask.setStorepass(keystorepassword);
            signTask.setAlias(keystorealias);
            signTask.setForce(signingForce);
            signTask.setTsacert(signingTsaCert);
            signTask.setTsaurl(signingTsaUrl);
            signTask.setMaxmemory(signingMaxMemory);
            signTask.setRetryCount(signingRetryCount);
            signTask.setJar(servlet);
            signTask.setSignedjar(new File(servletDir, "jnlp-servlet.jar"));
            signTask.execute();

            //archiver.addFile( servlet, "WEB-INF/lib/jnlp-servlet.jar" );
            archiver.addResource(new PlexusIoResource() {
                public @Override InputStream getContents() throws IOException {
                    return new ByteArrayInputStream(("" + "<web-app>\n" + "    <servlet>\n"
                            + "        <servlet-name>JnlpDownloadServlet</servlet-name>\n"
                            + "        <servlet-class>jnlp.sample.servlet.JnlpDownloadServlet</servlet-class>\n"
                            + "    </servlet>\n" + "    <servlet-mapping>\n"
                            + "        <servlet-name>JnlpDownloadServlet</servlet-name>\n"
                            + "        <url-pattern>*.jnlp</url-pattern>\n" + "    </servlet-mapping>\n"
                            + "    <servlet-mapping>\n"
                            + "        <servlet-name>JnlpDownloadServlet</servlet-name>\n"
                            + "        <url-pattern>*.jar</url-pattern>\n" + "    </servlet-mapping>\n"
                            + "    <mime-mapping>\n" + "        <extension>jnlp</extension>\n"
                            + "        <mime-type>application/x-java-jnlp-file</mime-type>\n"
                            + "    </mime-mapping>\n" + "</web-app>\n").getBytes());
                }

                public @Override long getLastModified() {
                    return UNKNOWN_MODIFICATION_DATE;
                }

                public @Override boolean isExisting() {
                    return true;
                }

                public @Override long getSize() {
                    return UNKNOWN_RESOURCE_SIZE;
                }

                public @Override URL getURL() throws IOException {
                    return null;
                }

                public @Override String getName() {
                    return "web.xml";
                }

                public @Override boolean isFile() {
                    return true;
                }

                public @Override boolean isDirectory() {
                    return false;
                }
            }, "WEB-INF/web.xml", archiver.getDefaultFileMode());
        }
        archiver.setDestFile(destinationFile);
        archiver.createArchive();

        if (signWar) {
            signTask = (SignJar) antProject.createTask("signjar");
            signTask.setKeystore(keystore);
            signTask.setStorepass(keystorepassword);
            signTask.setAlias(keystorealias);
            signTask.setForce(signingForce);
            signTask.setTsacert(signingTsaCert);
            signTask.setTsaurl(signingTsaUrl);
            signTask.setMaxmemory(signingMaxMemory);
            signTask.setRetryCount(signingRetryCount);
            signTask.setJar(destinationFile);
            signTask.execute();
        }

        // attach standalone so that it gets installed/deployed
        projectHelper.attachArtifact(project, "war", webstartClassifier, destinationFile);

    } catch (Exception ex) {
        throw new MojoExecutionException("Error creating webstartable binary.", ex);
    }
}

From source file:org.dllearner.algorithms.qtl.experiments.QTLEvaluation.java

public void run(int maxNrOfProcessedQueries, int maxTreeDepth, int[] exampleInterval, double[] noiseInterval,
        HeuristicType[] measures) throws Exception {
    this.maxTreeDepth = maxTreeDepth;
    queryTreeFactory.setMaxDepth(maxTreeDepth);

    if (exampleInterval != null) {
        nrOfExamplesIntervals = exampleInterval;
    }// w w  w .  j ava  2 s . com
    if (noiseInterval != null) {
        this.noiseIntervals = noiseInterval;
    }
    if (measures != null) {
        this.measures = measures;
    }

    logger.info("Started QTL evaluation...");
    long t1 = System.currentTimeMillis();

    List<String> queries = dataset.getSparqlQueries().values().stream().map(q -> q.toString())
            .collect(Collectors.toList());
    logger.info("#loaded queries: " + queries.size());

    // filter for debugging purposes
    queries = queries.stream().filter(q -> tokens.stream().noneMatch(t -> !q.contains(t)))
            .collect(Collectors.toList());

    if (maxNrOfProcessedQueries == -1) {
        maxNrOfProcessedQueries = queries.size();
    }

    //      queries = filter(queries, (int) Math.ceil((double) maxNrOfProcessedQueries / maxTreeDepth));
    //      queries = queries.subList(0, Math.min(queries.size(), maxNrOfProcessedQueries));
    logger.info("#queries to process: " + queries.size());

    // generate examples for each query
    logger.info("precomputing pos. and neg. examples...");
    final Map<String, ExampleCandidates> query2Examples = new HashMap<>();
    for (String query : queries) {//if(!(query.contains("Borough_(New_York_City)")))continue;
        query2Examples.put(query, generateExamples(query));
    }
    logger.info("precomputing pos. and neg. examples finished.");

    // check for queries that do not return any result (should not happen, but we never know)
    Set<String> emptyQueries = query2Examples.entrySet().stream()
            .filter(e -> e.getValue().correctPosExampleCandidates.isEmpty()).map(e -> e.getKey())
            .collect(Collectors.toSet());
    logger.info("got {} empty queries.", emptyQueries.size());
    queries.removeAll(emptyQueries);

    // min. pos examples
    Set<String> lowNrOfExamplesQueries = query2Examples.entrySet().stream()
            .filter(e -> e.getValue().correctPosExampleCandidates.size() < 2).map(e -> e.getKey())
            .collect(Collectors.toSet());
    logger.info("got {} queries with < 2 pos. examples.", emptyQueries.size());
    queries.removeAll(lowNrOfExamplesQueries);

    final int totalNrOfQTLRuns = heuristics.length * this.measures.length * nrOfExamplesIntervals.length
            * noiseIntervals.length * queries.size();
    logger.info("#QTL runs: " + totalNrOfQTLRuns);

    final AtomicInteger currentNrOfFinishedRuns = new AtomicInteger(0);

    // loop over heuristics
    for (final QueryTreeHeuristic heuristic : heuristics) {
        final String heuristicName = heuristic.getClass().getAnnotation(ComponentAnn.class).shortName();

        // loop over heuristics measures
        for (HeuristicType measure : this.measures) {
            final String measureName = measure.toString();
            heuristic.setHeuristicType(measure);

            double[][] data = new double[nrOfExamplesIntervals.length][noiseIntervals.length];

            // loop over number of positive examples
            for (int i = 0; i < nrOfExamplesIntervals.length; i++) {
                final int nrOfExamples = nrOfExamplesIntervals[i];

                // loop over noise value
                for (int j = 0; j < noiseIntervals.length; j++) {
                    final double noise = noiseIntervals[j];

                    // check if not already processed
                    File logFile = new File(benchmarkDirectory, "qtl2-" + nrOfExamples + "-" + noise + "-"
                            + heuristicName + "-" + measureName + ".log");
                    File statsFile = new File(benchmarkDirectory, "qtl2-" + nrOfExamples + "-" + noise + "-"
                            + heuristicName + "-" + measureName + ".stats");

                    if (!override && logFile.exists() && statsFile.exists()) {
                        logger.info(
                                "Eval config already processed. For re-running please remove corresponding output files.");
                        continue;
                    }

                    FileAppender appender = null;
                    try {
                        appender = new FileAppender(new SimpleLayout(), logFile.getPath(), false);
                        Logger.getRootLogger().addAppender(appender);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    logger.info("#examples: " + nrOfExamples + " noise: " + noise);

                    final DescriptiveStatistics nrOfReturnedSolutionsStats = new SynchronizedDescriptiveStatistics();

                    final DescriptiveStatistics baselinePrecisionStats = new SynchronizedDescriptiveStatistics();
                    final DescriptiveStatistics baselineRecallStats = new SynchronizedDescriptiveStatistics();
                    final DescriptiveStatistics baselineFMeasureStats = new SynchronizedDescriptiveStatistics();
                    final DescriptiveStatistics baselinePredAccStats = new SynchronizedDescriptiveStatistics();
                    final DescriptiveStatistics baselineMathCorrStats = new SynchronizedDescriptiveStatistics();

                    final DescriptiveStatistics bestReturnedSolutionPrecisionStats = new SynchronizedDescriptiveStatistics();
                    final DescriptiveStatistics bestReturnedSolutionRecallStats = new SynchronizedDescriptiveStatistics();
                    final DescriptiveStatistics bestReturnedSolutionFMeasureStats = new SynchronizedDescriptiveStatistics();
                    final DescriptiveStatistics bestReturnedSolutionPredAccStats = new SynchronizedDescriptiveStatistics();
                    final DescriptiveStatistics bestReturnedSolutionMathCorrStats = new SynchronizedDescriptiveStatistics();

                    final DescriptiveStatistics bestReturnedSolutionRuntimeStats = new SynchronizedDescriptiveStatistics();

                    final DescriptiveStatistics bestSolutionPrecisionStats = new SynchronizedDescriptiveStatistics();
                    final DescriptiveStatistics bestSolutionRecallStats = new SynchronizedDescriptiveStatistics();
                    final DescriptiveStatistics bestSolutionFMeasureStats = new SynchronizedDescriptiveStatistics();
                    final DescriptiveStatistics bestSolutionPredAccStats = new SynchronizedDescriptiveStatistics();
                    final DescriptiveStatistics bestSolutionMathCorrStats = new SynchronizedDescriptiveStatistics();

                    final DescriptiveStatistics bestSolutionPositionStats = new SynchronizedDescriptiveStatistics();

                    MonitorFactory.getTimeMonitor(TimeMonitors.CBD_RETRIEVAL.name()).reset();
                    MonitorFactory.getTimeMonitor(TimeMonitors.TREE_GENERATION.name()).reset();

                    ExecutorService tp = Executors.newFixedThreadPool(nrOfThreads);

                    // indicates if the execution for some of the queries failed
                    final AtomicBoolean failed = new AtomicBoolean(false);

                    // loop over SPARQL queries
                    for (final String sparqlQuery : queries) {

                        tp.submit(() -> {

                            logger.info("##############################################################");
                            logger.info("Processing query\n" + sparqlQuery);

                            try {
                                ExamplesWrapper examples = query2Examples.get(sparqlQuery).get(nrOfExamples,
                                        nrOfExamples, noise);
                                logger.info(
                                        "pos. examples:\n" + Joiner.on("\n").join(examples.correctPosExamples));
                                logger.info(
                                        "neg. examples:\n" + Joiner.on("\n").join(examples.correctNegExamples));

                                // write examples to disk
                                File dir = new File(benchmarkDirectory, "data/" + hash(sparqlQuery));
                                dir.mkdirs();
                                Files.write(Joiner.on("\n").join(examples.correctPosExamples),
                                        new File(dir, "examples_" + nrOfExamples + "_" + noise + ".tp"),
                                        Charsets.UTF_8);
                                Files.write(Joiner.on("\n").join(examples.correctNegExamples),
                                        new File(dir, "examples_" + nrOfExamples + "_" + noise + ".tn"),
                                        Charsets.UTF_8);
                                Files.write(Joiner.on("\n").join(examples.falsePosExamples),
                                        new File(dir, "examples_" + nrOfExamples + "_" + noise + ".fp"),
                                        Charsets.UTF_8);

                                // compute baseline
                                logger.info("Computing baseline...");
                                RDFResourceTree baselineSolution = applyBaseLine(examples,
                                        Baseline.MOST_INFORMATIVE_EDGE_IN_EXAMPLES);
                                logger.info("Baseline solution:\n" + owlRenderer
                                        .render(QueryTreeUtils.toOWLClassExpression(baselineSolution)));
                                logger.info("Evaluating baseline...");
                                Score baselineScore = computeScore(sparqlQuery, baselineSolution, noise);
                                logger.info("Baseline score:\n" + baselineScore);
                                String baseLineQuery = QueryTreeUtils.toSPARQLQueryString(baselineSolution,
                                        dataset.getBaseIRI(), dataset.getPrefixMapping());
                                baselinePrecisionStats.addValue(baselineScore.precision);
                                baselineRecallStats.addValue(baselineScore.recall);
                                baselineFMeasureStats.addValue(baselineScore.fmeasure);
                                baselinePredAccStats.addValue(baselineScore.predAcc);
                                baselineMathCorrStats.addValue(baselineScore.mathCorr);

                                // run QTL
                                PosNegLPStandard lp = new PosNegLPStandard();
                                lp.setPositiveExamples(examples.posExamplesMapping.keySet());
                                lp.setNegativeExamples(examples.negExamplesMapping.keySet());
                                QTL2Disjunctive la = new QTL2Disjunctive(lp, qef);
                                la.setRenderer(new org.dllearner.utilities.owl.DLSyntaxObjectRenderer());
                                la.setReasoner(dataset.getReasoner());
                                la.setEntailment(Entailment.SIMPLE);
                                la.setTreeFactory(queryTreeFactory);
                                la.setPositiveExampleTrees(examples.posExamplesMapping);
                                la.setNegativeExampleTrees(examples.negExamplesMapping);
                                la.setNoise(noise);
                                la.setHeuristic(heuristic);
                                la.setMaxExecutionTimeInSeconds(maxExecutionTimeInSeconds);
                                la.setMaxTreeComputationTimeInSeconds(maxExecutionTimeInSeconds);
                                la.init();
                                la.start();
                                List<EvaluatedRDFResourceTree> solutions = new ArrayList<>(la.getSolutions());

                                //                              List<EvaluatedRDFResourceTree> solutions = generateSolutions(examples, noise, heuristic);
                                nrOfReturnedSolutionsStats.addValue(solutions.size());

                                // the best returned solution by QTL
                                EvaluatedRDFResourceTree bestSolution = solutions.get(0);
                                logger.info("Got " + solutions.size() + " query trees.");
                                logger.info("Best computed solution:\n"
                                        + render(bestSolution.asEvaluatedDescription()));
                                logger.info("QTL Score:\n" + bestSolution.getTreeScore());
                                long runtimeBestSolution = la.getTimeBestSolutionFound();
                                bestReturnedSolutionRuntimeStats.addValue(runtimeBestSolution);

                                // convert to SPARQL query
                                RDFResourceTree tree = bestSolution.getTree();
                                //                  filter.filter(tree);
                                String learnedSPARQLQuery = QueryTreeUtils.toSPARQLQueryString(tree,
                                        dataset.getBaseIRI(), dataset.getPrefixMapping());

                                // compute score
                                Score score = computeScore(sparqlQuery, tree, noise);
                                bestReturnedSolutionPrecisionStats.addValue(score.precision);
                                bestReturnedSolutionRecallStats.addValue(score.recall);
                                bestReturnedSolutionFMeasureStats.addValue(score.fmeasure);
                                bestReturnedSolutionPredAccStats.addValue(score.predAcc);
                                bestReturnedSolutionMathCorrStats.addValue(score.mathCorr);
                                logger.info(score.toString());

                                // find the extensionally best matching tree in the list
                                Pair<EvaluatedRDFResourceTree, Score> bestMatchingTreeWithScore = findBestMatchingTreeFast(
                                        solutions, sparqlQuery, noise, examples);
                                EvaluatedRDFResourceTree bestMatchingTree = bestMatchingTreeWithScore
                                        .getFirst();
                                Score bestMatchingScore = bestMatchingTreeWithScore.getSecond();

                                // position of best tree in list of solutions
                                int positionBestScore = solutions.indexOf(bestMatchingTree);
                                bestSolutionPositionStats.addValue(positionBestScore);

                                Score bestScore = score;
                                if (positionBestScore > 0) {
                                    logger.info("Position of best covering tree in list: " + positionBestScore);
                                    logger.info("Best covering solution:\n"
                                            + render(bestMatchingTree.asEvaluatedDescription()));
                                    logger.info("Tree score: " + bestMatchingTree.getTreeScore());
                                    bestScore = bestMatchingScore;
                                    logger.info(bestMatchingScore.toString());
                                } else {
                                    logger.info("Best returned solution was also the best covering solution.");
                                }
                                bestSolutionRecallStats.addValue(bestScore.recall);
                                bestSolutionPrecisionStats.addValue(bestScore.precision);
                                bestSolutionFMeasureStats.addValue(bestScore.fmeasure);
                                bestSolutionPredAccStats.addValue(bestScore.predAcc);
                                bestSolutionMathCorrStats.addValue(bestScore.mathCorr);

                                for (RDFResourceTree negTree : examples.negExamplesMapping.values()) {
                                    if (QueryTreeUtils.isSubsumedBy(negTree, bestMatchingTree.getTree())) {
                                        Files.append(sparqlQuery + "\n", new File("/tmp/negCovered.txt"),
                                                Charsets.UTF_8);
                                        break;
                                    }
                                }

                                String bestQuery = QueryFactory.create(QueryTreeUtils.toSPARQLQueryString(
                                        filter.apply(bestMatchingTree.getTree()), dataset.getBaseIRI(),
                                        dataset.getPrefixMapping())).toString();

                                if (write2DB) {
                                    write2DB(sparqlQuery, nrOfExamples, examples, noise, baseLineQuery,
                                            baselineScore, heuristicName, measureName,
                                            QueryFactory.create(learnedSPARQLQuery).toString(), score,
                                            runtimeBestSolution, bestQuery, positionBestScore, bestScore);
                                }

                            } catch (Exception e) {
                                failed.set(true);
                                logger.error("Error occured for query\n" + sparqlQuery, e);
                                try {
                                    StringWriter sw = new StringWriter();
                                    PrintWriter pw = new PrintWriter(sw);
                                    e.printStackTrace(pw);
                                    Files.append(sparqlQuery + "\n" + sw.toString(),
                                            new File(benchmarkDirectory, "failed-" + nrOfExamples + "-" + noise
                                                    + "-" + heuristicName + "-" + measureName + ".txt"),
                                            Charsets.UTF_8);
                                } catch (IOException e1) {
                                    e1.printStackTrace();
                                }
                            } finally {
                                int cnt = currentNrOfFinishedRuns.incrementAndGet();
                                logger.info("***********Evaluation Progress:"
                                        + NumberFormat.getPercentInstance()
                                                .format((double) cnt / totalNrOfQTLRuns)
                                        + "(" + cnt + "/" + totalNrOfQTLRuns + ")" + "***********");
                            }
                        });

                    }

                    tp.shutdown();
                    tp.awaitTermination(12, TimeUnit.HOURS);

                    Logger.getRootLogger().removeAppender(appender);

                    if (!failed.get()) {
                        String result = "";
                        result += "\nBaseline Precision:\n" + baselinePrecisionStats;
                        result += "\nBaseline Recall:\n" + baselineRecallStats;
                        result += "\nBaseline F-measure:\n" + baselineFMeasureStats;
                        result += "\nBaseline PredAcc:\n" + baselinePredAccStats;
                        result += "\nBaseline MathCorr:\n" + baselineMathCorrStats;

                        result += "#Returned solutions:\n" + nrOfReturnedSolutionsStats;

                        result += "\nOverall Precision:\n" + bestReturnedSolutionPrecisionStats;
                        result += "\nOverall Recall:\n" + bestReturnedSolutionRecallStats;
                        result += "\nOverall F-measure:\n" + bestReturnedSolutionFMeasureStats;
                        result += "\nOverall PredAcc:\n" + bestReturnedSolutionPredAccStats;
                        result += "\nOverall MathCorr:\n" + bestReturnedSolutionMathCorrStats;

                        result += "\nTime until best returned solution found:\n"
                                + bestReturnedSolutionRuntimeStats;

                        result += "\nPositions of best solution:\n"
                                + Arrays.toString(bestSolutionPositionStats.getValues());
                        result += "\nPosition of best solution stats:\n" + bestSolutionPositionStats;
                        result += "\nOverall Precision of best solution:\n" + bestSolutionPrecisionStats;
                        result += "\nOverall Recall of best solution:\n" + bestSolutionRecallStats;
                        result += "\nOverall F-measure of best solution:\n" + bestSolutionFMeasureStats;

                        result += "\nCBD generation time(total):\t"
                                + MonitorFactory.getTimeMonitor(TimeMonitors.CBD_RETRIEVAL.name()).getTotal()
                                + "\n";
                        result += "CBD generation time(avg):\t"
                                + MonitorFactory.getTimeMonitor(TimeMonitors.CBD_RETRIEVAL.name()).getAvg()
                                + "\n";
                        result += "Tree generation time(total):\t"
                                + MonitorFactory.getTimeMonitor(TimeMonitors.TREE_GENERATION.name()).getTotal()
                                + "\n";
                        result += "Tree generation time(avg):\t"
                                + MonitorFactory.getTimeMonitor(TimeMonitors.TREE_GENERATION.name()).getAvg()
                                + "\n";
                        result += "Tree size(avg):\t" + treeSizeStats.getMean() + "\n";

                        logger.info(result);

                        try {
                            Files.write(result, statsFile, Charsets.UTF_8);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        data[i][j] = bestReturnedSolutionFMeasureStats.getMean();

                        if (write2DB) {
                            write2DB(heuristicName, measureName, nrOfExamples, noise,
                                    bestReturnedSolutionFMeasureStats.getMean(),
                                    bestReturnedSolutionPrecisionStats.getMean(),
                                    bestReturnedSolutionRecallStats.getMean(),
                                    bestReturnedSolutionPredAccStats.getMean(),
                                    bestReturnedSolutionMathCorrStats.getMean(),
                                    bestSolutionPositionStats.getMean(), bestSolutionFMeasureStats.getMean(),
                                    bestSolutionPrecisionStats.getMean(), bestSolutionRecallStats.getMean(),
                                    bestSolutionPredAccStats.getMean(), bestSolutionMathCorrStats.getMean(),
                                    baselineFMeasureStats.getMean(), baselinePrecisionStats.getMean(),
                                    baselineRecallStats.getMean(), baselinePredAccStats.getMean(),
                                    baselineMathCorrStats.getMean(),
                                    bestReturnedSolutionRuntimeStats.getMean());
                        }
                    }
                }
            }

            String content = "###";
            String separator = "\t";
            for (double noiseInterval1 : noiseIntervals) {
                content += separator + noiseInterval1;
            }
            content += "\n";
            for (int i = 0; i < nrOfExamplesIntervals.length; i++) {
                content += nrOfExamplesIntervals[i];
                for (int j = 0; j < noiseIntervals.length; j++) {
                    content += separator + data[i][j];
                }
                content += "\n";
            }

            File examplesVsNoise = new File(benchmarkDirectory,
                    "examplesVsNoise-" + heuristicName + "-" + measureName + ".tsv");
            try {
                Files.write(content, examplesVsNoise, Charsets.UTF_8);
            } catch (IOException e) {
                logger.error("failed to write stats to file", e);
            }
        }
    }

    if (write2DB) {
        conn.close();
    }

    if (useEmailNotification) {
        sendFinishedMail();
    }
    long t2 = System.currentTimeMillis();
    long duration = t2 - t1;
    logger.info("QTL evaluation finished in " + DurationFormatUtils.formatDurationHMS(duration) + "ms.");
}

From source file:org.dllearner.algorithms.qtl.experiments.PRConvergenceExperiment.java

public void run(int maxNrOfProcessedQueries, int maxTreeDepth, int[] exampleInterval, double[] noiseInterval,
        HeuristicType[] measures) throws Exception {
    this.maxTreeDepth = maxTreeDepth;
    queryTreeFactory.setMaxDepth(maxTreeDepth);

    if (exampleInterval != null) {
        nrOfExamplesIntervals = exampleInterval;
    }/*from  ww w  .  j av  a 2s  . c  om*/
    if (noiseInterval != null) {
        this.noiseIntervals = noiseInterval;
    }
    if (measures != null) {
        this.measures = measures;
    }

    boolean noiseEnabled = noiseIntervals.length > 1 || noiseInterval[0] > 0;
    boolean posOnly = noiseEnabled ? false : true;

    logger.info("Started QTL evaluation...");
    long t1 = System.currentTimeMillis();

    List<String> queries = dataset.getSparqlQueries().values().stream().map(q -> q.toString())
            .collect(Collectors.toList());
    logger.info("#loaded queries: " + queries.size());

    // filter for debugging purposes
    queries = queries.stream().filter(q -> queriesToProcessTokens.stream().noneMatch(t -> !q.contains(t)))
            .collect(Collectors.toList());
    queries = queries.stream().filter(q -> queriesToOmitTokens.stream().noneMatch(t -> q.contains(t)))
            .collect(Collectors.toList());

    if (maxNrOfProcessedQueries == -1) {
        maxNrOfProcessedQueries = queries.size();
    }

    //      queries = filter(queries, (int) Math.ceil((double) maxNrOfProcessedQueries / maxTreeDepth));
    //      queries = queries.subList(0, Math.min(queries.size(), maxNrOfProcessedQueries));
    logger.info("#queries to process: " + queries.size());

    // generate examples for each query
    logger.info("precomputing pos. and neg. examples...");
    for (String query : queries) {//if(!(query.contains("Borough_(New_York_City)")))continue;
        query2Examples.put(query, generateExamples(query, posOnly, noiseEnabled));
    }
    logger.info("precomputing pos. and neg. examples finished.");

    // check for queries that do not return any result (should not happen, but we never know)
    Set<String> emptyQueries = query2Examples.entrySet().stream()
            .filter(e -> e.getValue().correctPosExampleCandidates.isEmpty()).map(e -> e.getKey())
            .collect(Collectors.toSet());
    logger.info("got {} empty queries.", emptyQueries.size());
    queries.removeAll(emptyQueries);

    // min. pos examples
    int min = 3;
    Set<String> lowNrOfExamplesQueries = query2Examples.entrySet().stream()
            .filter(e -> e.getValue().correctPosExampleCandidates.size() < min).map(e -> e.getKey())
            .collect(Collectors.toSet());
    logger.info("got {} queries with < {} pos. examples.", emptyQueries.size(), min);
    queries.removeAll(lowNrOfExamplesQueries);
    queries = queries.subList(0, Math.min(80, queries.size()));

    final int totalNrOfQTLRuns = heuristics.length * this.measures.length * nrOfExamplesIntervals.length
            * noiseIntervals.length * queries.size();
    logger.info("#QTL runs: " + totalNrOfQTLRuns);

    final AtomicInteger currentNrOfFinishedRuns = new AtomicInteger(0);

    // loop over heuristics
    for (final QueryTreeHeuristic heuristic : heuristics) {
        final String heuristicName = heuristic.getClass().getAnnotation(ComponentAnn.class).shortName();

        // loop over heuristics measures
        for (HeuristicType measure : this.measures) {
            final String measureName = measure.toString();
            heuristic.setHeuristicType(measure);

            double[][] data = new double[nrOfExamplesIntervals.length][noiseIntervals.length];

            // loop over number of positive examples
            for (int i = 0; i < nrOfExamplesIntervals.length; i++) {
                final int nrOfExamples = nrOfExamplesIntervals[i];

                // loop over noise value
                for (int j = 0; j < noiseIntervals.length; j++) {
                    final double noise = noiseIntervals[j];

                    // check if not already processed
                    File logFile = new File(benchmarkDirectory, "qtl2-" + nrOfExamples + "-" + noise + "-"
                            + heuristicName + "-" + measureName + ".log");
                    File statsFile = new File(benchmarkDirectory, "qtl2-" + nrOfExamples + "-" + noise + "-"
                            + heuristicName + "-" + measureName + ".stats");

                    if (!override && logFile.exists() && statsFile.exists()) {
                        logger.info(
                                "Eval config already processed. For re-running please remove corresponding output files.");
                        continue;
                    }

                    FileAppender appender = null;
                    try {
                        appender = new FileAppender(new SimpleLayout(), logFile.getPath(), false);
                        Logger.getRootLogger().addAppender(appender);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    logger.info("#examples: " + nrOfExamples + " noise: " + noise);

                    final DescriptiveStatistics nrOfReturnedSolutionsStats = new SynchronizedDescriptiveStatistics();

                    final DescriptiveStatistics baselinePrecisionStats = new SynchronizedDescriptiveStatistics();
                    final DescriptiveStatistics baselineRecallStats = new SynchronizedDescriptiveStatistics();
                    final DescriptiveStatistics baselineFMeasureStats = new SynchronizedDescriptiveStatistics();
                    final DescriptiveStatistics baselinePredAccStats = new SynchronizedDescriptiveStatistics();
                    final DescriptiveStatistics baselineMathCorrStats = new SynchronizedDescriptiveStatistics();

                    final DescriptiveStatistics bestReturnedSolutionPrecisionStats = new SynchronizedDescriptiveStatistics();
                    final DescriptiveStatistics bestReturnedSolutionRecallStats = new SynchronizedDescriptiveStatistics();
                    final DescriptiveStatistics bestReturnedSolutionFMeasureStats = new SynchronizedDescriptiveStatistics();
                    final DescriptiveStatistics bestReturnedSolutionPredAccStats = new SynchronizedDescriptiveStatistics();
                    final DescriptiveStatistics bestReturnedSolutionMathCorrStats = new SynchronizedDescriptiveStatistics();

                    final DescriptiveStatistics bestReturnedSolutionRuntimeStats = new SynchronizedDescriptiveStatistics();

                    final DescriptiveStatistics bestSolutionPrecisionStats = new SynchronizedDescriptiveStatistics();
                    final DescriptiveStatistics bestSolutionRecallStats = new SynchronizedDescriptiveStatistics();
                    final DescriptiveStatistics bestSolutionFMeasureStats = new SynchronizedDescriptiveStatistics();
                    final DescriptiveStatistics bestSolutionPredAccStats = new SynchronizedDescriptiveStatistics();
                    final DescriptiveStatistics bestSolutionMathCorrStats = new SynchronizedDescriptiveStatistics();

                    final DescriptiveStatistics bestSolutionPositionStats = new SynchronizedDescriptiveStatistics();

                    MonitorFactory.getTimeMonitor(TimeMonitors.CBD_RETRIEVAL.name()).reset();
                    MonitorFactory.getTimeMonitor(TimeMonitors.TREE_GENERATION.name()).reset();

                    ExecutorService tp = Executors.newFixedThreadPool(nrOfThreads);

                    // indicates if the execution for some of the queries failed
                    final AtomicBoolean failed = new AtomicBoolean(false);

                    Set<String> queriesToProcess = new TreeSet<>(queries);
                    queriesToProcess.retainAll(query2Examples.entrySet().stream()
                            .filter(e -> e.getValue().correctPosExampleCandidates.size() >= nrOfExamples)
                            .map(e -> e.getKey()).collect(Collectors.toSet()));

                    // loop over SPARQL queries
                    for (final String sparqlQuery : queriesToProcess) {
                        CBDStructureTree cbdStructure = cbdStructureTree != null ? cbdStructureTree
                                : QueryUtils.getOptimalCBDStructure(QueryFactory.create(sparqlQuery));

                        tp.submit(() -> {
                            logger.info("CBD tree:" + cbdStructure.toStringVerbose());

                            // update max tree depth
                            this.maxTreeDepth = QueryTreeUtils.getDepth(cbdStructure);
                            logger.info("##############################################################");
                            logger.info("Processing query\n" + sparqlQuery);

                            // we repeat it n times with different permutations of examples
                            int nrOfPermutations = 1;

                            if (nrOfExamples >= query2Examples.get(sparqlQuery).correctPosExampleCandidates
                                    .size()) {
                                nrOfPermutations = 1;
                            }
                            for (int perm = 1; perm <= nrOfPermutations; perm++) {
                                logger.info("Run {}/{}", perm, nrOfPermutations);
                                try {
                                    ExamplesWrapper examples = getExamples(sparqlQuery, nrOfExamples,
                                            nrOfExamples, noise, cbdStructure);
                                    logger.info("pos. examples:\n"
                                            + Joiner.on("\n").join(examples.correctPosExamples));
                                    logger.info("neg. examples:\n"
                                            + Joiner.on("\n").join(examples.correctNegExamples));

                                    // write examples to disk
                                    File dir = new File(benchmarkDirectory, "data/" + hash(sparqlQuery));
                                    dir.mkdirs();
                                    Files.write(Joiner.on("\n").join(examples.correctPosExamples), new File(dir,
                                            "examples" + perm + "_" + nrOfExamples + "_" + noise + ".tp"),
                                            Charsets.UTF_8);
                                    Files.write(Joiner.on("\n").join(examples.correctNegExamples), new File(dir,
                                            "examples" + perm + "_" + nrOfExamples + "_" + noise + ".tn"),
                                            Charsets.UTF_8);
                                    Files.write(Joiner.on("\n").join(examples.falsePosExamples), new File(dir,
                                            "examples" + perm + "_" + nrOfExamples + "_" + noise + ".fp"),
                                            Charsets.UTF_8);

                                    // compute baseline
                                    RDFResourceTree baselineSolution = applyBaseLine(examples,
                                            Baseline.MOST_INFORMATIVE_EDGE_IN_EXAMPLES);
                                    logger.info("Evaluating baseline...");
                                    Score baselineScore = computeScore(sparqlQuery, baselineSolution, noise);
                                    logger.info("Baseline score:\n" + baselineScore);
                                    String baseLineQuery = QueryTreeUtils.toSPARQLQueryString(baselineSolution,
                                            dataset.getBaseIRI(), dataset.getPrefixMapping());
                                    baselinePrecisionStats.addValue(baselineScore.precision);
                                    baselineRecallStats.addValue(baselineScore.recall);
                                    baselineFMeasureStats.addValue(baselineScore.fmeasure);
                                    baselinePredAccStats.addValue(baselineScore.predAcc);
                                    baselineMathCorrStats.addValue(baselineScore.mathCorr);

                                    // run QTL
                                    PosNegLPStandard lp = new PosNegLPStandard();
                                    lp.setPositiveExamples(examples.posExamplesMapping.keySet());
                                    lp.setNegativeExamples(examples.negExamplesMapping.keySet());
                                    //                                 QTL2Disjunctive la = new QTL2Disjunctive(lp, qef);
                                    QTL2DisjunctiveMultiThreaded la = new QTL2DisjunctiveMultiThreaded(lp, qef);
                                    la.setRenderer(new org.dllearner.utilities.owl.DLSyntaxObjectRenderer());
                                    la.setReasoner(dataset.getReasoner());
                                    la.setEntailment(Entailment.SIMPLE);
                                    la.setTreeFactory(queryTreeFactory);
                                    la.setPositiveExampleTrees(examples.posExamplesMapping);
                                    la.setNegativeExampleTrees(examples.negExamplesMapping);
                                    la.setNoise(noise);
                                    la.setHeuristic(heuristic);
                                    la.setMaxExecutionTimeInSeconds(maxExecutionTimeInSeconds);
                                    la.setMaxTreeComputationTimeInSeconds(maxExecutionTimeInSeconds);
                                    la.init();
                                    la.start();
                                    List<EvaluatedRDFResourceTree> solutions = new ArrayList<>(
                                            la.getSolutions());

                                    //                              List<EvaluatedRDFResourceTree> solutions = generateSolutions(examples, noise, heuristic);
                                    nrOfReturnedSolutionsStats.addValue(solutions.size());

                                    // the best returned solution by QTL
                                    EvaluatedRDFResourceTree bestSolution = solutions.get(0);
                                    logger.info("Got " + solutions.size() + " query trees.");
                                    //                                 logger.info("Best computed solution:\n" + render(bestSolution.asEvaluatedDescription()));
                                    logger.info("QTL Score:\n" + bestSolution.getTreeScore());
                                    long runtimeBestSolution = la.getTimeBestSolutionFound();
                                    bestReturnedSolutionRuntimeStats.addValue(runtimeBestSolution);

                                    // convert to SPARQL query
                                    RDFResourceTree tree = bestSolution.getTree();
                                    tree = filter.apply(tree);
                                    String learnedSPARQLQuery = QueryTreeUtils.toSPARQLQueryString(tree,
                                            dataset.getBaseIRI(), dataset.getPrefixMapping());

                                    // compute score
                                    Score score = computeScore(sparqlQuery, tree, noise);
                                    bestReturnedSolutionPrecisionStats.addValue(score.precision);
                                    bestReturnedSolutionRecallStats.addValue(score.recall);
                                    bestReturnedSolutionFMeasureStats.addValue(score.fmeasure);
                                    bestReturnedSolutionPredAccStats.addValue(score.predAcc);
                                    bestReturnedSolutionMathCorrStats.addValue(score.mathCorr);
                                    logger.info(score.toString());

                                    // find the extensionally best matching tree in the list
                                    Pair<EvaluatedRDFResourceTree, Score> bestMatchingTreeWithScore = findBestMatchingTreeFast(
                                            solutions, sparqlQuery, noise, examples);
                                    EvaluatedRDFResourceTree bestMatchingTree = bestMatchingTreeWithScore
                                            .getFirst();
                                    Score bestMatchingScore = bestMatchingTreeWithScore.getSecond();

                                    // position of best tree in list of solutions
                                    int positionBestScore = solutions.indexOf(bestMatchingTree);
                                    bestSolutionPositionStats.addValue(positionBestScore);

                                    Score bestScore = score;
                                    if (positionBestScore > 0) {
                                        logger.info(
                                                "Position of best covering tree in list: " + positionBestScore);
                                        logger.info("Best covering solution:\n"
                                                + render(bestMatchingTree.asEvaluatedDescription()));
                                        logger.info("Tree score: " + bestMatchingTree.getTreeScore());
                                        bestScore = bestMatchingScore;
                                        logger.info(bestMatchingScore.toString());
                                    } else {
                                        logger.info(
                                                "Best returned solution was also the best covering solution.");
                                    }
                                    bestSolutionRecallStats.addValue(bestScore.recall);
                                    bestSolutionPrecisionStats.addValue(bestScore.precision);
                                    bestSolutionFMeasureStats.addValue(bestScore.fmeasure);
                                    bestSolutionPredAccStats.addValue(bestScore.predAcc);
                                    bestSolutionMathCorrStats.addValue(bestScore.mathCorr);

                                    for (RDFResourceTree negTree : examples.negExamplesMapping.values()) {
                                        if (QueryTreeUtils.isSubsumedBy(negTree, bestMatchingTree.getTree())) {
                                            Files.append(sparqlQuery + "\n", new File("/tmp/negCovered.txt"),
                                                    Charsets.UTF_8);
                                            break;
                                        }
                                    }

                                    String bestQuery = QueryFactory
                                            .create(QueryTreeUtils.toSPARQLQueryString(
                                                    filter.apply(bestMatchingTree.getTree()),
                                                    dataset.getBaseIRI(), dataset.getPrefixMapping()))
                                            .toString();

                                    if (write2DB) {
                                        write2DB(sparqlQuery, nrOfExamples, examples, noise, baseLineQuery,
                                                baselineScore, heuristicName, measureName,
                                                QueryFactory.create(learnedSPARQLQuery).toString(), score,
                                                runtimeBestSolution, bestQuery, positionBestScore, bestScore);
                                    }

                                } catch (Exception e) {
                                    failed.set(true);
                                    logger.error("Error occured for query\n" + sparqlQuery, e);
                                    try {
                                        StringWriter sw = new StringWriter();
                                        PrintWriter pw = new PrintWriter(sw);
                                        e.printStackTrace(pw);
                                        Files.append(sparqlQuery + "\n" + sw.toString(),
                                                new File(benchmarkDirectory,
                                                        "failed-" + nrOfExamples + "-" + noise + "-"
                                                                + heuristicName + "-" + measureName + ".txt"),
                                                Charsets.UTF_8);
                                    } catch (IOException e1) {
                                        e1.printStackTrace();
                                    }
                                } finally {
                                    int cnt = currentNrOfFinishedRuns.incrementAndGet();
                                    logger.info("***********Evaluation Progress:"
                                            + NumberFormat.getPercentInstance()
                                                    .format((double) cnt / totalNrOfQTLRuns)
                                            + "(" + cnt + "/" + totalNrOfQTLRuns + ")" + "***********");
                                }
                            }
                        });
                    }

                    tp.shutdown();
                    tp.awaitTermination(12, TimeUnit.HOURS);

                    Logger.getRootLogger().removeAppender(appender);

                    if (!failed.get()) {
                        String result = "";
                        result += "\nBaseline Precision:\n" + baselinePrecisionStats;
                        result += "\nBaseline Recall:\n" + baselineRecallStats;
                        result += "\nBaseline F-measure:\n" + baselineFMeasureStats;
                        result += "\nBaseline PredAcc:\n" + baselinePredAccStats;
                        result += "\nBaseline MathCorr:\n" + baselineMathCorrStats;

                        result += "#Returned solutions:\n" + nrOfReturnedSolutionsStats;

                        result += "\nOverall Precision:\n" + bestReturnedSolutionPrecisionStats;
                        result += "\nOverall Recall:\n" + bestReturnedSolutionRecallStats;
                        result += "\nOverall F-measure:\n" + bestReturnedSolutionFMeasureStats;
                        result += "\nOverall PredAcc:\n" + bestReturnedSolutionPredAccStats;
                        result += "\nOverall MathCorr:\n" + bestReturnedSolutionMathCorrStats;

                        result += "\nTime until best returned solution found:\n"
                                + bestReturnedSolutionRuntimeStats;

                        result += "\nPositions of best solution:\n"
                                + Arrays.toString(bestSolutionPositionStats.getValues());
                        result += "\nPosition of best solution stats:\n" + bestSolutionPositionStats;
                        result += "\nOverall Precision of best solution:\n" + bestSolutionPrecisionStats;
                        result += "\nOverall Recall of best solution:\n" + bestSolutionRecallStats;
                        result += "\nOverall F-measure of best solution:\n" + bestSolutionFMeasureStats;

                        result += "\nCBD generation time(total):\t"
                                + MonitorFactory.getTimeMonitor(TimeMonitors.CBD_RETRIEVAL.name()).getTotal()
                                + "\n";
                        result += "CBD generation time(avg):\t"
                                + MonitorFactory.getTimeMonitor(TimeMonitors.CBD_RETRIEVAL.name()).getAvg()
                                + "\n";
                        result += "Tree generation time(total):\t"
                                + MonitorFactory.getTimeMonitor(TimeMonitors.TREE_GENERATION.name()).getTotal()
                                + "\n";
                        result += "Tree generation time(avg):\t"
                                + MonitorFactory.getTimeMonitor(TimeMonitors.TREE_GENERATION.name()).getAvg()
                                + "\n";
                        result += "Tree size(avg):\t" + treeSizeStats.getMean() + "\n";

                        logger.info(result);

                        try {
                            Files.write(result, statsFile, Charsets.UTF_8);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        data[i][j] = bestReturnedSolutionFMeasureStats.getMean();

                        if (write2DB) {
                            write2DB(heuristicName, measureName, nrOfExamples, noise,
                                    bestReturnedSolutionFMeasureStats.getMean(),
                                    bestReturnedSolutionPrecisionStats.getMean(),
                                    bestReturnedSolutionRecallStats.getMean(),
                                    bestReturnedSolutionPredAccStats.getMean(),
                                    bestReturnedSolutionMathCorrStats.getMean(),
                                    bestSolutionPositionStats.getMean(), bestSolutionFMeasureStats.getMean(),
                                    bestSolutionPrecisionStats.getMean(), bestSolutionRecallStats.getMean(),
                                    bestSolutionPredAccStats.getMean(), bestSolutionMathCorrStats.getMean(),
                                    baselineFMeasureStats.getMean(), baselinePrecisionStats.getMean(),
                                    baselineRecallStats.getMean(), baselinePredAccStats.getMean(),
                                    baselineMathCorrStats.getMean(),
                                    bestReturnedSolutionRuntimeStats.getMean());
                        }
                    }
                }
            }

            String content = "###";
            String separator = "\t";
            for (double noiseInterval1 : noiseIntervals) {
                content += separator + noiseInterval1;
            }
            content += "\n";
            for (int i = 0; i < nrOfExamplesIntervals.length; i++) {
                content += nrOfExamplesIntervals[i];
                for (int j = 0; j < noiseIntervals.length; j++) {
                    content += separator + data[i][j];
                }
                content += "\n";
            }

            File examplesVsNoise = new File(benchmarkDirectory,
                    "examplesVsNoise-" + heuristicName + "-" + measureName + ".tsv");
            try {
                Files.write(content, examplesVsNoise, Charsets.UTF_8);
            } catch (IOException e) {
                logger.error("failed to write stats to file", e);
            }
        }
    }

    if (write2DB) {
        conn.close();
    }

    if (useEmailNotification) {
        sendFinishedMail();
    }
    long t2 = System.currentTimeMillis();
    long duration = t2 - t1;
    logger.info("QTL evaluation finished in " + DurationFormatUtils.formatDurationHMS(duration) + "ms.");
}

From source file:com.android.tools.idea.gradle.eclipse.GradleImport.java

private void exportSettingsGradle(@NonNull File file, boolean append) throws IOException {
    StringBuilder sb = new StringBuilder();
    if (append) {
        if (!file.exists()) {
            append = false;/*w ww  .j  av a2s.  co  m*/
        } else {
            // Ensure that the new include statements are separate code statements, not
            // for example inserted at the end of a // line comment
            String existing = Files.toString(file, UTF_8);
            if (!existing.endsWith(NL)) {
                sb.append(NL);
            }
        }
    }

    for (ImportModule module : getModulesToImport()) {
        sb.append("include '");
        sb.append(module.getModuleReference());
        sb.append("'");
        sb.append(NL);
    }

    String code = sb.toString();
    if (append) {
        Files.append(code, file, UTF_8);
    } else {
        Files.write(code, file, UTF_8);
    }
}