Search code examples
anylogic

Create nodes during startup using string input from database as node name


I would like to create a network via code at start up similar to the example in the following link. https://anylogic.help/markup/create-network-by-code.html

However, I might have 100s of nodes and I was hoping to read in the node name and the x,y,z coordinates from a database. Is that possible?

I'm not quite sure of the syntax to create node based on a string. I tried the following but obviously doesn't work because pn is a PointNode type and the name is a String type. Also it's trying to declare pn twice. But hopefully it explains what I'm trying to do.

Thanks

// Init variables
double x, y, z;
PointNode pn;
      
// Read from database
List<Tuple> rows = selectFrom(node_coord)
    .list();

// Loop for all nodes
for (Tuple row : rows) {
    
    pn = row.get( node_coord.name );
    x = row.get( node_coord.x_coord );
    y = row.get( node_coord.y_coord );
    z = row.get( node_coord.z_coord );
    
    pn = new PointNode();
    pn.setRadius(5);
    pn.setLineColor(red);
    pn.setPos(x, y, z);

    //Add to network
    n.addAll(pn) ;      
}

Solution

  • PointNode elements do not have a name, so you cannot assign one.

    So you can simply loop across your dbase entries and ignore that name:

    // Read from database
    List<Tuple> rows = selectFrom(node_coord)
        .list();
    
    // Loop for all nodes
    for (Tuple row : rows) {
        pn = new PointNode();
        pn.setRadius(5);
        pn.setLineColor(dodgerBlue);
        pn.setPos(node_coord.x_coord, node_coord.y_coord);
    
        //Add to network
        n.addAll(pn) ;      
    }
    

    If you want to use names, you should create an agent type "MyNodes" with a String param p_Name and another param p_MyNode of type PointNode. But that is a separate endeavor from this question