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.
2 Answers
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.

- 962
- 1
- 5
- 17
-
thanks a lot, can you just explain me where to put that CodeValue expression ? – Marco Dec 06 '19 at 11:46
-
In your simulation experiment, you add *variableA*. Put the code either in a button that the user can click to evaluate his expression in the textbox, or just add it in under *Java Actions*/*Before each experiment run* code box, so that it automatically gets executed at model start up. To learn how to pass a value from variable in the simulation experiment to the model parameters at startup, have a look at [this video](https://www.youtube.com/watch?v=pd8QMBL-ri4&feature=youtu.be). – Florian Dec 06 '19 at 12:58
-
It works but in this way I get a constant value for my parameter in the main (that depends on the type of distribution but it's still a static value during the run), on the contrary I'd like to get a stochastic delay time during the run – Marco Dec 06 '19 at 16:29
-
1In that case, you just forward the user input to a parameter of type String in the model. Create a function that uses the code above to create a new value from the text every time it is executed. In the fields such as delay time, just insert this function. – Florian Dec 09 '19 at 08:59
-
I am getting this error when using CodeValue: `Annotation processing got disabled, since it requires a 1.6 compliant JVM`, also once in a while I get: `error during model destruction: truncated class file`. Any ideas? @Florian – sdaza Dec 17 '21 at 10:36
This is a Java issue. You need to convert a String (your editbox content) to executable code. Not straight-forward but also not impossible, see Convert String to Code and similar posts.

- 10,603
- 3
- 16
- 28