<commit_before>package org.drools.contrib;

import java.io.File;
import java.io.InputStreamReader;

import org.apache.tools.ant.AntClassLoader;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.taskdefs.MatchingTask;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference;
import org.drools.analytics.Analyzer;
import org.drools.compiler.DrlParser;
import org.drools.compiler.DroolsParserException;
import org.drools.compiler.PackageBuilder;
import org.drools.lang.descr.PackageDescr;

public class DroolsAnalyticsAntTask extends MatchingTask {

	public static String BRLFILEEXTENSION = ".brl";
	public static String XMLFILEEXTENSION = ".xml";
	public static String RULEFLOWFILEEXTENSION = ".rfm";
	public static String DSLFILEEXTENSION = ".dslr";

	private File srcdir;
	private File toFile;
	private Path classpath;

	/**
	 * Source directory to read DRL files from
	 * 
	 * @param directory
	 */
	public void setSrcDir(File directory) {
		this.srcdir = directory;
	}

	/**
	 * File to serialize the rulebase to
	 * 
	 * @param toFile
	 */
	public void setToFile(File toFile) {
		this.toFile = toFile;
	}

	/**
	 * The classpath to use when compiling the rulebase
	 * 
	 * @param classpath
	 */
	public void setClasspath(Path classpath) {
		createClasspath().append(classpath);
	}

	/**
	 * Classpath to use, by reference, when compiling the rulebase
	 * 
	 * @param r
	 *            a reference to an existing classpath
	 */
	public void setClasspathref(Reference r) {
		createClasspath().setRefid(r);
	}

	/**
	 * Adds a path to the classpath.
	 * 
	 * @return created classpath
	 */
	public Path createClasspath() {
		if (this.classpath == null) {
			this.classpath = new Path(getProject());
		}
		return this.classpath.createPath();
	}

	/**
	 * Task's main method
	 */
	public void execute() throws BuildException {
		super.execute();

		// checking parameters are set
		if (toFile == null) {
			throw new BuildException(
					"Destination rulebase file does not specified.");
		}

		// checking parameters are set
		if (srcdir == null) {
			throw new BuildException("Source directory not specified.");
		}

		if (!srcdir.exists()) {
			throw new BuildException("Source directory does not exists."
					+ srcdir.getAbsolutePath());
		}

		try {
			// create a specialized classloader
			AntClassLoader loader = getClassLoader();

			Analyzer droolsanalyzer = new Analyzer();

			// get the list of files to be added to the rulebase
			String[] fileNames = getFileList();

			for (int i = 0; i < fileNames.length; i++) {
				compileAndAnalyzeFile(droolsanalyzer, fileNames[i]);
			}
			
			droolsanalyzer.fireAnalysis();
			droolsanalyzer.writeComponentsHTML(toFile.getAbsolutePath() + "/");
			
			System.out.println("Writing analytics report to " + toFile.getAbsolutePath() + "/report");
			
		} catch (Exception e) {
			throw new BuildException("RuleBaseTask failed: " + e.getMessage(),
					e);
		}
	}

	private void compileAndAnalyzeFile(Analyzer droolsanalyzer, String filename) throws DroolsParserException {
		PackageDescr descr = new DrlParser()
				.parse(new InputStreamReader(Analyzer.class
						.getResourceAsStream(filename)));
		
		droolsanalyzer.addPackageDescr(descr);
	}


	/**
	 * @return
	 */
	private AntClassLoader getClassLoader() {
		// defining a new specialized classloader and setting it as the thread<commit_msg>JBRULES-1227 analytics should be available after drools-analytics left experimental<commit_after>package org.drools.contrib;

import java.io.File;

import org.apache.tools.ant.AntClassLoader;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.taskdefs.MatchingTask;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference;
import org.drools.compiler.PackageBuilder;

public class DroolsAnalyticsAntTask extends MatchingTask {

	public static String BRLFILEEXTENSION = ".brl";
	public static String XMLFILEEXTENSION = ".xml";
	public static String RULEFLOWFILEEXTENSION = ".rfm";
	public static String DSLFILEEXTENSION = ".dslr";

	private File srcdir;
	private File toFile;
	private Path classpath;

	/**
	 * Source directory to read DRL files from
	 * 
	 * @param directory
	 */
	public void setSrcDir(File directory) {
		this.srcdir = directory;
	}

	/**
	 * File to serialize the rulebase to
	 * 
	 * @param toFile
	 */
	public void setToFile(File toFile) {
		this.toFile = toFile;
	}

	/**
	 * The classpath to use when compiling the rulebase
	 * 
	 * @param classpath
	 */
	public void setClasspath(Path classpath) {
		createClasspath().append(classpath);
	}

	/**
	 * Classpath to use, by reference, when compiling the rulebase
	 * 
	 * @param r
	 *            a reference to an existing classpath
	 */
	public void setClasspathref(Reference r) {
		createClasspath().setRefid(r);
	}

	/**
	 * Adds a path to the classpath.
	 * 
	 * @return created classpath
	 */
	public Path createClasspath() {
		if (this.classpath == null) {
			this.classpath = new Path(getProject());
		}
		return this.classpath.createPath();
	}

	/**
	 * Task's main method
	 */
	public void execute() throws BuildException {
		super.execute();

		// checking parameters are set
		if (toFile == null) {
			throw new BuildException(
					"Destination rulebase file does not specified.");
		}

		// checking parameters are set
		if (srcdir == null) {
			throw new BuildException("Source directory not specified.");
		}

		if (!srcdir.exists()) {
			throw new BuildException("Source directory does not exists."
					+ srcdir.getAbsolutePath());
		}

		try {
/* *** Uncomment for analytics support			
			
			// create a specialized classloader
			AntClassLoader loader = getClassLoader();

			Analyzer droolsanalyzer = new Analyzer();

			// get the list of files to be added to the rulebase
			String[] fileNames = getFileList();

			for (int i = 0; i < fileNames.length; i++) {
				compileAndAnalyzeFile(droolsanalyzer, fileNames[i]);
			}
			
			droolsanalyzer.fireAnalysis();
			droolsanalyzer.writeComponentsHTML(toFile.getAbsolutePath() + "/");
			
			System.out.println("Writing analytics report to " + toFile.getAbsolutePath() + "/report");
			
*/ 			
			
		} catch (Exception e) {
			throw new BuildException("RuleBaseTask failed: " + e.getMessage(),
					e);
		}
	}
	/* *** Uncomment for analytics support
	private void compileAndAnalyzeFile(Analyzer droolsanalyzer, String filename) throws DroolsParserException {
		PackageDescr descr = new DrlParser()
				.parse(new InputStreamReader(Analyzer.class
						.getResourceAsStream(filename)));
		
		droolsanalyzer.addPackageDescr(descr);
	}
	*/


	/**
	 * @return
	 */
	private AntClassLoader getClassLoader() {
		// defining a new specialized classloader and setting it as the thread
{'fraction_non_alphanumeric': 0.10824215900802334, 'fraction_numerical': 0.00087527352297593, 'mean_word_length': 3.670299727520436, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6616103', 'n_tokens_mistral': 2331, 'n_tokens_neox': 2043, 'n_words': 563}