question

Jon Abbott avatar image
0 Likes"
Jon Abbott asked Jon Abbott edited

What is the best way to make an OnResourceAvailable trigger fire only once?

I'm trying to create an OnResourceAvailable trigger for an Operator that runs once and then deletes itself so that it won't run again. Please see the attached model as a simplified example. On model reset, it adds an OnResourceAvailable trigger to Operator1, and that runs at time 30 when the Operator becomes available. The code then deletes the trigger using setnodestr. What seems to happen after time 30 is that it keeps running the code whenever the operator is available again (time 120, 210, etc. based on TimeTable1), even though the OnResourceAvailable trigger is now blank. This can be seen by stopping the model after the message box pops up at time 120 and looking at the triggers tab under the operator's properties. In my full model, the trigger is being added dynamically to an operator based on certain conditions, and I would like for it to run only once. I'm looking for a solution that does not use Process Flow if possible. Thanks for any help you can provide.

FlexSim 16.2.1
flexscriptoperatortriggersonresourceavailable
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Matt Long avatar image
2 Likes"
Matt Long answered Jon Abbott edited

If you want your On Resource Available to fire just once, your best option would be to add a label to your Operator that has a default value of 0. Then in your trigger you'd say:

if (!getlabel(current, "triggerFired")) {
	//Do stuff
	setlabel(current, "triggerFired", 1);
}
· 2
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Joerg Vogel avatar image Joerg Vogel commented ·

much easier is it to set the default value to 1 and change the value in the if-statement to 0.

if (getlabel(current, "triggerFired")) {
	//Do stuff
	setlabel(current, "triggerFired", 0);}
0 Likes 0 ·
Jon Abbott avatar image Jon Abbott commented ·

Thanks @Matt Long. For some reason I kept thinking I needed to delete the trigger, but I didn't need to. I tried your approach with dynamically overwriting the trigger with new code and it seems to work properly. See the attached model for an example. It adds code to the trigger at model reset, and it runs correctly at time 30, then at time 50, triggerFired is reset to 0 and there is new code overwritten to the trigger. It runs correctly at time 120, and it sets triggerFired to 2 (just to demonstrate that it is running rather than the code that was set at model reset), and it doesn't trigger again in the future.

0 Likes 0 ·
Mischa Spelt avatar image
1 Like"
Mischa Spelt answered Jon Abbott edited

Hi Jon,

I don't know why the trigger code keeps firing despite the fact that you rebuilt the node FlexScript. However, untoggling the node seems to help: if I add the line

switch_flexscript(OperatorORATrigger, 0);

in the DelOpTrigger code and of course re-enable it with

switch_flexscript(OperatorORATrigger, 1);

in the AddOpTrigger you seem to get the desired behavior.

I also tried untoggling the node, then setting the new content, then re-toggling and building it, but it seems like this cannot be done from the same function (probably if you would use a senddelayedmessage to re-toggle it, you could get it to work).

The solution will therefore probably not work if you want to change the code, but if you just want to disable it, this workaround may help you.

· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Jon Abbott avatar image Jon Abbott commented ·

Thanks @Mischa Spelt. I tried this and it does help, although if more code is later added to the Operator's OnResourceAvailable trigger, it still runs the previous code. I need the ability to add new code later that also runs once when the Operator becomes available. It appears that the Operator's trigger code in the running simulation can be in a different state than what is seen in the tree and in the Operator's Properties dialog. @Jörg Vogel, is this related to what you previously mentioned, "There are several problems creating an executable source code during a simulation run"?

0 Likes 0 ·
Joerg Vogel avatar image
0 Likes"
Joerg Vogel answered Jon Abbott commented

I would write source code inside a node and use the command "nodefunction" to execute this node. In the resource available trigger I would check only a node or a label used as a flag to see, if some code or nodefunction should be executed at all. This way I don't have to tweak or bend the flexsim engine structure to achieve using variable source code.

· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Jon Abbott avatar image Jon Abbott commented ·

Thanks @Jörg Vogel. I tried this approach but the node would not execute correctly, even after being set with switch_flexscript. I also ran switch_flexscript in query mode to see if the node was being set as FlexScript, and it wasn't, so there's a chance I was not referencing the node with the right syntax. Matt's answer appears to work so I will try and use that. Thanks again.

0 Likes 0 ·