Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial changes required to change our code to use the new database framework and some cleanup. #99

Merged
merged 4 commits into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 49 additions & 52 deletions code/factorbase/src/main/java/ca/sfu/cs/factorbase/app/RunBB.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,64 @@
import java.util.logging.Logger;

import ca.sfu.cs.common.Configuration.Config;
import ca.sfu.cs.factorbase.database.FactorBaseDataBase;
import ca.sfu.cs.factorbase.database.MySQLFactorBaseDataBase;
import ca.sfu.cs.factorbase.exporter.csvexporter.CSVPrecomputor;
import ca.sfu.cs.factorbase.tables.BayesBaseCT_SortMerge;
import ca.sfu.cs.factorbase.tables.BayesBaseH;
import ca.sfu.cs.factorbase.tables.KeepTablesOnly;
import ca.sfu.cs.factorbase.tables.MakeSetup;
import ca.sfu.cs.factorbase.util.LoggerConfig;

/* July 3rd, 2014, zqian
* input:
/**
* July 3rd, 2014, zqian
* input:
* @database@ (original data based on ER diagram)
* output:
* output:
* @database@_BN (e.g. _CP, Path_BayesNets, Score)
* @database@_CT (e.g. BiggestRchain_CT)
* @database@_setup (preconditions for learning)
*
* */
* @database@_CT (e.g. BiggestRchain_CT)
* @database@_setup (preconditions for learning)
*/
public class RunBB {
static String isAutomaticSetup;
private static Logger logger = Logger.getLogger(RunBB.class.getName());

public static void main(String[] args) throws Exception {
LoggerConfig.setGlobalLevel();
long t1 = System.currentTimeMillis();
logger.info("Start Program...");
setVarsFromConfig();
if (isAutomaticSetup.equals("1")) {
MakeSetup.runMS();
logger.info("Setup database is ready.");
} else {
logger.info("Setup database exists.");
}
runBBLearner();

long t2 = System.currentTimeMillis();
logger.info("Total Running time is " + (t2-t1) + "ms.");
}


public static void setVarsFromConfig(){
Config conf = new Config();
//1: run Setup; 0: not run
isAutomaticSetup = conf.getProperty("AutomaticSetup");
}

public static void runBBLearner() throws Exception {

//assumes that dbname is in config file and that dbname_setup exists.
private static Logger logger = Logger.getLogger(RunBB.class.getName());

BayesBaseCT_SortMerge.buildCT();
logger.info("The CT database is ready for use.");
logger.info("*********************************************************");
CSVPrecomputor.runCSV();
logger.info("CSV files are generated.");
logger.info("*********************************************************");
BayesBaseH.runBBH();
logger.info("\nFinish running BayesBaseH.");
logger.info("*********************************************************");
logger.info("Cleaning CT database");
//Now eliminate temporary tables. Keep only the tables for the longest Rchain. Turn this off for debugging.//
KeepTablesOnly.Drop_tmpTables();

}
public static void main(String[] args) throws Exception {
long t1 = System.currentTimeMillis();
LoggerConfig.setGlobalLevel();
Config config = new Config();
logger.info("Start Program...");

}
FactorBaseDataBase factorBaseDatabase = new MySQLFactorBaseDataBase(
config.getProperty("dbaddress"),
config.getProperty("dbname"),
config.getProperty("dbusername"),
config.getProperty("dbpassword")
);

// Generate the setup database if specified to.
if (config.getProperty("AutomaticSetup").equals("1")) {
factorBaseDatabase.setupDatabase();
logger.info("Setup database is ready.");
} else {
logger.info("Setup database exists.");
}

// Learn a Bayesian Network.
BayesBaseCT_SortMerge.buildCT();
logger.info("The CT database is ready for use.");
logger.info("*********************************************************");
CSVPrecomputor.runCSV();
logger.info("CSV files are generated.");
logger.info("*********************************************************");
BayesBaseH.runBBH();
logger.info("\nFinish running BayesBaseH.");
logger.info("*********************************************************");

// Now eliminate temporary tables. Keep only the tables for the longest Rchain. Turn this off for debugging.
logger.info("Cleaning CT database");
KeepTablesOnly.Drop_tmpTables();

long t2 = System.currentTimeMillis();
logger.info("Total Running time is " + (t2 - t1) + "ms.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ca.sfu.cs.factorbase.database;

import ca.sfu.cs.factorbase.exception.DataBaseException;

/**
* Methods expected to be implemented to enable the extraction of data from a database for FactorBase.
*/
public interface FactorBaseDataBase {
/**
* This method should setup all the extra tables required for FactorBase to learn a Bayesian
* network for the provided database.
*
* @throws DataBaseException if an error occurs when attempting to access the database.
*/
void setupDatabase() throws DataBaseException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package ca.sfu.cs.factorbase.database;

import java.io.IOException;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.MessageFormat;

import ca.sfu.cs.common.Configuration.Config;
import ca.sfu.cs.factorbase.exception.DataBaseException;
import ca.sfu.cs.factorbase.util.BZScriptRunner;

import com.mysql.jdbc.Connection;

public class MySQLFactorBaseDataBase implements FactorBaseDataBase {

private static final String CONNECTION_STRING = "jdbc:{0}/{1}";
private String baseDatabaseName;
private Connection baseConnection;


/**
* Create connections to the databases required by FactorBase to learn a Bayesian Network.
*
* @param dbaddress - the address of the MySQL database to connect to. e.g. mysql://127.0.0.1
* @param dbname - the name of the database with the original data. e.g. unielwin
* @param username - the username to use when accessing the database.
* @param password - the password to use when accessing the database.
* @throws SQLException if there is a problem connecting to the required databases.
*/
public MySQLFactorBaseDataBase(String dbaddress, String dbname, String username, String password) throws DataBaseException {
this.baseDatabaseName = dbname;
String baseConnectionString = MessageFormat.format(CONNECTION_STRING, dbaddress, dbname);

try {
this.baseConnection = (Connection) DriverManager.getConnection(baseConnectionString, username, password);
} catch (SQLException e) {
throw new DataBaseException("Unable to connect to the provided database.", e);
}
}


@Override
public void setupDatabase() throws DataBaseException {
BZScriptRunner bzsr = new BZScriptRunner(this.baseDatabaseName, this.baseConnection);
try {
bzsr.runScript(Config.SCRIPTS_DIRECTORY + "setup.sql");
bzsr.createSP(Config.SCRIPTS_DIRECTORY + "storedprocs.sql");
bzsr.callSP("find_values");
} catch (SQLException | IOException e) {
throw new DataBaseException("An error occurred when attempting to setup the database for FactorBase.", e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ca.sfu.cs.factorbase.exception;


/**
* Custom exception to wrap and throw the specific cause of an error. Should be thrown when an
* error occurs when attempting to access a database.
*/
public class DataBaseException extends Exception {


/**
* Serial version - auto-generated by Eclipse.
*/
private static final long serialVersionUID = -6623897407612813075L;


/**
* Used to wrap the root cause of an exception when attempting to access a database.
*
* @param message - useful message telling the user what has potentially gone wrong.
* @param cause - the exception that caused the error to occur when accessing a database.
*/
public DataBaseException(String message, Throwable cause) {
super(message, cause);
}
}
12 changes: 0 additions & 12 deletions travis-resources/config.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,15 @@ dbaddress = mysql://127.0.0.1
dbusername = root
dbpassword = 123456

dbBNschema = unielwin_BN
dbDataSchema = unielwin
dbOutputSchema = unielwin_db
pathBayesNet = Path_BayesNets

LinkAnalysis = 1

Continuous = 0
Grounded = 0

dbname = unielwin
AutomaticSetup = 1
LinkCorrelations = 1
ComputeKLD = 0

dbschema = unielwin_BN
dbcounts = unielwin_db
dbcondprob = unielwin_db

UseLocal_CT = 0

CrossValidation = 0

LoggingLevel = info