Skip to content

Commit

Permalink
A refactoring of ExperimentScheduler with the following features:
Browse files Browse the repository at this point in the history
- Encapsulation of the variables (esp. 'paused')
- Addition of IExperimentScheduler and AbstractExperimentScheduler
- Separation between ThreadedExperimentScheduler (for UI GAMA) and
NonThreadedExperimentScheduler (for headless GAMA).
  • Loading branch information
AlexisDrogoul committed Feb 2, 2022
1 parent a09e6c1 commit 7e83ce2
Show file tree
Hide file tree
Showing 16 changed files with 558 additions and 532 deletions.
@@ -1,12 +1,12 @@
/*******************************************************************************************************
*
* SqliteConnection.java, in irit.gaml.extensions.database, is part of the source code of the
* GAMA modeling and simulation platform (v.1.8.2).
* SqliteConnection.java, in irit.gaml.extensions.database, is part of the source code of the GAMA modeling and
* simulation platform (v.1.8.2).
*
* (c) 2007-2022 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
*
********************************************************************************************************/
package msi.gama.database.sql;

Expand All @@ -20,11 +20,10 @@
import java.util.ArrayList;
import java.util.List;

import org.sqlite.SQLiteConfig;

import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.sqlite.SQLiteConfig;

import msi.gama.metamodel.topology.projection.IProjection;
import msi.gama.runtime.IScope;
Expand Down Expand Up @@ -54,9 +53,12 @@ class SqliteConnection extends SqlConnection {
/**
* Instantiates a new sqlite connection.
*
* @param venderName the vender name
* @param database the database
* @param transformed the transformed
* @param venderName
* the vender name
* @param database
* the database
* @param transformed
* the transformed
*/
SqliteConnection(final String venderName, final String database, final Boolean transformed) {
super(venderName, database, transformed);
Expand All @@ -65,10 +67,14 @@ class SqliteConnection extends SqlConnection {
/**
* Instantiates a new sqlite connection.
*
* @param venderName the vender name
* @param database the database
* @param extension the extension
* @param transformed the transformed
* @param venderName
* the vender name
* @param database
* the database
* @param extension
* the extension
* @param transformed
* the transformed
*/
SqliteConnection(final String venderName, final String database, final String extension,
final Boolean transformed) {
Expand All @@ -81,18 +87,14 @@ public Connection connectDB()
throws ClassNotFoundException, InstantiationException, SQLException, IllegalAccessException {
Connection conn = null;
try {
if (vender.equalsIgnoreCase(SQLITE)) {
Class.forName(SQLITEDriver).newInstance();
final SQLiteConfig config = new SQLiteConfig();
config.enableLoadExtension(true);
conn = DriverManager.getConnection("jdbc:sqlite:" + dbName, config.toProperties());
// load Spatialite extension library
if (extension != null && new File(extension).exists()) {
load_extension(conn, extension);
}
} else {
if (!SQLITE.equalsIgnoreCase(vender))
throw new ClassNotFoundException("SqliteConnection.connectSQL: The " + vender + " is not supported!");
}
Class.forName(SQLITEDriver).newInstance();
final SQLiteConfig config = new SQLiteConfig();
config.enableLoadExtension(true);
conn = DriverManager.getConnection("jdbc:sqlite:" + dbName, config.toProperties());
// load Spatialite extension library
if (extension != null && new File(extension).exists()) { load_extension(conn, extension); }
} catch (final ClassNotFoundException e) {
e.printStackTrace();
throw new ClassNotFoundException(e.toString());
Expand Down Expand Up @@ -144,9 +146,7 @@ protected List<Integer> getGeometryColumns(final ResultSetMetaData rsmd) throws
final List<Integer> geoColumn = new ArrayList<>();
for (int i = 1; i <= numberOfColumns; i++) {
// Search column with Geometry type
if (vender.equalsIgnoreCase(SQLITE) && rsmd.getColumnType(i) == 2004) {
geoColumn.add(i);
}
if (SQLITE.equalsIgnoreCase(vender) && rsmd.getColumnType(i) == 2004) { geoColumn.add(i); }
}
return geoColumn;

Expand All @@ -158,7 +158,7 @@ protected IList<Object> getColumnTypeName(final ResultSetMetaData rsmd) throws S
final IList<Object> columnType = GamaListFactory.create();
for (int i = 1; i <= numberOfColumns; i++) {
// Search column with Geometry type
if (vender.equalsIgnoreCase(SQLITE) && rsmd.getColumnType(i) == 2004) {
if (SQLITE.equalsIgnoreCase(vender) && rsmd.getColumnType(i) == 2004) {
columnType.add(GEOMETRYTYPE);
} else {
columnType.add(rsmd.getColumnTypeName(i).toUpperCase());
Expand All @@ -173,13 +173,12 @@ protected String getInsertString(final IScope scope, final Connection conn, fina
final IList<Object> cols, final IList<Object> values) throws GamaRuntimeException {
final int col_no = cols.size();
String insertStr = "INSERT INTO ";
String selectStr = "SELECT ";
StringBuilder selectStr = new StringBuilder("SELECT ");
String colStr = "";
String valueStr = "";
// Check size of parameters
if (values.size() != col_no) {
if (values.size() != col_no)
throw new IndexOutOfBoundsException("Size of columns list and values list are not equal");
}
// Get column name
for (int i = 0; i < col_no; i++) {
if (i == col_no - 1) {
Expand All @@ -189,11 +188,9 @@ protected String getInsertString(final IScope scope, final Connection conn, fina
}
}
// create SELECT statement string
selectStr = selectStr + colStr + " FROM " + table_name + " LIMIT 1 ;";
selectStr.append(colStr).append(" FROM ").append(table_name).append(" LIMIT 1 ;");

if (DEBUG.IS_ON()) {
DEBUG.OUT("SqliteConnection.getInsertString.select command:" + selectStr);
}
if (DEBUG.IS_ON()) { DEBUG.OUT("SqliteConnection.getInsertString.select command:" + selectStr.toString()); }

try {
final IList<Object> col_Types = getColumnTypeName(scope, conn, table_name, cols);
Expand All @@ -206,24 +203,22 @@ protected String getInsertString(final IScope scope, final Connection conn, fina
// Value list begin-------------------------------------------
if (values.get(i) == null) {
valueStr = valueStr + NULLVALUE;
} else if (((String) col_Types.get(i)).equalsIgnoreCase(GEOMETRYTYPE)) { // for
} else if (GEOMETRYTYPE.equalsIgnoreCase((String) col_Types.get(i))) { // for

final WKTReader wkt = new WKTReader();
Geometry geo = wkt.read(values.get(i).toString());
// DEBUG.LOG(geo.toString());
if (transformed) {
geo = saveGis.inverseTransform(geo);
}
if (transformed) { geo = saveGis.inverseTransform(geo); }
valueStr = valueStr + WKT2GEO + "('" + geo.toString() + "')";
} else if (((String) col_Types.get(i)).equalsIgnoreCase(CHAR)
|| ((String) col_Types.get(i)).equalsIgnoreCase(VARCHAR)
|| ((String) col_Types.get(i)).equalsIgnoreCase(NVARCHAR)
|| ((String) col_Types.get(i)).equalsIgnoreCase(TEXT)) { // for
} else if (CHAR.equalsIgnoreCase((String) col_Types.get(i))
|| VARCHAR.equalsIgnoreCase((String) col_Types.get(i))
|| NVARCHAR.equalsIgnoreCase((String) col_Types.get(i))
|| TEXT.equalsIgnoreCase((String) col_Types.get(i))) { // for
// String
// type
// Correct error string
String temp = values.get(i).toString();
temp = temp.replaceAll("'", "''");
temp = temp.replace("'", "''");
// Add to value:
valueStr = valueStr + "'" + temp + "'";
} else { // For other type
Expand All @@ -238,14 +233,9 @@ protected String getInsertString(final IScope scope, final Connection conn, fina
}
insertStr = insertStr + table_name + "(" + colStr + ") " + "VALUES(" + valueStr + ")";

if (DEBUG.IS_ON()) {
DEBUG.OUT("SqliteConnection.getInsertString:" + insertStr);
}
if (DEBUG.IS_ON()) { DEBUG.OUT("SqliteConnection.getInsertString:" + insertStr); }

} catch (final SQLException e) {
e.printStackTrace();
throw GamaRuntimeException.error("SqliteConnection.insertBD " + e.toString(), scope);
} catch (final ParseException e) {
} catch (final SQLException | ParseException e) {
e.printStackTrace();
throw GamaRuntimeException.error("SqliteConnection.insertBD " + e.toString(), scope);
}
Expand All @@ -256,7 +246,7 @@ protected String getInsertString(final IScope scope, final Connection conn, fina
@Override
protected String getInsertString(final IScope scope, final Connection conn, final String table_name,
final IList<Object> values) throws GamaRuntimeException {
String insertStr = "INSERT INTO ";
StringBuilder insertStr = new StringBuilder("INSERT INTO ");
String selectStr = "SELECT ";
String colStr = "";
String valueStr = "";
Expand All @@ -275,9 +265,8 @@ protected String getInsertString(final IScope scope, final Connection conn, fina

final int col_no = col_Names.size();
// Check size of parameters
if (values.size() != col_Names.size()) {
if (values.size() != col_Names.size())
throw new IndexOutOfBoundsException("Size of columns list and values list are not equal");
}

if (DEBUG.IS_ON()) {
DEBUG.OUT("list of column Name:" + col_Names);
Expand All @@ -291,20 +280,18 @@ protected String getInsertString(final IScope scope, final Connection conn, fina
// Value list begin-------------------------------------------
if (values.get(i) == null) {
valueStr = valueStr + NULLVALUE;
} else if (((String) col_Types.get(i)).equalsIgnoreCase(GEOMETRYTYPE)) { // for
} else if (GEOMETRYTYPE.equalsIgnoreCase((String) col_Types.get(i))) { // for
final WKTReader wkt = new WKTReader();
Geometry geo = wkt.read(values.get(i).toString());
if (transformed) {
geo = getSavingGisProjection(scope).inverseTransform(geo);
}
if (transformed) { geo = getSavingGisProjection(scope).inverseTransform(geo); }
valueStr = valueStr + WKT2GEO + "('" + geo.toString() + "')";

} else if (((String) col_Types.get(i)).equalsIgnoreCase(CHAR)
|| ((String) col_Types.get(i)).equalsIgnoreCase(VARCHAR)
|| ((String) col_Types.get(i)).equalsIgnoreCase(NVARCHAR)
|| ((String) col_Types.get(i)).equalsIgnoreCase(TEXT)) {
} else if (CHAR.equalsIgnoreCase((String) col_Types.get(i))
|| VARCHAR.equalsIgnoreCase((String) col_Types.get(i))
|| NVARCHAR.equalsIgnoreCase((String) col_Types.get(i))
|| TEXT.equalsIgnoreCase((String) col_Types.get(i))) {
String temp = values.get(i).toString();
temp = temp.replaceAll("'", "''");
temp = temp.replace("'", "''");
// Add to value:
valueStr = valueStr + "'" + temp + "'";
} else { // For other type
Expand All @@ -318,28 +305,31 @@ protected String getInsertString(final IScope scope, final Connection conn, fina
}
}

insertStr = insertStr + table_name + "(" + colStr + ") " + "VALUES(" + valueStr + ")";
insertStr.append(table_name).append("(").append(colStr).append(") ").append("VALUES(").append(valueStr)
.append(")");

} catch (final SQLException e) {
e.printStackTrace();
throw GamaRuntimeException.error("SqliteConnection.getInsertString:" + e.toString(), scope);
} catch (final ParseException e) {
} catch (final SQLException | ParseException e) {
e.printStackTrace();
throw GamaRuntimeException.error("SqliteConnection.getInsertString:" + e.toString(), scope);
}

return insertStr;
return insertStr.toString();
}

/**
* Gets the column type name.
*
* @param scope the scope
* @param conn the conn
* @param tableName the table name
* @param columns the columns
* @param scope
* the scope
* @param conn
* the conn
* @param tableName
* the table name
* @param columns
* the columns
* @return the column type name
* @throws SQLException the SQL exception
* @throws SQLException
* the SQL exception
*/
// 18/July/2013
private IList<Object> getColumnTypeName(final IScope scope, final Connection conn, final String tableName,
Expand All @@ -351,7 +341,7 @@ private IList<Object> getColumnTypeName(final IScope scope, final Connection con
final IList<? extends IList<Object>> data = (IList<? extends IList<Object>>) result.get(2);

try (final Statement st = conn.createStatement()) {
st.executeQuery(sqlStr);
// st.executeQuery(sqlStr);
final int numRows = data.size();
for (int i = 0; i < numberOfColumns; i++) {
final String colName = ((String) columns.get(i)).trim();
Expand All @@ -360,11 +350,11 @@ private IList<Object> getColumnTypeName(final IScope scope, final Connection con
final String name = ((String) row.get(1)).trim();
final String type = ((String) row.get(2)).trim();
if (colName.equalsIgnoreCase(name)) {
if (type.equalsIgnoreCase(BLOB) || type.equalsIgnoreCase("GEOMETRY")
|| type.equalsIgnoreCase("POINT") || type.equalsIgnoreCase("LINESTRING")
|| type.equalsIgnoreCase("POLYGON") || type.equalsIgnoreCase("MULTIPOINT")
|| type.equalsIgnoreCase("MULTILINESTRING") || type.equalsIgnoreCase("MULTIPOLYGON")
|| type.equalsIgnoreCase("GEOMETRYCOLLECTION")) {
if (BLOB.equalsIgnoreCase(type) || "GEOMETRY".equalsIgnoreCase(type)
|| "POINT".equalsIgnoreCase(type) || "LINESTRING".equalsIgnoreCase(type)
|| "POLYGON".equalsIgnoreCase(type) || "MULTIPOINT".equalsIgnoreCase(type)
|| "MULTILINESTRING".equalsIgnoreCase(type) || "MULTIPOLYGON".equalsIgnoreCase(type)
|| "GEOMETRYCOLLECTION".equalsIgnoreCase(type)) {
columnType.add(GEOMETRYTYPE);
} else {
columnType.add(type);
Expand All @@ -380,9 +370,12 @@ private IList<Object> getColumnTypeName(final IScope scope, final Connection con
/**
* Load extension.
*
* @param conn the conn
* @param extension the extension
* @throws SQLException the SQL exception
* @param conn
* the conn
* @param extension
* the extension
* @throws SQLException
* the SQL exception
*/
// 23-July-2013
private void load_extension(final Connection conn, final String extension) throws SQLException {
Expand Down

0 comments on commit 7e83ce2

Please sign in to comment.