Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / All-Topics

Externalize your business rules

3.18/5 (20 votes)
21 May 2007CPOL12 min read 1   356  
This article highlights the advantages of rules-based applications over traditional systems development.

Abstract

This basic article highlights the advantages of rules-based applications over traditional systems development.

The last years have seen an increasing interest in a new type of software called Business Rules Management Systems (BRMS). These are systems to externalize business rules and to provide a facility for centralized business rule management. Rules-based applications address many of the urgent needs posed by every business organization: to change their business rules in order to adapt to accelerated business changes, and to overcome the restricting nature of slow IT change responses.

This article will show how benefits are derived from a rules-based processing system to achieve these goals. The first part provides an overview of rules-based design, and the second part explains how you can build rules-based applications using Smart Rules Lite, the free version of Smart Rules by Kontac.net.

Introduction

In recent years, a new type of software called Business Rules Management Systems (BRMS) has emerged. BRMS are expert systems, designed to provide flexible solutions to complex problems, allowing to accommodate new knowledge easily.

BRMS facilitates the task of separating the policy definitions from implementation and code details. By incorporating a BRMS, companies can support changes and cycles more effectively than traditional methods, clarifying the relation between business rules and business policies.

Why use BRMS?

Most of the complexities that requirements impose on today's software products are behavioral and functional, resulting in component implementations with complex business logic. Ideally, the business logics are subdivided into modules that are encapsulated in individual objects which are visible to all objects interested in the policy. Such objects encapsulate global or organizational policies, while rulesets that pertain to specific classes (such as clients or products) are stored within those objects.

With a BRMS, the business logic is separated out, and can be changed without impacting the remainder of the application. Separating the rules from the application implies that BRMS systems will store the knowledge in a centralized repository, and have them executed by independent applications. Ideally, a good BRMS should allow separated rulesets to be maintained and kept under version control.

Some benefits of using BRMS solutions are:

  • Flexibility: More and more businesses are enabling their applications to behave differently for different classes of customers or for individual customers. This may involve the same basic application that applies different rules based upon each customer's contract terms.
  • Agility: Implementing BRMS makes the process of changing business logic much easier, because change requests can be implemented without changes to program code by isolating the change, allowing to quickly deploy the rule changes into production and have them immediately accessible by applications to be executed at or near real-time.
  • Centralization: In rules-based applications, all of the business logic is located outside of the compiled application, in a separated repository, being scattered across the enterprise.

BRMS allow business analysts and even users to create, understand, and maintain the rules and policies of the business. With BRMS, responsibility for business rule implementation is transferred from developers back to where it should be, the business people. Business people have control on exactly how their business rules are being executed, and, perhaps more importantly, the system is designed to facilitate change when business rules change.

The components and technical features of a BRMS

A BRMS is a suite of tools that policy managers and software engineers use to build systems in which business policies are abstracted out of software code, where they can be directly authored, modified, and managed independently of the underlying software system.

BRMSs are related to expert systems, sometimes called knowledge-based systems that deal with symbolic, non-algorithmic methods of problem solving. These are systems that can give advice or make decisions in a narrowly defined area at or near the level of a human expert.

There are two main parts in a BRMS: a knowledge base, and an inference mechanism, or simply engine.

Image 1

Figure 1

In addition, there are often subprograms designed to facilitate interaction with users, to help build a knowledge base, to explain a line of reasoning, and so forth.

The knowledge base is the program's store of facts and associations it "knows" about a subject area such as loan or customer information. A critical design decision is how such knowledge is to be represented within the program. The knowledge base usually contains different kinds of knowledge; typically, these include knowledge about objects, procedures, and causal relationships. Knowledge about objects is usually stored in the form of an object model, XML schema, or data model. Procedural knowledge may be represented as .NET methods.

In BRMS systems, the knowledge is represented as conditional statements or rules of the following form:

IF: A and B are true,
THEN: C is true.

The antecedent of a rule as the premise or left-hand side (LHS), and the consequent as the action or right-hand side (RHS).

The inference mechanism can take many forms. It often speaks of the control structure or control of inference to reflect the fact that there are different controlling strategies for the system. For example, a set of rules may be chained together, as in this example:

If A, then B (Rule 1)
If B, then C (Rule 2)
A         (Data)
.'.C (Conclusion)

This is called forward chaining, or data-directed inference, because the data that are known (in this case, A) drive the inferences from left to right in rules, with rules chaining together to deduce a conclusion.

Implementing forward chaining efficiently is hard since when a rulebase becomes large, native algorithms for forward chaining become very slow because few changes are made to the facts in working memory at each cycle. Rete is a very efficient mechanism for solving this problem. Rete is much more efficient at determining the relevance of rules, given a particular data, than the equivalent nested if/then/else or select/case constructs. The Rete network modifies itself after each rule firing, so that unneeded rules do not fire. The greater the number of rules, the greater Rete's advantage over procedural code.

An example of a forward chaining rules application would be the determination of an auto insurance premium amount based on the applicant's age, marital status, and driving record.

The knowledge base and the inference engine are separated from one another to facilitate maintenance. After all, in most cases, knowledge and policies will change over time, and one does not want to rewrite the inference engine (the program code) whenever a new rule is added.

From Policy to Rules

The process of translating business policy and procedures into IT application code has traditionally been very error prone, and usually a source of contention and irritation among business users, analysts, and application developers. In spite of the existence of several documenting and authoring tools and application design and modeling software and tools, the process is fraught with danger, primarily from gaps in communication.

