Skip to content

Commit

Permalink
Created new database framework and moved the database setup logic int…
Browse files Browse the repository at this point in the history
…o it.

- Initial code for the new database framework that is an attempt to
  abstract the database logic from the Bayesian Network learning logic.
- Created a new FactorBaseDataBase interface so that other databases
  can be supported in the future more easily.
- Created a custom DataBaseException that can be thrown by any database
  technology that implements the FactorBaseDataBase interface.
- All the logic found in MakeSetup.java was moved into
  MySQLFactorBaseDataBase.java since the logic makes more sense
  to be in this file, i.e. it manipulates the given MySQL database,
  and MakeSetup.java was moved into a new subdirectory (called
  "refactored") of the obsolete directory.
- Updated RunBB.java to use the new database framework for the database
  setup logic portion of the process.
  • Loading branch information
rmar3a committed Feb 25, 2019
1 parent 0b34b77 commit 06d1626
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 2 deletions.
12 changes: 10 additions & 2 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,11 +3,12 @@
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;

/**
Expand All @@ -29,9 +30,16 @@ public static void main(String[] args) throws Exception {
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")) {
MakeSetup.runMS();
factorBaseDatabase.setupDatabase();
logger.info("Setup database is ready.");
} else {
logger.info("Setup database exists.");
Expand Down
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);
}
}
File renamed without changes.

0 comments on commit 06d1626

Please sign in to comment.