JBoss.org Community Documentation

- Prev
- Next

## Chapter 2. Spring Integration

## 2.1. Integration with Drools Expert

In this section we will explain the drools namespace.

### 2.1.1. Define a Resource

Execution nodes are a context to regsiter ksessions and kbases against for lookup.

#### 2.1.1.1. <drools:execution-node>'s parameters as attributes:

Table 2.1.

Attribute | Description | Required
--- | --- | ---
id | Bean's id is the name to be reference from other beans. | Yes

#### 2.1.1.2. <drools:resource>'s parameters as attributes:

Table 2.2.

Attribute | Description | Required
--- | --- | ---
id | Bean's id is the name to be reference from other beans. | Yes, but can be omitted when this tag is nested.
source | The path to the file containing the actual resource. | Yes
type | Type of the Resource as is defined in class `ResourceType`. | No, default to DRL

#### 2.1.1.3. <drools:resource>'s parameters as nested elements:

When configuring a decision table resource you can nest a <drools:decisiontable-conf> tag to configure the resource.

Table 2.3.

Attribute | Description | Required
--- | --- | ---
input-type | Decision Table input type: XLS or CSV | No
worksheet-name | The worksheet name to be used in the when using an XLS | No

Example 2.1. resource definition example

    <drools:resource source="classpath:org/drools/IntegrationExampleTest.xls"

                     type="DTABLE">

       <drools:decisiontable-conf input-type="XLS" worksheet-name="Tables_2" />

    </drools:resource>

#### 2.1.1.4. Define a KnowledgeBase

##### 2.1.1.4.1. <drools:kbase>'s parameters as attributes:

Table 2.4.

Attribute | Description | Required
--- | --- | ---
id | Bean's id is the name to be referenced from other beans. | Yes
node | Execution-Node context to register the kbase with | no

##### 2.1.1.4.2. <drools:kbase>'s parameters as nested elements:

A Knowledge Base has a 0..n resources. Has two nested elements, resources and configuratoins. Thoe resources can be inner elements or references, using the "ref" attribute.

Example 2.2. kbase definition example

    <drools:kbase id="kbase1" node="node1">

       <drools:resources>

          <drools:resource source="classpath:org/drools/spring/IntegrationExampleTest.xls"

                        type="DTABLE">

             <drools:decisiontable-conf input-type="XLS" worksheet-name="Table_2" />

          </drools:resource>

          <drools:resource ref="resource1"/>

          <drools:resource source="classpath:org/drools/container/spring/model.xsd" />

       </drools:resources>

       <drools:configuration>

           <drools:mbeans enabled="true" />

          <drools:accumulate-functions>

             <drools:accumulate-function name="func1" ref="func1Instance" />

             <drools:accumulate-function name="func1" ref="func2Instance" />

          </drools:accumulate-functions>

       </drools:configuration>

    </drools:kbase>

- advanced-process-rule-integration

  - enabled = true : false

- multithread

  - enabled = true : false

  - max-threads = 1..n

- mbeans

  - enabled = true : false

- event-processing-mode

  - mode = STREAM : CLOUD

- accumulate-functions

  - accumulate-function 0..n

    - name = String

    - ref = String

- evaluators

  - evaluator 0..n

    - name = String

    - ref = String

- assert-behavior

  - mode = IDENTITY : EQUALITY

Figure 2.1. Knowledge Base Configuration Options

#### 2.1.1.5. Define KnowledgeSessions

##### 2.1.1.5.1. <drools:ksession>'s parameters as attributes:

Table 2.5.

Attribute | Description | Required
--- | --- | ---
id | Bean's id is the name to be referenced from other beans. | Yes
type | is the session stateful or stateless? | Yes
name |  | No; defaults to id when omitted.
node | Execution-Node context to register the ksession with | no
listeners | Specifies the reference to the event listeners group (see 'Defining a Group of listeners' section below). | no

Example 2.3. ksession definition example

    <drools:ksession id="ksession1" type="stateless"

                      name="stateless1" kbase="kbase1"/>

    <drools:ksession id="ksession2" type="stateful" kbase="kbase1"/>

    <drools:ksession id="ksession3" type="stateful" kbase="kbase2>

       <drools:batch>

          <drools:insert-object ref="person" />

          <drools:set-global identifier="list1">

              <bean class="java.util.ArrayList" />

          </drools:set-global>

          <drools:startProcess process-id="start fire">

       </drools:batch>

       <drools:configurations>

         <drools:keep-reference enabled="false" />

         <drools:clock-type type="PSEUDO"  />

       </drools:configurations>

    </drools:ksession>

- keep-reference

  - enabled = true : false

- clock-type

  - type = REALTIME : PSEUDO

