<commit_before>package es.optsicom.lib.approx.experiment;

import java.io.File;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.NoResultException;

import es.optsicom.lib.Method;
import es.optsicom.lib.analyzer.ReportConf;
import es.optsicom.lib.analyzer.tool.FusionerReportCreator;
import es.optsicom.lib.analyzer.tool.FusionerReportCreator.ExperimentMethodConf;
import es.optsicom.lib.approx.ApproxMethod;
import es.optsicom.lib.expresults.DBExperimentRepositoryManagerFactory;
import es.optsicom.lib.expresults.ExperimentRepositoryFactory;
import es.optsicom.lib.expresults.db.DBManager;
import es.optsicom.lib.expresults.db.DerbyDBManager;
import es.optsicom.lib.expresults.manager.ExperimentRepositoryManager;
import es.optsicom.lib.expresults.model.ComputerDescription;
import es.optsicom.lib.expresults.model.DBProperties;
import es.optsicom.lib.expresults.model.Experiment;
import es.optsicom.lib.expresults.model.InstanceDescription;
import es.optsicom.lib.expresults.model.MethodDescription;
import es.optsicom.lib.expresults.model.Researcher;
import es.optsicom.lib.expresults.saver.ExperimentSaver;
import es.optsicom.lib.instancefile.InstanceFile;

public class FastExperimentExecutor {

	private static final String OS_NAME = "os.name";
	private static final String OS_ARCH = "os.arch";
	private static final String JAVA_HOME = "java.home";
	private static final String JAVA_VM_NAME = "java.vm.name";
	private static final String JAVA_VERSION = "java.version";
	private static final String JAVA_VM_VENDOR = "java.vm.vendor";

	private final ApproxExpConf approxExpConf;
	private final List<ExperimentMethodConf> expMethodConfs = new ArrayList<ExperimentMethodConf>();
	private ReportConf reportConf;

	private String dbDir = "derby_exp_repo";
	private long experimentId;
	private boolean localExecution = true;
	private DBManager dbManager;

	private String name;

	public FastExperimentExecutor(ApproxExpConf approxExpConf) {
		this.approxExpConf = approxExpConf;
	}

	public FastExperimentExecutor setLocalExperiment(boolean localExecution) {
		this.localExecution = localExecution;
		return this;
	}

	public FastExperimentExecutor addExperimentMethod(String experimentName, String methodName) {
		expMethodConfs.add(new ExperimentMethodConf(experimentName, methodName));
		return this;
	}

	public FastExperimentExecutor addOptimumExperimentMethod() {
		addExperimentMethod("predefined", "optimum");
		return this;
	}

	public FastExperimentExecutor setReportConf(ReportConf reportConf) {
		this.reportConf = reportConf;
		return this;
	}

	public FastExperimentExecutor setDBDir(String dbDir) {
		this.dbDir = dbDir;
		return this;
	}

	public FastExperimentExecutor setDBManager(DBManager dbManager) {
		this.dbManager = dbManager;
		return this;
	}

	public FastExperimentExecutor setName(String name) {
		this.name = name;
		return this;
	}

	public void execExperiment() {

		if (dbManager == null) {
			try {
				final File DB_DIR = new File(dbDir);

				dbManager = new DerbyDBManager(DB_DIR);
			} catch (SQLException e) {
				throw new RuntimeException("Error when opening database", e);
			}
		}

		ExperimentRepositoryFactory expRepoFactory = null;
		expRepoFactory = new DBExperimentRepositoryManagerFactory(dbManager);

		if (this.approxExpConf.getName() == null) {
			this.approxExpConf.setName(this.approxExpConf.getClass().getSimpleName());
		}

		experimentId = execExperiment(approxExpConf, expRepoFactory);

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		String docname = formatter.format(new Date()) + " " + name;
		FusionerReportCreator reportCreator = new FusionerReportCreator(approxExpConf.getProblem().getName(), docname,
				dbManager);

		reportCreator.addExperimentMethod(experimentId);
		reportCreator.addExperimentMethods(expMethodConfs);
		if (this.reportConf != null) {
			reportCreator.setReportConf(reportConf);
		}

		reportCreator.createReportAndShow();
	}

