Search code examples
anylogic

How to log frequently in a nice way in AnyLogic?


In my model I need to log some activities manually. Initially I used the QueryDSL insert query shown in the help, but sometimes two logs happen about the same time. Because of this, eventually the model execution stopped and after some investigation it turned out that the internal AnyLogic database got into a deadlock.

After this I tried to log the rows directly into a .csv file, but I also got an error because the simulation tried to open the file when it wasn't closed yet.

My solution right now is creating a Java class that can represent the information and when I need to log the activity, I create an instance of this class and add it to a LinkedList in the model. I have a cyclic event which iterates through the list and writes the contents into a .csv file, then clears the list. This solved the problem, but I don't think this is an optimal solution. Is there a better way to handle this issue?


Solution

  • Using a Java class is, in fact, the optimal solution.

    I'd normally not write it out until the end of the model run, unless your logs are so huge that you reach the limit of your machine's RAM. But then, the logs would be uselessly big, imo :)

    Make sure you only use primitive fields such as int, double and String. Avoid storing agents directly. (Not because that would eat memory, it would not, but because the agents themselves might not exist at the end of the model run)

    So use slim Java class instances and write them in one go into a csv at the end of the run. Done :)