Rule Mediator

Rule mediator is to integrate the WSO2 Rules component to the WSO2 ESB in order to define dynamic integration decisions in terms of rules.

Syntax

  <rule>

    <ruleset>
        <source [ key="xs:string" ]>
            [ in-Lined ]
        </source>
        <creation>
            <property name="xs:string" value="xs:string"/>*
        </creation>
    </ruleset>

    <session type="[stateless|stateful]"/>*

    <facts>
        <fact name="xs:string" type="xs:string" expression="xs:string" value="xs:string"/>+
    </facts>

    <results>
        <result name="xs:string" type="xs:string" expression="xs:string" value="xs:string"/>*
    </results>

    [ <childMediators>
        <mediator/>*
      </childMediators> ]

  </rule>
        

RuleSet

The source is the only mandatory element of the ruleset. However, the properties required for the creation of the ruleset can also be specified within ruleset. The key attribute is a registry key, which is to lookup the rule script from the registry if the rule script is in the registry. The rule set can be given as a child node of source elemenent. If the rule set is non-XML, you may need to wrap it with a CDATA section inside a XML tag ex: <X><![CDATA[ native code]]></X>

Note : The key or in-lined rule script must be defined. Otherwise, the rule mediator configuration is invalid.

RuleSession

Rule Session specify the stateful or stateless behavior of the rule engine. If this has value 'stateful', then the rule engine will execute rules in a stateful manner, for any other value, it assumes as stateless. There are optional properties, which are used when creating the rule session. Rule session is optional and the default one is stateful.

Facts / Results

  • name - the unique name
  • value - Represents any static value
  • expression - XPath expression for extracting data from the message being passed between ESB components. The default value is the first child of the SOAP Body.
  • type - Defines the type of the input data. There are a few built in types such as Message, Context, Map, POJO and Mediator and there are samples using these. Using the rule-component.conf, you can register new types along with adapters, which are responsible for formulating specified types from the data. If there is no registered adapter for a given type, then, we assume the given type as a valid java class name and uses POJO adapter to formulate the POJO instance whose class is the given type.

Facts specify the facts being used in rule set. It also defines how to formulate facts from the message, etc. Results define how to handle results from the rule set execution. Usually, results adapters enrich the message with results. For example, If you are using POJO adapter (i.e. a java class as the type; the class name of a result), then we transform POJO into an XML and attached to the SOAP message.

UI Configuration

Rule

Figure1: Rule Mediator

Each of configuration elements is described in following section

RuleSource

Rule script can be given as a registry key or an in-lined script. To give rule script as in-lined, you can use editor.

The format (language) of the rule script . If it is a XML, then this attribute should be 'xml'. Any other value, rule mediator assumes that the rule script is in native format. If the rule script is in native format, it should be given by wrapping as a CDATA section inside a XML tag ex: <X><![CDATA[ native code]]></X>.

Note: The key or in-lined rule script must be defined. Otherwise, rule mediator is invalid.

RuleSession

Session Type - Indicates the stateful or stateless behavior of the rule engine. If this has value ' stateful ' , then rule engine will executes rules in a state full manner , for any other value , it assumes as stateless.

property - Those are used when creating rule session.

RuleSet

It is possible to exists properties for rules set creation, registration, deregistration and there are add property options for each.

Example Usage: When using Drool native language (.drl) for rule script, it is needed to provide .dsl file .This can be done using attribute named 'source' and value 'drl'.

<ruleSet>

<creation>

<property name="source" value="drl"/>

</creation>

</ruleSet>

Facts / Results

Input defines what are the facts that should injected to Rule Engine in order to fire rules whereas Output describes what should do with return value form rule engine.

Adding Facts / Results

Figure 2 shows a one senario of addting input

Rule Mediator Inputs width="1319" height="122">

Figure 2 Inputs of the Rule Mediator

Type : According to current implementation valid types are as bellow.

Name : Value Type Static Value or XPath expression

Value / Expression : value - Represents any static value. expression- XPath expression is used to extract data from either external XML source or SOAP envelope. There is no default value.

Registry Key : The registry or Message context key .This is used for getting data from either registry or message context

Registry Browser : If the key is registry resource, the browser to select the resource.

NS Editor : You can click this link to add namespaces if you are providing an expression. You will be provided another panel named 'Namespace Editor' where you can provide any number of namespace prefixes and url

Action : Delete the input out put

Adding Properties

Adding properties option is in RuleServiceProvider , RuleSet and RuleSession. Figure 3 shows UI on adding property.

Property Adding

Figure3: Rule Mediator Properties

Name: Name for the property

Value: The type of the property value.

Action: Delete the property

Example

  1.  <rule>
            <ruleset>
            <source key="rule/sample.xml"/>
            </ruleset>
            <facts>
                <fact name="symbol" type="java.lang.String"
                      expression="//m0:getQuote/m0:request/m0:symbol/child::text()"
                      xmlns:m0="http://services.samples"/>
            </facts>
            <childMediators>
                <send>
                    <endpoint>
                        <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
                    </endpoint>
                    </send>
                    <drop/>
            </childMediators>
    
        </rule>
    
    In the above configuration, Rule script is picked from the registry with key rule/sample.xml. There is a one fact and it is a string variable. Its value is calculated from the current SOAP message using an expression. Rule engine uses these facts to decide what rules should fire.