anylogic

Using enums as parameter types in AnyLogic


I am using a enum "DistributionShape" as a parameter type in AnyLogic, the enum is defined in Main. However, the model does not compile as in the auto-generated Simulation experiment class the type "DistributionShape" is not recognized.

This is the Additional class code of Main:

public enum DistributionShape {
    UNIFORM,
    TRIANGTULAR,
    NORMAL,
    EXPONENTIAL,
}

This is the parameter: enter image description here

This is where I am getting the compilation error in Simulation.java:

  @Override
  @AnyLogicInternalCodegenAPI
  public void setupRootParameters( final Main self, boolean callOnChangeActions ) {
    final Main root = self; // for compatibility
    DistributionShape parameter_xjal;
    parameter_xjal = self._parameter_DefaultValue_xjal();
    if (callOnChangeActions) {
      self.set_parameter( parameter_xjal );
    } else {
      self.parameter = parameter_xjal;
    }
  }

The error: "Description: DistributionShape cannot be resolved to a type. Location: Probability Distribution Shapes/Simulation - Simulation Experiment"

What am I doing wrong and what is a way around?


Solution

  • The enum is defined in your Main agent (in Main.java), and not on the experiment (in experiment.java).

    There are two ways I'd recommend to deal with it. The first option would be to use AnyLogic Option Lists. This would be ideal; as the Option Lists are basically public enum but they are automatically recognized and imported whenever needed.

    As a second option, you can just add a Java File on your project which contains your enum definitions. This way, when using that file AnyLogic will auto-import the correct file.

    I would recommend using Option Lists though, they were created probably for the exact use case you have!