Rules Engine Visual Rules Solution Development Basic Course [serial 10]--The composition and execution principle of VisualRules

The composition and execution principle of VisualRules


         In the previous blogs, some examples were explained. Here is a brief summary:
1. VisualRulesSolution composes

         VisualRulesSolution products including rule engine, rule service, rule configurator, database configurator, interface configurator, and interface template editing. server, web framework, rule base management system, etc.




1. Rule engine

         The rule engine is the interface for external programs to call the rule package defined by the rule configurator. The rule engine is the bottom-level interface for calling the rule package. The currently implemented rule engine is the interface of the java language. The rule service is to provide an interface for calling rule packages to other languages ​​or systems.
         The rule engine is currently implemented in the java language, and the implemented interfaces include the following:


RuleEngineFactory interface

         The RuleEngineFactory interface is used to instantiate the RuleEngine interface, thereby invoking the execution rule package. The RuleEngineFactory interface mainly has the following methods:
         (1) public static RuleEngineFactory newInstance()
         This method is used to implement a default RuleEngineFactory instance
         (2) public static RuleEngineFactory getInstance(String name)
         This method is used to implement a named RuleEngineFactory instance, because The instance of RuleEngineFactory is to distinguish ClassLoader, that is to say, rule packages in different directories can be loaded in different RuleEngineFactory
         (3) public RuleEngineClassLoader getClassLoader()
         obtains different ClassLoader, and ClassLoader specifies different specified addresses.


RuleEngine interface

         The RuleEngine interface is used to execute rule packages. It mainly has the following methods:
         (1) public abstract int execute(String ruleName)
         Execute the rule package according to the rule package name. The return value is the execution status.
         (2) public abstract void put(String name, Object value)
         passes the parameters required for the execution of the rule package to the rule package.
         (3) public abstract Object get(String name)
         After the execution of the rule package is completed, the value returned after the execution of the rule package is obtained. In addition, the database connection class, database table class, query result set, etc. are also implemented in the rule engine.


2. Rule service

         Rule service is an interface provided to other languages ​​such as C# that can call rule packages. Therefore, the rule service will provide a variety of protocols to meet the invocation of the rule package. At present, the rule service provides the services of the following protocols:
         (1) Soap service
         VisualRulesSolution implements the Soap service of the rule package based on AXIS1.4. This service provides a service named getRule, which includes a string parameter and a return string. value.
The string parameter is an XML-structured text that contains information about calling the rule package. The returned string is an XML-structured text containing the returned information.
         To invoke this rule service requires the ability to package and parse XML-structured parameters and return values. Therefore, VisualRulesSolution provides the client code of rule service invocation for C# language and Java language respectively. The code implements automatic packaging and parsing of XML text.
         (2) Socket communication
         In order to access the rule package more quickly, the rule service opens port 1508 for other programs to call the rule package. When passing through port 1508, an execute method is provided, which has a string parameter and a string return value. The parameter is an XML text containing the rule package invocation information, and the return value is an XML text containing the return value information.
Therefore, VisualRuels also independently develops client classes for packaging and parsing XML text for C# and Java languages. It only needs to call the corresponding rule package through the provided client.
         (3) Servlet service
         Servlet service provides a convenient way to call the rule package of the server. When invoking, various kinds of information required for invoking the rule package are passed through post, and the returned result is returned in an XML-structured text.


3. Database Configurator

         (1) The database configurator is used to read database structure information, including test database connection information, read database table structure, view structure, and stored procedure structure. You can also test the execution of SQL statements.
         (2) The database configurator is used to generate the configuration file of the database connection.
         (3) After reading the database table information, the user can obtain the information of all fields in the table, including field name, type, corresponding java type, primary key, foreign key, length, null, default value, etc. information, and you can also set a description and other information.
         (4) In addition, foreign key information and index information between tables can be obtained.
         (5)取得存储过程的参数以及返回结果的结构信息。
         (6)可以定义模板,根据表结构信息,生成对应的代码。


4.页面模板编辑器

         页面模板编辑器用于生成调用规则包的程序代码。页面模板语言是采用java语言的模板语言,其语言和java语法是一致的,并且可以读取类库中可以取到的java类。同时还有些特定的对象,是模板语言特有的。
         其中exportModel变量,表示此用例的信息。包括用例名称、自定义属性、规则包名等。其中的variables变量是包含了用例中所有的字段信息。
