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.
To define a query
- Drag the Query element from the Professional section of the Connectivity palette to the graphical editor.
- Open the Properties view and modify the Name of the element. This name will be used to identify and access this query.
- In the Database edit box, specify the database this query will access.
-
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].
- Otherwise, if you need to retrieve some specific data only, select the SQL option and type your custom query in the edit box below.
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:
- 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: Feeding a Replicated Object from Access 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. Demo model: Feeding a Collection from Access Database Open the model page in AnyLogic Cloud. There you can run the model or download it (by clicking Model source files).
To add agents into a population based on query results
- Select the Query object that defines the query.
- Go to the Data feeding section of the Properties view.
- Select the For each row add check box.
- Select the Agent option.
- In the Agent population drop-down list, choose the agent population that you want to feed with new agents.
- 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.
- 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().
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.
To add elements to a collection based on query results
- Select the Query object that defines the query.
- Specify the database this query will access in the Database edit box.
-
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].
- Go to the Data feeding section of the Properties view.
- Select the For each row add check box.
- Select the Element in collection option.
- Choose the collection you want to feed with values in the Collection drop-down list.
- 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.
- 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?
-