Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fact checking before evaluating rules #204

Open
AlDante opened this issue May 24, 2021 · 2 comments
Open

Fact checking before evaluating rules #204

AlDante opened this issue May 24, 2021 · 2 comments

Comments

@AlDante
Copy link

AlDante commented May 24, 2021

Hi,

I am currently trying to use RuleBook to define a bidding system for bridge. It seems well suited for this - typically a bidding system has rules such as "If you have a no trump shape and 12-14 high card points, open One No Trump".

While testing my rule book I noticed that I had, in the test, forgotten to define some facts that the RuleBook needed to evaluate the hand. For example, maybe I forgot to set the value of the high card points - then the RuleBook has insufficient information to return a result.

Is there a standard way of checking that all required facts have been defined before a RuleBook call?

Code example below (where I have forgotten to define a fact "openingSuit" which the RuleBook wants).

class TestAcolBidRuleBook {

    private static RuleBook<Bid> acolBidRuleBook;
    private static final FactMap<Integer> acolBidFacts = new FactMap<>();



    @Test
    void testRules() {

        acolBidRuleBook = RuleBookBuilder.create(AcolBidRuleBook.class).withResultType(Bid.class)
            .withDefaultResult(Bid.NO_BID)
            .build();

        acolBidFacts.setValue("hcp", 12);
        acolBidFacts.setValue("maxSuitLen", 6);
        acolBidFacts.setValue("handType", HandType.NOTRUMP.getAsInt());

        acolBidRuleBook.run(acolBidFacts);

        Optional<Result<Bid>> bidResult = acolBidRuleBook.getResult();
        boolean bidPresent = bidResult.isPresent();


        acolBidRuleBook.getResult().ifPresent(result -> System.out.println("Hand would bid: " + result));
    }
}
@Clayton7510
Copy link
Collaborator

That's an interesting question. While there is no specific mechanism for checking that required facts exist, there are a few tools at your disposal with RuleBook. I also think that it might be a nice feature addition to add targeted functionality to specify required facts.

In the meantime, you could certainly have a precondition rule that does nothing but check facts and either return no result or throw an exception (based on your logic) if certain facts are not available. Another option could be to create a facade on top of your RuleBook that requires certain facts to be added prior to executing the RuleBook.

@AlDante
Copy link
Author

AlDante commented Jun 11, 2021

I like the precondition rule idea, thanks. How do I continue rule processing if the preconditions are satisfied? I thought rule processing stopped when the first matching rule fires.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants