Font size

Reading parameter value from DB

You can easily read the value of flowchart block’s or agent’s parameter from a specific cell of a database table.

Assume we have Delay block that processes parts of different types, and the processing time differs depending on the part type.

We have the processing times for each part stored in the database table processing_times:

If you have your data stored in an external database, first import them into the built-in AnyLogic database.

Now we can set our Delay block to read the required processing time for each part type from the database table.

To read the flowchart block’s parameter from the DB

  1. We want the Delay block to use the processing time specific for the type of the currently processed part. This requires us to create a custom agent type for parts. To create a new agent type, in the Projects view, right-click the model item, and choose New > Agent Type from the context menu. You will see the New agent wizard. On the first page, set the Agent type name: Part. On the second page, choose the animation shape for this agent, and on the next page add a parameter: name of type String. We will use this parameter to store the part type. Let your Source block generate agents of Part type (choose Part in its New agent field), and let it Set agent parameters from DB (you will find the detailed information in Source generates agents with parameters read from DB).
  2. Open the Properties of the Delay block.
  3. For the Delay time property, we want to switch from the value editor to the DB reference constructor. To do this, on the left of the Delay time field, click the button, and choose Database reference option from the popup list.

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

  6. Below, you will see the Choice conditions section where you can define a condition that specifies what particular value to select from the specified table column.

  7. 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. Therefore, choose part from the drop-down list.

  8. In the field below, type

Let us explain how it works. We have set the block to take a look at the built-in database for the Delay time value. The value is taken from processing_times table, processing_time column. Our agents of type Part have String parameter name storing the part type (it could be "Part A", "Part B", or "Part C"). For each new agent entering the Delay, the block will search in the specified database table for the record that has the value in the part column equal to the name of the agent currently being processed by the block (it 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. If the database contains several records with "Part B" value in the part column, you will see the error message since AnyLogic expects the values in this column to be unique.

How can we improve this article?