Process Rule

In this tutorial, we will create a process rule and test it

Use case

In this rule, we will reuse three simple rules:

  • Rule for deciding customer type
  • Rule for choosing rate according customer type
  • Rule for calculate discount for customers who is a teacher

The use case is, according a customer information, such as age and sex type and is a teacher or not, to calculate a final fee.

Steps

  • Open the Process Designer

    Open browser https://dev.ebaocloud.com/rule-web/ then input your url according to the case.

  • Click Rule Designer menu and select Process Rule in New drop down button. In above screenshot, you can see the process rule designer which can be split into further three areas

    • Process node sidebar

      In sidebar, you can choose common node such as Start, End, JS Script nodes. you also see some simple rule nodes belong to your tenant

    • Draw canvas You can drop some nodes from sidebar to canvas.

    • Properties editor panel In the editor panel, you can do some configuration for the nodes.

  • Select nodes from sidebar, and connect it like follow images:

    [!TIP] If simple rule node does'nt found then you should use simple rule editor to create and publish.

Node Configuration

Now we check every node/connection in the process

decideCustomerType

This simple rule will use age and sex type to decide customer quality type, it require two parameters and return String as rule result. In the property editor, switch of age and man are open, that means rule engine will auto get value by process variable/rule require variable which has same name.

The value of rule in runtime will saved as process variable with name customerType, and it will be used in next steps.

rateByCustomizeType

This simple rule will give fee rate according to customer type, it requires one parameter and return a double.

  • Type

    It will use the process variable named customerType as right switch is closed.

  • Result

    It will be saved to process variable named rate.

discountForTeacher

This simple rule will calc a discount for teacher according school. It require one parameter named school and give a double result.

  • School

It will use expression of @{request.school}, it means get the value from request.

  • Result

It will saved rule result to process variable named discount

Script Node One

Sometimes, it is convenient to use javascript to define complex logic. this script node is used to calculate the final fees:

var initFee=execution.getVariable('initFee') console.log('the init fee is [' + initFee + ']') var discount=execution.getVariable('discount') console.log('the discount is' + discount) var value=initFee-discount console.log(value) execution.setVariable('totalFee',value)

Script Node two

This node define a logic for another path

console.log('use fix discount ') var initFee=execution.getVariable('request.initFee') var discount=execution.getVariable('rate') * 1000 execution.getVariable('totalFee',initFee-discount)

Path one

One node can have multi out path, that means if condition matched, process engine will choose this path to execute. The condition on this path is defined by ${isTeacher}

Path Two

This Path will be choosed by process engine when condition ${!isTeacher} matched.

Save

You can click Save button in a toolbar.

Test

You can click Test button in a toolbar, test dialog will be shown like this:

The test data is:

{ "data": { "school": "XJTU", "age": 35, "man": true }, "variables": { "isTeacher": true, "initFee": 3000 }, "resultVariable": "totalFee" }
  • Data

    This data can be referenced in process engine by @{request.xxx}, such as @{request.school}. or auto bind as simple rule parameters.

  • Variables

    This variables will be push to process instance as process variable. so your can use process variable by name directly or by expression like ${isTeacher}

[!TIP |what is difference about data and variables ] There are no big difference, data means business data or struct. it can be a complex json object. However, variablemeans runtime config for this rule, it is a key/value struction.

  • ResultVariable

    This value is process variable name, means that rule engine should use process variable value as rule result finally.

After click the Run button, the execute result will be shown in the response area:

{ "success": true, "errMessage": null, "value": 2000, "uuid": "a2c5d54d-ac47-4a79-a8ce-c817b5f584b2", "extData": "172504", "highAction": { "highLightedFlows": [ "id_179ae88d-5bf9-c3f5-1dcc-286323bf69d7", "id_c718cda0-001b-6715-0ea8-42452f39ee23", "id_6ec805eb-730b-8e1d-d157-eb21315b41a3", "id_45a642f8-0dbf-429c-166d-5c8be0da9461", "id_475db1b2-5946-0ab5-9364-90220495cf06" ], "highLightedActivitis": [], "existed": true } }

The success field means that rule call is success or not, and the value field is the final result about current rule call. In this case, the final fee is 2000. You can also switch to trace panel for inner detail about rule execution:

After close the test dialog, you can see execution path of current call.

You can see the parameter value and the result for each simple rule, Eg. for simple rule customerTypeDecideRule, v2 is called, using 218ms, age param value is 28 and man param is true, finally, the simple rule return H as customer type.

Publish

After testing it, you can click Publish button to publish rule. After it will be published, you can call API via gateway and restful API

API call

You can invoke published rule via restful api like this:

var unirest = require("unirest"); var req = unirest("POST", "https://dev.ebaocloud.com/api/rule-service/rules/run"); req.headers({ "authorization": "Basic SW5jb21lLmFkbWluOkw3QklzeXlndkRM" }); req.type("json"); req.send({ "name": "finalDiscountRule4Teachers", "version": 1, "ruleId": null, "data": {}, "variables": { }, "resultVariable": "totalFee" }); req.end(function (res) { if (res.error) throw new Error(res.error); console.log(res.body); });

The request body is like that in test step, other tips are:

  • POST method should be used.
  • You should assign name or ruleId, use ruleId if both are provided.
  • If name provided, version can be optional, when call rule api without version,latest version with some rule name in current tenant will be used.
  • For other languages such as java, pls write your request codes like above.

results matching ""

    No results matching ""