Skip to content

Commit

Permalink
Temporary commit just to show current progress.
Browse files Browse the repository at this point in the history
  • Loading branch information
rmar3a committed Feb 15, 2019
1 parent d096267 commit 09b88c1
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ca.sfu.cs.factorbase.database;

import java.util.List;

import ca.sfu.cs.factorbase.graph.Edge;

/**
* Methods expected to be implemented to enable the extraction of data from a database for FactorBase.
*/
public interface FactorBaseDataBase {
public List<Edge> getForbiddenEdges(String idName, String pvid);
public List<Edge> getRequiredEdges();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package ca.sfu.cs.factorbase.database;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.mysql.jdbc.Connection;

import ca.sfu.cs.factorbase.graph.Edge;

public class MySQLFactorBaseDataBase implements FactorBaseDataBase {

private Connection baseConnection;
private Connection bnConnection;
private Connection ctConnection;


/**
* Create connections to the databases required by FactorBase to learn a Bayesian Network.
*
* @param connectionString - The string used to the database with the original data.
* e.g. jdbc:mysql://127.0.0.1/unielwin
* @param username - username to use when accessing the database.
* @param password - password to use when accessing the database.
* @throws SQLException - if there is a problem connecting to the required databases.
*/
public MySQLFactorBaseDataBase(String connectionString, String username, String password) throws SQLException {
baseConnection = (Connection) DriverManager.getConnection(connectionString, username, password);
bnConnection = (Connection) DriverManager.getConnection(connectionString + "_BN", username, password);
ctConnection = (Connection) DriverManager.getConnection(connectionString + "_CT", username, password);
}

@Override
public List<Edge> getForbiddenEdges(String idName, String pvid) {
try {
Statement statement = bnConnection.createStatement(); // Will change this to prepared statement.
ResultSet result = statement.executeQuery("SELECT * FROM Path_Forbidden_Edges WHERE " + idName + " = \"" + pvid + "\"");
List<Edge> forbiddenEdges = new ArrayList<Edge>();

// Parse results, can retrieve from cached value.
while (result.next()) {
String child = result.getString("child"); //.replace("`","");
String parent = result.getString("parent"); // .replace("`","");
forbiddenEdges.add(new Edge(parent, child));
}

return forbiddenEdges;
} catch (SQLException e) {
return null;
}
}

@Override
public List<Edge> getRequiredEdges() {
// TODO Auto-generated method stub
return null;
}
}
37 changes: 37 additions & 0 deletions code/factorbase/src/main/java/ca/sfu/cs/factorbase/graph/Edge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ca.sfu.cs.factorbase.graph;

/**
* Simple class to store information for an edge between a parent and a child node.
*/
public class Edge {

private String parent;
private String child;


/**
* Create an edge object that represents an edge between the given child and parent.
* @param parent - the parent node.
* @param child - the child node.
*/
public Edge(String parent, String child) {
this.parent = parent;
this.child = child;
}

/**
* Retrieve the parent for the given edge.
* @return the parent for the given edge.
*/
public String getParent() {
return this.parent;
}

/**
* Retrieve the child for the given edge.
* @return the child for the given edge.
*/
public String getChild() {
return this.child;
}
}

0 comments on commit 09b88c1

Please sign in to comment.