SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍

这是 Jerry 2021 年的第 44 篇文章,也是汪子熙公众号总共第 321 篇原创文章。

在 SAP ABAP On-Premises 产品工作过的 ABAP 开发人员,可能都接触或者听说过 Business Rule Framework(简称 BRF,后面还有升级版 BRF+)这个框架。

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第1张图片

SAP BRF+ 主要包含实现存储功能的规则仓库(Rules Repository),以及根据用户输入,分析并执行规则,返回给用户处理结果的规则处理器(Rules Processor)两部分。前台提供的是基于 ABAP Webdynpro 界面的规则编辑器,支持决策表,决策树和公式等多种方式的规则建模方式。规则仓库和规则处理器位于 ABAP 系统后台。

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第2张图片

了解 SAP ABAP BRF+,有助于我们学习其他的业务规则解决方案。比如 SAP Commerce Cloud,选择的是支持 Java Rules Engine API 标准的开源业务规则引擎和企业框架 Drools.

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第3张图片

无论是 SAP ABAP BRF+ 还是 Drools,尽管其技术实现细节不同,但工作场景和实现的需求都一致:

  • 业务规则制定者,在系统里根据建模工具维护业务规则
  • 业务规则解决方案暴露出接口,消费者调用该接口,传入输入,得到业务规则处理器执行后返回的输出。

本文简要介绍 SAP Business Technology Platform(下文简称 SAP BTP)上的 Business Rules Service.

登录 SAP BTP Cockpit,在 Service Marketplace 里找到 Business Rules Service,其描述信息强调,这是一个基于云的业务规则解决方案。

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第4张图片

Jerry 经历了从 SAP HANA Cloud Platform,到 SAP Cloud Platform,再到如今 SAP Business Technology Platform 的变迁史,切身感受到,随着时间的推移,开发人员消费这些云端服务的步骤越来越简捷了。

以前应用开发人员在 Service Marketplace 选择一个服务后,在使用之前,需要手动配置 Entitlements 和进行用户角色分配等工作。而如今使用 SAP BTP 提供的 Boosters,能将这些繁琐的人工配置自动化。

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第5张图片

点击 Boosters 面板里的 Set up account for Workflow Management, 可以一键完成 SAP BTP 上使用 Business Rules 的准备工作。这个辅助器同样也是完成使用 SAP BTP Workflow(工作流)所需配置的入口,后续 Jerry 介绍如何使用 SAP BTP Workflow 服务的文章里还会提到。

该 Boosters 帮助我们自动完成了如下配置工作:

(1)分配服务限额

(2)订阅 Business Rules 管理的 SaaS 应用

(3)创建 Business Rules 服务实例

(4)创建消费 Business Rules 所需的 Destination

(5)给当前用户分配必要的用户角色等权限

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第6张图片

下图是 Boosters 为我们分配好的 Entitlements 里包含的 Business Rules Service Plan:
SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第7张图片

当前登录用户已经被自动分配上了使用 Business Rules 服务所必须的用户角色组:

  • WorkflowManagementAdmin
  • WorkflowManagementBusinessExpert
  • WorkflowManagementDeveloper
  • WorkflowManagementEndUser

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第8张图片

SAP BTP 编辑 Business Rules 的入口,从 Subscriptions 面板里的 Workflow Management 订阅超链接进入:

https://304a49cetrial.cockpit...

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第9张图片

在打开的 Fiori Launchpad 里,选择 Manage Rule Projects:

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第10张图片

创建一个新的项目,取名为 DetermineEquipmentJerry01:

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第11张图片

SAP BTP 上的 Rules Project,包含了 Data Objects,Rule Services,Rulesets 和 Rules 几部分,下面分别介绍。

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第12张图片

  • Data Objects:相当于 ABAP Data Dictionary 定义的数据结构,用来定义业务规则的输入和输出模型。

比如设计一条给新员工入职(onboarding)分配工作电脑的业务规则,输入是员工信息(Employee),输出是分配的电脑型号(EquipmentInfo).

Employee 结构包含的属性:
SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第13张图片

