Step 2: Create your own SIL Function!
In Part 1 we created an add-on, which contains three SIL functions.
In Part 2 we will discuss, what should be changed in this add-on to add your own function.
1. Create your own class for your function
First of all you need to create your own class in the function folder. This class must extend the AbstractSILFunction<T> class.
Let's look at the available files in this folder (these files are provided as an example. If you develop you own add-on, you can remove these files).
SayHello.java:
public class SayHello extends AbstractSILFunction<MutableString> {
private static final SILType[][] types = {{ TypeInst.STRING }};
public SayHello(ClassLoader classLoader, String name) {
super(classLoader, name, types);
}
Override
public SILType<MutableString> getReturnType() { return TypeInst.STRING; }
@Override
protected SILValue<MutableString> runFunction(SILContext silContext, List<SILValue<?>> list) {
SILValue param = list.get(0);
return SILValueFactory.string( "Hello " + param.toStringValue());
}
@Override
public String getParams() { return "(name)"; }
Let's have a look at the first line:
private static final SILType[][] types = {{ TypeInst.STRING }};
This line defines, what kind of input variables to expect for this function. We call methods in SIL like this:
mymethod(myparameter);
By types = {{ TypeInst.STRING }}; we say that myparameter is a string. If you want to pass two parameters to mymethod then the line will be like this:
and you can call your method like this:
If your function can take either one parameter or two parameters, then you should define it like this:
Next line:
As you can see this class is extended from AbstractSILFunction<MutableString>, which means that this function will return the String type.
Next lines:
These lines define a constructor. You do not need to change anything there.
Next lines:
These lines define a function, which informs the SIL engine what object will be returned by this function. We will return a String.
Next lines:
These lines provide the logic, which will be executed by your function. This is the entry point for your function. This function accepts two parameters.
silContext (you can find here system variables like the issue, for which the script is executed, sil variables).
For example, if you need to take the issue for which the script is executed, you can use a code like this:
The list parameter, is the parameter for your input variables.
Line
returns a value from the function. You should use the SILValueFactory class to create return values for your function. This class can create all kind of value types available in SIL. We will see more examples later.
So, this function actually just accept a parameter and return a result Hello + this parameter. You can implement any logic you need in this function.
Next lines:
These lines define how your function will look in the SIL Manager. When you start typing in the SIL manager, you see suggestions for available functions. the (name) string means that the suggestion will look like this:
2. Add your class to SIL engine
We created a class, but how to tell the SIL engine how to use it? It is done in the ESLauncher.java file. You can see there lines like this one:
It says that we add the function from the SayHello class and it will be called SayHello.
What you need is to copy the line and change first SayHello to your class and the next SayHello to your name. The name of the class and the name of the function in SIL can be different.
Then also add a line like this:
Change SayHello to the name of your function. It will unload your function, if you disable your add-on.
3. More on SIL
You can see in SayHello2, we return a List as a return value.
where res is a List<String>.
To make it work you also need to change the return type to TypeInst.STRING_ARR and extend AbstractSILFunction with KeyableArraySILObject<MutableString> type.
In SIL you can use this function like this:
In SayHello3 we return a Map:
You can use SayHello3 in SIL like this:
We call the SIL array not by index, but by key. But if you notice, these functions have the same code to return a value:
It is because in SIL the KeyableArraySILObject contains a List and a Map to provide keys for the List, that is why the code is the same.
4. Pass an array as a parameter
Now we know how to pass a string parameter to a SIL function and return a string, a List or a Map.
Now let's have a look on how to pass an array as a parameter.
The runFunction function will look like this:
First you convert the parameter to the GenericArraySILObject class and then convert it to Java's Map.
You could use this function in SIL like this:
That is all I wanted to talk about in this article. I hope that now you have a good intro in developing your own add-on for the SIL engine.