Script (Tarifwerk) (en): Unterschied zwischen den Versionen
Ddang (Diskussion | Beiträge) |
Ddang (Diskussion | Beiträge) |
||
(5 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt) | |||
Zeile 1: | Zeile 1: | ||
[[de:Script (Tarifwerk)]] | [[de:Script (Tarifwerk)]] | ||
− | [[File: | + | [[File:Script_Zuschlag(en).png|frameless|right|super|475x413px|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 == | == 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 | + | === 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 33: | ||
*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 |
| | ||
− | == | + | == 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 ==: | |
<pre>int a = 5; | <pre>int a = 5; | ||
if (a == 5) { | if (a == 5) { | ||
....</pre> | ....</pre> | ||
− | + | Unfortunately, strings cannot be compared using this method. Instead, you have to use the equals() method: | |
− | <pre>String a = | + | <pre>String a = “test”; |
− | if (a.equals( | + | if (a.equals(“test”)) { |
...</pre> | ...</pre> | ||
− | + | 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: | |
<pre>shipment.getReceiverZipcode() | <pre>shipment.getReceiverZipcode() | ||
shipment.getShipmentWeight() | shipment.getShipmentWeight() | ||
Zeile 95: | Zeile 94: | ||
packageData.getPackageType()</pre> | packageData.getPackageType()</pre> | ||
− | === | + | === 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: | |
<pre>double d = 4.3d; | <pre>double d = 4.3d; | ||
float f = 5.4f; | float f = 5.4f; | ||
long l = 200000l;</pre> | long l = 200000l;</pre> | ||
− | == | + | == Example scripts == |
− | === | + | === Check for island delivery === |
<pre>shipment.isIsle()</pre> | <pre>shipment.isIsle()</pre> | ||
− | + | The corresponding island table for the respective carrier is required. | |
− | === | + | === Check for specific countries === |
− | <pre>final List<String> zielLaender = java.util.Arrays.asList( | + | <pre>final List<String> zielLaender = java.util.Arrays.asList(“DE”, “CH”, “AT”); |
return zielLaender.contains(shipment.getReceiverCountry());</pre> | return zielLaender.contains(shipment.getReceiverCountry());</pre> | ||
− | + | The elements DE, CH, AT must be replaced by the desired countries in ISO2 format. | |
− | === | + | === Check for specific shipping type === |
− | <pre>shipment.getServiceType().equals( | + | <pre>shipment.getServiceType().equals(“VID”)</pre> |
− | + | The dispatch type ID “VID” must be replaced by the desired shipping type. If several dispatch types are possible, you can also create a list here as in the example above. | |
− | === | + | === Cash on delivery shipment === |
<pre>shipment.getCodAmount() > 0d</pre> | <pre>shipment.getCodAmount() > 0d</pre> | ||
− | === | + | === Check packaging type === |
− | <pre>packageData.getPackageType().equals( | + | <pre>packageData.getPackageType().equals(“KT”)</pre> |
− | + | The packaging type should usually only be defined with package rate structures and surcharges. | |
| |
Aktuelle Version vom 18. Oktober 2024, 16:44 Uhr
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 dispatch type ID “VID” must be replaced by the desired shipping type. If several dispatch 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 rate structures and surcharges.