- jpa-persistence

  - transaction-manager

    - ref = String

  - entity-manager-factory

    - ref = String

  - variable-persisters

    - for-class = String

    - implementation = String

Figure 2.2. Knowledge Session Configuration Options

- insert-object

  - ref = String (optional)

  - Anonymous bean

- set-global

  - identifier = String (required)

  - reg = String (optiona)

  - Anonymous bean

- fire-all-rules

  - max : n

- fire-until-halt

- start-process

  - parameter

    - identifier = String (required)

    - ref = String (optional)

    - Anonymous bean

- signal-event

  - ref = String (optional)

  - event-type = String (required)

  - process-instance-id =n (optional)

Figure 2.3. Initialization Batch Commands

Example 2.4. ksession JPA configuraiton example

    <drools:kstore id="kstore" /> <!-- provides KnowledgeStoreService implementation -->

    <bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

       <property name="dataSource" ref="ds" />

       <property name="persistenceUnitName" value="org.drools.persistence.jpa.local" />

    </bean>

    <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">

       <property name="entityManagerFactory" ref="myEmf" />

    </bean>

    <drools:ksession id="jpaSingleSessionCommandService" type="stateful"  kbase="kbProcessWorkItems">

       <drools:configuration>

          <drools:jpa-persistence>

             <drools:transaction-manager ref="txManager" />

             <drools:entity-manager-factory ref="myEmf" />

          </drools:jpa-persistence>

       </drools:configuration>

    </drools:ksession>

##### 2.1.1.5.2. <drools:ksession>'s parameters as nested elements:

When configuring a Event Listener resource you can nest a <drools:eventListener> tag to configure the listener.

### 2.1.2. Event Listeners

Drools supports adding 3 types of listeners to KnowledgeSessions - AgendaListener, WorkingMemoryListener, ProcessEventListener

The drools-spring module allows you to configure these listeners to KnowledgeSessions using XML tags. These tags have identical names as the actual listener interfaces i.e., <drools:agendaEventListener....>, <drools:workingMemoryEventListener....> and <drools:processEventListener....>.

drools-spring provides features to define the listeners as standalone (individual) listeners and also to define them as a group.

#### 2.1.2.1. Defining Stand alone Listeners:

##### 2.1.2.1.1. Attributes:

Table 2.6.

Attribute | Required | Description
--- | --- | ---
ref | No | A reference to another declared bean.

Example 2.5. Listener configuration example - using a bean:ref.

      <drools:resource id="resource1" type="DRL" source="classpath:org/drools/container/spring/testSpring.drl"/>

      <drools:kbase id="kbase1">

        <drools:resources>

          <drools:resource ref="resource1"/>

        </drools:resources>

      </drools:kbase>

      <bean id="mock-agenda-listener" class="org.drools.container.spring.MockAgendaEventListener" />

      <bean id="mock-wm-listener" class="org.drools.container.spring.MockWorkingMemoryEventListener" />

      <bean id="mock-process-listener" class="org.drools.container.spring.MockProcessEventListener" />

      <drools:ksession id="statefulSession" type="stateful" kbase="kbase1" node="node1">

        <drools:agendaEventListener ref="mock-agenda-listener"/>

        <drools:processEventListener ref="mock-process-listener"/>

        <drools:workingMemoryEventListener ref="mock-wm-listener"/>

      </drools:ksession>

##### 2.1.2.1.2. Nested Elements:

- bean

  - class = String

  - name = String (optional)

Example 2.6. Listener configuration example - using nested bean.

      <drools:resource id="resource1" type="DRL" source="classpath:org/drools/container/spring/testSpring.drl"/>

      <drools:kbase id="kbase1">

        <drools:resources>

          <drools:resource ref="resource1"/>

        </drools:resources>

      </drools:kbase>

      <drools:ksession id="statefulWithNestedBean" type="stateful" kbase="kbase1" node="node1">

        <drools:agendaEventListener>

          <bean class="org.drools.container.spring.MockAgendaEventListener"/>

        </drools:agendaEventListener>

        <drools:workingMemoryEventListener>

          <bean class="org.drools.container.spring.MockWorkingMemoryEventListener"/>

        </drools:workingMemoryEventListener>

        <drools:processEventListener>

          <bean class="org.drools.container.spring.MockProcessEventListener"/>

        </drools:processEventListener>

      </drools:ksession>

##### 2.1.2.1.3. Empty Tag : Declaration with no 'ref' and without a nested bean

When a listener is defined without a reference to a implementing bean and does not contain a nested bean, <drools:workingMemoryEventListener/> the underlying implementation adds the Debug version of the listener defined in the API.

The debug listeners print the corresponding Event toString message to System.err.

