question

Caelyn R avatar image
0 Likes"
Caelyn R asked Caelyn R commented

Custom Code Release Token When It Fulfills Condition

I have a Process Flow model where I am attempting to use a Custom Code activity with the Release Token option to hold tokens at that Custom Code until/unless a certain condition is met. The actual model I'm working with is very large and contains proprietary information, so I created and attached an example model to demonstrate what I'm trying to do.

In this example model, I only want tokens to proceed to the next activity (the Delay) if their label num is equal to 1. The code I have inside the Custom Code activity is as follows:


1727889305498.png


Since I have the releasetoken function inside an if statement checking the value of num, tokens with num equal to 2 or 3 should not be able to proceed past the Custom Code activity, but when I run the model I find that all tokens proceed to the Delay, regardless of the value of num. How can I fix this so that only tokens that fulfill my condition are allowed to proceed from the Custom Code activity, and all others are held there? In my real model, the condition I'm checking is not related to a token label and will eventually be fulfilled for all tokens, so eventually all tokens will be able to/need to leave the Custom Code activity, but I need them to stay there until that condition is fulfilled by other operations in the model.

custom code release token example model.fsm

FlexSim 24.0.3
custom code
5 |100000

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

1 Answer

Regan Blackett avatar image
0 Likes"
Regan Blackett answered Caelyn R commented

Hi @Caelyn R ,

The natural behavior of activities is to release its tokens thru a connector if present, so the tokens at your Custom Code are being released because of its connector. Using the releasetoken() function doesn’t imply that the token is stopped first, which it sounds like is what you are expecting.

If the goal is to only allow tokens with that 'num' label equal to one move on, I would use a Decide activity instead. That way you can send the tokens forward if their label value equals one, and everything else you could send to connector that the decide activity doesn’t have like 99. If the connector doesn’t exist the Decide will hold those tokens indefinitely.

If you are also wanting to release those held tokens later, if somehow their label value changes there are some other things to consider. The logic of an activity is evaluated once after the token arrives. So even if you didn’t have a connector off of the Custom Code changing the label value after it arrived wouldn’t trigger the release. Typically releasetoken() and other options similar to it (Release Token activity, Stop Token, Resume Token in the Custom Code options) are to have the token at the activity tell a different token to be released.

In that case you might instead create a different token when the release signal should be sent and have that token and your “real” token come together at a Join activity. That way if the condition is met before the real token gets there they are instantly joined and the process continues, but if the signal isn’t sent yet, the token will wait for its partner to arrive and then get joined.

You may also want to investigate using Wait for Event to listen for when a label value changes which would release the token from the Wait for Event when the desired value is reached. Your label would need to be set as a tracked variable to have the proper events to listen to in that case.

custom-code-release-token-example-model_decide instead.fsm


· 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.

Caelyn R avatar image Caelyn R commented ·
Hi @Regan Blackett,

I just wanted to let you know that I called into the help line and Logan was able to help me; we ended up solving my problem by using a Wait for Event to monitor the activity that deletes rows from the global table.

Thanks!

1 Like 1 ·
Caelyn R avatar image Caelyn R commented ·

Hi @Regan Blackett,

Thanks so much for the answer, and I didn't realize you could use a Decide activity to hold tokens like that.

I've implemented that strategy in my model and I'm a lot closer to where I want to be, but now the problem is that, like you mentioned, the logic for the Decide only gets evaluated when the tokens first enter, so any tokens that don't fulfill that logic upon first entry are never allowed to leave. However, I do eventually need them to leave, once they do meet the Decide's condition, so is there some way to get the Decide logic to be re-evaluated upon a change elsewhere in the model?

For context, the conditions in my Decide are looking at a label on the token and the values in a global table that is populated elsewhere with values from that token label, and I want the token to only be able to leave once the table contains at/under a certain amount of values. As such, I'm wondering if it's possible to monitor the number of rows in that table (like with a Custom Code or a Wait for Event) and then trigger a re-evaluation of the Decide once the number of rows drops to where I want it to be.

Below is the code I have in my Decide activity:

1727893812733.png

0 Likes 0 ·
1727893812733.png (14.1 KiB)