Rule Accumulator

Sample: rules-golfer.ktr

Overview

The Rules Accumulator collects incoming rows and executes them against a rule set. This may be useful to determine the answer to a question or otherwise analyze a dataset.

Drools is the present rule engine implementation and its rule language can be referenced for use by this step. Drools documentation

Details

Once all incoming rows have been collected by the Rules Accumulator step (e.g. - the previous step shuts down) the rows are transformed into Rules.Row objects and passed into the rules engine to be executed against the given rule set.

Rules.Row is defined as a key / value Map of fields where key is the name of the field and value is the value of the field; as well as the externalSource boolean property to indicate whether the Rules.Row object was created in the rule set or injected from an external source.

Fields of a row are accessed as "Row (column["<fieldname>"])".

Example

All Rule Definitions should contain "import org.pentaho.di.trans.steps.rules.Rules.Row;" to give access to the Rules.Row class.

For the input with a row meta: name (String), position (Integer), color (String); a Rules.Row object will be created for each row with a Map containing those fields.

Rules.Row->row (Map)

name

position

color

 

Fred

1

blue

 

Fred

2

red

 

Bob

1

blue

 

Bob

2

blue

Rules can be defined and applied:

 rule "Golfers problem"
    dialect "mvel"
    when

	# Define Fred
	$fred : Row ( externalSource == true,
		column["name"] == "Fred"
	)

	# Define Bob
	$bob : Row ( externalSource == true,
		column["name"] == "Bob",
		column["position"] != $fred.column["position"],
		column["color"] == "blue",
		column["color"] != $fred.column["color"],
	)

    then

      Row fredRow = new Row();
      Row bobRow = new Row();

	fredRow.addColumn("name", "Fred");
	fredRow.addColumn("position", $fred.column["position"]);
	fredRow.addColumn("color", $fred.column["color"]);

	bobRow.addColumn("name", "Bob");
	bobRow.addColumn("position", $bob.column["position"]);
	bobRow.addColumn("color", $bob.column["color"]);


      insert(fredRow);
      insert(bobRow);

end

Rules.Row objects can be checked as in the above's left hand side.

Rules.Row objects can be created for the data stream as in the above's right hand side.

The step can be told what fields to pickup from the generated row objects by defining the field Map's name in the steps Results tab. Type conversions can be applied by setting the Result column type as well.