Hi Katinka,
To answer your question I'll need to explain three things:
1. The use of parameters in a function.
2. The use of Local variables in a function or any other piece of code.
3. The use of Global variables.
1. The use of parameters in a function.
When you define a function using the Function Editor you also define the number of parameters for that function. Parameters can be different things: it can be values, strings, atom references etc. A function could be for example:
In this example the first parameter is an atom reference "i" and the second parameter is a string "[ProductA]"
Another example:
The first parameter is again an atom reference, now "c" and the second parameter is a value "5".
In the function editor you can refer to the paramters using p(1), p(2) etc. This means for the first example:
Every time you use p(1) in the function code, the atom reference to "i" will be known. Using p(2) in the function code will be the same as the string [ProductA] as this was given as additional information in the 2nd parameter. So, in the function itself you only need to use p(1) and p(2) to get the information used between the brackets in the function call.
In this way we can also use the same function in another way:
In this example the atom reference for the function is "c" and the text in p(2) will return the string [MachineA]. In the function code we would still use p(1) and p(2)!
The function code could be for example:
2. The use of Local variables in function code.
To make your function code readable and understandable it is good to make use of LOCAL variables. Local means that the variable is known only within the function/piece of code itself and not outside in other places of the model. Local variables have to be defined using the word VAR. Local variables always have a variabletype which can be vbString, vbValue, vbAtom etc. Once you have defined a Local variable, you can store the parameter information in it. In the example below I store the atom reference from p(1) into a local variable with the name atmToChange and type vbAtom. I store the new name in a local variable with the name strNewName and type vbString.
In the example above we would get:
Code: Select all
Do(
{ declare vbAtom variable }
Var([atmToChange], vbAtom),
{ and store information from p(1), from now on atmToChange is the same as p(1) which is the same as "i". }
atmToChange := p(1),
{ declare vbString variable }
Var([strNewName], vbString),
{ and store information from p(2), from now on strNewName is the same as p(2) which is the same as [ProductA] }
strNewName := p(2),
{ Execute the code using the new variables }
Name(atmToChange) := strNewName
)
In the example it doesn't really make sense, but when you need to refer to a parameter more than 1 time it is better to use a variable instead of using p(1) all the time.
3. The use of Global variables.
In your post you also asked how to use the refWaitingOrders. The refWaitingOrders is a Global variable, created by the table atom itself (the word to declare a global variable is Dim, types can be vbAtom, vbString, vbValue again). A global variable is known anywhere in your model, so you may use that one within the function code itself. As long as the label to be used in the function is Label([CurRow], refWaitingOrders) every time you call the function, you can just leave that. There is no need to create a parameter for it.
The function for the entry trigger could be a function with 2 parameters: a reference to the warehouse c and a reference to the product i:
Without using local variables the code in the function editor would be (note that I use p(2) and p(1) multiple times):
Code: Select all
Do(
If(
Label([Direction], p(2)) = 2,
DestroyAtom( p(2)),
do(
Var([valRow], vbValue),
valRow := Label([CurRow], refWaitingOrders),
Cell( valRow, 1, refWaitingOrders) := label([aisle], p(2)),
Cell( valRow, 2, refWaitingOrders) := label([rack], p(2)),
Cell( valRow, 3, refWaitingOrders) := ptv( p(2)),
Cell( valRow, 4, refWaitingOrders) := ptv( p(1)),
Cell( valRow, 5, refWaitingOrders) := round(time),
Label([CurRow], refWaitingOrders) := Label([CurRow], refWaitingOrders) + 1,
If(
Label([CurRow], refWaitingOrders) > nRows( Label([CurRow], refWaitingOrders)),
nRows( refWaitingOrders) := nRows( refWaitingOrders) + 1000
)
)
)
)
With the use of local variables the code could be (now I use p(1) and p(2) only once:
Code: Select all
Do(
Var([atmRack], vbAtom),
atmRack := p(1),
Var([atmProduct], vbAtom),
atmProduct := p(2),
If(
Label([Direction], atmProduct) = 2,
DestroyAtom(atmProduct),
do(
Var([valRow], vbValue),
valRow := Label([CurRow], refWaitingOrders),
Cell( valRow, 1, refWaitingOrders) := label([aisle], atmProduct),
Cell( valRow, 2, refWaitingOrders) := label([rack], atmProduct),
Cell( valRow, 3, refWaitingOrders) := ptv(atmProduct),
Cell( valRow, 4, refWaitingOrders) := ptv(atmRack),
Cell( valRow, 5, refWaitingOrders) := round(time),
Label([CurRow], refWaitingOrders) := Label([CurRow], refWaitingOrders) + 1,
If(
Label([CurRow], refWaitingOrders) > nRows( Label([CurRow], refWaitingOrders)),
nRows( refWaitingOrders) := nRows( refWaitingOrders) + 1000
)
)
)
)
Good luck!
Regards, Marlies