	@SuppressWarnings("unchecked")
	public long execExperiment(ApproxExpConf approxExpConf, ExperimentRepositoryFactory expRepoFactory) {
<commit_msg>avoid NPE in name constructions¡<commit_after>package es.optsicom.lib.approx.experiment;

import java.io.File;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.NoResultException;

import es.optsicom.lib.Method;
import es.optsicom.lib.analyzer.ReportConf;
import es.optsicom.lib.analyzer.tool.FusionerReportCreator;
import es.optsicom.lib.analyzer.tool.FusionerReportCreator.ExperimentMethodConf;
import es.optsicom.lib.approx.ApproxMethod;
import es.optsicom.lib.expresults.DBExperimentRepositoryManagerFactory;
import es.optsicom.lib.expresults.ExperimentRepositoryFactory;
import es.optsicom.lib.expresults.db.DBManager;
import es.optsicom.lib.expresults.db.DerbyDBManager;
import es.optsicom.lib.expresults.manager.ExperimentRepositoryManager;
import es.optsicom.lib.expresults.model.ComputerDescription;
import es.optsicom.lib.expresults.model.DBProperties;
import es.optsicom.lib.expresults.model.Experiment;
import es.optsicom.lib.expresults.model.InstanceDescription;
import es.optsicom.lib.expresults.model.MethodDescription;
import es.optsicom.lib.expresults.model.Researcher;
import es.optsicom.lib.expresults.saver.ExperimentSaver;
import es.optsicom.lib.instancefile.InstanceFile;

public class FastExperimentExecutor {

	private static final String OS_NAME = "os.name";
	private static final String OS_ARCH = "os.arch";
	private static final String JAVA_HOME = "java.home";
	private static final String JAVA_VM_NAME = "java.vm.name";
	private static final String JAVA_VERSION = "java.version";
	private static final String JAVA_VM_VENDOR = "java.vm.vendor";

	private final ApproxExpConf approxExpConf;
	private final List<ExperimentMethodConf> expMethodConfs = new ArrayList<ExperimentMethodConf>();
	private ReportConf reportConf;

	private String dbDir = "derby_exp_repo";
	private long experimentId;
	private boolean localExecution = true;
	private DBManager dbManager;

	private String name = "";

	public FastExperimentExecutor(ApproxExpConf approxExpConf) {
		this.approxExpConf = approxExpConf;
	}

	public FastExperimentExecutor setLocalExperiment(boolean localExecution) {
		this.localExecution = localExecution;
		return this;
	}

	public FastExperimentExecutor addExperimentMethod(String experimentName, String methodName) {
		expMethodConfs.add(new ExperimentMethodConf(experimentName, methodName));
		return this;
	}

	public FastExperimentExecutor addOptimumExperimentMethod() {
		addExperimentMethod("predefined", "optimum");
		return this;
	}

	public FastExperimentExecutor setReportConf(ReportConf reportConf) {
		this.reportConf = reportConf;
		return this;
	}

	public FastExperimentExecutor setDBDir(String dbDir) {
		this.dbDir = dbDir;
		return this;
	}

	public FastExperimentExecutor setDBManager(DBManager dbManager) {
		this.dbManager = dbManager;
		return this;
	}

	public FastExperimentExecutor setName(String name) {
		this.name = name;
		return this;
	}

	public void execExperiment() {

		if (dbManager == null) {
			try {
				final File DB_DIR = new File(dbDir);

				dbManager = new DerbyDBManager(DB_DIR);
			} catch (SQLException e) {
				throw new RuntimeException("Error when opening database", e);
			}
		}

		ExperimentRepositoryFactory expRepoFactory = null;
		expRepoFactory = new DBExperimentRepositoryManagerFactory(dbManager);

		if (this.approxExpConf.getName() == null) {
			this.approxExpConf.setName(this.approxExpConf.getClass().getSimpleName());
		}

		experimentId = execExperiment(approxExpConf, expRepoFactory);

		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		String docname = formatter.format(new Date()) + " " + name + " - id: " + experimentId;
		FusionerReportCreator reportCreator = new FusionerReportCreator(approxExpConf.getProblem().getName(), docname,
				dbManager);

		reportCreator.addExperimentMethod(experimentId);
		reportCreator.addExperimentMethods(expMethodConfs);
		if (this.reportConf != null) {
			reportCreator.setReportConf(reportConf);
		}

		reportCreator.createReportAndShow();
	}

	@SuppressWarnings("unchecked")
	public long execExperiment(ApproxExpConf approxExpConf, ExperimentRepositoryFactory expRepoFactory) {

{'fraction_non_alphanumeric': 0.09208834872034592, 'fraction_numerical': 0.0, 'mean_word_length': 5.974735126324369, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '264803', 'n_tokens_mistral': 2753, 'n_tokens_neox': 2608, 'n_words': 518}