Rules engine Visual Rules Solution development basic tutorial [serial 24] -- use VisualRules rule engine to implement business logic

Implement business logic using VisualRules rule engine


        Using a rules engine can reduce application maintenance and scalability costs by reducing the complexity of components that implement complex business logic. This article shows how to use the VisualRules rules engine to make Java™ applications more adaptable to change. VisualRules has a native rule expression language and a rule editor plugin to make the application of VisualRules easier and faster

        . Most of the complexity imposed on today's software products is behavioral and functional, resulting in component implementations with complex business logic. . The most common way to implement business logic in a J2EE or J2SE application is to write Java code to implement the rules and logic of the requirements document. In most cases, the intricacies of this code make maintaining and updating the business logic of the application a daunting task, even for seasoned developers. Any change, no matter how simple, still incurs recompilation and redeployment costs.

        Rules engines attempt to solve (or at least reduce) the problems and difficulties inherent in the development and maintenance of application business logic. A rules engine can be thought of as a framework for implementing complex business logic. Most rule engines allow you to use declarative programming to express results that are valid for some given information or knowledge. You can focus on what is known to be true and its consequences, which is the business logic of the application.

        There are multiple rules engines available, including commercial and open source options. Business rules engines typically allow the use of a dedicated English-like language to express rules. Other rules engines allow rules to be written in scripting languages ​​such as Groovy or Python. This article introduces you to the VisualRules rules engine and uses sample programs to help you understand how to use VisualRules as part of the business logic layer in a Java application.


1. VisualRules version and feature description

       VisualRules is a business rules engine written in the Java language. VisualRules allow business logic to be expressed declaratively. Rules can be written in a unique local language for easy learning and understanding. And, you can also embed Java code directly into the rules file, which makes learning VisualRules even more attractive. VisualRules also has other advantages:
       • Very sound technical support
       • Ease of use
       • Fast execution speed
       • Rules are compiled into Java code, cross-platform
       • More than 10 years of dedicated R&D investment
       • Commercial after-sales service, making problems better 2. Setting up the


virtual scene

The following assumptions set the scene for the fictional problem that the application solves:
       • A company named XYZ builds two types of computer machines: Type1 and Type2. Machine types are defined by their architecture.
       • XYZ computers can provide a variety of functions. Four functions are currently defined: DDNS Server, DNS Server, Gateway, and Router.
       • XYZ performs multiple tests on each machine before it is shipped.
       • The tests performed on each machine depend on the type and function of each machine. Currently, five tests are defined: Test1, Test2, Test3, Test4, and Test5.
       • When assigning a test to a computer, assign the test due date to that machine as well. Tests assigned to computers cannot be executed after this due date. The expiration date value depends on the test assigned to the machine.
       • XYZ automates much of the process of performing tests using an in-house developed software application that determines machine type and functionality. Then, based on these properties, the application determines which tests to execute and their due dates.
       • Currently, the logic for assigning tests and test expiration dates to computers is part of the compiled code for the application. The components that contain this logic are written in the Java language.
       • The logic for assigning test and due dates changes multiple times a month. When developers need to implement this logic in Java code, they have to go through a tedious process.
       Because of the high cost to the company when making changes to the logic that assigns tests and due dates to computers, Director XYZ has asked software engineers to find a flexible way to "push changes to business rules with minimal effort". " to the production environment. So VisualRules took to the stage. Engineers decided they could save more time and effort if they used a rules engine to express the rules that determine which tests should be executed. They will only need to change the content of the rules file and then replace that file in production. For them, this is much simpler and less time-consuming than changing the compiled code and going through a lengthy process mandated by the organization when deploying the compiled code to production.
       Currently, the following business rules must be followed when assigning tests and due dates to machines:
       • If the machine is Type1, only Test1, Test2, and Test5 can be executed on it.
       • If the computer is Type2 and one of the functions is DNS Server, then Test4 and Test5 should be performed.
       • If the computer is Type2 and one of the functions is DDNS Server, then Test2 and Test3 should be performed.
       • If the computer is Type2 and one of the functions is Gateway, then Test3 and Test4 should be performed.
       • If the computer is Type2 and one of the functions is Router, Test1 and Test3 should be executed.
       • If Test1 is one of the tests to be performed on the computer, the test expiration date is 3 days from the machine's creation date. This rule takes precedence over all of the following rules for the test due date.
       • If Test2 is one of the tests to be performed on the computer, the test expiration date is 7 days from the machine's creation date. This rule takes precedence over all of the following rules for the test due date.
       • If Test3 is one of the tests to be performed on the computer, the test expiration date is 10 days from the machine's creation date. This rule takes precedence over all of the following rules for the test due date.
       • If Test4 is one of the tests to be performed on the computer, the test expiration date is 12 days from the machine's creation date. This rule takes precedence over all of the following rules for the test due date.
       • If Test5 is one of the tests to be performed on the computer, the test expiration date is 14 days from the machine's creation date.
       The current Java code that captures the above business rules for assigning tests and test due dates to machines is as follows:


3. Implementing the business logic using Java code Implementing

the business rule logic using an if-else statement
Machine machine = ...
                        // Assign tests
                        Collections .sort(machine.getFunctions());
                        int index;
                        if (machine.getType().equals("Type1")) {
                        Test test1 = ...
                        Test test2 = ...
                        Test test5 = ...
                        machine.getTests().add(test1);
                        machine.getTests().add(test2);
                        machine.getTests().add(test5);
                        } else if (machine.getType().equals("Type2")){
index = Collections.binarySearch(machine.getFunctions(), "Router");
                        if (index >= 0) {
                        Test test1 = ...
                        Test test3 = ...
                        machine.getTests().add(test1);
                        machine.getTests().add(test3);
                        }
                        index = Collections.binarySearch(machine.getFunctions(), "Gateway");
                        if (index >= 0) {
                        Test test4 = ...
                        Test test3 = ...
                        machine.getTests().add(test4);
                        machine.getTests().add(test3);
                        }
                        ...
                        }
                        // Assign tests due date
                        Collections.sort(machine.getTests(), new TestComparator());
                        ...
                        Test test1 = ...
                        index = Collections.binarySearch(machine.getTests(), test1);
                        if (index >= 0) {
                        // Set due date to 3 days after Machine was created
                        Timestamp creationTs = machine.getCreationTs();
                        machine.setTestsDueTime(...);
                        return;
                        }
                        index = Collections.binarySearch(machine.getTests(), test2);
                        if (index >= 0) {
                        // Set due date to 7 days after Machine was created
                        Timestamp creationTs = machine.getCreationTs();
                        machine.setTestsDueTime(...);
                        return;
                        }
                        ...

       上述所示代码不是太复杂,但也并不简单。如果要对其进行更改,需要十分小心。一堆互相缠绕的 if-else 语句正试图捕获已经为应用程序标识的业务逻辑。如果您对业务规则不甚了解,就无法一眼看出代码的意图。
       使用VisualRules规则配置的方式表示上述定义的业务规则。它包含以下内容:
       1. VisualRules规则配置器。
       2. JDk1.6
       3. Tomcat5
       4. VisualRules核心引擎
       如图所示:

       1. VisualRules规则配置器





       JDK,Tomcat,核心引擎将不做展示,使用即可
       2.创建规则工程





       打开规则配置器,点文件菜单,选择新建规则工程,工程名为:机器功能测试,存放路径可自由选择
       规则工程创建完成后,我们再为当前场景创建一个规则包,在规则工程上面点右键,选择:新建规则包,并命名为test。





       在规则包创建完成后,我们再把规则对象添加到该规则包的对象库中。




       对象添加完成后,我们就可以来配置业务规则了。





       如上图所示:我们按照测试项目的优先级来配置规则,一共有5个测试项目。
       在规则配置完成以后,按照业务逻辑可以在规则中进行单元测试,例如:我们设定计算机为:type2,测试功能为:DDNS Server,机器创建日期为:2013-05-14,那么在规则执行完成后,我们可以得到如下的测试结果。





       从测试的结果可以看出:
       针对type2这台机器我们需要进行2个测试
       1:在2013-05-07对它进行test2测试
       2:在2013-05-04对它进行test3测试

       VisualRules规则文件
       如上面我们看到的规则配置,VisualRules规则文件可以包含一个或多个规则集或者规则。每个规则集或者规则中又可以包含一条或多条规则。
       结束语
       使用规则引擎可以显著降低实现 Java 应用程序中业务规则逻辑的组件的复杂性。使用规则引擎以声明方法表达规则的应用程序比其他应用程序更容易维护和扩展。正如您所看到的,VisualRules是一种功能强大的灵活的规则引擎产品。使用VisualRules的特性和能力,您可以灵活的配置应用程序的复杂业务逻辑。VisualRules采用中文化的规则配置方式使得学习和使用VisualRules规则引擎产品对业务人员来说变得相当容易。

Guess you like

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