Create network by code

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.

Demo model: Create Network by Code

In this example we create a network that consists of a point node and a rectangular node, which are connected by a path. Agents enter the network in the point node. They then move along the path to the to the rectangular node. Inside the rectangular node, agents move to the attractor.

To create a network programmatically, provide Java code in the On startup action of the Main agent (Agent actions properties section). In our demo model the code is the following:

// Create an attractor
Attractor a = new Attractor( 25.0, 25.0, 4.7 );

// Create a rectangular node
rn = new RectangularNode( this, SHAPE_DRAW_2D3D, true,
                               300.0, 250.0, 0.0, 100.0, 90.0, 0.0,

                                null, dodgerBlue, 2.0, LINE_STYLE_DASHED,
                                POSITION_CHOICE_BY_ATTRACTORS, a );


// Create point nodes
pn = new PointNode(this, SHAPE_DRAW_2D3D, true, 50.0, 200.0, 0.0, 1.0, dodgerBlue );

// Create segments
MarkupSegmentLine ms1 = new MarkupSegmentLine(  50.0, 200.0, 0.0, 350.0, 200.0, 0.0 );
MarkupSegmentLine ms2 = new MarkupSegmentLine( 350.0, 200.0, 0.0, 350.0, 250.0, 0.0 );

// Create paths
Path path = new Path( this, SHAPE_DRAW_2D3D, truetrue,
                                PATH_DASHEDLINE, dodgerBlue, 2.0, pn, rn, ms1, ms2 );


// Create a network
Network n = new Network( this, "myNetwork", SHAPE_DRAW_2D3D, 0, truetrue, pn, rn, path );

// Add point nodes/paths to presentation
presentation.add(pn);
presentation.add(rn);
presentation.add(path);

Creating an attractor

First, we create an attractor with the help of the the Attractor constructor and provide the attractor's X-Y coordinates and orientation angle (in radians) as arguments.

Creating a rectangular node

Next, we create a rectangular node by providing the following arguments to the RectangularNode constructor: 

Creating point nodes

Now we create two point nodes. We use the PointNode constructor and provide the coordinates of the point node, the point's radius and the point color as its arguments.

Creating markup segments

Next, we create two linear markup segments, which we will later combine into a single two-line path. The MarkupSegmentLine constructor accepts six arguments: X, Y, Z coordinates of the line's start point followed by the end point's coordinates. 

Creating paths

To create a path, we use the Path constructor. For each path we do the following:

Creating network

We now have all the elements that are needed to create a network. In the Network constructor, we provide the reference to the agent the network will be added to (this), the name of the network ("myNetwork"), specify drawing mode (2D, 3D or both) and the network's Z-coordinate (which is zero in our case). We pass true value for isPublic and visible arguments and provide the references to nodes and paths that we created earlier (pn, pn1, rn, path1, path2):

Network n = new Network( this, "myNetwork", SHAPE_DRAW_2D3D, 0, truetruepn, pn1, rn, path1, path2 );

Adding elements into presentation group

Finally, we add the created nodes and paths into the presentation group to make them visible at runtime. To do this, we call the add() function of the presentation group for each network element.