views:

5697

answers:

15

Hi,

Does anyone know of a good .NET library rules library (ideally open-source)? I need something that can do nested logic expressions, e.g., (A AND B) AND (B OR C OR D). I need to do comparisons of object properties, e.g., A.P1 AND B.P1. (Ideally, I could compare any property -- A.P1 AND B.P2).

It should store the rules in a database (I've got a lot of simple configurable logic). And it should have a rule creation/management API. The management tool would have to inspect the instances to determine which properties are available and which constraints exist.

Thanks!

+3  A: 

The official MS solution for this is Windows Workflow. Although I wouldn't call it "simple", it meets all of your specifications (which would require an extensive framework to meet, anyhow).

Will
There is a drawback to the current WF rules engine...it uses a very small subset of codedom expressions but actually does string parsing to generate the underlying code - not the CodeDom classes.
Scott Dorman
+2  A: 

Well, since logical expression are just a subset of mathematical expression, you may want to try NCalc - Mathematical Expressions Evaluator for .NET over on CodePlex.

James Curran
Hey, that's actually pretty neat. Although from the examples, I think there could be a more elegant way of evaluating delegate expressions. This could be the an extension point to insert some reflection on the expression operands.
Kurtz
+2  A: 

None of the open sourced .NET rules-engine have support for storing rules in the database. The only ones that stored the rules in a database are commercial. I've created some UIs for custom rule engines that run off the database, but this can be non-trivial to implement. That's usually the main reason you won't see that feature for free.

As far as I know, none of them will meet all of your criteria, but here is a list of the ones I know of:

Simplest one is SRE
http://sourceforge.net/projects/sdsre/

One with rule management UI is NxBRE
http://www.agilepartner.net/oss/nxbre/

Drools.NET uses JBOSS rules
http://droolsdotnet.codehaus.org/

I personally haven't used any of them, because all of the projects I worked with never wanted to use something built in-house. Most business think that this is pretty easy to do, but end up wasting too much time coding and implementing it. This is one of those areas that the Not Invented Here Syndrome (NIH) rules.

hectorsosajr
Drools.NET is not a good idea, depends on a JVM implementation in .Net still in beta, tried it, and it's really not production ready IMHO.
pmlarocque
Has anyone here used SRE? What was the experience?
Kurtz
A: 

Yes, I wouldn't call Windows Workflow simple either. I don't see that my requirements require an extensive framework. To do fairly simple object comparison like

If(car.Mileage < 200000) OR car.Age > 20)

could be accomplished using fairly standard reflection techniques. Think of how a library like Castle DynamicProxy works. Something where domain types are wrapped in library types that the framework can understand.

Kurtz
+4  A: 

Here is a class I have used in the past. It evaluates strings just like eval() does in Javascript.

String result = ExpressionEvaluator.EvaluateToString("(2+5) < 8");

All you need to do is construct a string to be evaluated from your business objects and this will take care of all the complicated nested logic etc.

using System;
using System.CodeDom.Compiler;
using System.Globalization;
using System.Reflection;
using Microsoft.JScript;

namespace Common.Rule
{
  internal static class ExpressionEvaluator
  {
    #region static members
    private static object _evaluator = GetEvaluator();
    private static Type _evaluatorType;
    private const string _evaluatorSourceCode =
        @"package Evaluator
            {
               class Evaluator
               {
                  public function Eval(expr : String) : String 
                  { 
                     return eval(expr); 
                  }
               }
            }";

    #endregion

    #region static methods
    private static object GetEvaluator()
    {
      CompilerParameters parameters;
      parameters = new CompilerParameters();
      parameters.GenerateInMemory = true;

      JScriptCodeProvider jp = new JScriptCodeProvider();
      CompilerResults results = jp.CompileAssemblyFromSource(parameters, _evaluatorSourceCode);

      Assembly assembly = results.CompiledAssembly;
      _evaluatorType = assembly.GetType("Evaluator.Evaluator");

      return Activator.CreateInstance(_evaluatorType);
    }

    /// <summary>
    /// Executes the passed JScript Statement and returns the string representation of the result
    /// </summary>
    /// <param name="statement">A JScript statement to execute</param>
    /// <returns>The string representation of the result of evaluating the passed statement</returns>
    public static string EvaluateToString(string statement)
    {
      object o = EvaluateToObject(statement);
      return o.ToString();
    }

    /// <summary>
    /// Executes the passed JScript Statement and returns the result
    /// </summary>
    /// <param name="statement">A JScript statement to execute</param>
    /// <returns>The result of evaluating the passed statement</returns>
    public static object EvaluateToObject(string statement)
    {
      lock (_evaluator)
      {
        return _evaluatorType.InvokeMember(
                    "Eval",
                    BindingFlags.InvokeMethod,
                    null,
                    _evaluator,
                    new object[] { statement },
                    CultureInfo.CurrentCulture
                 );
      }
    }
    #endregion
  }    
}
Shaun Bowe
+1. Impressive!
David
A: 

