A simple rule engine?

I am looking for some engine that could handle such situations:

I have an order object with a customer object attached to it.
Rule:
If order.customer.id = 186 and order.industry = 23, then order.price = 100

I found NxBRE, but does this seem redundant for this?

What do other people do for such situations? Just copy it or use Eval?

+4
source share
3 answers

I also faced this dilemma about two years ago, since it was something simple enough, I didn’t want to go overboard, and time limited, I ended up creating something using an individual interpretation of logic for analysis ==, eg,! =,>, etc., using Linq and the strategy template as the basis of the rule evaluation mechanism

Although if you know the Windows Workflow Foundation, then, apparently, you can use the mechanism of your rules without actually using WF

+3
source

I also came across similar situations and thought about creating my own engine instead of using the existing ones, because when there is any change in my current logic or happens with new reasons, it will be a big pain. If we find out how the engine works, we are open to any logic, and the best we can build is to find local and global optimization!

Refer to the link below in which the spoon feeds the engine and helped me create my new engine!

Click here to run.

+1
source

If you are looking for a much simpler version and want to write your code like this ...

[TestMethod] public void GreaterThanRule_WhenGreater_ResultsTrue() { // ARRANGE int threshold = 5; int actual = 10; // ACT var integerRule = new IntegerGreaterThanRule(); integerRule.Initialize(threshold, actual); var integerRuleEngine = new RuleEngine<int>(); integerRuleEngine.Add(integerRule); var result = integerRuleEngine.MatchAll(); // ASSERT Assert.IsTrue(result); } 

... or like that ...

 [TestMethod] public void GreaterThanRule_WhenGreater_ResultsTrue() { // ARRANGE int threshold = 5; int actual = 10; // ACT var integerRule = new IntegerGreaterThanRule(threshold); var integerRuleEngine = new RuleEngine<int>(); integerRuleEngine.ActualValue = actual; integerRuleEngine.Add(integerRule); // Get the result var result = integerRuleEngine.MatchAll(); // ASSERT Assert.IsTrue(result); } 

... then maybe check out my blog where I slowly create a rule engine. http://www.duanewingett.info/2015/01/21/SimpleCRuleEnginePart1TheRuleEngine.aspx

0
source

Source: https://habr.com/ru/post/1486265/


All Articles