article by Frank Nimphius, January 2019
As you may know, the Common Response component (System.CommonResponse) can be used to iteratively stamp response items sourced from an array. Sometimes however it is not the repeated rendering of a response item that bot designers need but the repeated looping over a dialog flow state or many dialog flow states (block). In this quick tip I explain how to create a loop in the dialog flow of a skill bot that gets terminated when a specific condition is met.
Building Loops in OBotML
OBotML is the YAML based syntax of the dialog flow in a skill bot. In the following I explain how you create a bot response as shown below by iterating over states in a dialog flow. Its easy for you then to modify the sample for a more complex use case you may have.
The state in the dialog flow that this sample iterates over references a System.Output component. For each iteration, the current index (the value of the runner variable) of the loop is printed. The sample is configured such that the loop is discontinued after the 3rd round of printing. In a real bot use case, you would use e.g. the number of entity values found (${iResult.value.entityMatches['DRINKS']?size) or the number of intents matches for a given user string (${iResult.value.intentMatches?size}) to ask additional questions or to save the information sequentially. Basically any array can be used.
The image below shows the dialog flow for the sample in this quick tip. The "setVariable" state at the beginning of the flow sets the initial value of the "runner" in the iteration. Arrays are usually zero based, so that it would make sense for you to set the initial value to -1.
The "increment" state then uses an Apache FreeMarker expression – "?number" – to increase the value of the "runner" variable by 1. The "startLoop" state is where you define the block that should be iterated over. You can add one (as in this example) or many states for this. Only make sure that the last state in the block of states to iterate over has its "next" transition set to "checkExitCondition". The other states in the iteration block use the "next" transition to point to the next state to go to (which usually will be the state below)
The "checkExitCondition" uses a System.Switch component and Apache FreeMarker expression to determine whether or not the loop should exist.
Related Content
TechExchange – Using Apache Freemarker Expression in Oracle Intelligent Bots
TechExchange Quick-Tip: Accessing Attribute Names and Values of a Data Object
TechExchange: Displaying the Top 3 Runner-Up Intents in case of Unresolved Intent Resolution