Search code examples
while-loopnetlogofile-existspause

NetLogo : How to make the model pause as it waits for a file


I am building a NetLogo model that needs to integrate with another model (not NetLogo). So if the NetLogo model is set up to run continuously, at each tick I want it to wait for a file created by the linked model to appear within the parent folder. Once this file appears, NetLogo reads it in and continues with all the steps specified in Go. So essentially, i would like NetLogo to pause while it waits for the file and then once the file appears, resume. I tried using the 'while' function, but it did not do the job.

For example, when I set it up this way

to go

while [file-exists? "biosim.asc"][

grow-grass
grow-herd
delete-biosim
tick]

Here, NetLogo reads in the biosim file and after the procedures grow-grass, grow-herd are completed, the program deletes the ascii file biosim. Ideally I would like it to then wait for a new file by the name biosim to appear in the working folder and then repeat the steps. The above code does not get me what I want. Any help would be appreciated.

Thanks, Rekha


Solution

  • What happens if you instead use the while loop to hold the run when the file is not there, and then put the actions outside the loop. For example:

    to go
      while [not file-exists? "biosim.asc"] [wait 1]
      grow-grass
      grow-herd
      delete-biosim
      tick
    end
    

    You could probably simply have [] instead of the wait but I suspect that would be fairly inefficient as it would constantly check. This instead checks every 1 second (you can make the check shorter or longer of course depending on how often the file is created) and once the file is there, it moves on to the other code.

    Another option depends on what language the other model is built in. If it's something like R of java, you can control the NetLogo model from that code and couple the models directly.