deliveredtechnologies / rulebook

100% Java, Lambda Enabled, Lightweight Rules Engine with a Simple and Intuitive DSL
http://www.deliveredtechnologies.com
Apache License 2.0
716 stars 124 forks source link

Auditing Rules when defining them through an Object with defineRules #167

Closed mikelear closed 4 years ago

mikelear commented 5 years ago

Hi @Clayton7510 before I keep digging further could you clarify , that I'm not missing something or doing something wrong.

If you look at the test

ruleBookAuditorCanAuditRules

it checks that the Rules are correctly Audited. But I'm trying to Audit Rules that are defined in a Class with defineRule overridden.

So if I create a similar test but instead write this

RuleBook rulebook = RuleBookBuilder.create(SimpleRuleAdds.class).asAuditor()
.build();

  rulebook.run(new FactMap());
  Auditor auditor = (Auditor)rulebook;

  Assert.assertEquals(auditor.getRuleStatus("Rule1"), RuleStatus.EXECUTED);

where SimpleRuleAdds is

public class SimpleRuleAdds extends CoRRuleBook {

@Override
public void defineRules() {

    addRule(
            RuleBuilder.create().withName("Rule1")
            .when(facts -> true)
            .then(facts -> { } )
            .build()
            );

then the _auditor is never set. This is because the addRule call isn't using the RuleBookAuditor , but I'm not sure how it should be. If I try to extend RuleBookAuditor instead of CoRRuleBook that doesn't work either.

I can dig some more and am happy to do so , but just wanted to check that there isn't a correct way to do this now (perhaps extending RuleBookAuditor), before I carry on debugging the code.

mikelear commented 5 years ago

I've create a pull request #168

That I think solves this issue , the SimpleRuleAdds class has to extend RuleBookAuditor and you have to have a constructor that create the RuleRunner.

I've added a new test in RuleBookAuditorTest that shows this working (with some changes to pass the Audit Map).

Clayton7510 commented 5 years ago

Cool, I'll check it out. Thanks.

Clayton7510 commented 4 years ago

I pulled over your code, merged it and gave your attribution in the POM. Thanks!