AnyLogic
Expand
Font size

public class ModelDatabase
extends Object
This class represents the built-in AnyLogic Model Database. Almost all the methods of this class are internal. The database object may be accessed from getEngine().getModelDatabase() and may be used for import / export with external data sources.
Author:
AnyLogic North America, LLC https://anylogic.com

Nested Class Summary

Modifier and TypeClassDescription
static class ModelDatabase.DatabaseCacheKey
This class is internal and shouldn't be called by user.
it may be removed/renamed in future.

Field Summary

Modifier and TypeFieldDescription
static final StringAL_DB_OBJECTS 
static final StringAL_OBJECT_NAME 
static final StringAL_OBJECT_TYPE 
static final StringAL_OBJECT_USAGE 
static final StringAL_OBJECT_USAGE_LOG 
static final StringAL_SELECTED_LOG_OBJECT_NAME 
static final StringAL_SELECTED_LOG_OBJECT_TYPE 
static final StringAL_SELECTED_LOG_OBJECTS 
static final StringANYLOGIC_IDENTIFIER_PREFIX 
static final StringCURRENT_VERSION 
static final StringDATABASE_DB_PATH 
static final StringDATABASE_DIRECTORY 
static final StringDROP_SQL_OBJECT 
static final StringLOG_OBJECT_SUFFIX 
static final StringPUBLIC_SCHEMA_NAME 
static final StringSQL_FUNCTION 
static final StringSQL_TABLE 
static final StringSQL_VIEW 
static final StringUNIQUE_AUTOINC_COLUMN_NAME 

Constructor Summary

ConstructorDescription
ModelDatabase(Engine engine, ModelProperties modelProperties, DatabaseLogProperties logProperties)
This constructor is internal and shouldn't be called by user.
it may be removed/renamed in future.

Method Summary

Modifier and TypeMethodDescription
static voidcreateLogSchema(Connection connection)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
static voiddeleteExistingLogObjects(Connection connection)
This method deletes all the model log data (if any were turned on) from the model database
voiddisconnect()
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
ResultSetexecute(String sqlStatement)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
voidexportToExternalDB(String sourceTableName, Connection targetConnection, String targetTableName, boolean clearTargetTable, boolean autoCommit)
Exports a single table data from this database to an external database.
intgenerateAgentElementId()
This method returns id property of dynamically create model elements
intgenerateAgentId()
Internal method, used to initialize Agent.getId() property
intgetAgentTypeElementId(LoggingType loggingType, Agent agent, String elementName)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
intgetAgentTypeId(Agent agent)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
<T extends Enum<T> & IStatechartState<?, T>>
int
getAgentTypeStateId(Agent agent, Statechart<T> statechart, T state)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
LRUCache<ModelDatabase.DatabaseCacheKey,Object>getCache()
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
<R> ObjectgetCachedValue(com.querydsl.sql.SQLBindings sqlBindings, String operation, Supplier<R> f)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
ConnectiongetConnection() 
static StringgetCreateTableSQL(String tableName, String tableContent, boolean cached)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
com.querydsl.sql.ConfigurationgetDSLConfiguration()
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
intgetEventId(EventOriginator event)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
intgetStatechartId(Statechart<?> statechart)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
voidimportFromExternalDB(Connection sourceConnection, String sourceTableName, String targetTableName, boolean clearTargetTable, boolean autoCommit)
Imports a single table data from an external database.
voidlog(ILogEntry logEntry)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
voidresetBeforeStart()
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
static voidrestoreUsersViews(Connection connection)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
static voidrestoreUsersViews(Connection connection, Collection<String> viewNames)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
ResultSetselect(String sqlQuery)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
ResultSetselectFrom(String tableName)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
ResultSetwrapResultSet(ResultSet results)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

Methods inherited from class java.lang.Object

equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Field Details

CURRENT_VERSION

@AnyLogicInternalAPI
public static final String CURRENT_VERSION
See Also:
Constant Field Values

DATABASE_DIRECTORY

@AnyLogicInternalAPI
public static final String DATABASE_DIRECTORY
See Also:
Constant Field Values

DATABASE_DB_PATH

@AnyLogicInternalAPI
public static final String DATABASE_DB_PATH

