simulationprobabilitydistributionanylogiceditbox

Anylogic: can you type a probability distribution into an edit box?


In the simulation window, before the run, I'd like to change some probability distributions (e.g. delay time) by typing, for example, "triangular(5, 20, 15)" into a specific editbox linked to a variable. I know how to do this with static values, but I couldn't figure out how to do the same with probability distributions.


Solution

  • AnyLogic offers a built-in functionality for that with com.anylogic.engine.database.CodeValue.

    It is originally meant that a distribution function stored as text in the internal database can be parsed to java code and executed, but it actually also works without the database and for any kind of code. It is the same idea as in Benjamin's answer, just that you do not need to add any external java library.

    Use it like this:

    CodeValue myCode = new CodeValue(this,"....java code to be executed");
    myCode.execute();
    

    And in your specific case, assuming you have a variable named variableA and an editbox named editbox, use the following to evaluate the expression, get a value and set it for the variable:

    CodeValue myCode = new CodeValue(this,"variableA = "+editbox.getText());
    myCode.execute();
    

    Obviously, allowing the user to type any command there and running it without a check or error treatment is a bad idea, be aware of that.