页面模板语言还可以通过CALL、INCLUDE等关键字实现共享模板。


5.页面配置器

         目前VisualRulesSolution将页面配置器的功能集成到了规则配置器中,根据规则配置的字段信息以及页面模板,生成对应的web页面代码。
页面配置包括模板编辑、代码生成、web框架。
         (1)模板编辑
         支持嵌入式java语言
         支持配置信息的读取
         支持出错检查、错误定位
         支持模板的导入、编辑、存储
         支持模板之间的相互调用
         (2)代码生成器
         支持多个模板导入,生成不同的代码
         支持自定义配置属性的编辑
         支持生成不同的语言
         支持模板语言的调试
         (3)web框架
         多种提供各种界面操作所需的JS构件
         窗体显示的框架结构
         菜单显示和管理
         用户管理,包括用户登录、权限分配、信息维护等。
         权限管理,包括权限定义、角色定义、权限分配等。
         日志管理,包括用户操作页面的记录。


6.规则配置器

         规则配置器是VisualRulesSolution的核心功能,目前提供的规则配置器采用C/S的程序运行。通过规则配置器可以开发规则包。同时可以测试规则包运行等。规则配置器还可以连接规则服务器,打开服务器中的规则包进行修改等。
规则配置器支持的功能主要包括:
         规则包对象库支持定义传入传出变量、临时变量、类对象、常量、静态方法、自定义方法。
         自定义方法支持嵌入java语言。
         支持直接导入数据库结构,并自动生成调用数据库的方法。
         规则包支持规则集和规则的定制。
         支持嵌套规则、公共规则、互斥规则、循环规则。
         支持异常处理。
         支持表达式、方法调用、判断语句等语法。
         支持测试用例的定制,自动测试。
         支持导出可执行规则包。


7.规则库管理系统

         规则包的存储可以放在文件系统中,也可以放在数据库中。基于数据库存储的规则系统,称之为规则库管理系统。
规则库管理系统主要有以下功能:
         (1)存储规则库的表结构
         规则库是有一批表组成的,外部程序可以通过接口或者服务,将规则存取到服务器中。这些表结构,可以建立在SQL Server,Oracle,MySQL等数据库中。
         (2)访问规则库的服务程序
         外部程序需要读取并访问规则库中的规则包,比如规则编辑器需要打开规则库中的规则包,需要根据服务程序来进行读取。服务程序包括两种方式,一种是直接在客户端通过访问数据库连接来读取。一种是通过规则库管理系统提供的Servlet服务来提供的。
         (3)在线管理规则
         通过管理系统来管理规则,具体功能包括,用户管理、权限管理、菜单管理、日志管理、规则管理等功能。其中规则管理功能包括:查阅规则、审核规则、发布规则等功能。


二、规则包执行原理

         规则包是通过规则配置器定义的包含所有逻辑处理的执行单位。究竟规则引擎是如何执行规则包的,调用时需要注意哪些问题,这是程序员最关心的。
         规则包在通过规则配置器编辑之后,在实际执行时,是全部对应生成了java类,并且编译成了可执行代码。外部程序通过规则引擎调用规则包时,做的工作只有两件事情,一是将规则包对应的可执行类实例化,二是将数据缓冲区作为参数传递给规则包对应的方法执行。
         以jsp页面通过规则引擎调用规则包为例,如下图所示:






         jsp服务端代码从页面提交中取得数据之后,首先通过规则引擎取得RuleEngine实例类,RuleEngine实例类中,有一个Map类型的属性,这个属性值就是用于和规则包交互的数据缓冲区。Jsp服务端代码将提交的数据以key/value的方式将其放到RuleEngine的缓冲区中。然后RuleEngine将规则包实例化,并且将缓冲区直接传递给规则包的实例类,规则包执行后,会更新缓冲区的数据。Jsp服务端从缓冲区中取得需要返回的数据,传递到前端页面。
         规则包在执行时,其执行原理是首先从缓冲区中取得数据,将其赋值到对象库中的各个变量中,然后依次调用每个规则来执行,执行完毕后,将对象库中变量的值,按照key/value的方式更新到缓冲区中。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326806987&siteId=291194637