top of page
Writer's pictureVitor Lemos

Using Predicates in AnyLogic

AnyLogic has a set of out-of-the-box functions that make our lives so much easier. If you go into the help section and look up UtilitiesCollection you will be presented with many different and extremely useful static methods in that class. For instance:

  • findAll

  • findFirst

  • filter

  • min

  • ...

Let's take a closer look at findAll, which given any collection will return another collection of which elements match a given condition. Looking at the method signature, we find that the second argument to the method is Predicate:



Looking at the official java documentation for Predicates we find:


"This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference."

If you know what a functional interface is, everything makes sense, and you can go on your way.

P.S. If you don't we will be releasing an Advanced Java for AnyLogic course soon, so watch this space...


The Predicate interface is a very simple interface. It has a method, boolean test(T t), without default behavior, so we have to implement our own when creating a predicate.


Example

Imagine we have a collection or population of Agent. We can create a predicate that takes an agent and checks any condition, returning a boolean as a result.


We can write this in two different ways. The first is by using an anonymous class, where we can create an instance of this class that implements the desired behavior:

The class above will simply return true when the test method is called on an agent if the agent ID is equal to 1.


Another way to write this is to use a lambda expression:

Lambda expressions are cleaner but in my opinion, harder to understand at first.


Since we stored this predicate in a variable we can use it as we please. For instance, in a findAll or a findFirst function:

This is clean and simple. You could have done this with your own custom code, a function to replace the findAll, and another to replace the findFirst, with a fixed set of conditions checks.


With predicates, you can create a function that accepts one as its argument and make the code generic. Check here an example with a custom implementation of findAll for Agent:

The principles of Predicate, a functional interface, apply to many other methods part of the CollectionUtiltilies. Take your time exploring it and your life coding up AnyLogic models will be much easier 😎🌴


Vitor Lemos


You can download an example model below or from the AnyLogic Cloud here




What next?

If you liked this post, you are welcome to read more posts by following the links above to similar posts. Remember to subscribe, so that you can get notifications via email or you can also join the mobile app here! Or follow us on any of the social media accounts for future updates. The links are in the Menu bar at the top or the footer at the bottom.


If you want to contact us for some advice, maybe a potential partnership or project or just to say "Hi!", feel free to get in touch here, and we will get back to you soon!




569 views0 comments

Commenti


bottom of page