views:

785

answers:

4

hi

I have requirement to build new rule engine in which i have rule like this which is stored in Excel sheet, in tabular format.

<If>  "Name = Nita "
<Value> "200"
<else> "Name = Gita"
<value> "300"
<LookInto> "/Name/@Income"

I have two files say 1 n 2. i need to see in first file that whether Name is Nita or Gita. Based on execution i will get value. Now this result value i need to compare with second file , which is a xml file and whose path has been defined into .

can anybody suggest me that is there anything in C# that i can use effectively to develop the same,, or can anybody suggest me how i can achieve this with C#.. i need some idea for class design of the same.

I am using .Net 1.1.

+2  A: 

Firstly, you need to create classes that can define your rules. Once you have this structure in place, you will be able to build an engine that utilises those classes.

I would look at creating something like:

class RuleEngine 
{
  public RuleMatch[] RuleMatches { get; set; }
  public void RunEngine(inputdata...) 
  {  
    // do processing in here
  }
}

class RuleMatch 
{
  public Rule[] Rules { get; set; }
  public Object ValueIfMatched { get; set; }
}

class Rule 
{
  public String FieldName { get; set; }
  public MatchType Match { get; set; }
  public Object Value { get; set; }
)

enum MatchType 
{
  Equal = 1,
  NotEqual = 2,
  GreaterThan = 4,
  LessThan = 8,
  Like = 16
}

Then go on from there....

This structure would be better if it was changed to have a group of rules that could be added to a group of rules, for example (a AND b) OR (c AND d) - I'll leave this to you to think about for now.


Please note I'm using some C# 3.0 constructs here, you will need to create full private properties in 1.1.

ck
thanks for your response. Ok i can use Rule Class as IfRule and ElseRule.. but about the if condition which has condition like If((cond1 and Cond2) or Cond3)..how i can handle it with this class design..
OK, I'll make a change to the structure...
ck
+1. Nice answer. This sort of thing lends itself to a Composite Design pattern. Why did you pick 2, 4, 8 for the Enum?
Dead account
@Ian Quigley - so that you can add together, for example GreaterThan and Equal could be used together (bitwise addition)
ck
hey ck is that true that this evaluate only (a AND b) OR (c AND d) kind of rule n not (a OR b) OR (c AND d)... i m not getting ValueIfMatched? :(
I've only provided this as an example, to implement this as you need, you will need to extend it somewhat. You could add a rule to a rul to be checked if the first does not match to handle the elseif condition.
ck
A: 

You could create a DSL with boo, but I don't know if that will work with 1.1

Paco
what's this DSL? is this stand for Domain specific lang?
yes, I read this book: http://www.manning.com/rahien/
Paco
A: 

You're going to be very limited by .NET 1.1. I think your first step will be to upgrade.

Subsequent steps could involve the rules engine built into Windows Workflow Foundation; this engine, and the graphical designers associated with it, were designed to be usable outside of WF.

Another thing to look at will be the Validation Application Block that is part of the Microsoft Enterprise Library.

None of these things existed six years ago when .NET 1.1 ruled the Earth.

John Saunders
yup thats true.. that i dont have much choice with 1.1. But i cant upgarade it now.. i have to provide the solution with 1.1 only.. and i just have simple if,else rules only,, provided class design could have work for me..if i would have get answer that how can i handle join condition...
A: 

Or you could just let someone else do this work and use a good .NET rules engine like InRule Technology's product, IBM/ILOG Rules for .Net or Blaze Advisor for .Net. They work well and will give you a powerful rules environment in .Net

James Taylor