How to Use Input Parameters and Output Parameters in Rules, Sequences, Functions and Procedures

In all methods (rules, sequences, functions, procedures) you can use input parameters as well as return (or output) parameters. This article shows you how to use input and output parameters.

Parameters in Rules

Input Parameters

This approach can be helpful if you want to parametrize rules or want to handle information between rules. Input parameters need to be defined right after the rule name and before the curly brackets defining the rule content. The parameters are defined like variables, meaning they require a variable name and data type definition. Depending on the type of input parameter required, you have to use different syntaxes. Here you can find a quick overview:

Basic type variables (int, string, double, ...)rule myRule(var str:string, var nb:int){…}
Nodesrule myRule(n:NodeClass){…}
Edgesrule myRule(-e1:EdgeClass-, -e2:DirEdgeClass->){…}
Container type variables (array, set, map, deque)rule myRule(ref s:set<int>, ref a:array…){…}

As you can see, input parameters have to be given in parenthesis and multiple parameters are separated by commata. Please also see the example below.

Output Parameters

Output parameters need to be defined after a colon behind the rule name and before the curly brackets defining the content of the rule. If the rule also uses input parameters, then the output is defined after the input (see example below). Again, the parameters have to be given in parentheses and multiple parameters have to be separated by commata. However, unlike with input parameters, you only need to define the data type for the output parameters.

Additionally, the return of one or more output parameters needs to be triggered using the command return in the replace/modify part of a rule. If return types are specified, the return statement is mandatory. Otherwise, no return statement must occur. Make sure that the values you return match the data type specified for the output parameter and in case multiple parameters are returned, also watch out for the correct order.

An example could be:
1 rule findPartsOfParent(var nb:int, n:BomNode):(Part, double){
2 //Rule with input and output arguments
3  part:Part;
4    if{isReachableOutgoing(n,part,Contains) && nb == part.productid;}
5     modify{
5       return(part, part.cost); //Return Part node and cost of part 
6   } 
7 }

Please visit the Rules & Sequences articles for more information on coding.

Parameters in Sequences

Input Parameters

Basic type variables (int, string, ...)sequence mySequence(str:set<string>){…}
Nodessequence mySequence(n:NodeClass){…}
Edgessequence mySequence(-e1:UndirEdgeClass-, -e2:DirEdgeClass->){…}
Container type variablessequence mySequence(arr:array<string>){…}

Just like in rules, input parameters in sequences are defined after the sequence name and have to be given in parentheses. Multiple parameters have to be separated by commata. Unlike in rules, no keywords like var or ref are required to define data type input parameters in sequences. The definition of node and edge type variables is similar to that in rules. Please also see the example below.

Output Parameters

Sequences can also return one or more values or elements. However, calling a method within a sequence always returns a boolean value. When the method executes correctly a true is returned, else, a false is returned. That is why output parameters in rules need to be treated like variables, i.e. they need to be defined with a name and type. Just like in rules the definition is done after a colon after the sequence name and, if existing, after the input parameter definition. To return an actual value or element it needs to be assigned to the output parameter in the course of the sequence. Please see the example below for details.

Here a double-type output parameter named potential is used. The return is created by assigning the value of the procedure myCalcProcedure, which needs to be of type double, to the output parameter potential.

sequence cleanUp(perc:int, mat:string):(potential:double) {
  {(potential) = myCalcProcedure(mat,perc)}

Parameters in Functions/Procedures

Input Parameters

Basic type variables (int, string, double, ...)function myFunction:string(var str:string){…}
Nodesprocedure myProcedure(n:NodeClass){…}
Edgesfunction myFunction:string(-e:DirEdgeClass->){…}
Container type variables (array, set, map, deque)procedure myProcedure(ref NumAr:array<double>){…}

Input parameters for functions and procedures work just like in rules.

Output Parameters

Functionfunction myFunction:int{
} // double, string, set<string>, …
Procedureprocedure myProcedure:(int,string){
return(5,“How are you?“);
} // double, string, set<string>, …

Output parameters for functions and procedures work a lot like in rules. However, a function always needs to have exactly one return value. This means that a function definition always needs to be followed by a colon and a single type definition without parentheses.

function myFunction(var perc:int):string {

A procedure can have one or more multiple return values, which can also be empty. So, if the procedure has a return the definition is similar to that in rules. If it has no return value no output parameter needs to be defined. Still, an empty return statement needs to be included as the last step of the procedure, as shown in the example below:

procedure myProcedureWOReturn(var perc:int, ref mat:set<NodeClass>) {
  for(n:Node in mat){
    if(n.intAtt == perc){emit("Matching Node found!\n");}

Was this article helpful?

Related Articles