Skip to content

paulk-asert/groovy-rules

Repository files navigation

groovy-rules

Source code examples for this talk:

http://www.slideshare.net/paulk_asert/groovy-rules

Solves various logic puzzles using a variety of approaches and tools. Some of the examples use various DSL techniques to provide a friendly readable layer above the underlying tool/library api.

Tortoises and Cranes

There are some cranes and tortoises. They are 7 in total, and their legs are 20 in total. How many cranes and tortoises are there?

This is a basic "hello world" puzzle. It is solved using:

Menu Puzzle

This puzzle is inspired by this xkcd webcomic:

It is solved using:

  • the Choco constraint solving library
  • the OrTools constraint solving library

Einstein's Riddle

Einstein's Riddle (a Zebra Puzzle variant). This version is modelled on the one from this book of the same name.

Background

There are five houses painted five different colors. A person with a different nationality lives in each house. The five house owners each drink a different beverage, play a different sport, and keep a different pet. Who owns the fish?

The Rules

  • The man from the center house drinks milk
  • The Norwegian owns the first house
  • The Dane drinks tea
  • The German plays hockey
  • The Swede keeps dogs
  • The Briton has a red house
  • The owner of the green house drinks coffee
  • The owner of the yellow house plays baseball
  • The person known to play football rears birds
  • The man known to play tennis drinks beer
  • The green house is on the left side of the white house
  • The man known to play volleyball lives next to the one who keeps cats
  • The man known to keep horses lives next to the man who plays baseball
  • The man known to play volleyball lives next to the one who drinks water
  • The Norwegian lives next to the blue house

It is solved using:

  • prolog directly (for comparative purposes)
  • prolog underneath a Groovy DSL
  • the Choco constraint solving library beneath a Groovy DSL
  • the JaCoP constraint solving library beneath a Groovy DSL

The interesting thing to note is that the "business rules" are the same for each DSL solution. The "DSL helper code" would typically be hidden from the user.

The prolog4j generic prolog interface api is used along with the tuprolog prolog engine but you can try some of the other engines supported by prolog4j if you wish.

Choco and JaCoP offer similar features as far as this problem is concerned. Given that the JaCoP package isn't available in a public Maven repository and has a restrictive GPL license, we have a preference for Choco for this example; but see the respective documentation of the two packages to see which better suits your needs. Follow the instructions in the JaCoP directory for downloading the needed jars if you want to try it. The other examples should run automatically.

Further Information

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published