Create rail yard by code

If needed, you can create a railway network programmatically. Please note that you can do it only on model startup, and the network cannot be modified later.

In this article we study the following model: 

Demo model: Create Rail Yard by Code

In this example we create a railway network that consists of three railway tracks interconnected via the 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 or rt3 track (the choice is regulated by the SelectOutput element of the model flowchart).

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 segments
MarkupSegmentLine ms1 = new MarkupSegmentLine( 50, 50, 0, 600, 50, 0);
MarkupSegmentLine ms2 = new MarkupSegmentLine( 600, 50, 0, 900, 50, 0);
MarkupSegmentLine ms3 = new MarkupSegmentLine( 600, 50, 0, 900, 150, 0);

// Create tracks
rt1 = new RailwayTrack(this, SHAPE_DRAW_2D3D, true,
                                   PATH_RAILROAD, black, 2, ms1);
rt2 = new RailwayTrack(this, SHAPE_DRAW_2D3D, true,
                                   PATH_RAILROAD, black, 2, ms2);

rt3 = new RailwayTrack(this, SHAPE_DRAW_2D3D, true,
                                   PATH_RAILROAD, black, 2, ms3);

// Create switch
RailwaySwitch rs = new RailwaySwitch( this, SHAPE_DRAW_2D3D, true,
                                   2, black, null, rt1, rt2, rt3 );

// Create position on track
pt = new PositionOnTrack( this, SHAPE_DRAW_2D3D, true,
                                   black, rt1, 200);

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

// Initialize network
rn.addAll( rt1, rt2, rt3, rs, pt );


//Add tracks/switch/position on track on presentation

Creating markup segments

First, we create three linear markup segments. The MarkupSegmentLine constructor accepts six arguments: X, Y, Z coordinates of the line's start point followed by the end point's coordinates.

Creating railway tracks

Next, we create three railway tracksRailwayTrack constructor takes the following arguments:

Creating railway switch

We create the railway switch using the RailwaySwitch constructor. The constructor arguments are quite the same, but you end with the list of the railway tracks connected by this switch (rt1, rt2, rt3).

Creating position on track

We create a Position on track in a similar way. In the PositionOnTrack constructor, provide the reference to the railway track where you put the element (rt1) and specify the offset from the track's start point (200) as the last two arguments.

Creating network

We now have all the elements that are needed to create a railway network. In the RailwayNetwork constructor, 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).

Initializing network

Next, 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.

Initialize the railway network by calling the initialize() function on the previously created rn network.

Adding elements into presentation group

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