Script (Tarifwerk): Unterschied zwischen den Versionen
Ddang (Diskussion | Beiträge) |
Ddang (Diskussion | Beiträge) |
||
Zeile 7: | Zeile 7: | ||
== Scripteditor == | == 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 | + | === 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.util.Date | ||
Zeile 34: | Zeile 34: | ||
*java.util.HashMap | *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 ==== | ==== 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 === | === 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: | + | *ATrim: Removes all spaces at the beginning and end of the string. |
− | *Left: | + | *Left: Returns the first n digits of a string. |
− | *Right: | + | *Right: Returns the last n digits of a string. |
− | *DEBUG: | + | *DEBUG: Displays the value of a variable in a message box. (Please never save a script that contains these functions) |
− | *DateTimeFormat: | + | *DateTimeFormat: Converts a string into a date. |
− | *FormatDateTime: | + | *FormatDateTime: Converts a date into a string. |
− | *GetListItem: | + | *GetListItem: Extracts the nth element from a string that was separated by a separator. |
− | *LeftPad: | + | *LeftPad: Fills a string on the left with the specified number of digits and characters |
| |
Version vom 18. Oktober 2024, 16:33 Uhr
Scripte bilden die zentrale Stelle im Tarifwerk um Regeln für Zuschläge und Tarifwerke aufzubauen. Dies können simple Abfragen sein, wie z.B. prüfen, ob es sich um eine Inselzustellung handelt, oder aber komplexe Konstrukte, die Frachtführer- oder Kundenspezifische Anforderungen umsetzen.
Alle Scripte verwenden den Pegasus Parser, eine Eigenentwicklung der Heidler Strichcode GmbH. Die Scriptsprache basiert auf der Programmiersprache Java, was manche Dinge komplizierter machen kann, dafür aber deutlich mehr Flexibilität und Performance bietet.
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.