How to Ensure Your Model is Serializable

For 99% of models you do not have to do anything to enable their serialization, i.e. saving their state at runtime: all AnyLogic objects are serializable, including all standard library objects. However, there are things you should be aware of.

As you know, AnyLogic allows you to use arbitrary Java classes in your model, namely in:

If you wish all the objects to be saved and restored, you have to either use only serializable classes, or define custom serialization, or exclude them from the snapshot at all.

In the properties of Parameter, Variable and Collection you will find the checkbox Save in Snapshot that is checked by default. If you leave that checkbox checked AnyLogic will take care of serialization, but the type of parameter, variable or a collection element should then be one of:

By unchecking that checkbox you tell AnyLogic to exclude the corresponding object from standard serialization. You may then specify custom serialization for it by defining two functions in the agent's Additional class code:

Please note that if you are defining custom serialization for a field defined in the Additional class code (i.e. not graphically and for which there is no Save in snapshot checkbox that you can uncheck) you need to declare them as transient by adding this word before the field name.

The wizard for creating a new Java class within AnyLogic IDE now has an option Enable saving this class in model snapshots. If it is checked, the new class will be generated as implementing interface and contain the code line

private static final long serialVersionUID = <SOME_VALUE>;

where <SOME_VALUE> is a kind of class fingerprint which normally should be changed when class structure changes (e.g. when some fields are added to (removed from) the class). Please note that in the models created earlier the Java classes were not implementing that interface by default and you may need to add the corresponding code manually. Same applies to the inner classes if there are any.

The Java classes used as statechart transition messages MUST be serializable.

Also, there is one common recommendation for making advanced structures Serializable. All user-defined cross-object links except links to the embedded objects must have custom serialization (and Save in snapshot checkbox should be unchecked for the corresponding model elements). An example of one-level-links is field “next” of some object, which stores reference to the object of the same class and thus also having field “next” with reference to third object and so on. Link to owner is link from one object located inside another object, to its parent.

Some classes require additional custom deserialization code for their instances, if any are created manually (i.e. in the code) and stored in the serializable fields (e.g. in collections or variables) of model objects. These classes are:

Note, that custom deserialization code is automatically generated for all elements added from the Palette in the AnyLogic graphical editor.

Java Classes which need custom serialization, i.e. require special handling during the serialization and deserialization process must implement special functions with these exact signatures:

    private void writeObject( out)

        throws IOException

    private void readObject( in)

        throws IOException, ClassNotFoundException;

The following description is borrowed from API JavaDoc of standard Java interface