上面的结构可以类比 ABAP Data Dictionary 里描述 Employee 信息的 Structure:

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第14张图片

  • Rules:定义具体的业务规则。SAP BTP Business Rules 支持 Decision Table 和 Text Rule 两种类型的规则模型。

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第15张图片

下面这个决策表包含的两条规则的语义是:

  • 如果员工所在国家代码为 USA,就给其分配 ID 为 106847 的电脑;
  • 如果员工所在国家代码为 DEU,就给其分配 ID 为 106840 的电脑。

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第16张图片

Text Rule 类型的规则,本文不会用到,但将来 Jerry 介绍 SAP BTP Workflow 的文章会提到,所以这里也顺带举个例子。

下图 Text Rule 的语义是,如果电脑的价格大于 800,就将 EmployeeApproval 结构的 ApprovalNeeded 字段设置为 true. 这个字段在后续的 Workflow 文章介绍里,会触发额外的审批流程。

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第17张图片

始终记住我们正在使用的是基于云的 Business Rules 服务,规则模型创建完毕之后,得能够被外界消费才行。

因此新建一个 Rule Service,我们可以将其类比成支持 RFC 调用的 ABAP Remote Enabled Function Module:

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第18张图片

定义这个 Rule Service 的输入(Input)结构为 Employee,输出(Result)结构为 EquipmentInfo. 当然这两个 Data Objects 必须提前创建好。

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第19张图片

最后,创建一个 Rule Set,将上面创建的包含 Decision Table 的 Rule,以及 Rule Service,依次分配给该 Rule Set:

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第20张图片
SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第21张图片

完成 Business Rules 编辑之后,点击 Release Version,发布一个新版本:

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第22张图片

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第23张图片

创建好 Release 版本的 Rule Service 之后,将其部署到 Cloud 运行时环境:

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第24张图片

最后,我们在 SAP API Hub 提供的测试工具里,消费这个 Business Rule Service.

登录 SAP API Hub,搜索关键字 business rule:

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第25张图片

使用 Rule Execution API for Cloud Foundry:

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第26张图片

在使用 HTTP Post 调用部署好的 Rule Service 之前,我们需要先在 API Hub 的测试页面里,新建一个 API Environment,指向被测试 Rule Service 所部署在的 SAP BTP trial 环境。

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第27张图片

创建一个 API Environment,需要完成下列字段的维护。

runtimeurl 字段,将下面 Url 中的 region,替换成实际值:

bpmruleruntime.rule.cfapps..hana.ondemand.com

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第28张图片

比如我使用的 SAP BTP 运行环境的 Region 为 Singapore 即 ap21:

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第29张图片

由于 SAP BTP Business Rules Service 采取 OAuth 2.0 作为服务消费的认证方式,因此还需要在环境设置里,维护 Client ID,Secret 和 tokenurl.

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第30张图片

这三个值从 SAP BTP Cockpit 里获得。给 wm_businessrules 创建一个新的 Service Key:

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第31张图片

在创建好的 Service Key 里,能找到这三个字段的值:

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第32张图片

完成所有必填字段的维护,保存 Environment,命名为 Jerry Business Rule.在 API Hub 测试工具里选择该环境,然后点击 Invoke Rule Service,准备以 HTTP Post 的方式,消费部署在 SAP BTP 上的 Rule Service.

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第33张图片

在 HTTP 请求正文里,维护待调用的 Service ID,Version 和 RevisionVersion,这三个字段值在我们之前做 Release Version 操作时能找到。

其中版本号 1.0.1,需要换算成格式 000001000000000001.

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第34张图片

依次进行测试,首先将请求里的 countryofCompany 字段维护为 USA,得到返回的电脑 ID 为 106847:

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第35张图片

将 countryofCompany 值改成 DEU,得到的电脑 ID 为 106840:

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第36张图片

测试结果和我们之前在 Business Rule 里维护的 Decision Table 里的值一致。

SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍_第37张图片

有了本文 Business Rules 的基础,后续 Jerry 也会简要介绍 SAP BTP 上的 Workflow(工作流),敬请期待。

更多Jerry的原创文章,尽在:"汪子熙":

更多阅读

你可能感兴趣的:(SAP 业务技术平台(BTP) 上的 Business Rules Service 使用介绍)