Script (Tarifwerk) (en)

Aus Wiki - Heidler Strichcode GmbH
Zur Navigation springen Zur Suche springen


TW Script Editor.png

Scripts are the central point in the Tarifwerk for creating rules for surcharges and rate structures. These can be simple queries, such as checking whether it is an island delivery, or complex constructs that implement carrier or customer-specific requirements.

All scripts use the Pegasus parser, a proprietary development of Heidler Strichcode GmbH. The scripting language is based on the Java programming language, which can make some things more complicated, but offers significantly more flexibility and performance.

Scripteditor

The script editor is divided into three areas:

Information panel

The information panel is located at the top left. It shows you all the information you need for script development.

First of all, it lists which variables you have available and which type (which class) the variables have. In the Tarifwerk, you will usually see the variable shipment, which corresponds to the current shipment, and possibly the variable packageData, which corresponds to the current package.

The Expected return type is the type of value that you must return in this script. As a rule, only Boolean (true or false) or Double (floating point number with double precision) is expected in the Tarifwerk.

Button bar

The button bar is located at the top right. You can use it to make additional adjustments and tests.

Imports

The Imports button opens an editor in which you can add additional classes (functionalities) to the current script. You can use the Search button to search for the currently entered class. If successful, this class will be displayed in the list above.

The following classes and interfaces are imported by default and do not need to be specified:

  • java.util.Date
  • java.text.SimpleDateFormat
  • java.util.ArrayList
  • java.util.List
  • java.util.Map
  • java.util.HashMap

Variables

The variable editor allows you to set up various test scenarios by changing the values of the given variables as required. For more complex variable types (such as shipment or package), the variable is displayed and edited in JSON format. The usual rules for escape characters apply.

Test

You can test your current script using the Test button. If there is a syntax error, this is displayed along with the line. In addition, you can view the complete class created in the folder Tarifwerk\compile\scriptbase as long as the results window is open.

Editor

You can implement your script in the editor. The editor has several features to make creating scripts easier:

  • If a script is single-line and does not end with a semicolon, you do not need a return statement. This is added automatically.
  • The script editor has code highlighting to separate variables, comments and classes from each other
  • The script editor has limited code completion features. These are activated via the key combination Ctrl+Space

Additional functions

The script editor comes with some additional functions as standard. Many of these functions correspond to the parser expressions from the HVS32.

The detailed description and use can be found in the code completion (Ctrl+Space). Frequently used and useful functions are

  • ATrim: Removes all spaces at the beginning and end of the string.
  • Left: Returns the first n digits of a string.
  • Right: Returns the last n digits of a string.
  • DEBUG: Displays the value of a variable in a message box. (Please never save a script that contains these functions)
  • DateTimeFormat: Converts a string into a date.
  • FormatDateTime: Converts a date into a string.
  • GetListItem: Extracts the nth element from a string that was separated by a separator.
  • LeftPad: Fills a string on the left with the specified number of digits and characters

 

Special features

Since the Pegasus parser is based on Java, there are a few things to consider that are not common in other scripting languages (such as Javascript).

String comparisons

Numbers are compared as usual using a double ==:

int a = 5;
if (a == 5) {
....

Unfortunately, strings cannot be compared using this method. Instead, you have to use the equals() method:

String a = “test”;
if (a.equals(“test”)) {
...

Case-insensitive comparisons are made using the equalsIgnoreCase() method.

Elements of variables

Elements of variables (such as the destination address zip code of the shipment) are read and set using getter and setter methods.

These always have the prefix “get” or “set” and the next character is written as an uppercase letter. For Booleans, “is” is used instead of “get”. Examples:

shipment.getReceiverZipcode()
shipment.getShipmentWeight()
shipment.isIsle()
packageData.getPackageType()

Direct definition of non-int numbers.

If you want to define a double, float or long directly in the source code, they require a corresponding suffix after the number:

double d = 4.3d;
float f = 5.4f;
long l = 200000l;

Example scripts

Check for island delivery

shipment.isIsle()

The corresponding island table for the respective carrier is required.

Check for specific countries

final List<String> zielLaender = java.util.Arrays.asList(“DE”, “CH”, “AT”);
return zielLaender.contains(shipment.getReceiverCountry());

The elements DE, CH, AT must be replaced by the desired countries in ISO2 format.

Check for specific shipping type

shipment.getServiceType().equals(“VID”)

The shipping type ID “VID” must be replaced by the desired shipping type. If several shipping types are possible, you can also create a list here as in the example above.

Cash on delivery shipment

shipment.getCodAmount() > 0d

Check packaging type

packageData.getPackageType().equals(“KT”)

The packaging type should usually only be defined with package tariff units and surcharges.