Replacing a single node with it's child model

Hi, I have a situation where nodes in a model can have child models as well.
I was wondering if there is any way to replace a node in parent model with its respective child model.

That sounds intriguing, but I don’t know what you mean. Could you provide a minimal code sample that exemplifies what you mean, along with sketches before and after?

What should happen if there are multiple “child models” for the node being replaced?

Hi Walter, please see below before and after diagrams.

In our situation: We can have at most one child model per node.
Note for you: The screenshots are quite big in size. Please save them before reading.

Before


After


We are doing node traversing for an internal business requirement. So, the main intention of replacing a parent block with the child model is to simplify the traversing logic for complex diagrams. We don’t have to over do recursions.

Well, what I would do is:

  1. add the “child model” to the diagram
  2. change the Link.fromNode of each Link coming out of the “Start” Node(s) to come from the Node(s) coming into the “parent” Node
  3. change the Link.toNode of each Link going to the “child model”'s “End” Node(s) to go to the Node(s) connected by Links coming out of the “parent” Node
  4. delete the original “parent” Node and the copied “child model” “Start” Node(s) and “End” Node(s).

Of course you must do everything within a single transaction.

You’ll need to deal with possible inconsistencies, such as having a mismatching number of “Start” Nodes in the “child” model compared to the number of input ports on the “parent” node.

Samples that do similar, but different, things include:

I understood your approach for the requirement posted above.
While adding the child model to the main diagram, the entire main model gets replaced.
Also, while adding the model.addNodeData(child data) to main model, I get the below errors:

  1. Duplicate key(probably in the child model)
  2. Cannot modify the collection during iteration.

Could you please help on this ?

How are you doing that?

Duplicate key(probably in the child model)

Did you modify the makeUniqueKey function?

Cannot modify the collection during iteration.

This could be any number of things, but most likely you are trying to modify some set of data, such as the model’s node data, while looping through an iterator for that node data.

Hi Simon, please find the requested details below:

How are you doing that?

myDiagram.model.addNodeDataCollection(node.data.child);
where “node” has child model.

Did you modify the makeUniqueKey function?

No I did not modify the makeUniqueKey function. Basically I am copying a model from objects diagram1(child diagram) to diagram0(parent diagram).
Relation between both the diagrams is: diagram0 → node → child = diagram1 → model

Could you please let me know how to add child model to an existing diagram ?

The value of node.data.child is an Array or in an Iterable of node data objects?

You can add one model to another using addNodeDataCollection. For example, there are two models in the Flowchart sample: myDiagram’s model and myPalette’s model. This code would add the Palette’s model to the Diagram:

myDiagram.model.addNodeDataCollection(myPalette.model.nodeDataArray);

That example will work if you just paste that into the developer console. You ought to be able to do something similar with diagram0’s model and diagram1’s model.

If you already have two Diagrams, you could try calling Diagram.copyParts.

Thank you. I am able to add new nodecollection to existing model. The only issue is the new model being copied has conflicting keys(duplicate).
Is there any way I can set/generate series of custom unique keys as the user drag-drops items from pallette ?

You could try what I suggested: create or reuse an unseen Diagram by replacing its Diagram.model with the model that you get or construct from the parent’s data.

Then try calling Diagram.copyParts to your main Diagram.

Thank you walter. I was able to achieve the replacing mechanism.
Cheers :slight_smile: