I’m releasing a new open source library, RuleBook. Inspired by Inform 7‘s rulebook feature, it’s a way to make functions by defining a set of rules to evaluate. RuleBooks let you define functions are easy to mod and update at runtime.
What are rulebooks?
Rulebooks are essentially a fancy form of C#’s Func<>
and Action<>
generics.
Func<>
and Action<>
can hold a reference to a C# method, or an lambda expression/statement. But the thing they hold is essentially a black box – you cannot do much with it except run it, or check equality.
RuleBook provies FuncBook<>
and ActionBook<>
, which work similarly to their counterparts. But these rulebook objects are built out of individual rules, which can be individually inspected and mutated.
Overall, rulebooks give a systematic way of handling a bunch of useful programming patterns, including events, multiple dispatch, modding and code weaving.
Rulebooks are not an elaborate rules evaluation engine, it’s a lightweight way of stitching together bits of functionality.
Wow, this is a really interesting library. My first big question is, how do you manage ordering in a non-trivial example? You mentioned distributing the rules around the code base, to where they make sense, which is great. However, how do you ensure that the ordering is correct without having to then know about all the other rules? This is an issue we come up against all the time, and i would love to know how one might manage that with this lib. Thanks!
Hi Jos, I’ve tried to copy Inform 7’s approach. There are some simple heuristics for ordering, or you can provide an integer priority to give specific control. Inform 7 has much smarter heuristics, but it requires introspecting the body of the rule.
There’s also a feature “Order before/after” that sorts one rule directly before or after another.