You can read the value of a flowchart block or agent parameter from a specific cell of a database table.
For example, we have a Delay block that processes parts of different types, and the processing time differs depending on the part type.
If your data is in an external database, first import it into the built-in AnyLogic database. Assume the processing times for each part are stored in a database table called processing_times:
Now, we can configure our Delay block to retrieve the necessary processing time for each part type from the database table.
To read the flowchart block’s parameter from the database
-
We want the Delay block to use processing times specific to the type of the part being processed. This requires us to create a custom agent type for parts.
To create a new agent type, find the Source block on the flowchart. In the Agent section of its properties, click the box in the New agent property and select Create new agent from the subsequent menu. The New agent wizard will open. On the first page, set the Agent type name to Part and select an animation shape for this agent. On the next page, add a parameter: name of the String type. We will use this parameter to store the part type.
After creating an agent, configure your Source block to generate agents of Part type:- In the general section of the Source properties, enable the Set agent parameters from DB option.
- In the Database table property, select processing_times.
- In the Agent section, make sure New agent is set to Part.
- In the mapping table below, select the check box next to the name parameter and select part as Column. This will instruct AnyLogic to assign a value from the database column to the name parameter of an agent each time the agent is generated.
- Next, open the properties of the Delay block.
-
For the Delay time property, witch from the value editor to the database reference constructor. To do this, click the
button to the left of the Delay time field. The property value will change to database reference.
-
Click the property value. The Database reference wizard will appear.
In the Table edit box, select the database table containing the data: processing_times. -
In the Column edit box, select the column of the table that contains the data you need: processing_time.
-
Below, you can see the Choice conditions section where you define a condition that specifies which particular value to select from the specified table column.
To add a new condition, first click Add.
We want to select the value corresponding to the specific part, so we will use values from the part column to find the required record. To do this, select part from the drop-down list. - In the field below, type agent.name.
- Click Ok to apply the changes and close the wizard.
As a result, your property value should look like this:
We configured the block to query the built-in database for the Delay time. The value comes from the processing_times table, column processing_time. Agents of type Part have a String parameter name that stores the part type (for example, Part A, Part B, Part C). Each time an agent enters the Delay block, the block looks up a record where part equals the agent’s name (accessed as agent) and uses the matching processing_time as the delay.
For example, if the agent’s name is Part B, the delay is 20 seconds. If the database contains multiple rows with part = Part B, an error occurs because part is expected to be unique. Ensure that part values in processing_times are unique, or restructure the table accordingly.
-
How can we improve this article?
-