laitimes

How to develop custom applications using Java lambda syntax and an external rules engine

Written by | Soham Sengupta and Srijeeb Roy

Translated by | Chen Liangfen

Planned by | Ding Xiaoyun

Complex enterprise applications often have different business logic. The preconditions and subsequent system actions (what we call rules) in these business logics are always changing. Moreover, the rules we are talking about here require more domain-specific knowledge than technology and programming. Instead of thinking about code when implementing these rules, we should reside outside the code base and write the rules by people with core domain expertise (who require minimal technical and programming knowledge). There is a specific type of software tool, the rule engine, that can help solve difficult business rule requirements. Domain experts don't need to be good at coding and technology, just as corporate brands and marketing teams don't need to know the underlying technology of enterprise portals and mobile apps, but they need to be good at writing editorial images, banners, and other content (which can be easily done with an Instagram account). Adobe aem is one of the content management systems that provides no-code / low-code content authoring. Emerging technologies and cloud platforms continue to propose low-code and no-code solutions, and these solutions have gained wide acceptance in the demand market. This article describes a lightweight approach to externalizing business operations into low-code tools that can also help people with domain expertise in implementing business rules.

Although we can already use many rules engines in the market, such as Drools (which is a feature-rich business rules management system), Easy Rules, Rule Book, Oracle Rules SDK, Blaze (fico), IBM Decision Manager, and so on. These rule engines enable rule management declaratively through their own rich features, including versioning, which is often useful for many applications. However, in some less complex solutions, they tend to be redundant and underutilized. Instead, maintaining this additional component is more of a liability than an asset, with more overhead than actual use value.

In this article, we try to illustrate how to take advantage of the inherent nature of Java and implement externalization rules in the simplest possible way, without being limited to any pass-through dependencies of additional frameworks. This approach is useful when technical rules (snippets of code written in Java) need to be externalized and can change frequently. Therefore, this approach is of equal value in any Java ecosystem, regardless of the framework. Provides a simple declarative model-based POJO for rules loaded from external sources, such as files or URLs, that represent a predicate or a Java code snippet equivalent to a lambda expression. External sources are Java lambda-style expressions or Java code snippets that range from local databases and cloud resources so that rules can be written outside the application without even application downtime. We can easily integrate it with Spring microservices and cloud configurations, without the need for a cloud bus. This approach provides encryption at rest to ensure the security (confidentiality and integrity) of business rules. In addition to supporting Jasypt and Spring Config Ciphering, any custom security can be plugged into it.

Rule Engine: The Traditional Way

The most traditional and ideal way to handle frequent changes in business logic is the rules engine. A rule is typically a set of IF-THEN conditions.

Rule≡ Preconditions + Action (RULE≡ CONDITION+ACTION)

The rules engine is a software tool that, in simple terms, gives us the ability to set rules outside of the source code. The rules engine enables semi-technical/non-programmers to set rules differently. Leveraging domain-specific knowledge, these rule engines can provide GUI-driven, intuitive rule writing. Drools, DTRules, Oracle Policy Automation system, Easy-Rules, etc. are commonly used rules engines.

Traditional rule engines help domain experts write rule sets and behaviors outside the code base, which is useful for complex, large business environments. However, for smaller and uncomplicated systems, considering the recurring costs of running on-premises or cloud infrastructure and the cost of licenses, the results prove that for such small and simple systems, the rules engine function is not necessary and difficult to be fully utilized. For small teams, adding any component that requires an extra skill set is a waste of bandwidth. Some commercial rule engines have a steep learning curve and have been pursuing better rule engine performance, with less consideration for the cost performance used by users. In this article, we try to illustrate how to successfully maintain rules outside of source code to execute medium-sized systems running on Java Tech-Stack (like Spring Boot), making it easier for other users to customize these rules.

This approach is suitable for teams that cannot afford the overhead of a dedicated rule engine and its infrastructure, maintenance, recurring costs, and so on, and the domain experts on the team have a software foundation, or the team members wear several hats.