PUBLIC_SCHEMA_NAME

@AnyLogicInternalAPI
public static final String PUBLIC_SCHEMA_NAME
See Also:
Constant Field Values

DROP_SQL_OBJECT

@AnyLogicInternalAPI
public static final String DROP_SQL_OBJECT
See Also:
Constant Field Values

LOG_OBJECT_SUFFIX

@AnyLogicInternalAPI
public static final String LOG_OBJECT_SUFFIX
See Also:
Constant Field Values

ANYLOGIC_IDENTIFIER_PREFIX

@AnyLogicInternalAPI
public static final String ANYLOGIC_IDENTIFIER_PREFIX
See Also:
Constant Field Values

UNIQUE_AUTOINC_COLUMN_NAME

@AnyLogicInternalAPI
public static final String UNIQUE_AUTOINC_COLUMN_NAME
See Also:
Constant Field Values

AL_SELECTED_LOG_OBJECTS

@AnyLogicInternalAPI
public static final String AL_SELECTED_LOG_OBJECTS
See Also:
Constant Field Values

AL_SELECTED_LOG_OBJECT_NAME

@AnyLogicInternalAPI
public static final String AL_SELECTED_LOG_OBJECT_NAME
See Also:
Constant Field Values

AL_SELECTED_LOG_OBJECT_TYPE

@AnyLogicInternalAPI
public static final String AL_SELECTED_LOG_OBJECT_TYPE
See Also:
Constant Field Values

SQL_TABLE

@AnyLogicInternalAPI
public static final String SQL_TABLE
See Also:
Constant Field Values

SQL_VIEW

@AnyLogicInternalAPI
public static final String SQL_VIEW
See Also:
Constant Field Values

SQL_FUNCTION

@AnyLogicInternalAPI
public static final String SQL_FUNCTION
See Also:
Constant Field Values

AL_DB_OBJECTS

@AnyLogicInternalAPI
public static final String AL_DB_OBJECTS
See Also:
Constant Field Values

AL_OBJECT_NAME

@AnyLogicInternalAPI
public static final String AL_OBJECT_NAME
See Also:
Constant Field Values

AL_OBJECT_TYPE

@AnyLogicInternalAPI
public static final String AL_OBJECT_TYPE
See Also:
Constant Field Values

AL_OBJECT_USAGE

@AnyLogicInternalAPI
public static final String AL_OBJECT_USAGE
See Also:
Constant Field Values

AL_OBJECT_USAGE_LOG

@AnyLogicInternalAPI
public static final String AL_OBJECT_USAGE_LOG
See Also:
Constant Field Values

Constructor Details

ModelDatabase

@AnyLogicInternalAPI
public ModelDatabase(Engine engine,
 ModelProperties modelProperties,
 DatabaseLogProperties logProperties)
This constructor is internal and shouldn't be called by user.
it may be removed/renamed in future.

Method Details

getCache

@AnyLogicInternalAPI
public LRUCache<ModelDatabase.DatabaseCacheKey,Object> getCache()
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

getCachedValue

@AnyLogicInternalAPI
public <R> Object getCachedValue(com.querydsl.sql.SQLBindings sqlBindings,
 String operation,
 Supplier<R> f)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

createLogSchema

@AnyLogicInternalAPI
public static void createLogSchema(Connection connection)
                            throws SQLException
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
Throws:
SQLException

restoreUsersViews

@AnyLogicInternalAPI
public static void restoreUsersViews(Connection connection,
 Collection<String> viewNames)
                              throws SQLException
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
Throws:
SQLException

restoreUsersViews

@AnyLogicInternalAPI
public static void restoreUsersViews(Connection connection)
                              throws SQLException
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
Throws:
SQLException

deleteExistingLogObjects

public static void deleteExistingLogObjects(Connection connection)
                                     throws SQLException
This method deletes all the model log data (if any were turned on) from the model database
Parameters:
connection - database connection
Throws:
SQLException

getCreateTableSQL

@AnyLogicInternalAPI
public static String getCreateTableSQL(String tableName,
 String tableContent,
 boolean cached)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

log

@AnyLogicInternalAPI
public void log(ILogEntry logEntry)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

getAgentTypeId

