RuleEngine WhenToUse NotToUse
RuleEngine WhenToUse NotToUse
https://softwareengineering.stackexchange.com/questions/378813/rules-engine-suggestion-for-hard-
coded-if-then-else
Currently all of these are hard-coded in various programs with duplication and
hard to maintain/technical debt. We want remove hard coding to configurable
way.
There are many such lookups happening and each lookup has hundreds of such
business rules.
What is the best way to remove hard coding ? I am thinking about using rules
engine but not sure which criteria to apply to pick one for such use case. Also,
are there any other methods to move this kind of logic out of code ?
Thanks.
Doc Brown
155k2727 gold badges291291 silver badges447447 bronze badges
asked Sep 22 '18 at 3:44
akumar
1911 bronze badge
I took the freedom to make a slight change to your question to remove the red herring of a "3rd
party resource request", for which you already got a close vote by an anonymous voter (someone who is
obviously more interested in close voting for picky reasons than answering your question, which is IMHO
a good one). – Doc Brown Sep 22 '18 at 7:15
3
For future reference: When a question gets closed/put on-hold, you have the option to edit the question to
improve it. If the edit is good enough to make the question answerable, it will get reopened. That
mechanism is generally preferred by the community over asking a new question. – Bart van Ingen
Schenau Sep 22 '18 at 8:45
add a comment
4 Answers
Active OldestVotes
1
I think it is important to realize your goals first - it is typically not
just "better maintainability". A common motivation behind removing
hard-coded rules from code to configuration is often to shift
responsibility for maintaining the rules from the developer team
back to the business teams (and I bet this is your case here as well).
You
Unfortunately, there is seldom a "one size fits all" solution to this .
have probably lots of different use cases, each one requiring a
different level of complexity. I would recommend to try
out different approaches, start with a simple one and look how
well it works. Here are some ideas
1. For some cases, using a decision table like the one shown
in your example may be fully sufficient as a configuration.
Put the table into a spreadsheet or a database (pick what
your business team prefers), and write a program which
evaluates the table and processes the data according to
those rules.
2. For some cases, it may be sensible to implement your own
Domain Specific Language. This can be very simple, or it can
be as complex like other full-scale programming languages.
(For example, I did this a few times using a tabular form,
with a column for preconditions, one for actions, and one for
parameters, which is a very simple approach, easy to
implement but still powerful enough for lots of cases.) Then
one can implement an interpreter for the DSL. Note the DSL
needs to stay simple enough your business people (or at
least some "power users" amongst them) can handle it.
To give you another example: I once had some users who
were no devs, but had some experience with SQL. They
required a validation tool for a database with >100
validation rules, and they wanted to manage those by
themselves. The data model was quite cryptic, but the
business team had a good understanding of its semantics,
better that every one else in our dev team. We made them
an Excel sheet where they could store the relevant parts for
a SELECT statement by themselves, and defined something
like a "DSL" for processing the result sets in various ways.
The program we gave them then read the SQLs, run them
against the DB and interpreted the results according to the
DSL. The solution is still in production, over years, with zero
maintenance requirements for the devs.
3. Using a rule engine. This is some form of a
"predefined, general-purpose DSL" for maintaining rules.
It may be a good solution for your case, but it also bears
the risk that you end up needing specialists for writing
and maintaining the rules, ending with a solution which
can be harder to maintain and debug than your current
system (just by different people). I recommend you go
through all the answers to this older SO question "When
should you NOT use a Rules Engine?", and check which
of the pro and con arguments apply to your specific
situation.
shareimprove this answerfollow
edited Sep 23 '18 at 14:39
answered Sep 22 '18 at 7:56
Doc Brown
155k2727 gold badges291291 silver badges447447 bronze badges
add a comment
0
The wrong way
It would be tempting to tell you to put each mapping in a
database table and query by providing all the relevant fields.
This would however miss the fact that rules may be based on
partial matching, and that when several matching rules are
found, the more specific should apply:
Example:
Mapping 2:
Firm Category =
InternalCode( Enriched )
B b1 = ACCT3
Mapping 3
Firm =
InternalCode( Enriched )
B acc2 = ACCT2
This kind of approach is used in high performance pricing
engines (and even resulted in patent litigations that
concluded that it was a well known approach: disclaimer:
I'm not lawyer, and that's my personal understanding and
not legal advice).
This approach can be used either in a record by record way
(going through each set of mapping for each record). But it
can also be used in a more efficient way if you've uploaded
your CSV in a database, using some successive update
statements that don't update account values already filled
by previous steps.
Other solutions
Another approach could be to use some more
sophisticated rule engine, and translate all your if-then-else
into business rules. The advantage is that you don't have to
determine the mapping tables as you have done. You don't
have to think about mapping dependencies (e.g. if on ogf
the mapping field is in fact determiend by a previous
mapping). It's also easy to add new rules.
The inconvenience is that the rule engine is invoked for
each CSV record. So it might be more heavy than the table
based approach (see solution 2). Also, it's difficult for the
rule writer to understand the interaction between different
kind of rules.
If you don't want to use an existing rule based engine and
develop your own, you could be interested in this SE
question.
shareimprove this answerfollow
answered Sep 23 '18 at 18:17
Christophe
39.9k55 gold badges5959 silver badges9999 bronze badges
add a comment
-1
You can make the mappings centralised by keeping them in
database and exposing get/update/delete/edit functionality
of a single/some/all mappings via RESTful APIs. Mapping
could be fetched before a processing of each batch.
shareimprove this answerfollow
answered Sep 22 '18 at 7:26
Kashan Danish
111 bronze badge
The idea looks appealing but misses the problem: if the mappings are
all centralized, they may nonetheless be defined in a generic manner by
not specifying some of the fields. For example how would you determine
the ACCT for a CSV record (B, b1,sc2,c3) for which several rules would
match (taking into account the null fields in the rule, which mean any
value). – Christophe Sep 23 '18 at 18:24
add a comment
-1
I think the simple and fast solution is to convert the conditions and
corresponding look ups in Redis database like this :
In this scenario you should consider all possible conditions for once
and set theme in Redis and for the lookup you can use Redis clients
directly or write a web service or etc.
Mojtaba Tajik
10944 bronze badges
The idea looks good but misses the problem: the records may have all the
fields all the time but the rules do not require all the fields. So the keys generated
for records will not necessary find the right rule. – Christophe Sep 23 '18 at
18:20
I have a pretty decent list of the advantages of using a Rules Engine, as well as some
reasons to use them, what I need is a list of the reasons why you should NOT use a Rules
Engine
I get very nervous when I see people using very large rule sets (e.g., on the order of
thousands of rules in a single rule set). This often happens when the rules engine is a
singleton sitting in the center of the enterprise in the hope that keeping rules DRY will make
them accessible to many apps that require them. I would defy anyone to tell me that a Rete
rules engine with that many rules is well-understood. I'm not aware of any tools that can
check to ensure that conflicts don't exist.
I think partitioning rules sets to keep them small is a better option. Aspects can be a way to
share a common rule set among many objects.
I will give 2 examples from personal experience where using a Rules Engine was a bad
idea, maybe that will help:-
1. On a past project, I noticed that the rules files (the project used Drools)
contained a lot of java code, including loops, functions etc. They were
essentially java files masquerading as rules file. When I asked the architect
on his reasoning for the design I was told that the "Rules were never
intended to be maintained by business users".
Lesson: They are called "Business Rules" for a reason, do not use rules when you cannot
design a system that can be easily maintained/understood by Business users.
2. Another case; The project used rules because requirements were poorly
defined/understood and changed often. The development team's solution
was to use rules extensively to avoid frequent code deploys.
Lesson: Requirements tend to change a lot during initial release changes and do not
warrant usage of rules. Use rules when your business changes often (not requirements).
Eg:- A software that does your taxes will change every year as taxation laws change and
usage of rules is an excellent idea. Release 1.0 of an web app will change often as users
identify new requirements but will stabilize over time. Do not use rules as an alternative to
code deploy.
I think a good way to rephrase your lesson #2 is "don't implement DSL when you know the
abstraction contained by the DSL is likely to change." Designing and implementing DSL is a
resource-heavy process that you aim to reap awards in the future. If you have to re-create DSL
every cycle, then the rules engine won't be a good fit yet until some stabilization happens.
I'm a big fan of Business Rules Engines, since it can help you make your life much
easier as a programmer. One of the first experiences I've had while working on a
Data Warehouse project was to find Stored Procedures containing complicated
CASE structures stretching over entire pages. It was a nightmare to debug, since it
was very difficult to understand the logic applied in such long CASE structures, and
to determine if you have an overlapping between a rule at page 1 of the code and
another from page 5. Overall, we had more than 300 such rules embedded in the
code.
Cheers,
Nicolae
The one poit I've noticed to be "the double edged sword" is:
Robert Gould
In my experience, rules engines work best when the following are true:
DaveParillo
his>_Software developers with experience creating expert systems_< If you read the drools docs carefully
you will realize that business rules should be implemented by software developers that have a strong
understanding of the Rete Algorithm. Access to the parameterization of rules can be provided to business
users via spreadsheets or CSV. Rules are nevertheless described by business users, but implemented
by, as you say, software developers with expertise in expert systems. The drools docs describe
this. – Matt Friedman Feb 24 '18 at 0:29
That's certainly a good start. The other thing with rules
engines is that some things are well-understood,
deterministic, and straight-forward. Payroll withholding is
(or use to be) like that. You could express it as rules that
would be resolved by a rules engine, but you could express
the same rules as a fairly simple table of values.
So, workflow engines are good when you're expressing a
longer-term process that will have persistent data. Rules
engines can do a similar thing, but you have to do a lot of
added complexity.
Rules engines are good when you have complicated
knowledge bases and need search. Rules engines can
resolve complicated issues, and can be adapted quickly to
changing situations, but impose a lot of complexity on the
base implementation.
Many decision algorithms are simple enough to express as
a simple table-driven program without the complexity
implied by a real rules engine.
shareimprove this answerfollow
answered Apr 21 '09 at 23:57
community wiki
Charlie Martin
2 revs
muruga
https://jessrules.com/guidelines.shtml
Additional Note
A rules engine tool can be very helpful during software development, regardless of other
considerations: