Loop Node

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. Arrays can often be returned by web services or database end points. 

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 with caution - long running graphs within Xponent are likely to hit the timeout threshold (usually this is 50 seconds) - and may fail to execute correctly. If you feel the need to loop through a lot of data 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 - have 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. 

Creating a Loop Node

Loop nodes can only be added to a graph via the right-click Add Node to Graph menu option and can not be created via the New menu option. 

Right click and chose Add Node to Graph and choose the Loop Node 


Select the Loop Node in the graph and on the right of the window provide a schema location and a graph from the drop down that will 

In this example graph the schema location loopData is being used:

and 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' each loop and at the end the entire sentence will be in one schema location.

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 is shown below along with the testing console output.

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.



Privacy Policy
© 2022 CSG International, Inc.