@AnyLogicInternalAPI
public int getAgentTypeId(Agent agent)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

generateAgentElementId

@AnyLogicInternalAPI
public int generateAgentElementId()
This method returns id property of dynamically create model elements

generateAgentId

@AnyLogicInternalAPI
public int generateAgentId()
Internal method, used to initialize Agent.getId() property

getEventId

@AnyLogicInternalAPI
public int getEventId(EventOriginator event)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

getStatechartId

@AnyLogicInternalAPI
public int getStatechartId(Statechart<?> statechart)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

getAgentTypeElementId

@AnyLogicInternalAPI
public int getAgentTypeElementId(LoggingType loggingType,
 Agent agent,
 String elementName)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

getAgentTypeStateId

@AnyLogicInternalAPI
public <T extends Enum<T> & IStatechartState<?,
T>>
int getAgentTypeStateId(Agent agent,
 Statechart<T> statechart,
 T state)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

getConnection

@AnyLogicInternalAPI
public Connection getConnection()

disconnect

@AnyLogicInternalAPI
public void disconnect()
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

resetBeforeStart

@AnyLogicInternalAPI
public void resetBeforeStart()
                      throws Exception
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.
Throws:
Exception

getDSLConfiguration

@AnyLogicInternalAPI
public com.querydsl.sql.Configuration getDSLConfiguration()
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

select

@AnyLogicInternalAPI
public ResultSet select(String sqlQuery)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

execute

@AnyLogicInternalAPI
public ResultSet execute(String sqlStatement)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

selectFrom

@AnyLogicInternalAPI
public ResultSet selectFrom(String tableName)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.

importFromExternalDB

public void importFromExternalDB(Connection sourceConnection,
 String sourceTableName,
 String targetTableName,
 boolean clearTargetTable,
 boolean autoCommit)
Imports a single table data from an external database. Column types should match or be convertible.
Parameters:
sourceConnection - Connection object for external database to copy data from
sourceTableName - name of the table in external database to copy data from
targetTableName - table in this database where the data should be copied to
clearTargetTable - if true the target table will be cleared before data is copied
autoCommit - if this parameter is false, this action will not open or commit any transaction (which may decrease performance in some cases).
Usage example (performing multiple operations in a single transaction)
 ModelDatabase modelDB = getEngine().getModelDatabase();
 Connection targetConnection = modelDB.getConnection();
 targetConnection.setAutoCommit(false);
 try {
        modelDB.importFromExternalDB( ... , false);
        modelDB.importFromExternalDB( ... , false);
        modelDB.importFromExternalDB( ... , false);
        targetConnection.commit();
 } catch (Exception e) {
        targetConnection.rollback();
 } finally {
        targetConnection.setAutoCommit(true);
 }
 

exportToExternalDB

public void exportToExternalDB(String sourceTableName,
 Connection targetConnection,
 String targetTableName,
 boolean clearTargetTable,
 boolean autoCommit)
Exports a single table data from this database to an external database. Column types should match or be convertible.
When exporting to Database#getConnection(), depending on the external database implementation, the data may be actually saved when the model is stopped (when Engine will automatically call Database#disconnect() at the end). For custom experiment this requires calling engine.stop() at the end.
Parameters:
sourceTableName - table in this database to copy data from
targetConnection - Connection object for external database to copy data to
targetTableName - table in external database where the data should be copied to
clearTargetTable - if true the target table will be cleared before data is copied
autoCommit - if this parameter is false, this action will not open or commit any transaction (which may decrease performance in some cases).
Usage example (performing multiple operations in a single transaction)
 ModelDatabase modelDB = getEngine().getModelDatabase();
 Connection targetConnection = targetDB.getConnection();
 targetConnection.setAutoCommit(false);
 try {
        modelDB.exportToExternalDB( ... , false);
        modelDB.exportToExternalDB( ... , false);
        modelDB.exportToExternalDB( ... , false);
        targetConnection.commit();
 } catch (Exception e) {
        targetConnection.rollback();
 } finally {
        targetConnection.setAutoCommit(true);
 }
 

wrapResultSet

@AnyLogicInternalAPI
public ResultSet wrapResultSet(ResultSet results)
This method is internal and shouldn't be called by user.
it may be removed/renamed in future.