Example 2.7. Listener configuration example - defaulting to the debug versions provided by the Knowledge-API .

      <drools:resource id="resource1" type="DRL" source="classpath:org/drools/container/spring/testSpring.drl"/>

      <drools:kbase id="kbase1">

        <drools:resources>

          <drools:resource ref="resource1"/>

        </drools:resources>

      </drools:kbase>

      <drools:ksession id="statefulWithDefault" type="stateful" kbase="kbase1" node="node1">

        <drools:agendaEventListener/> <!-- attaches the org.drools.event.rule.DebugAgendaEventListener -->

        <drools:workingMemoryEventListener/> <!-- attaches the org.drools.event.rule.DebugWorkingMemoryEventListener -->

        <drools:processEventListener/> <!-- attaches the org.drools.event.DebugProcessEventListener -->

      </drools:ksession>

##### 2.1.2.1.4. Mix and Match of different declaration styles

The drools-spring module allows you to mix and match the different declarative styles within the same KnowledgeSession. The below sample provides more clarity.

Example 2.8. Listener configuration example - mix and match of 'ref'/nested-bean/empty styles.

      <drools:resource id="resource1" type="DRL" source="classpath:org/drools/container/spring/testSpring.drl"/>

      <drools:kbase id="kbase1">

        <drools:resources>

          <drools:resource ref="resource1"/>

        </drools:resources>

      </drools:kbase>

      <bean id="mock-agenda-listener" class="org.drools.container.spring.MockAgendaEventListener" />

      <drools:ksession id="statefulWithMixAndMatchOfStyles" type="stateful" kbase="kbase1" node="node1">

         <drools:workingMemoryEventListener>

          <bean class="org.drools.container.spring.MockWorkingMemoryEventListener"/>

        </drools:workingMemoryEventListener>

        <drools:agendaEventListener ref="mock-agenda-listener"/>

        <drools:processEventListener/> <!-- attaches the org.drools.event.DebugProcessEventListener -->

      </drools:ksession>

##### 2.1.2.1.5. Defining multiple listeners of the same type

It is also valid to define multiple beans of the same event listener types for a KnowledgeSession.

Example 2.9. Listener configuration example - multiple listeners of the same type.

      <drools:resource id="resource1" type="DRL" source="classpath:org/drools/container/spring/testSpring.drl"/>

      <drools:kbase id="kbase1">

        <drools:resources>

          <drools:resource ref="resource1"/>

        </drools:resources>

      </drools:kbase>

      <bean id="mock-wm-listener" class="org.drools.container.spring.MockWorkingMemoryEventListener" />

      <drools:ksession id="statefulWithMultipleSameType" type="stateful" kbase="kbase1" node="node1">

        <!-- 2 different implementations of the same listener attached to one ksession -->

        <drools:workingMemoryEventListener>

          <bean class="org.drools.container.spring.ConsoleWorkingMemoryEventListener"/>

        </drools:workingMemoryEventListener>

        <drools:workingMemoryEventListener ref="mock-wm-listener"/>

      </drools:ksession>

#### 2.1.2.2. Defining a Group of listeners:

drools-spring allows for grouping of listeners. This is particularly useful when you define a set of listeners and want to attach them to multiple sessions. The grouping feature is also very useful, when we define a set of listeners for 'testing' and then want to swtich them for 'production' use.

##### 2.1.2.2.1. Attributes:

Table 2.7.

Attribute | Required | Description
--- | --- | ---
ID | yes | Unique identifier

##### 2.1.2.2.2. Nested Elements:

- drools:agendaEventListener...

- drools:workingMemoryEventListener...

- drools:processEventListener...

## Note

The above mentioned child elements can be declared in any order. Only one declaration of each type is allowed in a group.

##### 2.1.2.2.3. Example:

Example 2.10. Group of listeners - example

      <drools:resource id="resource1" type="DRL" source="classpath:org/drools/container/spring/testSpring.drl"/>

      <drools:kbase id="kbase1">

        <drools:resources>

          <drools:resource ref="resource1"/>

        </drools:resources>

      </drools:kbase>

      <bean id="mock-wm-listener" class="org.drools.container.spring.MockWorkingMemoryEventListener" />

      <drools:ksession id="statelessWithGroupedListeners" type="stateless" kbase="kbase1" node="node1" listeners="

    mockListeners
    "/>

      <drools:eventListeners id="mockListeners">

        <drools:agendaEventListener ref="mock-agenda-listener"/>

        <drools:processEventListener ref="mock-process-listener"/>

        <drools:workingMemoryEventListener ref="mock-wm-listener"/>

      </drools:eventListeners>

      <drools:eventListeners id="debugListeners">

        <drools:agendaEventListener/>

        <drools:processEventListener/>

        <drools:workingMemoryEventListener/>

      </drools:eventListeners>

- PrevChapter 1. Apache Camel Integration
- Top of page
- Front page
- NextChapter 3. Drools Server