If needed, you can create a network programmatically. 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:
Demo model: Create Transporter Network by Code
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
To create a network programmatically, we create a custom function which returns the object of the type
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:
First, we create a
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
with the help of the the
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
block, since if there is an attractor in the node, the transporter will always go to it anyway.
We create a
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
and specify that it supports movement in both ways. To do this, we pass
argument of the
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. Note, that initialization must be the last step when you create network by code, since after this no changes can be done to the network.
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.