If needed, you can create a network from code. Please note that you can do it only on model startup, and the network cannot be modified later.
In this article we look at the code example of the following model:
We create a network that consists of a point node and a rectangular node, which are connected by a path. The path consists of two segments that create a straight angle. Transporters enter the network in the point node. They then move along the path to the attractor inside the rectangular node.
To create a network from code, we create a custom function which returns the object of the type Level. In the body of the function we provide Java code which builds a network, places it inside a level and initializes the level. The code is the following:
// Create a rectangular node with attractor inside rn = new RectangularNode(); rn.setPos(300.0, 350.0, 0.0); rn.setSize(100.0, 90.0); rn.addAttractor( new Attractor(25.0, 25.0, 4.7)); // Create a point node pn = new PointNode(); pn.setRadius(5); pn.setLineColor(dodgerBlue); pn.setPos(50.0, 300.0); // Create a path between nodes Path path = new Path( ); path.setBidirectional(true); path.addSegment(new MarkupSegmentLine(50.0, 300.0, 0.0, 350.0, 300.0, 0.0)); path.addSegment(new MarkupSegmentLine(350.0, 300.0, 0.0, 350.0, 350.0, 0.0)); path.setTarget(rn); path.setSource(pn); // Create a network with the path and the nodes n = new Network( this , "MyNetwork"); n.addAll(rn, pn, path) ; // Create a level with the network and initialize the level Level level = new Level(this, "MyLevel", SHAPE_DRAW_2D3D, 0); level.add(n); level.initialize(); // Cannot be changed after initialization! return level;
First, we create a rectangular node and define its position and size. The position is defined by the X-Y-Z coordinates of the upper left corner of the node. The size is defined by the width and height of the node in pixels.
Next, we add an attractor with the help of the the addAttractor()function of the node where we provide the attractor’s X-Y coordinates and orientation angle (in radians) as arguments. We Don’t need to specify this exact attractor as transporter’s destination in the MoveByTransporter block, since if there is an attractor in the node, the transporter will always go to it anyway.
We create a point node and define its radius, line color, and position. The position is defined by the X-Y coordinates of the point node’s center.
Now we create a path and specify that it supports movement in both ways. To do this, we pass true as the bidirectional argument of the setBidirectional() function of the path.
The path consists of two segments. We add them in the order of their placement from the starting point of the path. Each path segment is created via the MarkupSegmentLine constructor passed as an argument of the addSegment() function. This constructor accepts six arguments: X, Y, Z coordinates of the line’s start point followed by the end point’s coordinates.
Next, we provide the links to the source and target nodes: setTarget(rn) and setSource(pn).
We now have all the elements that are needed to create a network. In the Network constructor, we provide the reference to the agent where this network will be placed (this) and the name of the network ("myNetwork").
After the network is created, we add nodes and paths that we created earlier to it by providing them as arguments of the network’s add() function:
n.addAll( rn, pn, path );
The last object we need to create is the level where the network will exist. We pass the agent where the level will be added (this) and the name of the level, and specify drawing mode (2D, 3D or both) and the network’s Z-coordinate (which is zero in our case):
Level level = new Level(this, "MyLevel", SHAPE_DRAW_2D3D, 0);
Next, we add the network to the level and initialize the level itself.
Next, we specify that the function returns the created level.
Finally, we add the created level to the presentation group to make it visible at runtime. To do this, we call the add() function of the presentation group in the On startup action of the Main agent (Agent actions properties section) and pass the myLevel variable as the argument.
How can we improve this article?