Red Hat Training

A Red Hat training course is available for Red Hat Fuse

8.6. Knowledge Services

8.6.1. Knowledge Services

Knowledge Services are SwitchYard services that leverage Knowledge, Innovation and Enterprise (KIE) and provide knowledge based content as outputs. The Knowledge Services leverage Drools and jBPM. Drools and jBPM are tightly integrated under KIE, and hence both SwitchYard's BPM component and Rules component share most of their runtime configuration.

8.6.2. Actions

When you invoke a SwitchYard operation, it results in the execution of the corresponding Action. Actions are how Knowledge Services know how to map service operation invocations to their appropriate runtime counterparts. For example, when you invoke a method myOperation, it may result in execution of actions like execute some business rules or start a business process. Here is an example of Actions attribute illustrating myOperation method and an action of type ACTION_TYPE:
<actions>
 <action id="myId" operation="myOperation" type="ACTION_TYPE">
  <globals>
   <mapping/>
  </globals>
  <inputs>
   <mapping/>
  </inputs>
  <outputs>
   <mapping/>
  </outputs>
 </action>
</actions>
Here, the placeholder ACTION_TYPE can hold values for the actual ActionTypes specific to the BPM or Rules components.

8.6.3. Mappings

You can use Mappings to move data in or out of the action for an operation. You can specify as many mappings as you like for an action. The mappings are categorized as global, input, and output mappings:
  • Global Mappings: Use the global mappings to provide data that is applicable to the entire action. You can use them in an in/out parameter or a data-holder/provider structure. An example of a global mapping is a global variable specified within a Drools Rule Language (DRL) file.
  • Input Mappings: Use the input mappings to provide data that represents parameters provided to an action. An example of an input mapping for BPM is a process variable used while starting a business process. An example of an input mapping for Rules is a fact to insert into a rules engine session.
  • Output Mappings: Use the output mappings to return data out of an action. An example of an output mapping is a BPM process variable that you want to set as the outgoing (response) message’s content.

8.6.4. MVEL expressionType

The mappings support a default expressionType called MVEL. You can use following variables with MVEL:
  • exchange: The current org.switchyard.Exchange.
  • context: The current org.switchyard.Context.
  • message: The current org.switchyard.Message.
Here are some examples of the expressions using default variables:
expression="message.content" - This is the same as message.getContent().
expression="context[‘foo’]" scope="IN" - This is the same as context.getProperty("foo", Scope.IN).getValue(), in a null-safe manner.
Note
Specify the scope attribute only if you use the context variable inside your expression. If you do not specify a scope, the default Context access is done with Scope.EXCHANGE for global mappings, Scope.IN for input mappings, and Scope.OUT for output mappings.
It is important to specify a global variable for a rule, or a process variable to put into (or get out of) a BPM process. However, if you need to use the result of an expression as facts for rule session insertion, then you need not specify a variable name. Here is an XML example of how you can specify variable name:
 <mapping expression="theExpression" expressionType="MVEL" scope="IN" variable="theVariable"/>

8.6.5. Channels

Drools support the use of Channels that are the exit points in your Drools Rule Language (DRL) file. Channels must implement org.kie.runtime.Channel. Here is an example illustrating the use of channels in a method:

package com.example
rule "example rule"
    when
        $f : Foo ( bar > 10 )
    then
        channels["Bar"].send( $f.getBar() );
end

The following example illustrates use of channels in XML:

<channels>
    <channel class="com.example.BarChannel" name="Bar"/>
</channels>

8.6.6. SwitchYard Service Channel

SwitchYard provides an out-of-the-box channel called SwitchYard Service channel, which allows you to invoke (one-way) other SwitchYard services directly and easily from your DRL. Here is an example:

<channel name="HelloWorld" reference="HelloWorld" operation="greet"/>

Here,
  • class: The channel implementation class. Default value is SwitchYardServiceChannel.
  • name: The channel name.
  • reference: The service reference qualified name.
  • operation: The service reference operation name.
  • input: The service reference operation input name.

8.6.7. Listeners

You can use Listeners to monitor specific types of events that occur during Knowledge execution. For example, you can use listeners to audit a BPM process and save the audit details into a database while the process progresses. The listener then reports the audit details at a later time. Drools and jBPM provide many out-of-the-box listeners. If you write your own listener, ensure that it implements java.util.EventListener.
To register your listener, you must implement one of the KIE/Drools/jBPM Listener interfaces. For example:
  • org.drools.event.WorkingMemoryEventListener
  • org.drools.event.AgendaEventListener
  • org.kie.event.process.ProcessEventListener
Here is an example of listener implementation:

<listeners><listener class="org.drools.event.DebugProcessEventListener"/>
<listener class="org.kie.event.rule.DebugWorkingMemoryEventListener"/>
<listener class="com.example.MyListener"/>
</listeners>

8.6.8. Loggers

Loggers are special types of listeners, which you can use to output the events that occur during Knowledge execution. Support for loggers use a dedicated configuration element. You can log events to the console or a file. If events log are directed to a file, you can open those logs with the JBoss Developer Studio or JBoss Developer Studio Integration Stack. Here is an example of a logger implementation:
<loggers>
    <logger interval="2000" log="myLog" type="THREADED_FILE"/>
    <logger type="CONSOLE/>
</loggers>