Query regarding delete tree of nodes


I am using go js to build flow diagrams, which represent a bot process, for instance, login to netflix and browse through set of movies. This is very primitive use case but flows can be very complex based on how a user builds the diagram.
Below is a sample diagram which automates login to Netflix .

Now say, user wants to delete a normal node like “Sleep for 500ms” or “Click Try Again”, I don’t see any challenge from go js perspective, as we simply delete that node and relink previous node to next node.

The complexity is when user tries to delete any conditions node, with Then/ Else ports. Functionally what we want to achieve is that,

  1. Delete entire sub tree coming out of Else port
  2. Relink the sub tree from Then port to previous node.

For instance, in the above diagram, if user deletes “Was login successful?” node, then the diagram should be updated as below:

When I tried to use below code snippet to clean up else branch, it is even deleting nodes that are common for both Then/ Else branches.

const removeConditionNode (diagram, node) => {
diagram.commit(function (d) {
                // Some code that gets elsePortId

		// Get all immediate nodes connecting out from else port.
		const nextNodeOutOfElse = node.findNodesOutOf(elsePortId).value;

		// Remove subtree, coming out of else branch.

	}, 'Removing Action node from diagram');

So I am looking for any convenient API to delete only those nodes/ edges that are in else branch but are not common in Then branch.

With the current code in place, it leaves the diagram in below state, after deleting the condition node, "Was login successful? "

I think you’re going to have to implement that yourself. Walk the graph starting with the “If” node following the “Then” path. Accumulate the nodes that you find in a Set, so that you can avoid any problems with cycles.

Then walk the graph starting from the “Else” path, and accumulate those nodes in a separate second Set, but also make sure that you stop whenever you see a node from the first Set. Eventually you will end up with a Set consisting only of the “Else” node and all of the nodes accessible from there, but none of the nodes in the first Set that includes the “Then” branch and the “If” node, in case of cycles.

1 Like

Yeah. Before writing this custom code, just wanted to make sure whether there is anything available OOTB to find out intersection points between two sub graphs. Will implement this custom behavior.

Thanks a lot for your inputs :)

Also, with the above approach, do we need to delete both nodes/ edges separately or simply call diagram.remove(…) for each node in else branch that’s not present in then branch ?

Yes, just call remove: https://gojs.net/latest/api/symbols/Diagram.html#remove

But I would call: https://gojs.net/latest/api/symbols/Diagram.html#removeParts as part of your transaction.

1 Like

Sounds good. Thank you