# Drools Rules accumulator

## <img src="/files/GDstGorf7iXEFvxyE9Nt" alt="" data-size="line"> Rules accumulator

### Description <a href="#description" id="description"></a>

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](https://www.drools.org/) is the present rule engine implementation and its [rule language](https://docs.drools.org/7.68.0.Final/drools-docs/html_single/index.html#_droolslanguagereferencechapter) can be referenced for use by this transform. [Drools documentation](https://docs.drools.org/7.68.0.Final/drools-docs/html_single/index.html#_welcome).

| Hop Engine | <sup>✓</sup> |
| ---------- | ------------ |
| Spark      | ?            |
| Flink      | ?            |
| Dataflow   | ?            |

### Details

Once all incoming rows have been collected by the Rules Accumulator transform (e.g. - the previous transform 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>"])".

#### Rules Tab

The *Rules* tab is where you enter your rule definition or a reference to the rule file..

| Option                  | Description |
| ----------------------- | ----------- |
| Rules filename          |             |
| Rules script to execute |             |

#### Rules result Tab

Rules result tab defines the layout of the Rules output fields.

| Option             | Description |
| ------------------ | ----------- |
| Result column name |             |
| Result column type |             |

### Example

All Rule Definitions should contain "import org.apache.hop.pipeline.transforms.drools.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  | 1        | Red   |

Rules can be defined and applied:

```highlight
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 transform can be told what fields to pickup from the generated row objects by defining the field Map’s name in the transform’s Results tab. Type conversions can be applied by setting the Result column type as well.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.primeur.com/data-shaper-1.21/knowing-the-data-shaper-designer/pipelines/transforms/rulesaccumulator.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
