Own extensions

TL-Script can easily be extended with own functions via a plug-in mechanism. To do this, you need to create a Java implementation of the TL-Script function and register it in the application configuration.

Implementation

The TL-Script function is implemented in a derivative of com.top_logic.model.search.expr.GenericMethod. The actual function is implemented in the overridden method com.top_logic.model.search.expr.Info.eval(Object, Object[], EvalContext). The method gets the self argument as first parameter and all other arguments in the arguments array. As a result, the method must return the function result of the TL-Script function.

Additionally, a builder for the function class must be created. This is created as a derivative of com.top_logic.model.search.expr.config.operations.AbstractSimpleMethodBuilder<I>. This builder creates an instance of the function implementation from above. The builder implementation is registered in the application configuration.

A minimal implementation of a TL-Script function that rounds off a number might look like this:

public class Floor extends SimpleGenericMethod {
   protected Floor(String name, SearchExpression self, SearchExpression[] arguments) {
      super(name, self, arguments);
   }

   @Override
   public GenericMethod copy(SearchExpression self, SearchExpression[] arguments) {
      return new Floor(getName(), self, arguments);
   }

   @Override
   public TLType getType(TLType selfType, List<TLType> argumentTypes) {
      return selfType;
   }

   @Override
   public Object eval(Object self, Object[] arguments) {
      return Math.floor(asDouble(self));
   }

   public static final class Builder extends AbstractSimpleMethodBuilder<Floor> {
      public Builder(InstantiationContext context, Config<?> config) {
         super(context, config);
      }

      @Override
      public Floor build(Expr expr, SearchExpression self, SearchExpression[] args)
            throws ConfigurationException {
         checkNoArguments(expr, self, args);
         return new Floor(getConfig().getName(), self, args);
      }
   }
}

Configuration

The builder for the function implementation is registered in the application configuration in the section com.top_logic.model.search.expr.config.SearchBuilder. The above function could be registered as my_floor as follows:

<config service-class="com.top_logic.model.search.expr.config.SearchBuilder">
   <instance>
      <methods>
         <method name="my_floor" class="my.package.Floor$Builder"/>
      </methods>
   </instance>
</config>

Usage

Like built-in functions, custom functions can be called using the name assigned in the configuration. To prevent name conflicts with future updates, it is recommended to use a name prefix like my_ in the configuration example above.

The function registered above can then be called as follows:

my_floor(4.2)

The expected result would then be 4.