AnyLogic
Expand
Font size

Reading parameter value from the database

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.

The processing times for each part are stored in a database table called processing_times:

If your data is in an external database, first import it into the built-in AnyLogic database.

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

  1. 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, right-click the model item in the Projects view and select New > Agent Type from the context menu. The New agent wizard will open. On the first page, set the Agent type name: Part. On the second page, 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.
    Let your Source block generate agents of Part type:
    • In the general section of the Source properties, enable the Set agent parameters from DB option.
    • In the Agent section, set New agent: Part.
    • In the Agent parameters mapping, set Parameter: name and Column: part. 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.
  2. Next, open the Properties of the Delay block.
  3. 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 and select the Database reference option from the drop-down list.

  4. In the Table property below, select the database table containing the data: processing_times.
  5. From the Value column drop-down list, select the column of the table that contains the data you need: processing_time.

  6. 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.
    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.

  7. In the field below, type agent.name

Let us explain how it works. We have configured the block to query the built-in database for the Delay time value. This value is taken from the processing_times table and the processing_time column. Our agents of the Part type have a String parameter called name that stores the part type (it could be "Part A", "Part B", or "Part C"). Each time a new agent enters the Delay block, the block searches the specified database table for a record where the value in the part column is equal to the name of the agent (which, in turn, is accessed as agent). This value is set as the delay time for the current agent.

For example, if the current agent has name "Part B", the block will set the delay time equal to 20 seconds. However, if the database contains several records with "Part B" value in the part column, an error message will appear because AnyLogic expects the values in this column to be unique. To avoid this, make sure that the part column in the processing_times table contains only unique values, or consider restructuring the table.

How can we improve this article?