Pattern 1 - In App Extensions for Cloud Applications
This is the first blog of my
blog series on
Business Rules Consumption Patterns. In this Pattern 1, I will explains how business rules can be used directly and indirectly in your custom applications (which can be deployed on-premise or on-cloud)
Usage Scenario #1
Use SAP Cloud Platform Business Rules in your custom applications
Business Rules can be used directly in custom applications to extend backend business logic that varies often for functional reasons. For that you model, activate and deploy the business rules from web-tool and then use the REST-based rule service APIs to invoke the rules in your application.
- Follow the blog frommuralidaran.shanmugham2 to learn how to author and deploy the rules in SAP Cloud Platform.
- You can also try and build rules on your own in your SAP Cloud Platform trial account.
- Refer my blog to enable to the business rules service in SAP Cloud Platform
Once you have modelled and deployed your business rules in cloud, all you need to do is use
Public RESTful APIs to invoke the rules in your application.
As an example, I will show you how you can call the rule service in SAPUI5 application (in javascript based controller file). But before I explain that let me give you a glimpse of rule artefacts like project, data objects, ruleset, rule and rule-service that are already modelled, activated and deployed in my cloud account. (for more information on what these various rules artefacts and how they are related, refer to the
official documentation)
The rule used in the example is a discount rule – which calculates the discount on the
Product based on
Quantity and
Category of the product.
Product: Data object that is used as input to rules service
DiscountOutputDO: Data object that is used as output to rules service
DiscountRuleservice: This service is deployed in cloud platform and is available as RESTful service which will be used to invoke from the application. Note the Input and Result data object.
DiscountRules: Decision table based rules with conditions based on
Product data object and
DiscountOutputDO as result data object:
Now, let us see how you can invoke the rule service (
DiscountRuleservice) from your HTML5 application. It involves 2 steps: First call the API to get CSRF token and then call the service to invoke the business rules.
// First get the CSRF token
$.ajax({
url: "/bpmrulesruntime/rules-service/v1/rules/xsrf-token",
method: "GET",
headers: {
"X-CSRF-Token": "Fetch"
},
success: function(result, xhr, data) {
var token = data.getResponseHeader("X-CSRF-Token");
//Then invoke the business rules service via public API
$.ajax({
url: " /bpmrulesruntime/rest/v1/rule-services/ShoppingCartPromotionRules/DiscountRuleservice",
method: "POST",
contentType: "application/json",
data: InputPayload,
async: false,
headers: {
"X-CSRF-Token": token
},
success: function(result1, xhr1, data1) {
if (result1 != null) {
//info: the output from rules will be wrapped in result1 object, you can
// access this json object to get the output variable.
result1[0].Discount;
}
}
});
}
});
Few important things to note here:
- First you need to get the CSRF token while using the public API.
- In the URL, you will notice bpmrulesruntime - it is the destination that is already configured if you enable business rules service in your account. If your application is deployed in the same account as business rules then just having bpmrulesruntime in the URL would suffice, else you need to create the destination and use them appropriately via neo-app. json file of your application, as shown in the screenshot here:
- After you successfully get the CSRF token, then you need to make a POST call to invoke the business rules using public API. Here are the details of the parameters:
- content Type: is mandatory and have to be application/json
- data: is the JSON payload matching the Input data type of the rule service.
From the example above, the Input data type of the rule service (DiscountRuleservice) is Product with attributes Name, Category, Quantity, Soldby and Price then your InputPayload to the ajax call would be:
{
"__type__":"Product",
"Category":"Flat Screen TVs",
"Quantity":40,
"Soldby": "Very Best Screens",
"Price":2000
}
Note the
__type__ field. This is mandatory attribute to be provided as input payload to business rules. It is nothing but the data-type name. Additionally, the JSON element names have to be
exact match of the attributes of the data types (!!! Even a lower-case 'b' in Soldby could cause problem !!!)
Refer the
API documentation for more details on other parameters
- Result after invoking the rule can be obtained in the success method. The result is a JSON structure that matches the result data-type of the rule service
From the example above, the result data-type associated with the rule service is DiscountOutputDO with attributes Discount then the result payload structure will look like:
{
"__type__": " DiscountOutputDO ",
"Discount": 10
}
Suggestion: I would suggest running the rule service once with the actual payload in POSTMAN or any other rest client to be sure of the header parameters and the input and output payload you are expecting.
This completes the first blog of the series. In this blog, I focussed on
Consumption Pattern #1 and showed you how-to consume business rules service using public APIs from your custom application. The consumption involved 3 main steps (a) configuring the
neo-app.json for
bpmrulesruntime destination (b) getting the xsrf token and (c) invoking the service. Setup and use the sample application to learn more about this consumption pattern.
Sample Reference Application
For reference, you can download sample rules project from
GitHub and import it in your cloud platform account. To do so,
- first import ShoppingCartPromotionRules.zip rules project in your business rules editor & deploy the DiscountRuleservice and then deploy the shoppingcart.zip application
(Follow the README instructions to setup the sample rules and application)
Related Blogs
SAP Cloud Platform Business Rules – Extensions and Consumption Patterns
Embed Business Rules SAPUI5 control in your custom applications
Using Business Rules in SAP Cloud Platform Workflow