question

SudheerReddy avatar image
0 Likes"
SudheerReddy asked Matthew Gillespie commented

Ternary Operator

I am trying to understand the below code how is it working.

In getprocessflowvar() the parameter token is passed, why we are passing token into it.

expression ? 1: expression how this ternary operator is working.

return /**/getprocessflowvar(processFlow, "ItemDependentDepletion", token) ? 1 : getprocessflowvar(processFlow, "DepletionUnit", token)/**direct*/;
FlexSim 20.0.0
ternary operator
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

Matthew Gillespie avatar image
2 Likes"
Matthew Gillespie answered Matthew Gillespie commented

The ternary operator is a condensed if-else statement. From wikipedia: An expression a ? b : c evaluates to b if the value of a is true, and otherwise to c. One can read it aloud as "if a then b otherwise c".

So a is true if it's any value other than 0 or null.

You pass token into the getprocessflowvar() command because that command uses the token to figure out which instance of the process flow you want to look at. This is important because you can define local process flow variables where each instance of the process flow can have a different value for the same named variable.

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

SudheerReddy avatar image SudheerReddy commented ·

@Matthew Gillespie Thanks for your answer.

I have follow on Questions after having a look into the below image

First command is double processTime, I am clear on this

But in second command how can we understand that if we send token as parameter FlexSim will consider that as instance. How the background logic or FlexSim understands that as instance?

double processTime = getprocessflowvar(processflow, "processTime"); 

treenode processor = getprocessflowvar(processflow, "processor", token);

In the description for getprocessflowvar() command is written as

Description Gets the value for the Process Flow Variable. The processflow parameter can either be a reference to the Process Flow object in the Tools or the name of the Process Flow object.

Here we are writing processflow parameter as processFlow (here processFlow is Process Flow object in the Tools or the name of the Process Flow object)

0 Likes 0 ·
ins.jpg (34.6 KiB)
Matthew Gillespie avatar image Matthew Gillespie ♦♦ SudheerReddy commented ·

If you pass in a string for the processflow parameter it will assume it's the name of a process flow object and will look for that object by name in Tools/ProcessFlow. Otherwise it will assume you're passing a reference to a process flow object. With that little code snippet it's impossible to know which one it's doing because we can't see what kind of variable processflow is. It's either a string:

string processflow = "MyFlow";
double processTime = getprocessflowvar(processflow, "processTime");

Or a node:

treenode activity = param(2);
treenode processflow = ownerobject(activity);
double processTime = getprocessflowvar(processflow, "processTime");

Since a treenode processFlow variable is defined in all the code headers in Process Flow I think you'll usually see that.

The token parameter is an optional parameter. Every token is associated with a certain instance of a Process Flow. So if you pass in a token then it finds its instance and gets the local value of the process flow variable for that instance.

0 Likes 0 ·