Loop Node
  • 23 Aug 2024
  • 3 Minutes to read
  • Contributors
  • Dark
    Light
  • PDF

Loop Node

  • Dark
    Light
  • PDF

Article summary

We have included an Interactive Training to provide a more engaging and visual learning experience. End users are required to complete their training through the CSG University.


The Loop Node allows a sub-graph to be executed many times from the same position in a graph.

To use a loop node, you will need a schema location that contains a JavaScript array and a graph to process each element in the array. The loop node will call the graph once for each entry in the array sequentially. If the data provided to the loop node is not a JavaScript array, it will be converted into an array of length one. Web services or database endpoints can often return arrays. 

A typical use of the loop node is to process individual items in the array that may have been returned from some other system or process.

Loop nodes should be used cautiously - long-running graphs within Xponent are likely to hit the timeout threshold (usually 50 seconds) - and may fail to execute correctly.

Suppose you feel the need to loop through a lot of data. In that case, it is recommended that you split that data into multiple items as quickly as possible, say perhaps by writing individual messages to a message queue and then having a subsequent graph that processes those individual messages.

Xponent is designed to process small events in a massively parallel fashion - having large, long-running graphs is not a good design pattern.

If you need assistance with this, please speak to your Xponent Customer Success Manager, who can assist. 


Adding a Loop Node

Loop Node only can be added to a graph using the below steps and can not be created via the New menu option.

  • Right-click on the graph and click Add Node to Graph.
  • Select the Loop Node and click Add Node.

  • Select the Loop Node in the graph, and provide a schema location and a graph from the dropdown on the right-hand side of the page.


For Example, the schema location loopData is being used:

The loopData location is set to this value in the Set Node below : [{"val":"a"},{"val":"b"},{"val":"c"}]

The sub-graph just returns the 'val' attribute of the object:

The graph shows one transaction executing, but the testing console will show the iterations of the loop node, and the Display JSON button will show the state of the data store at the end of that iteration. The output of the subgraph can be seen in each transaction as the loop node iterates over the array.


Notes

A loop node graph can not call itself, and the graph will not appear on the Using Graph dropdown.

The loop node processes the array sequentially, and the graph state is global so that changes to the schema or a public variable in one sub-graph will be available to the next sub-graph.

To use the Loop Node as a way to insert multiple records into a database within the loop, use a DB node to insert where the singular record of data exists.

Another Example

This example will loop through an array of words and concatenate them together into one sentence. Each time the loop runs, it will concatenate the next word onto the previous words, slowly building the sentence. It will return 'true' in each loop, and the entire sentence will be in one schema location at the end.

This is the array of words:

["The","quick","brown","fox","jumps","over","the","lazy","dog."]

The set node below sets a schema location with this array of words, which is then used in the loop node. 

These are the two graphs. The upper picture is the main graph, and the bottom picture is the graph the loop runs with.

The JavaScript node's output is stored in a separate schema location compared to where the words are stored. The code and the testing console output are shown below.

The following screenshot shows the final result of the graphs.

The array of words is stored in the 'data' portion of the schema, while the built sentence is stored in the 'result' part of the schema, both under the 'loopData' section.


Is it helpful? React and share your comment

Changing your password will log you out immediately. Use the new password to log back in.
First name must have atleast 2 characters. Numbers and special characters are not allowed.
Last name must have atleast 1 characters. Numbers and special characters are not allowed.
Enter a valid email
Enter a valid password
Your profile has been successfully updated.
ESC

Eddy AI, facilitating knowledge discovery through conversational intelligence