# What is Rule-Engine?

Here I’m trying to explain rule-engine in a very simple way. Let’s start with the problem. Suppose if I tell you that you have to build a bank application, where you have to implement the logic like mentioned below.

Logic:

`A person is eligible for car loan if, he has monthly salary more than 70K and his credit score is more than 900 then, approve the car loan and sanction the 60% of requested amount.`

You can easily implement these types of rules or logic in your application. But If you will get some additional requirements like:

To achieve all these requirements in our application, we can use the rule-engine. But before starting the rules-engine, let’s go through with few terminologies and background.

## Terminologies:

`Rule = Condition + Action`

The condition also knows as a fact or antecedents or patterns. And action also knows as a consequent.

Knowledge in the form of rules:

`Rule 1: A person is eligible for car loan?if:   1. He has monthly salary more than 70K.   2. His credit score is more than 900.then:   1. Approved car loan.   2. Sanctioned 60% of requested car loan amount.Rule 2: A person is eligible for car loan?if:   1. He has monthly salary more than 35K.   2. His credit score is more than 700.then:   1. Approved car loan.   2. Sanctioned 90% of requested car loan amount.`

Now let’s try to understand rule-engine.

# Rule-Engine

It is an expert-system program, which runs the rules on the data and if any condition matches then it executes the corresponding actions.

In the above diagram, it’s showed that we collect knowledge in the form of rules (if-then form) and stored them in any store. The rules could be stored in any storage like files or databases. Now inference engine picks the rules according to requirements and runs them on input data or query. If any patterns/condition matches then it performs the corresponding action and returns the result or solution.

# Inference-Engine

The inference engine is the component of the intelligent system in artificial intelligence, which applies logical rules to the knowledge base to infer new information from known facts. The first inference engine was part of the expert system. Inference engine commonly proceeds in two modes, which are:

Inference-Engine’s program works in three phases to execute the rule on given data.

Phase 1 — Match: In this phase, the inference engine matches the facts and data against the set of rules. This process called pattern matching.

An algorithm which we can use for pattern matching are:

Drools is one of the implementations of rule-engine and use the Rete Algorithm for pattern matching. It is one of the best algorithms for pattern matching.

The output of the first phase is a conflict set. Conflict set means, for the same fact or condition, it might be possible that more than one rule is satisfied. So it returns the set of conflict rules.

Phase 2 — Resolve: In this phase, the inference engine manages the order of conflicting rules. It resolves the conflict and gives the selected one rules. For resolving conflict it could use any of the following algorithms.

Phase 3 — Execute: In this phase, the inference engine simply runs the action of the selected rule on given data and return the output/result to the client.

## Inference Methods:

Rule engines generally use one of the following inference methods to implement an inference engine.

But before understanding the inference method, let’s understand the reasoning. There are two types of reasoning.

1. Goal-Directed/Backward Reasoning: It is working backward from the goal. Here we start from the main goal and then will go for sub-goals. So in goal-directed reasoning, if we want to achieve the main goal then we have to think that “to achieve the main goal, what sub-goals we have to achieve.”

Example: If we plan for an evening out, and for this, we plan to go for a movie, outing, and dinner. Then evening out is our main goal and the movie, outing and dinner are the sub-goals of the main goal.

2. Data-Driven/Forward Reasoning: It starts with the available data and uses rules to extract more data until a goal is reached. Here we look at data and if we found some pattern then it performs respective action.

Example: Suppose we have to figure out the color of a pet named Fritz with given rules and data.

Rules:

`1. If X croaks and X eats flies - Then X is a frog2. If X chirps and X sings - Then X is a canary3. If X is a frog - Then X is green4. If X is a canary - Then X is yellow`

Data:

`1. Fritz croaks2. Fritz eats flies`

Here using given rules and data we can extract more data like:

`Fritz is a frog.Fritz is green.`

So now let’s discuss the Inference Methods:

Forward chaining:

Backward chaining:

There is one more category called Hybrid chaining. Drools use it. It is a combination of both forward and backward chaining.

We can consider the all above specific requirements in the given example as the advantages of the rule engine.

# Implementation of rule-engine

There are many implementations are available for rules-engine. Few are like:

--

--

## More from Ramesh Katiyar

Interested in learning and exploring new technologies and skills.

Love podcasts or audiobooks? Learn on the go with our new app.

## Ramesh Katiyar

95 Followers

Interested in learning and exploring new technologies and skills.