Learn how to implement a WarpScript function in Java, so you can make your own extension or contribute to the source code of Warp 10.

In this tutorial, we will learn how to implement a WarpScript function in Java. For example, this can be useful if you want to contribute to Warp 10 source code, or if you want to build a custom WarpScript extension.
Learn more about WarpScript. |
A first example
A WarpScript function is a Java class that implements the interface WarpScriptStackFunction
. That is, it must have an apply
method that takes a WarpScriptStack
object as argument (which is the execution environment of the WarpScript functions), uses it to retrieve its arguments and then returns its results.
For instance, in what follows, we write a function that generates a random ASCII String (we will just wrap RandomStringUtils.randomAscii
into a WarpScript function):
Then, this function needs to be added to a library of functions. For example, you can put it in the static functions
map of the WarpScriptLib
source file if this function is part of a pull request for the Warp 10 Platform or if you are working on your own fork.
Another situation is when this function is part of a WarpScript extension. A WarpScript extension is simply a class that extends WarpScriptExtension
. It has a method called getFunctions()
which returns a Map
of WarpScript functions indexed by their names. For example, you can write one like this:
For more information on packaging and loading an extension, you can refer to the documentation. To publish, manage, or automatically install an extension, there is WarpFleet.
An example of a function with multiple arguments
In the previous example, the function that is implemented has only one argument, so its interactions with the stack are simple: retrieve the argument, check its type and apply the function on the WarpScript execution environment (the stack).
If there are multiple arguments, the interactions with the stack can be more complex, extracting multiple arguments, checking their type and throwing exceptions for incorrect syntax, and handling arguments that are optional and those that are not.
Learn how to host web content in Warp 10 |
If you want to simplify some of that work, the abstract class FormattedWarpScriptFunction
can handle some of those tasks automatically, so that its subclass can focus on implementing the function's operation. In the child's class, the arguments are specified using a builder pattern: an ArgumentsBuilder
object has methods that register the arguments of the function. These arguments are used by the parent class to create a Map
of parameters. The subclass can then use this map directly, rather than interacting with the stack.
For example, the following code wraps the method RandomStringUtils.Random
into a WarpScript function:
After adding this function to WarpScriptLib or to an extension, there are two possibilities in WarpScript to call it:
-
count:LONG letters:BOOLEAN letters:BOOLEAN RANDOMSTRING
,- for instance
34 false false RANDOMSTRING
- for instance
{...} RANDOMSTRING
where the map contains at least the mandatory arguments- for instance
{ "count" 27 "letters" false "numbers" false "chars" "warp" } RANDOMSTRING
- for instance
Additionally, it is possible to automatically generate the documentation of a FormattedWarpScriptFunction
: see the class DocumentationGenerator
and the class RunAndGenerateDocumentationWithUnitTests
.
Unit Tests
Unit testing the implementation of a WarpScript function is good practice for obvious reasons.
For example, a unit test of the previous examples can be done by the following class:
Unit testing your functions can also be done directly in WarpScript code by providing a .mc2
file containing those tests.
Conclusion
Congratulations! With this tutorial, you have learned to write custom WarpScript functions in Java. So you can build your own WarpScript extension and/or contribute to the source code.
You can retrieve this example on its GitHub repository.
Read more
What's new in the Warp 10 Ecosystem
2022 review of the Warp 10 platform
Leveraging WarpScript from Pig to analyze your time series

Machine Learning Engineer