External Java libraries in AnyLogic

One of the greatest advantages of AnyLogic is its extendability. Because AnyLogic itself is developed in Java, integrating any other Java package is effortless. In this article I'll show how this is done at the example JUNG, a Java package to work with graph networks (for example network route finding).

What you need

After you found a package that meets your technical requirements, you need to prepare yourself to work with it. Find out what the package name prefix is, something like "com.organisation.package". This can be found in the Java API doc, which is basic HTML style documentation of the classes and fields a package contains. Try to get tutorials, examples and guides on the package.

For this article I choose the package JUNG, which is very well documented:

How to integrate into AnyLogic

  1. Download the package
  2. Find the JARs that you need, in this case these four:

    • collections-generic.jar
    • jung-algorithms.jar
    • jung-api.jar
    • jung-graph-impl.jar
  3. Reference those JAR files in the "Dependencies" tab of your project properties in AnyLogic

Dependencies

  1. In your Main properties, under "Advanced Java", import the package name prefix into your class: import edu.uci.ics.jung.*;. This step is not obligatory, but if you don't do it, in your code you will always have to reference the full package path of every library item you want to access. Example: With the import you can write anywhere in your project Graph<INode,Path> and it will work, without the import you would have to always write edu.uci.ics.jung.Graph<INode,Path>.

Imports

How to work with the package

Since you loaded the whole package name prefix, you can now access all classes of the package as if they were directly part of AnyLogic. In the package there is a class called graph. I can set an AnyLogic variable to the type Graph<INode,Path> and give it the initial value new SparseMultigraph<INode,Path>().

Variable with Graph class

I just created an empty graph using a normal AnyLogic variable, without any additional code. That's the beauty of integrating external Java packages.

Conclusion

It is easy and can be very powerful to use external Java libraries in AnyLogic.

Another thing to consider: If you have projects with lots of algorithms and data structures, why not develop them outside of AnyLogic, in a standard Java development environment like Eclipse? This way debugging and unit testing can be much more intensive then it is possible in AnyLogic and you can reuse this components easily in other Java or AnyLogic projects. You can pack your code into a jar and import it in AnyLogic like any other external Java package.