Warm tip: This article is reproduced from stackoverflow.com, please click
anylogic

Iterating through objects in project tree

发布于 2020-04-21 10:54:08

As the title says: I wonder, whether it's possible to iterate through the object tree of an AnyLogic project.

Probably it would help, if I provide more information what I want to do. Take the following project tree:

project tree

I want to check for every delay object in my model, whether or not it currently contains agents. One possibility would be to add all delay objects manually to a collection object and to iterate through the elements of the collection. However, this approach would result in a lot of manual work, as I require to perform similar requests for other object types. In my opinion, the most convenient solution is to iterate through the project tree and to identify with 'isinstanceof' the objects I want to analyze.

Does anyone know a solution or another approach, which requires similar less effort?

Best regards,

Sebastian

Questioner
Slango
Viewed
41
Benjamin 2020-02-06 16:32

You can loop through all objects in Main doing this:

for (Object currObject : ((Agent)getRootAgent()).getEmbeddedObjects()) { if (currObject instanceof Delay) { // you found a Delay object } }