How to Stop and Start a Source based on a Global Variable

All topics on coding 4Dscript in Enterprise Dynamics.
Post Reply
CyrusMMXI
Posts: 9
Joined: Friday 04 November, 2011 - 11:51

How to Stop and Start a Source based on a Global Variable

Post by CyrusMMXI »

I want to stop or start a Source from releasing Products to the model based on the value of a Global variable. Seemingly it is possible to stop the Source by putting an IF statement in the SENDTO but then the problem is how to start it again.
Can you please conduct me?
MarvinH
Posts: 93
Joined: Tuesday 25 January, 2011 - 11:07
Contact:

Re: How to Stop and Start a Source based on a Global Variabl

Post by MarvinH »

Hello!

There are several ways to implement your desired functionality. My first 2 suggestions are as follows:

- When you change the value of the global variable (for example on entry of some server), use CloseOutput or OpenOutput on the source. For example, in case the global variable is increased OnEntry of Server1, then close the output the source by the following statement:

Code: Select all

If(
  { * Use your global variable here * }
  GlobalVariable > 10,
  { * Close the output of the Source * }
  CloseOutput( In(1, c) )
)
Now, OnExit of the Server the global variable is decreased, so you can put the following statement OnExit:

Code: Select all

If(
  { * Use your global variable here * }
  GlobalVariable < 8,
  { * Close the output of the Source * }
  OpenOutput( In(1, c) )
)
This code assumes that the source is connected to the first input channel (i.e. in(1, c)) of the atom on which the code is executed;

- Another option is to use the SendTo. Connect the second output channel of the source to another atom, in which the "lost" products are stored. Now in the SendTo, use some code like:

Code: Select all

If(
  { * Use your global variable here * }
  GlobalVariable < 8,
  { * Send into the system * }
  1,
  { * Send to "lost" sink * }
  2
)
When a product is created and the value of the global variable satifies some kind of condition, the product is sent into the model, otherwise it is "lost".

Please check which solution suits your model best, or try to find a another solution.
CyrusMMXI
Posts: 9
Joined: Friday 04 November, 2011 - 11:51

Re: How to Stop and Start a Source based on a Global Variabl

Post by CyrusMMXI »

Thanks Marvin! In my case I am trying to switch on/off a Source based on the Content of a Queue, but the Queue is not directly connected to the Source. To implement your 1st suggested approach, I was thinking to use an intermediary atom which listens both to the Queue and Source and they are both are connected through their Central Channels to the Input Channels 1 & 2 of the intermediary atom. Which sort of atom fits as the intermediary atom best? What is your suggestion? According to Tutorial ED 7, Page 19 the Central Channel is designed to "receive but not send" the information, however when we use a Monitor, it should be connected to the Central channel of the under concern atom! It doesn't seem that Monitor sends information to the atom it is connected but receives information from it. What is your suggestion here?
MarvinH
Posts: 93
Joined: Tuesday 25 January, 2011 - 11:07
Contact:

Re: How to Stop and Start a Source based on a Global Variabl

Post by MarvinH »

Hello!
CyrusMMXI wrote:In my case I am trying to switch on/off a Source based on the Content of a Queue, but the Queue is not directly connected to the Source. To implement your 1st suggested approach, I was thinking to use an intermediary atom which listens both to the Queue and Source and they are both are connected through their Central Channels to the Input Channels 1 & 2 of the intermediary atom. Which sort of atom fits as the intermediary atom best?
It is not necessary to use an intermediary atom! If you for example receive products in the queue through input channel 1 to n, then you can use input channel n + 1 to connect it to the central channel of the source. This way you can control your source directly from your queue. So in the code of my previous post, instead of using In(1,c) to refer to the source, you can replace the 1 by the input channel of the queue that is connected to the central channel of the source.
CyrusMMXI wrote:According to Tutorial ED 7, Page 19 the Central Channel is designed to "receive but not send" the information, however when we use a Monitor, it should be connected to the Central channel of the under concern atom! It doesn't seem that Monitor sends information to the atom it is connected but receives information from it. What is your suggestion here?
I understand your question, it might be a bit confusing. The central channel is not used to send actual atoms, it is used to refer to particular atoms, just as in my suggestion above. The queue is not actually using the connection to send atoms/information, but it uses the connection to control the behaviour of the source.

Regarding the Monitor, you are right about the fact that it seems that the information is sent to the monitor through the central channel, but actually this is not the case. The monitor needs to be informed about the atom of concern. This is done by connecting it to the central channel of the atom of concern. The connected atom does nothing, it does not send nor receive information to/from the Monitor. The monitor is just using the connection to "check" information about the connected atom, to be displayed in the monitor.

Hope this clarifies the central channel concept for you!

Regards,

Marvin
Post Reply