Question

What I'm trying to do is create a Web based rules engine where users can create conditional statements, store them in a db, then execute them on a dataset. (E.g., If color equals green, then append something to description)

So what I'm thinking is using a JavaScript front-end rules builder (https://github.com/joshuamcginnis/rules-builder) to build an expression in JSON, and store that command expression in MongoDB.

Now when those rules need executed, they get pulled from the DB, and built and executed via Ruler - A stateless PHP rules engine (https://github.com/bobthecow/Ruler)

So the missing piece for me is how I can translate the JSON command into a chained PHP command that Ruler understands? Is there a design pattern that addresses something like this?

Was it helpful?

Solution

So this sounds like you're going to have to write a parser (or interpreter), one that can evaluate commands such as equal and equalOrGreaterThan or short synax = and >= from the JSON file and build the expressions based of these commands.

Take a look at this link http://www.slideshare.net/relaxnow/lets-build-a-parser, it will give you some insight into what I mean. I wish I could quote it but its a slide show.

OTHER TIPS

I think a batter approach is using a service rule engine and expose it as a service. Then your javascript app can simply communicate to the service, execute and manipulate rules.

The benefit of this approach is your rule execution does not happen on client, and you can do interesting stuff, like connecting to database back ends without compromising anything and run task as your action of rules. It is worth mentioning not all the rules are capable of being executed on client side, because different types of dependencies: data, service, resource... So running them on server side (in most of the cases) are preferable. And most importantly when run on server, those rules then can be shared between multiple clients.

As an example you can check decision as a service sample and see how a generic javascript can communicate to a server for execution.

We just released and actively support project called Gandalf, you can find it here: http://gandalf.nebo15.com. Gandalf consists from two separate project: decision engine back-end and front-end for it.

I looks like if fits your needs, except it's build on different libraries.

Pros and Cons for you:

  • you don't need to re-invent GUI, or to worry about back-end support.
  • but you will need to remove current solution and use new one via API.

It's an old question you've done, but today (Feb-2020) there is a nice package that handles exactly your problem. Check it out: https://github.com/nicoSWD/php-rule-parser.

You can store a plain text rule and then use this package to parse it and execute it. It does really cool stuff, like this simple statement:

$variables = ['foo' => 6];
$rule = new Rule('foo in [4, 6, 7]', $variables);
var_dump($rule->isTrue()); // bool(true)

Or this way more complex stuff:

use nicoSWD\Rule;

$ruleStr = '
// This is true
2 < 3 && (
    // This is false
    foo in [4, 6, 7] ||
    // True
    [1, 4, 3].join("") === "143"
) && (
    // True
    "foo|bar|baz".split("|" /* uh oh */) === ["foo", /* what */ "bar", "baz"] &&
    // True
    bar > 6
)';

$rule = new Rule($ruleStr);
var_dump($rule->isTrue()); // bool(true)

I hope it fits your needs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top