I’m planning to write an e-commerce app using PHP & MySQL with lots & lots of custom rules. Say for example, I want to have a rule like “Give Customer X, a 10% a discount for Product Y if he bundles it with Product Z“
I’ve heard about Business rules engine(in Java and others). I intend to implement a flexible business rules functionality without hard-coding anything into PHP and with a good, solid architecture with the goal of maintainability and re-usability with the help of PHP OOPS.
Please advice me on the following aspects:
Does decision table fit my intended purpose?
How can I change rule/business logic of the PHP application without redeploying/re-writing?
1
I used to work with magento, which was an e-commerce platform (in php-mysql) that was doing exactly that. There is too-much effort for doing the functionality you want from scratch, so integrating an off-the-shelf solution might be more appropriate.
From the answer above: “Forget business people updating them, they don’t have the necessary skill.” This is timeless advice. People have tried it in the past, it always failed…
4
No matter whether you write the rules in PHP, JavaScript or some other, possibly domain-specific language. If the requirements are sufficiently complex (yours seem to be), they will require something with expressiveness of programming language and will require programming skill to update. Forget business people updating them, they don’t have the necessary skill.
Now if programmer has to be around to update the rules, they are best written in existing language for which you can easily get somebody who understands it. For example PHP.
Now to the point of hard coding. PHP is not a compiled language. It is not even precompiled. The interpreter actually reparses it on each request. So when you change the file with business rules definition, it will immediately take effect. You want to version that file separately from the core of the application or you may even store it in the database, but PHP is probably simplest to implement, quite simple to maintain and also fastest as any rule interpreter written in PHP is going to add noticeable overhead.
You may want to use some sandbox (quick google reveals at least two implementations for PHP) to ensure that the business rules file can’t touch anything outside the transaction object. This can be added anytime later, you can start without bothering about it. You may also later add some graphical language that will generate PHP into the database or something, but it’s been repeatedly shown in practice that it takes a programmer to edit nevertheless, so it’s effort with questionable return (though it might be a selling point).
(Edit) As I looked at decision tables, they don’t seem to be very flexible. They need a column for every possible criterion, so as the rules grow in complexity, the table will need more and more columns and it won’t be easy to understand any more anyway.
Also don’t forget that an e-commerce package does not live in void, but needs to be integrated with various other systems. Most shops will probably already have a accounting system where they need to export data about finished transactions and store management system where they need to export data about goods to ship or in case of software shops accounts to create and licenses to assign, they will be using different payment processor to which you have to redirect and receive payment confirmation, etc. For these the logic will be different, so simple rules won’t cut it. You absolutely need to support writing this in PHP, independent from the main core just like the business rules you mentioned.
6
For my own purposes I considered https://github.com/bobthecow/Ruler – a production rules engine. It is applicable to your needs: Give Customer X, a 10% a discount for Product Y if he bundles it with Product Z.
I’ll try to write some sort of a pseudo-code:
Context: your shopping cart items
Rules: cart contains ProductY and ProductZ
Execute: ProductY->discount(10%)
1
We just have released our Decision Tables Engine that can make decisions on data that you will send to it. Take a look, maybe it will fit your needs.
You simply need to send data to it, and to set-up set of rules (without coding) that will return decision to your product. Also you can re-use this rules in any other projects.
https://gndf.io/
I think right now its the only free decision engine with open code that is built on PHP. Other free alternative is Drools (works on Java JBoss) that was recommended in other answers.
0