Hoa\Ruler, hack book – Hoa

Business rules (like “all the customers that have reached 100€ in one time receive a voucher of 10% on the next purchase”) are most of the time defined outside the application. They are even often written in a different language than the used programming language. The Hoa\Ruler library provides an engine allowing to simply execute business rules while being efficient and very extensible.

This is a companion discussion topic for the original entry at https://hoa-project.net/En/Literature/Hack/Ruler.html

Automatic backup from previous comment system

Stéphane PY said (Thu Apr 02 2015 15:30:39 GMT+0200 (CEST)):

Good documentation, easy to read…

Automatic backup from previous comment system

Daniel G said (Tue Oct 27 2015 22:26:07 GMT+0100 (CET)):

I was thinking of doing something similar but much simpler. Thanks for sharing!

Automatic backup from previous comment system

Vincent Clair said (Fri Apr 01 2016 10:15:27 GMT+0200 (CEST)):

Thanks for this awesome library. I have a question about security: if I use a app that construct a rule on front-end and send it to the back-end to make request on a database. Need I validates values from injections ? The Ruler compiler could only give String, Boolean, Float, Int. The only problem could come from String. But as I use prepare queries and inject values by parameters, I think there could not be a vulnerability. Am I right ? Do you see other problems ?

Automatic backup from previous comment system

@Hywan said (Fri Apr 01 2016 15:11:39 GMT+0200 (CEST)):

You must still prevent XSS & co. attacks, but a rule represents only a boolean. You can still use the Hoa\Ruler\Ruler::interpret method to syntactically validate your rule.

Automatic backup from previous comment system

Ana said (Fri Apr 22 2016 12:41:17 GMT+0200 (CEST)):

I got here via Google, and was not able to find a link to this page via the Literature link on the menu. Could you please tell if there are other resources that are (unintentionally) hidden this way? And how to find them?

Can the Hoa\Math be used in conjunction with Ruler? So you could ask if “20 * 40 > 20”

Can the syntax for variables be defined (for example variables looking like [user] or :user or {{user}}) ?

Hello :slight_smile:,

The hoaproject/Ruler#11 issue is opened and is about this specific problem. Merging two grammars is a difficult problem, and an important engineering effort is required. However, you can still create a times function and use it like this: times(20, 40). This is very convinient but it does the job quite well.

You can revive the opened issue :slight_smile:.

Hello :slight_smile:,

The syntax for variable is unprefixed identifier, like x or user to take your example. Why would you need a more complex syntax? What is your usecase?

Note that […] is already used to represent an array.

My use case is that I already have existing rules in existing system and variables were tagged as [var] there. Everything else was the same. I wanted to switch to using an existing rules engine rather than continuing building my own, but need a way to use/convert the existing rules. For example, see how these guys have solved this feature: https://packagist.org/packages/nicoswd/php-rule-parser They allow something like this: $rule->registerToken(Tokenizer::TOKEN_VARIABLE, ‘[[a-zA-Z_][\w-]*]’); Which would do exactly what I would like. I was hoping for something equivalent.

I understand your usecase but we are not planning to change the grammar, or to add the ability to change the grammar yet. However, you can still fork the library and change the Grammar.pp file. Be aware that [ and ] are already used to represent an array.

If you think this is a really important feature, please open an issue on Github (https://github.com/hoaproject/Ruler) and ask the community for a feedback.