The relationship between business and IT has often been fraught with misunderstanding, frustration, and even hostility. Part of this disconnect between the IT organization and the business groups is a natural disparity between the work cycles of the two groups, where the information passes through different phases and is prone to errors at every stage.

Finally, when the application is tested and business functions do not meet the expected requirements, the issue becomes which party should be held responsible, because at this stage, the user and analyst are unable to understand the language used by the application developer and designer.

Rule-based processing mitigates these problems in different ways.

  • The ability to use natural language and English-like syntax support for a rule repository or knowledge base and versioning of business rules all work in tandem to reduce or even eliminate risk in the traditional software development process.
  • With the ability to define business rules independent of application code, the application need not be aware of the rule conditions or actions to be performed as a result of a matching rule condition.
  • Another advantage of rules-based design is that you can make the rules visible to the business workers and allow them to modify or define new rules.

Business language support helps the most in reducing communication issues between the different groups involved in application development. The rule repository, as presented earlier, creates a single location where all of the business logic and rules are stored. This reduces, if not almost eliminates, the error-correction process, which is a common problem during the system and acceptance testing processes. The ability to test and debug the rules from within the rules development tool is also an important aspect in solving this problem.

When do you use a rules engine?

Obviously, not all applications should use a rules engine. In traditional information systems, the business policies get hard-coded into the application; we should consider the use of a rule engine if code changes can cause major financial losses; a simple change to one line of code can cost an organization thousands of dollars.

When changes need to be made, the entire software development life cycle must restart - understand the requirement, design it into the system, make sure it doesn't adversely impact anything else, implement the change, test it, and deploy it. Changes are significantly less likely to introduce errors when you can express the logic using a declarative approach instead of an imperative programming language.

Rule enabling an application

Smart Rules Lite is an free Rules Engine. To .NET 2.0 framework, that uses the Rete algorithm to evaluate the rules you write. Smart Rules Lite lets you express your business logic rules in a declarative way. You can write rules using Smart Rules Studio, a one-stop shop for creating and maintaining business rules syntax. The visual interface makes it easy for developers and business workers to define and deploy new rules or release a new version of an existing rule.

The problem to solve

This article shows how to use Smart Rules Lite as part of the business logic layer in a sample .NET Web application. The following assumptions set the scenario for the fictitious problem the application solves:

  • A company named ABC is a newspaper distributor of Miami Herald, Orlando Sentinel, Los Angeles Daily News, and the New York Times.
  • The company uses a simple Web Application that computes the total costs of newspaper orders based on the number of items purchased, applying discounts, and adding the sales tax if the newspapers are shipped to the state of the original newspapers. As shown in Figure 2.
  • Currently, the logic is part of the application's compiled code. The component that contains this logic is written in a compatible .NET language.
  • The logic for applying discounts is changed more than once a month. Developers must go through a tedious process every time they need to implement it in code.

Once the problem is stated in this way, the solution becomes clear: the expression of business policies and the ability to change them need to be rigorously decoupled from the technology that implements them, allowing:

  • Business policy experts to manage and evolve business policies using the methods and vocabulary with which they are most familiar.
  • Technology experts to manage and evolve technology using the methods and vocabulary most suitable to their tasks.

Because the company incurs high costs whenever changes are made to the logic that assigns discounts to a computer, the ABC executives have ordered to engineers to build systems in which business policies are abstracted out of software, where they can be directly authored, modified, and managed independently of the underlying software system. Here's where a BRMS (in this case, Smart Rules Lite) comes to play.

Image 2

Figure 2

Currently, these is the business rules that must be followed when applying a discount to a new order:

If Quantity ordered is Greater than or Equal To Quantity for Discount
            then provide a discount. 

For this demo, both 120 and 50 Miami Herald newspapers are ordered from Florida, California, and New York. The initial rule applies a discount if 100 or more newspapers are solicited.

Figure 3 shows the response; as can be seen, there is a 20% discount by the newspaper when 120 or 200 are purchased.

Image 3

Figure 3

The next figure shows the rule that computes the price:

Image 4

Figure 4

Figure 2 shows that the Tax Rate is 0%; for the next rule the Tax Rate will be set to 8% if the newspaper is shipped to the state where the newspaper does the business. Follow the instructions to create a new Rule from the Help file (make a copy of the rule set and add a new rule from the context menu).

Figure 5 shows the remaining rules in Smart Rules Studio. As you've probably guessed, these rules pertain to the assignment of the Tax Rate:

Image 5

Figure 5

First, note that the order in which these rules should execute does matter. The outcome is affected by the order in which these rules are fired. If you review the business rules detailed in Figures 4 and 5, you'll notice that the priority property (in the property grid) rules for assigning a discount have a precedence order.

Now that a new rule has been added to the rule set, we can check the result of the application. Notice that the ship to state is California, as is the state where "Los Angeles Daily News" is shipped.

Image 6

Figure 6

As you can see, the Tax rate is now 8% to California.

This concludes the sample. The code is available for download from the above links. Also, you should download the freeware BRMS Smart Rules Lite, from the Kontac site.

Please follow the instructions from the readme.html file in order to import the XML dictionaries and rule set to your new repository.

Conclusion

Using a rules engine can significantly reduce the complexity of components that implement the business rules logic in your applications. As you've seen, approach has a higher chance of being more maintainable.

We demonstrated a simple application using Smart Rules Lite as a solution, and introduced the notion of rule-based programming, including how these rules are resolved at runtime.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)