Font size

Phase 3. Creating rail car types

As you might have seen while running your current model, we had numerous rail cars differed by colors only. Phase 3 will be devoted to defining different types of the rail cars.

We provide you with the handy Rail Car Type creation Wizard. Simply specify the name of the new rail car type and choose the animation from the list of ready-to-use 3D objects.

Create new rail car types

  1. Create new rail car type by dragging  Rail Car Type element from the  Rail Library palette on the  Main diagram.

  2. Rail Car Type wizard window will open. Specify Agent type name as Locomotive and leave the Create the agent type "from scratch" selected. Press Next.

  3. In the next step choose Locomotive 14.1m as its 3D animation shape.

  4. Add 5 more rail car types in the same way: BoxCar, OpenCar, GondolaCar, TankCar, and HopperCar. Set the corresponding 3D shapes for them.
  5. Similarly, create Train type (by dragging the  Train Type element from the Rail Library palette) and name it Train.

Modify TrainSource block

We will set the trainSource block to generate trains of our custom type  Train by modifying its parameters in the Trains and cars section.

  1. New train:  Train

  2. In the same Trains and cars section, specify dynamic expression in the New rail car field.
    Switch to the code field by clicking on the icon which will turn into allowing you to insert the code in the field right next to it. Type the expression as shown in the figure below.

    When typing Java code, use Code completion master, accessible by clicking Ctrl + space (macOS: Alt + space) in the code field. It provides you with the list of available functions and model elements, so you can simply choose the required item from the list and avoid mistypes when referring to it.
    We use conditional operator to set the railcar type for the rail cars of the train being created. We want to diversify railcars of our freight train. The first railcar should be of type  Locomotive. Successive railcars can be of any other railcar type.
    When TrainSource creates a new train, it successively calls the expression specified in the TrainSource field for every railcar of the train. Using the local variable carindex we pass the index of the currently created railcar to the expression.
    The conditional operator allows you to choose between two given alternatives depending on the value of a Boolean expression. In our example, if the carindex == 0 expression evaluates to true, then the current railcar is the first car of the train and we set its type to Locomotive. Otherwise, the expression evaluates to false and for the successive railcars we call the randomlyCreate() function and provide the classes of the railcar types to randomly choose the types for other railcars in the train.
    As a result, we will have a train with a loco as the first car, followed by ten rail cars of our custom rail car types.

Watch the model in 3D mode

  1. Add  3D Window from the  Presentation palette, place it below your flowchart and animation.

  2. Run the model.
    When you create a 3D window, AnyLogic adds a view area that allows you to easily navigate to the 3D view at runtime. To switch to this 3D view while the model is running, open the developer panel by clicking the Developer panel  control in the right corner of the control panel. In the developer panel, expand the  TrainSource list and select select view area to navigate from the list.

    The view area expands the 3D animation scene to the model window’s full size.
  3. Now you can see the train consisting of a locomotive as the first rail car, followed by ten rail cars of other types we have defined in this phase.

Demo model: Hump Yard - Phase 3 Open the model page in AnyLogic Cloud. There you can run the model or download it (by clicking Model source files).
How can we improve this article?