AnyLogic
Expand
Font size

Query element

With the Query connectivity tool, you construct SQL queries to databases graphically. These databases must be associated with your model via the Database element. The Query element represents the general SQL operation of retrieving data from database tables.

Using Query, you can feed up a model with data returned by the specified query. Query result represents a set of rows. Each row contains a set of values from the table’s columns you have specified in the query. You can use the query result to construct:

  • Agent population. Each row of the query result will be used to add one more agent into a population. Values will be used to setup the parameters of the agent. This option is very useful in agent based modeling when you want to populate your model with agents, who have characteristics defined by parameters stored in some database. Demo model: Agents Parameterized from Database Open the model page in AnyLogic Cloud. There you can run the model or download it (by clicking Model source files).
  • Collection. Each row of the query result will be used to add one more element to a collection. Values will be stored in the fields of the collection variable.

Creating a query

To define a query

  1. Drag the Query  element from the Professional section of the Connectivity palette to the graphical editor.
  2. Open the Properties view and modify the Name of the element. This name will be used to identify and access this query.
  3. In the Database edit box, specify the database this query will access.
  4. If you need to retrieve all data from some table of the specified database, just select the Select all from the table option in the Query section and specify the name of the table in the edit box to the right.
    If you are working with Access 2007 database, specify the name of the table in the square brackets, e.g.: [Table].
  5. Otherwise, if you need to retrieve some specific data only, select the SQL option and type your custom query in the edit box below.

Feeding a model with data from a database

Using Query connectivity object, you can feed up a model with data returned by the specified query. Query result represents a set of rows. Each row contains a set of values from the table’s columns you have specified in the query. You can use the query result to construct:

Constructing an agent population based on query results

To add agents into a population based on query results

  1. Select the Query object that defines the query.
  2. Go to the Data feeding section of the Properties view.
  3. Select the For each row add check box.
  4. Select the Agent option.
  5. In the Agent population drop-down list, choose the agent population that you want to feed with new agents.
  6. In the table below, map the parameters of the agent you want to setup with the query columns to get these values from. Each mapped pair parameter-column is defined in individual row of the table. Choose the parameter you want to update in the Parameter/Field cell and type the name of the column of the database’s table to get values from in the Column cell.
  7. If you want to populate the population with new agents right on the model startup, select the Execute on startup check box. Otherwise you need to execute this query by calling its function execute().

Constructing a collection based on query results

You obviously feed a collection where elements are instances of some Java class. So, first you need to create a Java class with the required fields.

All the fields of this Java class should be declared as public to enable access to them.

To add elements to a collection based on query results

  1. Select the Query object that defines the query.
  2. Specify the database this query will access in the Database edit box.
  3. If you need to retrieve all data from some table of the specified database, just select the Select all from the table option and specify the name of the table in the edit box to the right.
    If you are working with Access 2007 database, specify the name of the table in the square brackets, e.g.: [Table].
  4. Go to the Data feeding section of the Properties view.
  5. Select the For each row add check box.
  6. Select the Element in collection option.
  7. Choose the collection you want to feed with values in the Collection drop-down list.
  8. In the table below, map the fields of the collection variable you want to setup with the query columns to get these values from. Each mapped pair field-column is defined in individual row of the table. Type the name of the field you want to update in the Parameter/Field cell and type the name of the column of the database’s table to get values from in the Column cell.
  9. If you want to populate the collection with new elements right on the model startup, select the Execute on startup check box. Otherwise you need to execute this query by calling is function execute().
How can we improve this article?