Basics of Logic

From ADempiere
Jump to: navigation, search
This Wiki is read-only for reference purposes to avoid broken links.

Introduction

Let's start with a simple question: what happens in my brain when I try to make a decision? Or maybe your brain!
I have a memory which stores lots of different things and subjects such as I know my dad's birthday; I know the URL of ADempiere Wiki; I know if my boss wears glasses and so on. This is called my knowledge of the world, call it my knowledge base. Every second my knowledge base grows bigger and bigger since each moment I feel something new like I smell the apple I am eating right now; I see my computer's background picture; I hear the clock ticking constantly. Besides I have a brain by which I think and make decisions, but how? This is the question we are going to find its answer in this document.

A Real Example

Suppose I am driving my automobile (Oh! My own one!) back home at 80 km/h. Everything goes fine until the moment I see a dog on the road, 30 meters ahead! So I suddenly push my foot on the brake pedal and turn right to avoid crashing the poor dog, and the dog survives! How all this happens? When I see the dog, I infer that there's an obstacle on the road. I know that I should not hit an obstacle, so I infer I should stop the automobile. I know I am driving at 80 km/h so I infer that I will be able to stop at this speed in 30 meters, so I infer I have to push the brake pedal. . And that's all. I made the right decision.

Let's take a deeper look at what I have done:
(A)
I know that:

1) A dog is on the road, in front of me.
2) I am driving at 80 km/h.
3) To stop the automobile I have to push the brake pedal.
4) I am 30 meters away from the obstacle.


(B)
I also know the rules that:

A) If there is anything on the road in front of me then it's an obstacle.
B) If there is an obstacle on the road then I have to stop the automobile.
C) If I m driving at less than 85 km/h then I can stop the automobile.
D) If I want to stop the automobile then I have to push the brake pedal.


(C)
So I infer:

(1) and (A) gives A dog is on the road then an obstacle is on the road.
(A) and (B) gives An obstacle is on the road then I have to stop the automobile.
(2) and (C) gives I am driving at 80 km/h which is less than 85 km/h then I can stop the automobile .
(3) and (D) gives I want to stop the automobile then I have to push the brake pedal.



Facts and Rules

See how disciplined is our brain?! The first table shows what I know of the universe. They are all simple statements each of which providing me with some information about the world, no predictions and no conditions at all. Only simple and plain information. They are called the facts that I know.
Therefore a fact is a simple and plain piece of information with no conditions and predictions.

The second table totally contains statements of the form: “If X then Y”. Their form clearly implies they are conditions and conclusions so let's name them as rules. X part contains the facs and Y part contains the conclusions.

The third table shows the inference process, how facts are inserted into an appropriate rule to form conclusions.

How Do They Relate to Business?

ADempiere has a database which stores all the data it needs to do the right job, just like our memory. ADempiere's universe is the records which users insert into its database and what users enter into windows' widgets, both of them are called the data. So data is its knowledge of its universe. It should be now clear to you that in ADempiere's terms data is fact.

But what about the rules? Rules play the logical part of the ADempiere, therefore when you write down a piece of code like

 if (customer.getCredit() < 100 && customer.getInvoiceAmount() >= 100) 
    throw OutOfCreditException;
 }

you are writing a rule. The facts in this rule are customer.getCredit() and customer.getInvoiceAmount(). If they both evaluate to true then they will form a conclusion: throw OutOfCreditException. Simple, isn't it?

More Information

Now that you know what are facts and rules and how conclusions are formed, you may want to take a look at the Integrating a Rules Engine into ADempiere proposal.
To learn more about rules engines see the external links, please.

External Links

  • JBossRules documentation [1]
  • Wikipedia [2]

See also