<commit_before>package edu.mit.streamjit.tuner;

import static com.google.common.base.Preconditions.checkNotNull;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.google.common.collect.ImmutableSet;

import edu.mit.streamjit.api.OneToOneElement;
import edu.mit.streamjit.api.Worker;
import edu.mit.streamjit.impl.blob.BlobFactory;
import edu.mit.streamjit.impl.common.Configuration;
import edu.mit.streamjit.impl.common.ConnectWorkersVisitor;
import edu.mit.streamjit.impl.common.Workers;
import edu.mit.streamjit.impl.compiler.CompilerBlobFactory;
import edu.mit.streamjit.impl.distributed.DistributedBlobFactory;
import edu.mit.streamjit.test.Benchmark;
import edu.mit.streamjit.test.BenchmarkProvider;
import edu.mit.streamjit.test.apps.bitonicsort.BitonicSort;
import edu.mit.streamjit.util.json.Jsonifiers;

/**
 * ConfigGenerator generates {@link Configuration} of an application and stores
 * it into a database. Later, Opentuner can read this configuration for tuning.
 * In this way, Opentuner can start and stop the StreamJit app for each tuning
 * try so that Opentuner can tune JVM parameters such as heapsize,
 * inlinethreshold, GCpausetime, etc as well.
 *
 * @author Sumanan <EMAIL>
 * @since Sep 10, 2013
 */

public class ConfigGenerator {

	/**
	 * TODO: Need to remove the string "class" from the {@link Configuration}
	 * jsonifiers. Once it is done, this method can be removed.
	 *
	 * @param cfg
	 * @return
	 */
	private String getConfigurationString(Configuration cfg) {
		String s = Jsonifiers.toJson(cfg).toString();
		String s1 = s.replaceAll("__class__", "ttttt");
		String s2 = s1.replaceAll("class", "javaClassPath");
		String s3 = s2.replaceAll("ttttt", "__class__");
		return s3;
	}

	/**
	 * Generates configuration for the passed provider.
	 *
	 * @param provider
	 *            Only first benchmark is used to generate configuration. i.e.,
	 *            only first benchmark will be tuned.
	 * @param factory
	 */
	public void generate(BenchmarkProvider provider, BlobFactory factory) {
		checkNotNull(provider);

		sqliteAdapter sqlite;
		try {
			sqlite = new sqliteAdapter();
		} catch (ClassNotFoundException e) {
			System.err
					.println("Sql lite database not found...couldn't update the database with the configutaion.");
			e.printStackTrace();
			return;
		}

		String dbPath = "streamjit.db";
		sqlite.connectDB(dbPath);
		sqlite.createTable(
				"apps",
				"name string PRIMARY KEY NOT NULL, configuration string, location string, classname string");

		sqlite.createTable("FinalResult",
				"Name string, JVMOption string, SJConfig string, Round int, Exectime real");

		String jarFilePath = provider.getClass().getProtectionDomain()
				.getCodeSource().getLocation().getPath();

		String className = provider.getClass().getName();

		Benchmark app = provider.iterator().next();
		// TODO: BlobFactory.getDefaultConfiguration() asks for workers. But<commit_msg>error message formatted<commit_after>package edu.mit.streamjit.tuner;

import static com.google.common.base.Preconditions.checkNotNull;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.google.common.collect.ImmutableSet;

import edu.mit.streamjit.api.OneToOneElement;
import edu.mit.streamjit.api.Worker;
import edu.mit.streamjit.impl.blob.BlobFactory;
import edu.mit.streamjit.impl.common.Configuration;
import edu.mit.streamjit.impl.common.ConnectWorkersVisitor;
import edu.mit.streamjit.impl.common.Workers;
import edu.mit.streamjit.impl.compiler.CompilerBlobFactory;
import edu.mit.streamjit.impl.distributed.DistributedBlobFactory;
import edu.mit.streamjit.test.Benchmark;
import edu.mit.streamjit.test.BenchmarkProvider;
import edu.mit.streamjit.test.apps.bitonicsort.BitonicSort;
import edu.mit.streamjit.util.json.Jsonifiers;

/**
 * ConfigGenerator generates {@link Configuration} of an application and stores
 * it into a database. Later, Opentuner can read this configuration for tuning.
 * In this way, Opentuner can start and stop the StreamJit app for each tuning
 * try so that Opentuner can tune JVM parameters such as heapsize,
 * inlinethreshold, GCpausetime, etc as well.
 *
 * @author Sumanan <EMAIL>
 * @since Sep 10, 2013
 */

public class ConfigGenerator {

	/**
	 * TODO: Need to remove the string "class" from the {@link Configuration}
	 * jsonifiers. Once it is done, this method can be removed.
	 *
	 * @param cfg
	 * @return
	 */
	private String getConfigurationString(Configuration cfg) {
		String s = Jsonifiers.toJson(cfg).toString();
		String s1 = s.replaceAll("__class__", "ttttt");
		String s2 = s1.replaceAll("class", "javaClassPath");
		String s3 = s2.replaceAll("ttttt", "__class__");
		return s3;
	}

	/**
	 * Generates configuration for the passed provider.
	 *
	 * @param provider
	 *            Only first benchmark is used to generate configuration. i.e.,
	 *            only first benchmark will be tuned.
	 * @param factory
	 */
	public void generate(BenchmarkProvider provider, BlobFactory factory) {
		checkNotNull(provider);

		sqliteAdapter sqlite;
		try {
			sqlite = new sqliteAdapter();
		} catch (ClassNotFoundException e) {
			System.err
					.println("Sqlite3 database not found...couldn't update the database with the configutaion.");
			e.printStackTrace();
			return;
		}

		String dbPath = "streamjit.db";
		sqlite.connectDB(dbPath);
		sqlite.createTable(
				"apps",
				"name string PRIMARY KEY NOT NULL, configuration string, location string, classname string");

		sqlite.createTable("FinalResult",
				"Name string, JVMOption string, SJConfig string, Round int, Exectime real");

		String jarFilePath = provider.getClass().getProtectionDomain()
				.getCodeSource().getLocation().getPath();

		String className = provider.getClass().getName();

		Benchmark app = provider.iterator().next();
		// TODO: BlobFactory.getDefaultConfiguration() asks for workers. But
{'fraction_non_alphanumeric': 0.10709117221418235, 'fraction_numerical': 0.004019938896928766, 'mean_word_length': 4.446584938704028, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2395430', 'n_tokens_mistral': 2022, 'n_tokens_neox': 1874, 'n_words': 506}