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 rate structure 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 tariff structure, 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 tariff plant.

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

 

Besonderheiten

Da der Pegasus Parser auf Java basiert, muss man einige Dinge beachten, die in anderen Scriptsprachen (wie z.B. Javascript) nicht üblich sind.

Stringvergleiche

Zahlen werden wie üblich über ein doppeltes == verglichen:

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

Strings können damit leider nicht verglichen werden. Stattdessen muss man die Methode equals() verwenden:

String a = "test";
if (a.equals("test")) {
...

Vergleiche ohne Beachtung von Groß- und Kleinschreibung geschehen über die Methode equalsIgnoreCase().

Elemente von Variablen

Elemente von Variablen (wie z.B. die Zieladress-PLZ der Sendung) werden über Getter- und Setter-Methoden gelesen und gesetzt.

Diese besitzen immer den Prefix "get" bzw. "set" und das nächste Zeichen wird als Großbuchstabe geschrieben. Bei Boolean wird "is" statt "get" verwendet. Beispiele:

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

Direktes definieren von Nicht-int Zahlen.

Wenn Sie einen double, float oder long direkt im Quelltext definieren wollen, benötigen diese nach der Zahl einen entsprechenden Suffix:

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

Beispielscripte

Prüfen auf Inselzustellung

shipment.isIsle()

Es wird die entsprechende Inseltabelle für den jeweiligen Frachtführer benötigt.

Auf bestimmte Länder prüfen

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

Die Elemente DE, CH, AT müssen durch die gewünschten Länder im ISO2 Format ersetzt werden.

Auf bestimmte Versandart prüfen

shipment.getServiceType().equals("VID")

Die Versandart ID "VID" muss durch die Gewünschte ersetzt werden. Falls mehrere Versandarten in Frage kommen, kann man auch hier eine Liste wie im Beispiel darüber erstellen.

Nachnahme-Sendung

shipment.getCodAmount() > 0d

Verpackungsart prüfen

packageData.getPackageType().equals("KT")

Die Verpackungsart sollte meist auch nur mit Packstück-Tarifwerken und -Zuschlägen definiert werden.