Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

MyReverseString()

For this example, we imagine let's suppose that we need to reverse certain strings within JIRAJira, and a reverse string routine might be handy. Since SIL does not provide such a function, let's implement it.

...

In the main package (com.mycompany.silexample) create a new class , we'll and name it ReverseStringRoutine.

The code is presented below, along with the comments:.

Code Block
languagejava
linenumberstrue
/*
 * Created at Sep 2, 2011T5:39:27 PM+02.
 *
 * File: ReverseStringRoutine.java
 */
package com.mycompany.silexample;

import java.util.*;

import com.keplerrominfo.common.util.MutableString;
import com.keplerrominfo.sil.lang.*;
import com.keplerrominfo.sil.lang.type.TypeInst;
import com.keplerrominfo.sil.lang.SILValue;
import com.keplerrominfo.sil.lang.SILValueFactory;

/**
 * Reverses a string
 *
 * @author Radu Dumitriu (rdumitriu@gmail.com)
 * @since 1.0
 */
public class ReverseStringRoutine extends AbstractRoutine<MutableString> {
    private static final SILType[][] types = {{ TypeInst.STRING }}; //all possible type combinations are listed here

    public ReverseStringRoutine(String name) {
        super(name, types);
    }

    /**
     * Returns the type of the returned value. For routines that return
     * a value of variable type, this should return null
     *
     * @return the type of the returned value
     */
    @Override
    public SILType<MutableString> getReturnType() {
        return TypeInst.STRING;
    }

    /**
     * The execution of the routine
     * @param silValues the list of values (parameters)
     * @return the SIL value
     */
    @Override
    protected SILValue<MutableString> executeRoutine(SILContext context, List<SILValue<?>> silValues) {

        //AbstractRoutine checks the parameters and their types
        SILValue param = silValues.get(0);

        //We know for sure this is a string
        String val = param.toStringValue();

        //we calculate the reversed value
        String reversedVal = new StringBuilder(val).reverse().toString();

        //We'll prepare the return here
        return SILValueFactory.string(reversedVal);
    }

    /**
     * This returns the description of the parameters
     * @return the part that will be appended to the routine at editing time
     */
    @Override
    public String getParams() {
        return "(str)"; //that's all
    }
}


Our The routine is now completed, we now you need to register it on the SIL. Let's To do this, modify the launcher we you created in the previous step:.

Code Block
@Override
public void doAtLaunch() {
    super.doAtLaunch();

    //register the routine
    RoutineRegistry.register(new ReverseStringRoutine("myReverseString"));
}

@Override
public void doAtStop() {
    //first, make sure that super is called, even if will have an exception here
    try {
        //unregister the routine
        RoutineRegistry.unregister("myReverseString");
    } catch(Exception e) {
        LOG.error("Failed to unregister!", e);
    }
    
    super.doAtStop();
}

...


Things happening under the hood

...

The above routine inherits a lot of the functionality from AbstractRoutine (http://confluence.kepler-rominfo.com/javadoc/katl-commons/2.5/com/keplerrominfo/jira/commons/sil/AbstractRoutine.html). The AbstractRoutine class , and this class is responsible for:

  1. Checking the type of the parameters
  2. Converting automatically the parameters to the required typestypes automatically
  3. Calling the real execution code with the parameters transformed.

The AbstractRoutine class follows in fact a Template Method Pattern. You are not required to inherit AbstractRoutine. In fact, sometimes you should sometimes implement Routine interface (http://confluence.kepler-rominfo.com/javadoc/katl-commons/2.5/com/keplerrominfo/jira/commons/sil/Routine.html) to implement a routine interface to avoid or implement custom checking of the parameters.

Note

Don't forget to unit test your code (the above example lacks it. We didn't include it in the example, but you should feel obliged to do it).

Running

...

the example

Open a terminal ( or a console ) and run atlas-debug in the root of the project. It will launch a JIRA Jira with both SIL Engine and (katl-commons) and silexample add-ons installed.

In the SIL Manager, you may open a test .sil file and write something like:

...