Skip to content

Rule and function scripts

Vitalii Fedorenko edited this page Nov 13, 2016 · 4 revisions

There are two kinds of files that can be defined in a grules DSL:

Rule script — a script that contains specification of pre-processing rules that must be applied to incoming data. The main purpose of a rules script is to ensure that all external values have been properly pre-formatted and sanitized before updating an application model. Subsequently, after the input is processed by a rules script it may be considered clean.

Function script — a class that defines validation and conversion functions used by the rules scripts.

To be evaluated as a rules script, the file name must end with the suffix Grules. As a functions script you can specify any Java or Groovy class with static methods. For convenience, you can use the class-level @Functions annotation, which adds the static modifier to all class methods at compile time, for example:

@Functions
class StringFunctions {
  String trim(String value) {
    value.trim()
  }

  String contains(String value, String text) {
    value.contains(text)
  }  
}

Then, in the grules script:

import static StringFunctions.*

myParam contains("myText") >> trim