views:

468

answers:

7

I am working on a decision engine / rule evaluation engine. For example:

Input: Customer and all the offences done by the customer

Output: Consequences of the offences

A sample case would be:

Input: Customer(Jhonny Chimpo, 999-00-1111), Offences ( Broke window, slapped boss, kicked team lead in his groin)

Output: Gets pink slip

So the piece of code I want to write evaluates different offences based on rules for each offence and combined offence. The current code is just a maze of if and else statements. I am sure such business problems are common. What design/enterprise pattern is usually used to solve a problem like this?

Is it the specification pattern? I want the code to be open for extension, clean and flexible.

A: 

I think you're trying to develop an expert system. You can check the term and then check the appropriate programming languages as prolog etc.

Aykut
I am not trying to build an expert system.
Perpetualcoder
@Perpetualcoder actually, what you have listed is precisely an expert system.
NickLarsen
isnt an expert system also a learning machine for doing a specific job ?
Perpetualcoder
+2  A: 

I can suggest you a tool we used to solve a similar problem.

Take a look at JBoss Drools: http://www.jboss.org/drools/

It's a BRMS: Business Rules Management System

Here it is an introductory video: http://www.jboss.com/products/platforms/brms/

Andrea Zilio
I am trying to develop in .net platform +1 for mentioning the great product
Perpetualcoder
+1  A: 

Basically business rules look like

forall rules:
  if <condition> then doAction();

What about categorizing all offences by severity using scores, perhaps extra bonus for frequent "evil-doers", some offences may become time-barred and whatever required.

Then a rough draft of an algorithm could be:

  • Sum of all scores of a customer (weigthed)
  • compare to maximum

This would be straight forward using data structures instead of many (possibly deeply nested) if..then..else things.

stacker
the current code we have is maze of if-else loops
Perpetualcoder
+1  A: 

I'm not sure any of the answers above have been that helpful.

I have written similar components using expression trees. You can build lambda expressions that represent predicates, compile and execute them, all dynamically, and then trigger some action in response. This approach is powerful, flexible and strips out all the if/else horror (which is definitely not the way to go).

However, what you are really talking about is logic programming. There are numerous implementations of Prolog over .NET. Prolog is a logic based language, used alot for AI applications, that starts to become seriously powerful once you have your head around its paradigm.

Have a look at some of these ..

flesh
this is something really interesting. I am not a fan of business rule engines out there either
Perpetualcoder
but..:)...I am not a Prolog fan either..i just find it amazing :)
Perpetualcoder
check out expression trees.
flesh
A: 

You could try something similar to this "event based" rules engine

Nick Miller
A: 

I think any RETE Algorithm based Rule Engine would work for your case. You can try drools.

Mervin Yan