-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Temporary commit just to show current progress.
- Loading branch information
Showing
3 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
code/factorbase/src/main/java/ca/sfu/cs/factorbase/database/FactorBaseDataBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
61 changes: 61 additions & 0 deletions
61
code/factorbase/src/main/java/ca/sfu/cs/factorbase/database/MySQLFactorBaseDataBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
37
code/factorbase/src/main/java/ca/sfu/cs/factorbase/graph/Edge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |