Open jubianchi opened 8 years ago
That's an excellent idea. I would change the API though. Why not having:
Rules::add(string $id, $ruleKind, int $priority)
where $ruleKind
is a Then
or other rule.
Thus, behind add
, we will have a graph.
Thoughts?
@Hywan you are totally right. I made some mistake while writing the issue. They are now fixed.
@jubianchi What do you think of my API proposal?
yeah! far better!
@Hywan I'm totally ok with your API proposal, I edited the issue's description:
- [the
Hoa\Ruler\Rules
class] has aadd(string $id, Rule $rule, int $priority)
method, [...]- the
$rule
is a standard ruler rule (a string or a compiled rule) wrapped in aThen
orThenElse
class,
Ruler is well designed for writing rules and making assertions on them (i.e checking if some input validates the rule).
An nice feature would to be able to produce any result in response of these assertions (i.e if some input validates a rule then the engine should provide us a value).
Let's take a simple example:
With only ruler, I would have to write two rules:
$r1 = 'person.age >= 16'
$r2 = 'person.age >= 18'
And some PHP code:
This is not really efficient: the more rules I have, the more code I need to write. If the results change, I'll have to change my PHP code, ...
What I propose here is something more flexible:
Let me explain:
Hoa\Ruler\Rules
is a collection of rules, ordered by a priority,add(string $id, Rule $rule, int $priority)
method,$id
lets us reuse the rule result in following rules,$rule
is a standard ruler rule (a string or a compiled rule) wrapped in aThen
orThenElse
class,$priority
is used to order the collection (higher priority will run first);getBestResult
) which will be the result of the higher priority rule validating the context,getAllResults
) of valid rulesThis kind of feature will allow for more generic code when it comes to work with rules and produce results depending on them.
The
Rules
collection can easily be built with results coming from a database.What has to be done:
Rules
collection (first with anSplPriorityQueue
and then with thehoa/heap
library)Then
rule class (it returns a result when the rule validates the input)ThenElse
rule class (it returns a result when the rule validates the input and another when the rule does not validate the input)hoa/graph
could be a good candidate for this kind of work.rule()
function to reference the result of one rule from another ruleLet me know if I forgot something. I have a working POC which needs some cleanup.