2019-03-02 15:10:46 +01:00
|
|
|
/*
|
|
|
|
* To change this license header, choose License Headers in Project Properties.
|
|
|
|
* To change this template file, choose Tools | Templates
|
|
|
|
* and open the template in the editor.
|
|
|
|
*/
|
|
|
|
package DataLayer;
|
|
|
|
|
|
|
|
import FunctionLayer.SimilarityMatrix;
|
|
|
|
import FunctionLayer.CustomError;
|
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.PreparedStatement;
|
|
|
|
import java.sql.ResultSet;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.sql.Statement;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
2019-03-03 13:17:07 +01:00
|
|
|
import java.util.LinkedHashMap;
|
2019-03-02 15:10:46 +01:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @author install1
|
|
|
|
*/
|
|
|
|
public class DataMapper {
|
|
|
|
|
|
|
|
public static void createTables() throws CustomError {
|
|
|
|
Connection l_cCon = null;
|
|
|
|
PreparedStatement l_pStatement = null;
|
|
|
|
ResultSet l_rsSearch = null;
|
|
|
|
try {
|
|
|
|
l_cCon = DBCPDataSource.getConnection();
|
|
|
|
String l_sSQL = "CREATE TABLE IF NOT EXISTS `ArtificialAutism`.`Sentences` (`Strings` VARCHAR(256) NOT NULL, PRIMARY KEY (`Strings`))\n"
|
|
|
|
+ "ENGINE = InnoDB;";
|
|
|
|
l_pStatement = l_cCon.prepareStatement(l_sSQL);
|
|
|
|
l_pStatement.execute();
|
|
|
|
l_sSQL = "CREATE TABLE IF NOT EXISTS `ArtificialAutism`.`WordMatrix` (`Str1` VARCHAR(254) NOT NULL, `Str2` VARCHAR(254) NOT NULL,\n"
|
|
|
|
+ " `Distance` DOUBLE NOT NULL, `ID` INT NOT NULL AUTO_INCREMENT,\n"
|
|
|
|
+ " PRIMARY KEY (`ID`))\n"
|
|
|
|
+ "ENGINE = InnoDB;";
|
|
|
|
l_pStatement = l_cCon.prepareStatement(l_sSQL);
|
|
|
|
l_pStatement.execute();
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
throw new CustomError("failed in DataMapper " + ex.getMessage());
|
|
|
|
} finally {
|
|
|
|
CloseConnections(l_pStatement, l_rsSearch, l_cCon);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static List<String> getAllStrings() throws CustomError {
|
|
|
|
List<String> str = new ArrayList();
|
|
|
|
Connection l_cCon = null;
|
|
|
|
PreparedStatement l_pStatement = null;
|
|
|
|
ResultSet l_rsSearch = null;
|
|
|
|
try {
|
|
|
|
l_cCon = DBCPDataSource.getConnection();
|
|
|
|
String l_sSQL = "SELECT * FROM `Sentences`";
|
|
|
|
l_pStatement = l_cCon.prepareStatement(l_sSQL, java.sql.ResultSet.TYPE_FORWARD_ONLY,
|
|
|
|
java.sql.ResultSet.CONCUR_READ_ONLY);
|
|
|
|
l_pStatement.setFetchSize(Integer.MIN_VALUE);
|
|
|
|
l_rsSearch = l_pStatement.executeQuery();
|
|
|
|
while (l_rsSearch.next()) {
|
|
|
|
str.add(l_rsSearch.getString(1));
|
|
|
|
}
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
throw new CustomError("failed in DataMapper " + ex.getMessage());
|
|
|
|
} finally {
|
|
|
|
CloseConnections(l_pStatement, l_rsSearch, l_cCon);
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void InsertMYSQLStrings(List<String> str) throws CustomError {
|
|
|
|
Connection l_cCon = null;
|
|
|
|
PreparedStatement l_pStatement = null;
|
|
|
|
ResultSet l_rsSearch = null;
|
|
|
|
String l_sSQL = "INSERT IGNORE `Sentences` (`Strings`) VALUES (?)";
|
|
|
|
try {
|
|
|
|
if (str != null && str.size() > 0) {
|
|
|
|
l_cCon = DBCPDataSource.getConnection();
|
|
|
|
l_pStatement = l_cCon.prepareStatement(l_sSQL, java.sql.ResultSet.TYPE_FORWARD_ONLY,
|
|
|
|
java.sql.ResultSet.CONCUR_READ_ONLY);
|
|
|
|
l_pStatement.setFetchSize(Integer.MIN_VALUE);
|
|
|
|
for (String str1 : str) {
|
|
|
|
System.out.println("adding str1: " + str1 + "\n");
|
|
|
|
l_pStatement.setString(1, str1);
|
|
|
|
l_pStatement.addBatch();
|
|
|
|
}
|
|
|
|
l_pStatement.executeBatch();
|
|
|
|
}
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
throw new CustomError("failed in DataMapper " + ex.getMessage());
|
|
|
|
} finally {
|
|
|
|
CloseConnections(l_pStatement, l_rsSearch, l_cCon);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getSementicsDBRows() {
|
|
|
|
int count = 0;
|
|
|
|
try (Connection l_cCon = DBCPDataSource.getConnection()) {
|
|
|
|
try (Statement s = l_cCon.createStatement();
|
|
|
|
ResultSet r = s.executeQuery("SELECT COUNT(*) AS rowcount FROM WordMatrix")) {
|
|
|
|
r.next();
|
|
|
|
count = r.getInt("rowcount");
|
|
|
|
}
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
Logger.getLogger(DataMapper.class.getName()).log(Level.SEVERE, null, ex);
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static List<SimilarityMatrix> getAllSementicMatrixes() throws CustomError {
|
|
|
|
//https://stackoverflow.com/questions/5157476/resultset-behavior-with-mysql-database-does-it-store-all-rows-in-memory/5159999#5159999
|
|
|
|
//https://stackoverflow.com/questions/3682614/how-to-read-all-rows-from-huge-table
|
|
|
|
int count = getSementicsDBRows();
|
|
|
|
int counter2 = 0;
|
|
|
|
int hardCapRetrieveCount = 500000;
|
|
|
|
List<SimilarityMatrix> WS4JList = new ArrayList(count + 1);
|
|
|
|
while (count > counter2) {
|
|
|
|
try (Connection l_cCon = DBCPDataSource.getConnection()) {
|
|
|
|
l_cCon.setAutoCommit(false);
|
|
|
|
String l_sSQL = "SELECT * FROM `WordMatrix` WHERE ID > " + counter2 + " AND ID < " + (counter2 + hardCapRetrieveCount);
|
|
|
|
try (PreparedStatement l_pStatement = l_cCon.prepareStatement(l_sSQL, java.sql.ResultSet.TYPE_FORWARD_ONLY,
|
|
|
|
java.sql.ResultSet.CONCUR_READ_ONLY)) {
|
|
|
|
l_pStatement.setFetchSize(Integer.MIN_VALUE);
|
|
|
|
try (ResultSet l_rsSearch = l_pStatement.executeQuery()) {
|
|
|
|
int i = 0;
|
|
|
|
while (l_rsSearch.next() && i < hardCapRetrieveCount) {
|
|
|
|
SimilarityMatrix ws4j = new SimilarityMatrix(l_rsSearch.getString(1), l_rsSearch.getString(2), l_rsSearch.getDouble(3));
|
|
|
|
//find something cheaper than arraylist probably
|
|
|
|
WS4JList.add(ws4j);
|
|
|
|
System.out.println("i: " + i + "\n" + "free memory: " + Runtime.getRuntime().freeMemory() + "\ncounter2: " + counter2 + "\n");
|
|
|
|
i++;
|
|
|
|
counter2++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
Logger.getLogger(DataMapper.class.getName()).log(Level.SEVERE, null, ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return WS4JList;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void insertSementicMatrixes(List<SimilarityMatrix> WS4JListUpdate) throws CustomError {
|
|
|
|
Connection l_cCon = null;
|
|
|
|
PreparedStatement l_pStatement = null;
|
|
|
|
ResultSet l_rsSearch = null;
|
|
|
|
String l_sSQL = "INSERT IGNORE `WordMatrix` (`Str1`,`Str2`,`Distance`) VALUES (?, ?, ?)";
|
|
|
|
try {
|
|
|
|
l_cCon = DBCPDataSource.getConnection();
|
|
|
|
l_pStatement = l_cCon.prepareStatement(l_sSQL, java.sql.ResultSet.TYPE_FORWARD_ONLY,
|
|
|
|
java.sql.ResultSet.CONCUR_READ_ONLY);
|
|
|
|
l_pStatement.setFetchSize(Integer.MIN_VALUE);
|
|
|
|
System.out.println("Matrix update size: " + WS4JListUpdate.size());
|
|
|
|
for (SimilarityMatrix ws4j : WS4JListUpdate) {
|
|
|
|
l_pStatement.setString(1, ws4j.getPrimaryString());
|
|
|
|
l_pStatement.setString(2, ws4j.getSecondaryString());
|
|
|
|
l_pStatement.setDouble(3, ws4j.getDistance());
|
|
|
|
l_pStatement.addBatch();
|
|
|
|
}
|
|
|
|
l_pStatement.executeBatch();
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
throw new CustomError("failed in DataMapper " + ex.getMessage());
|
|
|
|
} finally {
|
|
|
|
CloseConnections(l_pStatement, l_rsSearch, l_cCon);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 13:17:07 +01:00
|
|
|
public static LinkedHashMap<String, LinkedHashMap<String, Double>> getAllRelationScores() {
|
|
|
|
int count = getSementicsDBRows();
|
|
|
|
int counter2 = 0;
|
|
|
|
int hardCapRetrieveCount = 500000;
|
|
|
|
LinkedHashMap<String, LinkedHashMap<String, Double>> LHMSMX = new LinkedHashMap();
|
|
|
|
while (count > counter2) {
|
|
|
|
try (Connection l_cCon = DBCPDataSource.getConnection()) {
|
|
|
|
l_cCon.setAutoCommit(false);
|
|
|
|
String l_sSQL = "SELECT * FROM `WordMatrix` WHERE ID > " + counter2 + " AND ID < " + (counter2 + hardCapRetrieveCount);
|
|
|
|
try (PreparedStatement l_pStatement = l_cCon.prepareStatement(l_sSQL, java.sql.ResultSet.TYPE_FORWARD_ONLY,
|
|
|
|
java.sql.ResultSet.CONCUR_READ_ONLY)) {
|
|
|
|
l_pStatement.setFetchSize(Integer.MIN_VALUE);
|
|
|
|
try (ResultSet l_rsSearch = l_pStatement.executeQuery()) {
|
|
|
|
int i = 0;
|
|
|
|
LinkedHashMap<String, Double> LHMLocal = new LinkedHashMap();
|
|
|
|
while (l_rsSearch.next() && i < hardCapRetrieveCount) {
|
|
|
|
String str1 = l_rsSearch.getString(1);
|
|
|
|
String str2 = l_rsSearch.getString(2);
|
|
|
|
Double score = l_rsSearch.getDouble(3);
|
|
|
|
LHMLocal.put(str2, score);
|
|
|
|
while (l_rsSearch.next() && i < hardCapRetrieveCount && str1.equals(l_rsSearch.getString(1))) {
|
|
|
|
str2 = l_rsSearch.getString(2);
|
|
|
|
score = l_rsSearch.getDouble(3);
|
|
|
|
LHMLocal.put(str2, score);
|
|
|
|
i++;
|
|
|
|
counter2++;
|
|
|
|
}
|
|
|
|
LHMSMX.put(str1, LHMLocal);
|
|
|
|
System.out.println("i: " + i + "\n" + "free memory: " + Runtime.getRuntime().freeMemory() + "\ncounter2: " + counter2 + "\n");
|
|
|
|
i++;
|
|
|
|
counter2++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
Logger.getLogger(DataMapper.class.getName()).log(Level.SEVERE, null, ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return LHMSMX;
|
|
|
|
}
|
|
|
|
|
2019-03-02 15:10:46 +01:00
|
|
|
public static void CloseConnections(PreparedStatement ps, ResultSet rs, Connection con) {
|
|
|
|
if (rs != null) {
|
|
|
|
try {
|
|
|
|
rs.close();
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
Logger.getLogger(DataMapper.class.getName()).log(Level.SEVERE, null, ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ps != null) {
|
|
|
|
try {
|
|
|
|
ps.close();
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
Logger.getLogger(DataMapper.class.getName()).log(Level.SEVERE, null, ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (con != null) {
|
|
|
|
try {
|
|
|
|
con.close();
|
|
|
|
} catch (SQLException ex) {
|
|
|
|
Logger.getLogger(DataMapper.class.getName()).log(Level.SEVERE, null, ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-03 13:17:07 +01:00
|
|
|
|
2019-03-02 15:10:46 +01:00
|
|
|
}
|