Ruler
Ruler copied to clipboard
Produce results in response of rule evaluations
See #94 for detailed information.
Todo:
- [x] Implement the
Rulescollection with anSplPriorityQueue - [ ] Use the
hoa/heaplibrary for theRulescollection - [x] Implement the
Thenrule class (it returns a result when the rule validates the input) - [x] Implement the
ThenElserule class (it returns a result when the rule validates the input and another when the rule does not validate the input) - [ ] Implement an algorithm to resolve rules dependencies (indeed, a rule can depend on another one).
hoa/graphcould be a good candidate for this kind of work. - [x] Implement a
rule()function to reference the result of one rule from another rule
Some notes about the technical choices:
- I used some
clones in the code: this is to guarantee immutability of the passed objects. For example theHoa\Ruler\Rules::initializeRuler()may alter the ruler's asserter. Before doing this, we clone the ruler and then alter its content. Finally we return it to be used where needed. - I introduced a
Hoa\Ruler\Resultclass (with a magic method: this will change). It also does acloneof the result in some cases: this is to avoid mutating the value returned by the rules. - This class (
Hoa\Ruler\Result) is also here to return a structured result letting developers keep track of the returned value, the rule that produced it and its name in the collection
@jubianchi Tell me when you need a review.
@Hywan you can start reviewing when you want: any comment is good.
There are still some things to do but I'd be glad to fix any issue you already saw ;)
ping?
@jubianchi I am interested in this feature as well. Is there anything I can help you with?
I can finish this issue if someone can give me:
- clear feedbacks,
- code review,
- clear TODOs
With that I would be able to ship new code and fix everything you spotted in a single iteration. TBH I won't have time to come back here after every push so I'd rather do everything in a single iteration.
Hi,
I took a look at the code because it seems similar to something I'm looking for. afaict, this PR provides classes to associate a result with a rule (Then & ThenElse) and a class to get the first valid result of a set of rules. While that's useful it's not exactly what I'm after. I was hoping to have rules themselves return a result instead of a boolean, eg:
$rule = 'apply_discount(price, if(premium, 0.9, 1))';
$ruler = new Hoa\Ruler\Ruler();
$context = new Hoa\Ruler\Context();
$context['user'] = true;
$context['product'] = '100';
// add apply_discount
assert($ruler->execute($rule, $context) === '90');
is this out of the scope of this PR ?
Coverage remained the same at 97.383% when pulling 2e8f5138f702c4fe9bf0d75a555ffa8f6ad16962 on jubianchi:rule-results into 28174caf3be01144e7e213fce75598404570e094 on hoaproject:master.
Coverage remained the same at 97.383% when pulling 2e8f5138f702c4fe9bf0d75a555ffa8f6ad16962 on jubianchi:rule-results into 28174caf3be01144e7e213fce75598404570e094 on hoaproject:master.
@jubianchi Should we do another round review?
@Hywan yes you can if you think this feature really makes sense in Ruler.
I was asking myself if it should stay here or go somewhere else...
BTW I updated the PR to use hoa/heap instead of SplHeap as requested in previous discussions.