When dealing with complicated and ever-growing program conditions brought on by new business requirements, it's easy for what was once a small conditional block of code to grow to evaluating hundreds of unique conditions. Unfortunately, much like kudzu, that bad practice begins to creep into other areas of code. Micah Breedlove says that incorporating a rules engine to handle the conditional logic is a great way to reduce the code smells wafting from a multi-hundred line conditional. Converting the conditional into one small block of code which can retrieve and interpret a rule from another source—a database, XML, YAML, or some other repository—alleviates unnecessary logic. Micah presents different approaches to implementing a rules engine including event-driven and direct injection to give a better appreciation for its ability to simplify the growing complexity of conditional statements. Cleaning up conditional logic is an important gateway to writing more concise code which leads to an easier-to-maintain application.
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
Them’s the Rules: Using a Rules Engine to Wrangle Complexity
1. BT5
Design & Code
11/17/2016 11:30:00 AM
Them’s the Rules: Using a Rules Engine
to Wrangle Complexity
Presented by:
Micah Breedlove
iostudio
Brought to you by:
350 Corporate Way, Suite 400, Orange Park, FL 32073
888--‐268--‐8770 ·∙ 904--‐278--‐0524 - info@techwell.com - http://www.stareast.techwell.com/
2. Micah Breedlove
iostudio
Micah Breedlove is a senior developer and dev team lead at iostudio. He began
working with PHP in late 1998 and has been developing full time using Symfony
since 2009. Micah has written and maintained applications for companies
including Procter & Gamble, Walmart, and the National Guard. Previously a
senior developer at Franklin American Mortgage, he was instrumental in building
the PHP development team. Micah lives with his wife and three kids in Nashville
where he enjoys growing fruit trees, making maple syrup, and vegetable
gardening. Micah is a firearms enthusiast and enjoys spending time at the range.
Find more about Micah at his website or follow him on Twitter @druid628.
3. Them’s the Rules
by
Micah Breedlove
Using a Rules Engine to Wrangle
Complexity
PHP Dev since ‘98
Symfony Evangelist
@druid628
Sr. Developer / Team Lead @ iostudio
Who is this guy?
5. Some definitions
Conditional Logic
“hypothetical logic (of a proposition) consisting
of two component propositions... so that the
proposition is false only when the antecedent is
true and the consequent false.”
How we handle the unknown.
6. Rules Engines
“A business rules engine is a software system
that executes one or more business rules in a
runtime production environment.”
“An added layer of complexity to carry out,
often complicated, business logic of the
application”
in other words...
Business Logic?
● Company policies
● Legal Regulations
8. ...
Should be kept trimmed,
maintained and under control
Or else...
Pandemonium Ensues
Conditional statements are like Kudzu
9. Fixable?
Something to …
● Abstractly handle multiple conditions
With
● Minimal conditional logic
and is
● Reusable
Simplifies a block into a generic block
Support more than a simple/singular condition
Abstraction
10. Minimize Conditional Logic
● Drop the ever-building conditionals
● Replace with smaller defined block
Reusability
Ability to support all existing conditions
Individual rules can be evaluated in other
RuleSets
12. Ruler - Elements of the Equation
3 Parts
● Context
● Rule
● Execution/Evaluation
Context - All Applicable Data
Variable Data:
‘bond_district’ => ‘collegedale’,
Control Data:
‘district’ => ‘collegedale’,
‘district_fee’ => ‘25’,
‘fee_adjustment’ => ‘+’
● Variable Data
○ Request
○ User Specific
(includes session)
● Control Data
○ Specific to the Rule
13. Rule
$rule = $rb->create(
$rb['bond_district']->EqualTo($rb['district'])
);
How the Context Stacks Up
● How to evaluate the data
● What to evaluate
Variable Data:
‘bond_district’ => ‘collegedale’,
Control Data:
‘district’ => ‘collegedale’,
‘district_fee’ => ‘25’,
‘fee_adjustment’ => ‘+’
Rule (cont.)
Compound Rules
$rule = $rb->create(
$rb->LogicalAnd(
$rb['bond_district']->EqualTo($rb['district']),
$rb['bond_amount']->GreaterThan('20000')
)
);
14. Evaluation/Execution
Evaluate - (boolean) Did it meet the condition?
Execution - (void) If it’s true do something
Evaluation Example
Rule:
$rule = $rb->create(
$rb['bond_district']->EqualTo($rb['district'])
);
$status = $rule->evaluate($context);
echo $status; // false
Control Data:
‘district’ =>
‘collegedale’,
‘district_fee’ => ‘25’,
‘fee_adjustment’ => ‘+’
Variable Data:
‘bond_district’ => ‘collegedale’