Hmm, now that I think about it, the solution requires a dynamic Specification built for comparison against run-time instances. The specifications would be used in the expression evaluation. The challenge is to create those specs and manage their life-cycle for comparison. The actual expressions won't be very hard. In the code, it's just a simple Composite tree that has to be walked, called eval along the way. In the database, a self-referential table can store the expression trees. So the challenge is to make the system type-agnostic.

Having a DSL syntax and parser isn't necessary at this time. The UI I'm creating is going to be bound to the constraints of a type's properties. For example, the Person type can have Person.Sex either 'Male' or 'Female'. The UI would bind to this constraint and allow only those choices for the Spec.

Kurtz
A: 

Depending on what you are trying to do using Lambda expressions (and expression trees) can work for this concept. Essentially, you provide an expression as a string that is then compiled on the fly into a lambda expression/expression tree, which you can then execute (evaluate). It's not simple to understand at first, but once you do it's extremely powerful and fairly simple to set up.

Scott Dorman
Right, I understand the use of Lambda expressions and would use them if I were building this from scratch. But, I'm hoping the guts of this library already exists or can be cobbled together.
Kurtz
Not that I know of...there are several rules engine type libraries available but none of them are particularly simple and/or efficient and none make use of lambda expressions.
Scott Dorman
A: 

Oh, one more thing. As a rules-engine, I need to include the concept of Actions (Commands). These are what execute when the expression returns:

If (expression.Evaluation) { actions.Execute(); }

So I see a rule as something like:

class Rule
{
Expression Exp;
Actions[] Actions;
Run() { if(Exp.Evaluate()) { foreach(action in Actions) { action.Execute() ; } } }
}
Kurtz
+15  A: 

Agreeing with will I would say use something from the workflow engine family although not workflow. Examine System.Workflow.Activities.Rules Namespace a little bit - it's supported in .Net 3, and built into .Net3.5. You have everything in hand for free to use like you mentioned :

  • RuleCondition for conditions , RuleAction for actions

  • standardized format for describing metacode (CodeDom - CodeExpressions)

  • you can plugin any kind of complexity into that (to tell the truth except Linq and lambdas and so extension methods of some kind) via TypeProviders

  • there's a builtin editor for rule editing with intellisense

  • as the rule is serializable it can be easily persisted

  • if you meant to use the rules over a database scheme then via typeprovider it can be implemented too

For a starter : Using rules outside of a workflow

Ps.: we're using it extensively and there're much more in that namespace than you ever imagine -> a complete meta algorithm language

And the most important : it's easy to use - really

Nicolai Ustinov
A: 

I thought prolog was built for this purpose.

http://www.dcs.ed.ac.uk/home/stg/Psharp/

iterationx
+1  A: 

I would look at Windows Workflow. Rules engines and workflow tend to start simple and get progressively more complex. Something like Windows Workflow Foundation is not too difficult to start with and provides room for growth. Here is a post that shows it's not too difficult to get a simple workflow engine going.

cisellis
A: 

Maybe check out SmartRules. Its not free, but the interface looks simple enough.

Only know about it because I've used the SmartCode codegen utility from there before.

Here is an example rule from the Website:

BUSINESS RULES IN NATURAL LANGUAGE      

Before
If (Customer.Age > 50 && Customer.Status == Status.Active) {
policy.SetDiscount(true, 10%);
}

After (with Smart Rules)
If Customer is older than 50 and
the Customer Status is Active Then
Apply 10 % of Discount
Brendan Kowitz
A: 

It's not free, as you can't easily untangle it from its BizTalk parentage, but the Business Rules Engine components of BizTalk are a separate entity from the core BizTalk engine itself, and comprise a very powerful rules engine that includes a rules / policy based GUI. If there was a free version of this it would fit your requirements (buying BizTalk just for the BRE wouldn't really work commercially.)

Hugo Rodger-Brown
+2  A: 

Windows Workflow Foundation does give you a free forward chaining inference engine. And you can use it without the workflow part. Creating and Editing rules is ok for developers.

If you want to have non-programmers edit and maintain the rules you can try out the Rule Manager.

The Rule Manager will generate a working visual studio solution for you. That should get you started rather quickly. Just click on File \ Export and selecte the WFRules format.

Sentient
A: 

You can use a RuEn, an simple open source attribute based Rule Engine created by me:

http://ruen.codeplex.com

Bhaskar