This project is read-only.

Using RulesEngine with generic functions

Nov 18, 2012 at 1:26 AM

Hi,

 

I'm currently using your RuleEngine in functions like this -

 

public override bool validateCondition (SymbolInfo symbolToValidate)
{
       _RuleEngine = new Engine ();
       T value = symbolToValidate.getFieldValue <T> (_FieldToCheck);
            _RuleEngine.For <SymbolInfo> ()
                       .Setup (symbolInfo => value)
                       .MustEqual (_ExpectedResultAsT);

       return _RuleEngine.Validate (symbolToValidate);
}

Where T is a type, such as string, int and so on. However, whenever I do Validate it always seems to return True, even when the condition is false. Can the engine be used with generics?

 

Thanks,
Stephen

Nov 19, 2012 at 1:32 AM
Edited Nov 19, 2012 at 1:33 AM

Hi,

Try this:

 

public override bool validateCondition (SymbolInfo symbolToValidate)
{
       _RuleEngine = new Engine ();
       _RuleEngine.For<SymbolInfo>()
                    .Setup(s => s.getFieldValue<T>(_FieldToCheck))
                    .MustEqual(s => _ExpectedResultAsT);

       return _RuleEngine.Validate (symbolToValidate);
}

 

You should cache the RulesEngine once it has been created though. It is expensive and unnecessary to build one for every call to validateCondition.

Let me know if this helps.

 

Regards

Arnaud

Nov 19, 2012 at 3:17 PM

Hi,

 

Yeah that worked brilliant, working now :) Didn't know you could cache the engine like that (thought you had to create it with the values you wanted to check each time) but now I'm just using the cache version it has nearly halfed the time it takes to process the rules so thanks for pointing that out :)