Drools Petstore Example in Prolog
http://sujitpal.blogspot.com/2009/09/drools-petstore-example-in-prolog.html I recently came across Prova, a Prolog dialect over the JVM using Mandarax as its inference engine. I have used SWI-Prolog before, but only as a simple inference engine for ontology relationships. Trying to develop an example in Prolog to try out with Prova, I realized that I couldn't code Prolog to save my life, so I set about trying to remedy that. This post is a result of that effort.
The example here is based on the Drools Pet Store Example. The Petstore in our simplified example sells only (a single unspecified species of) fish, fish food and fish tanks. Fish are $5/ea, food packets are $2/ea, and fish tanks are $40/ea. At checkout, some amount of upselling and customer coddling takes place, which are enshrined in the following rules:
- Give customer free fish food, one packet for every 5 fish (s)he bought.
- If customer has purchased fish, ask customer how much more fish food he wants. Add this to cart.
- If customer has purchased 10 fish or more, and no tank to put them in, ask if (s)he wants a tank. If customer says yes, add to cart.
- For orders over $50, apply a 10% discount.
The equivalent rules in Prolog are shown below. It is very likely that the code could have been more concise, had it been written by someone with more Prolog programming experience, but c'est la vie. The idea is that checkout goal would be invoked (possibly from a Java application) at the checkout phase, and it will be passed the shopping cart as a Prolog sequence. The cart can then be queried from the factbase using cart(X). Currently the code is written as a standalone Prolog application, it will probably require some changes when it is hooked up to a Java caller.