bottom

Control Functions

Control Functions

Those functions are provided to implement some logic with test-branch, loops, etc. Using those control functions along with parameters managements allow you to write little scripts into GEDIS.

IF(cond,expr1,expr2) : If-Then-Else

This function returns the value of expr1 if cond evaluates to true and expr2 otherwise. Example:

IF(
INLIST(a,c,vf,e,g,1,q,a,34),
"present",
"missing"
)

returns "present" since the value 'a' is in the list.

FOR(expr1,expr2,expr3): Loop

This control structure allows to use imperative programming style. Before iterating in the loop, expr1 is evaluated. Then, if expr2 evaluates to true, expr3 is evaluated. It goes on like this until expr2 evaluates to false. In this case, the last value of expr3 is returned.

Example: consider the following C-like piece of code:

 int i,s = 0; 
while (i < 5) {
s += i;
i += 1;
}
return s;

The expression corresponding to the loop

FOR( 
/* Loop Initialization, init parameters in the field scope.*/
SET(i,0,field)
SET(S,0,field),
/* Loop condition : as long as value of i is less than 5 */
LESS(GET(i),5),
/* Loop Body */
INCR(S,GET(i))
INCR(i,1)
)
GET(S)

The value returned by this expression is '10' (the sum 0 + 1 + 2 + 3 + 4)

REPEAT(what,count,separator,update)

This function repeat count times the value provided by the expression what. Each repeated value is delimited with separator. If update is true then the expression what is updated, otherwise it is always the same string that is repeated.

For example, the expression REPEAT(RANGE(1,10),4,:,false) produces

2,4,1,8
5,6,3,7
../..

While the expression REPEAT(RANGE(1,10),4,:,false) produces

2,2,2,2
4,4,4,4
6,6,6,6

This function also accepts -1 for the count value. In that case, the repeatition will occurs as long as the source value is not exhausted. Obviously this can raise an infinit loop if used with a never ending source function.

EVAL(expr): Expression evaluation

This function allows to interpret the string "expr" as an expression and evaluate it. This is useful to dynamically build and evaluate expressions. Example: UPPER(sum(2,4)) returns SUM(2,4) whereas EVAL(UPPER(sum(2,4))) returns 6.

FIELD(GenID): Produced values access

This function allows to read the value produced by the generator whose ID is GenID. If this ID does not exist, an error is raised. Note that refering to the value produced by another generator introduces a dependancy relationship. Consequently, the generator GenID will be updated before the expression GenID is evaluated.

You can also use the special prefix ".." to refer to the UID of the parent of the expression where is used FIELD(). For example, if your expression has UID GEN-001-002 then a FIELD(..-003) will refer to GEN-001-003 ! 

NEXT(UID-1,UID-2,...): explicit generators update

This function allows to explicitly update the generators UID-1, UID-2, ... This function must be used very carefully as it leads to circumventing the normal updating process of the generator. Note that the NEXT function does not return any value. Example: Suppose that we have a generator REF-1 which produces the sequence of values: "AAAA,BBBB,CCCC,DDDD,EEEE".

Consider now the following expression: "Current value=FIELD(REF-1)NEXT(REF-1) Next value=FIELD(REF-1)"

The values produced by this expression will be:

Record #1 : "Current value=AAAA Next value=BBBB", Record #2 : "Current value=CCCC Next value=DDDD", etc.

In this example, the REF-1 generator is updated once between each record (as usual) plus each time the NEXT function is evaluated. Warning: the NEXT(UID) expression leads to a recursive update of generators. If the generator UID depends on some others generators, these generators will also be updated.

Add comment


Security code
Refresh