Supported Data Types, Operators and Statements

Groovy is a lightweight scripting language optimized for security and performance within Primavera Gateway. Groovy syntax includes additional syntax for working with PEL (Primavera Cloud expression language).

Groovy supports several data types, operators, and statements. Refer to the sections below for more information on Groovy supported data types and programming constructs.

Supported Data Types

The following table lists Groovy data types supported in Gateway.

Supported Data Type

Example

Boolean

true

Date

new Date()

Double

5.0

String

"Hello World"

Operators

Groovy provides operators for computation and comparison. Operators can only be applied to data types they support. The following tables list Groovy supported operators in Gateway.

Supported Numeric Operators

Use numeric operators to perform calculations on numeric data types, such as doubles.

Operator

Name

Description

Applicable Data Types

Return Data Type

Example

+

Addition

Sums two Double values.

Double

Double

2 + 5; //returns 7

-

Subtraction

Subtracts two Double values.

Double

Double

5 - 3; //returns 2

*

Multiplication

Multiplies two Double values.

Double

Double

2 * 5; //returns 10

/

Division

Divides two Double values.

Double

Double

10 / 2; //returns 5

3 / 2; //returns 1.5

%

Modulo (remainder)

Returns the remainder resulting from the division of two Doubles.

Double

Double

5 % 2; //returns 1

**

Exponential

Returns the result of raising one Double to the power of another.

Double

Double

5 ** 2; //returns 25

Unary -

Unary Minus

Indicates a negative value

Double

Double

-5; // returns -5.

-5 + 2; // returns -3.

Supported Logical Operators

Supported logical operators to manipulate and combine Boolean values and to construct conditional statements.

Operator

Name

Description

Applicable Data Types

Return Data Type

Example

||

OR

Returns the result of combining Boolean values using a logical OR. If one value or expression contained in the OR statement evaluates to true, the OR expression returns true.

Boolean

Boolean

true || false;

//returns true

&&

AND

Returns the result of combining Boolean values using a logical AND. If both values or expressions in the AND statement evaluate to true, the AND expression returns true.

Boolean

Boolean

true && false;

//returns false

!

NOT

Negates the specified Boolean value. Applying the NOT operator to an expression that evaluates to true will return false.

Boolean

Boolean

!true;

//returns false

Supported Comparison Operators

Use comparison operators to measure values against each other. The data types of compared values must match, otherwise the application will return an error. The result of a comparison operation is a Boolean, true or false.

Operator

Name

Description

Applicable Data Types

Return Data Type

Example

==

Equal

Tests if two values are equal.

All

Boolean

1.0 == 1.0;
//returns true

1.0 == 2.3
//returns false

"Hello" == "Hello" //returns true

!=

Does Not Equal

Tests if two values are not equal.

All

Boolean

2 + 2 != 5;
//returns true

2 + 2 != 4;
//returns false

"Hello" != "Goodbye" //returns true

>

Greater Than

Tests if one value is greater than another.

All

Boolean

50 > 100; //returns false

50 > 5; //returns true

"2" > "10" //returns true

"Hello" > "World" //returns false

5 > "4" //error. The types of compared values must match.

>=

Greater Than or Equal To

Tests if one value is greater than or equal to another

All

Boolean

60 >= 50; //returns true

60 >= 70 //returns false

60 >= 60 //returns true

"Hello" >= "World" //returns false

<

Less Than

Tests if one value is less than another.

All

Boolean

50 < 100; //returns true

50 < 40 //returns false

"2"< "10" // returns false

"Hello" < "World" //returns true

"3/1/17" < new Date() //error the types of compared values must match

<=

Less Than or Equal To

Tests if one value is less than or equal to another.

All

Boolean

60 <= 50; //returns false

60 <= 90 // returns true

60 <= 60 //returns true

"10" <= "2" //returns true

Supported Special Operators

Use special operators to create structure in your Groovy code and specify how Gateway should interpret and execute the expressions.

Operator

Name

Description

Applicable Data Types

Example

+

Concatenation

Combines a String with another data type, yielding a new String. String concatenation is applied when the leftmost operand of the + operator is of the data type String. The right operand can be a value of any type. When concatenated, Date values may not serialize in the format anticipated.

String

"Hello" + " World" //returns "Hello World"

"Lucky Number " + 7; //returns "Lucky Number 7"

"a" + 1 + 2 //returns "a12"

5 + "Hello"; //returns an error. To perform concatenation, the leftmost operand must be a String.

=

Assignment

Assigns a value of a supported data type to a variable.

All

def myVar = 50;

( )

Parentheses

Specifies logical grouping and evaluation order. Statements in parentheses have increased precedence.

All

(5 + 2) * 2;

//returns 14

new

Constructor

Instantiates a new instance of a class, resulting in an object of that class.

Date

def currDate = new Date();

//

Comment

Indicates a single-line comment. Commented lines are not evaluated. Use comments to describe and annotate your formula script. Comments extend to the end of a line.

All

//def a = 5; //because this code is commented, it will not be evaluated.

/**/

Multi-line Comment

Indicates an extended comment. Multi-line comments are not evaluated. Multi-line comments may span multiple lines.

All

/* Multi-line comments span

multiple lines.

*/

Supported Groovy Statements

Groovy supports a variety of statements you can use to determine the control flow of Groovy code. The following tables list Groovy statements.

Statement

Description

Example

def

Specifies a variable. Variables in Groovy are strongly typed and restricted to the data type of their initial assignment. Variables are not accessible outside the scope of the code block they are declared in. Variables declared outside of code blocks or other control structures are global and may be accessed from anywhere within the script.

def x = "My Variable";

def y = 2;

def z = new Date();

x = y; //error, after the initial declaration and assignment, x can only have a data type of String. Tried to assign a data type of Double.

return

Specifies a value that should be returned as the result of a script and serve as the default value of the configured field associated with the formula script. The return statement ends script execution. If no return statement is specified, Groovy scripts automatically return the last statement evaluated.

return "Hello World"
// script exits, the text Hello World is returned as the configured field value.

5 + 2;
//unreachable code--will not be evaluated. Script execution ends when a return statement is evaluated.

{ } (block)

Specifies a sequence of expressions to evaluate. Used to structure Groovy scripts, establish variable scope, and improve readability.

Variables defined and assigned values within a code block are accessible only within the code block and other structures the code block contains.

def a = 8;

{

def b = 10;

}

return a + b; // error. B is not defined within scope.

if/else

Code contained in an if block will only be evaluated if the condition specified by the if statement evaluates to true. Code in the else block will be evaluated if the specified condition evaluates to false.

if (1 + 1 == 2) {

//block executed if condition is true

return "True";

} else {

//block executed if condition is false

return "Not true!";

}

Unsupported Programming Constructs

For improved efficiency and security, the following Groovy programming constructs are not supported in Gateway:



Legal Notices | Your Privacy Rights
Copyright © 2018 2021

Last Published Tuesday, March 30, 2021