AnyLogic
Expand
Font size

Create rail yard by code

If needed, you can create a railway network programmatically.

Creating the railway network with code is possible on model startup only. The network cannot be modified later.

In this article we study the following model:

Demo model: Create Rail Yard by Code Open the model page in AnyLogic Cloud. There you can run the model or download it (by clicking Model source files).

In this example we create a railway network that consists of four railway tracks interconnected via the all-to-all switch. The network includes the Position on track element which sets the initial position for trains. Trains enter the network on the rt1 track and then continue moving either along the rt2, rt3, or rt4 track. The choice can be regulated manually by the call of the switch’s toggle() function which is placed in the Action field of the button on the model’s presentation.

To create a rail yard 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 tracks
rt1 = new RailwayTrack();
rt1.startDrawing(50, 150, 0);
rt1.lineTo(450, 150, 0);

rt2 = new RailwayTrack();
rt2.startDrawing(450, 150, 0);
rt2.lineTo(900, 150, 0);

rt3 = new RailwayTrack();
rt3.startDrawing(450, 150, 0);
rt3.lineTo(900, 100, 0);

rt4 = new RailwayTrack();
rt4.startDrawing(450, 150, 0);
rt4.lineTo(900, 200, 0);

// Create switch
RailwaySwitch rs = new RailwaySwitch();
rs.setTracks(rt1, rt2, rt3, rt4);
rs.setAllToAllType();

// Create position on track
pt = new PositionOnTrack();
pt.setPointOnTrack(rt1, 200);

// Create network
RailwayNetwork rn = new RailwayNetwork( this, "myRailwayNetwork", SHAPE_DRAW_2D3D, 0 );

// Add tracks/switch/position on track to railway network
rn.addAll( rt1, rt2, rt3, rt4, pt, rs );

// Create and initialize level
Level level = new Level( this , "myLevel" , SHAPE_DRAW_2D3D, 0 );
level.add( rn );
level.initialize(); // No changes are possible after this!

// Add the level to the presentation group to make it visible at runtime
presentation.add( level );

Creating railway tracks

First, we create four railway tracks. After declaring each new railway track, we use the startDrawing() function to define the X-Y-Z coordinates of the track’s starting point and the lineTo() function to create a linear segment of the track. We pass the X-Y-Z coordinates of the track’s ending point as arguments of the lineTo() function.

Creating railway switch

We declare the railway switch and define the tracks that will be connected by this switch as arguments of the setTracks() function of the switch. To define the type of the switch, we use the setAllToAllType() function.

Creating position on track

We create a Position on track in a similar way. In the setPointOnTrack() function we provide the reference to the railway track where we put this element (rt1) and specify the offset from the track’s start point (200) as the function’s arguments.

Creating network

We now have all the elements that are needed to create a railway network. In the RailwayNetwork constructor, we provide the reference to the agent the network will be added to (this), the name of the network ("myRailwayNetwork"), drawing mode (2D, 3D or both), and the network’s Z-coordinate (which is zero in our case).

Adding elements to the network

Next, we add elements to the network by calling the network’s addAll() function and passing the elements that we created earlier (railway tracks, railway switch, and position on track) as its arguments.

Creating and initializing the level

We use the Level constructor to provide reference to the agent the level will be added to (this), the name of the level ("myLevel"), drawing mode (2D, 3D or both), and the level’s Z-coordinate.

Next, we add the previously created network to the level by calling the level’s add() function and passing the network as its argument.

We initialize the level by calling its initialize(). Remember, that after you initialize the level, you will not be able to make any further changes to the created rail yard.

Adding elements into presentation group

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 for the level.

How can we improve this article?