This minimalist approach is suitable for entrepreneurs. Looking deeply into the origins of large companies, it is not difficult to find that many large companies start out as small teams, budgets must be spent on the cutting edge, with excellent talents and innovative ideas. So, taken together, while the size of the team may not be completely certain, teams of 10 and under 10 people can take advantage of this approach when faced with the same constraints.

Larger teams can also benefit from this approach. In addition, cutting out some dedicated rule engines can also benefit, unless all the cool core features (the main features of the commercial rule engine) are required.

Overview of results & Novelty

The steps listed in the following diagram describe these components and also show an overview of the higher-level functionality under the mechanism.

How to develop custom applications using Java lambda syntax and an external rules engine

The configuration stores rules that are simply java methods or Boolean expressions. To avoid the length of Java syntax, we also use Lamda and method references, which provide an additional layer of abstraction on top of the syntax of Java methods.

The configuration stores rules written by domain experts. The following are valid statements, written in lambda-like syntax to capture the intent of the expert. For example, to classify adult video games as graded by the Entertainment Software Rating Board (ESRB) into products in the appropriate age range, a domain expert could write:

The configuration can be a file, raw byte stream, or URL, and can be placed anywhere. The placement options are widely chosen, including the following:

A file on a local disk.

A database (including SQL and NoSQL).

A remote network location (such as AN URL, raw TCP socket, and so on).

Placed in one place on the cloud (e.g. Amazon Cloud Technology's S3 bucket or Google's EVS drive, etc.)

The framework provides a comprehensive abstraction of source encoding, security, and transport. Any standard or proprietary codec (CODEC) can be used for storage and retrieval rules, and the same applies to the two main security paradigms – confidentiality and integrity. In this regard, everyone can write their own adapter. Only then should the final step in its retrieval process be to produce a rule in a Java expression or predicate.

By doing a little additional work, add a mechanism that makes streaming rules suitable for browsers that use any standard protocol (e.g., WebSocket, HTTP/2, XMPP, MQTT). This approach can even be combined with Web-assembly to integrate security and complex rules in web browsers.

Getting Started Guide

While traditionally, it's best to understand the structure of the SDK before we start using it, for convenience and simplicity, we've reversed the order. In this section, we will illustrate how rules are externalized through simple problem statements or use cases.

prerequisite

Users must have experience with software development using Java SE 1.8 or later. In addition to this, a standard machine with Java SE 1.8 (with IDE) is required. In all the examples throughout the article, the IDE will use Eclipse and the build tools will use MAVEN.

Step 1

