0
votes

I am looking to dynamically add each Rectangular Node from my network to a particular collection, on simulation startup. I'm doing this because I will have 1000+ nodes and adding them manually is undesirable.

Each of these nodes is named using a convention and start with either "lane" or "grid".

If it is "lane" then it adds to one collection, and "grid" to the other.

Currently I am using the code:

for (Node n : network.nodes()) {
    String layoutIdentifier = n.getName().split("_")[0];
    if (layoutIdentifier.equals("lane")) {
        laneNodes.add(n);
        traceln(n);
         //Lane newLane = add_lanes(n);
    } else if (layoutIdentifier.equals("grid")) {
        gridNodes.add(n);
    }
}

This is working fine and adds them to the collections as Nodes, but I was really wanting to add them to collections of Rectangular Nodes (which is what they are) as I need to use this type in my Agents.

I tried this code (changing Node to RectangularNode):

for (RectangularNode n : network.nodes()) {
    String layoutIdentifier = n.getName().split("_")[0];
    if (layoutIdentifier.equals("lane")) {
        laneNodes.add(n);
        traceln(n);
         //Lane newLane = add_lanes(n);
    } else if (layoutIdentifier.equals("grid")) {
        gridNodes.add(n);
    }
}

but get the error Type mismatch: cannot convert from element type Node to RectangularNode. Location: pbl_congestion_simulation/Main - Agent Type

Is there a way to convert the Node to RectangularNode? Or a better way to run through all the nodes in the network and add them as Rectangular nodes to collections of the same?

I can see that the Nodes are referenced as com.anylogic.engine.markup.RectangularNode@293cc7d0 so was hoping that RectangularNode portion could be accessed.

many thanks.

1

1 Answers

0
votes

Your first code is fine, you can have a collection with elements of type RectangularNode and the only thing you need to change on that code is:

laneNodes.add((RectangularNode)n);
gridNodes.add((RectangularNode)n);

You can transform a node into a rectangular node, but not the other way around, that's why your second code doesn't work.

If you have other nodes on the network, that are not rectangular nodes, then you can add something like this:

if(n.getClass().equals(RectangularNode.class))
     laneNodes.add((RectangularNode)n);