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

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

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.

Related

LabVIEW - can you use a numeric control as an indicator

I've written LabView code for a locking system.
The lock has a motion timer that relies on input from a numeric control. I've added a script file reader that needs to be able to change that timer value. Using a selector, I can switch between values, but I'd like it to update the value in the control, rather than override it, so that I can see it on the screen.
How can this be accomplished?
This is currently how I switch between the scripted version and the direct numeric input from the control:
So how can I get the script value to update the control box or is that not possible...?
Do you mean something like this? I created a little vi to demonstrate how the control is updated.
In most cases "property nodes" are the way to go. Every control has a lot of different options to chose from and usually if you look through the properties you will find what you're looking for :)
A little hint:
If you want to add "code" to your question so that other users can test it, you can create a .png file. To do this, you need to select the parts of the vi that you want to share, and click on "Edit > Create VI Snippet from Selection". Then you save that generated .png and upload it here as a picture. Then others can drag&drop it into their block diagram.
Important: Check the .png before uploading and make sure that you're not accidentally posting sensitive data of your company.

Is there a method for script file loading control in labview?

first, I cannot attach my vi files, sorry. I'm not allowed, but I can attach snippets.
I've got a vi that opens and executes functions from a script file, and I'd like to be able to continuously click a button to reload the script file without having to restart the program. Currently the script file commands sit outside my main while loop and and uses a case statement to put the system in idle mode (manual control) when the button is not depressed before launching the program, or if it is, it will instantly open a dialog box looking for a script file upon program launch. I'd like to be able to open a script file numerous times during the execution of my program, but don't fully understand how, and this may be my own misunderstanding of what's going on with the code if I move it inside the main while loop. how is this best accomplished?
If you put your code outside of the loop, it is only executed once (very important: "dataflow"). You need to put the code into a loop to execute it multiple times.
You can insert the vi-snippet into your vi by drag&drop.
My vi contains two different options. You can change the vi as you need it, my vi is incomplete. I inserted a simple 2D-Array because I'm not sure if the vi you use after building the path is selfmade or given by LabVIEW.
For both options you should let the code run in some kind of state machine and use an Event Structure (I think you already implemented your program this way since you wrote about a main loop).
Version 1:
Everytime you click the button, the event is triggered and the code inside the event structure is executed.
Version 2:
Here you set a boolean if the button is pressed and handle the event with that value.
Since you wrote that you already have a main while loop, this option might be better four you. The first loop would be your main-loop, second one would be the loop in Version 2. You just need to add another case for the script to be loaded in.
VI:
I hope this is helpful for your problem.
Feel free to ask if you need more help or if you have any questions :)

Calling another VI at runtime

I have created two vi's in LabVIEW: one to acquire serial data and another to plot the acquired data on an XY graph.
The second VI gets called when a Value Change event occurs on a button in the first VI. But the problem is that when the second VI is called the first VI suspends its operation, hence the values don't get updated.
Is there any solution for this?
First VI block diagram:
First VI front panel:
Second VI (ALL DATA) block diagram:
Well, you are doing some nasty stuff with global variables. This works but is not considered as good practice. (Have a look at queues and notifiers). Further, I don't see how your data gets written to those variables...
In any case, put your 2nd VI in a separate while-loop and schedule it to about 100ms (that is usually enough to update front panels or to interact with users. I'm not sure if your button-event is the right way to go. That is exactly because, the second VI waits for the callback. Just use a simple button and a true-false case to let the second VI keep running (this should even be the solution if you don't want to move the case to a second VI). Just make sure that you change the mechanics of the button to be a switch because you're checking its value not at infinite speed and you want to ensure that it gets caught every time, you click it;)
You will need to use the VI Server functionality. The exact method has changed over the years, but I believe the current recommended implementation is to use 'Start Asynchronous Call'
There is an example that you can view using the example finder. To open the example finder navigate to Help>Find Examples. Then select the 'Search' tab and search for 'asynchronous'. Finally select the VI called 'Asynchronous Call and Forget.vi'
There are other variations for asynchronous implementations, but this is probably a good place to start.

Block a drag and drop operation

In a vb.net , how can I block a drag and drop operation , if these condition are true :
1) If the user drag a folder
2) If the user drag a file that is not an Excel file or Word file
3) If the user drag more than 1 file
Thank you !
Alex,
Your trying to accomplish much in a routine that requires very little code. The Drag event is supposed to return very quickly so that the UI can continue rendering and the UI remains responsive. For you to burden it with complicated file checking stuff would hider its responsiveness and introduce a very bad situation.
In the case of your UI crashing, that is supposed to be handled by you via error handling. The fact that your UI becomes inoperative means your not handling the situation properly.
The best approach is to constrain the user via file extension, a single file, not a folder and file size (meaning file size is less than 4MB for example). Then your code should assume the file is valid. Then pass the file to Excel (or what every the next process is), and let that process throw an exception. Then handle that exception and present a meaningful error information to the user like: "The file is not a valid Excel spreadsheet.".
Please understand that we are trying to help you implement a best practice and not a preferred practice; which is most cases makes your product unusable or unstable.

vim: discard pending keyboard input

Background:
I'm writing a fuzzy-finding vim plugin which runs a graphical program as a separate process. It opens a window, you type into it, press return, and then the vim plugin processes its output and navigates to the file / buffer you selected.
The time between triggering the find function and the new process grabbing keyboard input is a fraction of a second, so it's possible to accidentally type too soon, which causes that input to be delivered to vim.
Given that vim is single-threaded, I know (and have verified) that the errant keyboard input is not actually processed until the program finishes - the input is buffered while the program runs, and afterwards the random keys I typed are interpreted as vim actions.
Question:
How can I discard input that's sitting in vim's input buffer but has not yet been processed?
inputsave() takes pending keyboard input and saves it onto a stack. This would be what I want, except that it causes a memory leak if I don't match it with a call to inputrestore(). Is it possible to discard the stored input without executing it?
If that's not directly possible, can anyone think of a good way to discharge those events into a safe place? i.e call inputrestore() in such a state that the stored input will have no visible effect.
I need to do this in both a normal-mode mapping, and a command-mode mapping. So I'd prefer a solution which doesn't abandon the current command-mode input.