Download the Library from (https://github.com/trainerpb/easyrule/blob/master/externalize-rules.jar) and save it on a local path, e.g. D:\ externalize-rules.jar。 Here is the full source code (https://github.com/trainerpb/easyrule).

Step 2

Create a Maven project that sets the compiler source level to 1.8 with dependencies, as shown in the figure.

How to develop custom applications using Java lambda syntax and an external rules engine

Problem Statement: An upscale clothing retail store is one that often offers discounts to customers on different occasions, based on a range of different reference criteria, such as occupation, income level, place of work/residence, education level, etc. To be uncomplicated, let's assume a simple customer model class as follows:

We further assume a class Repo_Customer that serves as a source of customer information and selects customers who qualify for the discount based on specific eligibility criteria.

How to develop custom applications using Java lambda syntax and an external rules engine

We can then define discount eligibility rules using any one or several of the following:

How to develop custom applications using Java lambda syntax and an external rules engine

Step 3

It's worth noting that Lambdas isn't as lengthy. Subject matter experts can use Java programming languages to define rules without having to know them well.

We leave the discount rule outside of the codebase because it can change frequently. Then select the simplest of all available storage options and store the rules in a file on your local disk. The SDK helps load and execute these rules. The following files are stored on a local disk:

How to develop custom applications using Java lambda syntax and an external rules engine

We created our own markup standard in the file (it can be any design and implementation, not limited by any particular implementation):

Step 4

Finally, let's write a little code to check our rules to see if they execute smoothly:

How to develop custom applications using Java lambda syntax and an external rules engine

In the SDK

Now that we've successfully launched and written a simple quick launcher, we can go back and explore what this SDK does, as shown below.

1. Overall programme structure:

How to develop custom applications using Java lambda syntax and an external rules engine
How to develop custom applications using Java lambda syntax and an external rules engine
How to develop custom applications using Java lambda syntax and an external rules engine

1. Load the original rule base from the configuration source

2. Synthesize the rule base fragment into java code (e.g. Lamda)

3. Compile the composite Ja va code

4. Finally load the class into main memory in order to execute the rules.

Defines the concrete implementation of the framework

We have already used PredicateRuleExecutor in [2]. This is a special implementation

It performs the following 3 tasks:

1. Load the rule that evaluates to true or false. That is, it converts the original rulebase or lambda expression into a java method that accepts an Object and returns a Boolean value, i.e. forms one

2. Its framework, specifically the DocumentExecutionContextLoader, is a subclass of the RuleExecutionContextLoader and provides an abstraction for the underlying tasks.

3. If it is necessary to load an encrypted, encoded / code obfuscated sequence of bytes, according to some standard / custom codecs, the method must be rewritten,

4. One advantage of this method is that it works in bytes. As a result, rules can be loaded from file systems, URLs, cloud storage, databases, and so on.

We will briefly explain how to load rules from several major and commonly used source codes.

How to develop custom applications using Java lambda syntax and an external rules engine

Some implementations of common source code

In this section, we will explore some common and important source files that can load configurations.

The remote URL on HTTP

How to develop custom applications using Java lambda syntax and an external rules engine
How to develop custom applications using Java lambda syntax and an external rules engine
How to develop custom applications using Java lambda syntax and an external rules engine

2. Amazon Cloud Technology S3 bucket

To load rules from the S3 bucket, we only need to make the following changes:

How to develop custom applications using Java lambda syntax and an external rules engine

Arrange the pieces in the correct order

Roughly speaking, the SDK's utility is a PoC (Patch Output Converter Batch Output Converter). However, creators are working to shape it into an open source project. In this section, the last part of the article, we will highlight some of its salient features:

The framework can easily collaborate with different security frameworks, such as JASYPT, to ensure the integrity and confidentiality of business rules, so you can neither abuse or inject your own rules.

Because it's just plain Java, it's perfect for all popular frameworks, for example

Spring and Spring BOOT

JSF

Play

ZK

Whether it's for a Spring Cloud Config with a cloud bus or a Spring Cloud Config without a cloud bus, it's a great fit.

Protocol neutrality allows it to be used collaboratively with any source code, whether the source code is deployed in a cloud service from a cloud provider or in an on-premises store.

People can implement their own custom codecs on any cloud provider or on-premises storage.

We can use JMX hooks to overload the loaded configuration without restarting the application.

About the Author:

Soham Sengupta has 16 years of experience in academia, research and industry. As a Master in Mobile Computing and Networking Technology, he has been undertaking digital transformation for PAYBACK Corporation (India). He is a sincere admirer of basic science, an avid lover of literature, a passionate programmer who loves to share his ideas on social platforms, and these ideas often prove to be ahead of the curve and innovative. If you want to know more about the author, you can find it on his LinkedIn.

Srijeeb Roy has over 23 years of experience in the IT industry. He holds a Bachelor of Science in Computer Science and Engineering from jadavapur University, India. He has led the Java Base team for TCS Vertical Insurance in the past and currently focuses on a variety of digital technology accelerators that help TCS customers accelerate their digital journeys. He has over 20 years of experience in Java/J2EE/JEE, Spring technology, and hybrid mobile application frameworks. He has written several articles on Java SE, EE, and ME on Infoworld.com (formerly JavaWorld.com).

View the original English text:

https://www.infoq.com/articles/java-external-rules-engine/

Read on