In general, the variables which comprise the Configuration, Observation, and Action need to be types that are JSON-serializable (for a list, see the w3schools article on JSON Data Types).
For Bonsai specifically, it must follow Inkling’s supported data types. In essence, this means that variables can be one of three classifications: numeric (int, double), fixed-sized arrays (e.g., int[], double[][]), or classes (objects comprised of any combination of these three classifications).
To make Java classes JSON-serializable – specifically with the Jackson library – there are two simple requirements:
-
You must define an empty constructor. Others may be defined as well, however Jackson will need the empty option.
-
Define your desired fields as class variables and set their visibility to be public.
While AnyLogic agent types are technically Java classes, they are not JSON-serializable on their own. To resolve this, you can create a “wrapper” class, taking the agent instance as input. You can then fill out the class fields based on the agent’s properties in the constructor (shown in the example below).
/**
JSON-serializable class for the Person agent type
**/
public class PersonWrapper {
// declare data types and set some default values
public double age = 0.0;
public int is_male = 0;
public int fav_color_composite = 0;
public PersonWrapper() {}
public PersonWrapper(Person person) {
age = person.age;
is_male = person.isMale ? 1 : 0;
fav_color_composite = person.favoriteColor.getRGB();
}
}