How to design a rule engine?

妖精的绣舞 提交于 2019-11-29 21:04:25

If you're using .NET 3.0 or later, you can use the Rules Engine of Windows Workflow Foundation without having to acutally use Workflow.

I've done this on a project, and you can use SQL or XML as the backend, and it works great. You can use the IDE that comes with the Workflow examples and put it in your own apps. It's excellent.

  1. I cannot believe you would implement your own considering there are so many available commercially and open source.

  2. I recommend taking a look at InRule as a great commercial option that is reasonable priced, or NxBRE in the open source space.

What kind of Rule engine you looking for? For styling practices? If so, go check out StyleCop. Not the answer, but there might already be something out there for you.

Are you given any indication on method? (ie if this is supplemented by course material what are you currently learning?) If this is meant to be a fairly basic system you might find success looking into to Deterministic Finite State Machine and Nondeterministic Finite State Machine

If you have business analysts to program the high level rule engine, then fine - pick one of the before-mentioned rule engines or roll your own (including workflows). If not, then just code your business logic in code and if you ever need to hire business analysts and redo the system, you're in a good place to be.

If you want to write your implementation something 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 this...

[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 simple rule engine here: http://www.duanewingett.info/2015/01/21/SimpleCRuleEnginePart1TheRuleEngine.aspx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!