package jeops.examples.hanoi; /** * Rule base used to solve the towers of hanoi problem. */ public ruleBase BaseHanoi { rule createsFirstSubProblem { // Passing (n-1) discs to the intermediary pin declarations Hanoi p; conditions p.getDiscs() > 1; !p.getOk(); p.getSub1() == null; actions Hanoi ps1; ps1 = new Hanoi(p.getDiscs() - 1, p.getSource(), p.getIntermediate()); p.setSub1(ps1); assert(ps1); modified(p); } rule createsSecondSubproblem { // First one is ok, doing the second. declarations Hanoi p; localdecl Hanoi ps1 = p.getSub1(); conditions p.getDiscs() > 1; !p.getOk(); ps1 != null; ps1.getOk(); p.getSub2() == null; actions p.addMove(p.getSource(), p.getDestination()); Hanoi ps2; ps2 = new Hanoi(p.getDiscs() - 1, p.getIntermediate(), p.getDestination()); p.setSub2(ps2); assert (ps2); modified(p); } rule bothSubproblemsOk { // If both subproblems are ok, then the problem is ok. declarations Hanoi p; localdecl Hanoi ps1 = p.getSub1(); Hanoi ps2 = p.getSub2(); conditions ps1 != null; ps2 != null; !p.getOk(); ps1.getOk(); ps2.getOk(); actions p.setOk(true); modified(p); } rule oneDisc { // Base case declarations Hanoi p; conditions p.getDiscs() == 1; !p.getOk(); actions p.addMove(p.getSource(), p.getDestination()); p.setOk(true); modified(p); } }
jeops.examples.hanoi.Hanoi |
package jeops.examples.hanoi; import java.util.Vector; /** * This class models an encapsulation for a solution for the Towers * of Hanoi problem. * * @version 0.01 15 Mar 1998 * @author Carlos Figueira Filho (csff@cin.ufpe.br) */ public class Hanoi { /** * The number of discs in this problem. */ private int discs; /** * The pin from where the discs come. */ private int source; /** * The pin the disks have to be moved to. */ private int destination; /** * The first subproblem used to solve this problem. */ private Hanoi sub1; /** * The second subproblem used to solve this problem. */ private Hanoi sub2; /** * Flag which indicates whether this problem is solved. */ private boolean ok; /** * The problem solution, made up by several Strings in the * form " |
TestHanoi |
package jeops.examples.hanoi; /** * Class used to test the Towers of Hanoi example for JEOPS. * * @version 0.01 06 Apr 2000 * @author Carlos Figueira Filho (csff@cin.ufpe.br) */ public class TestHanoi { /** * Starts the application. * * @param args a command-line arguments array. None is needed, * but one can pass the number of discs to be moved. */ public static void main(java.lang.String[] args) { int noDiscs; if (args.length == 0) { noDiscs = 3; } else { noDiscs = Integer.parseInt(args[0]); } Hanoi h = new Hanoi(noDiscs, 1, 3); BaseHanoi kb = new BaseHanoi(); kb.assert(h); kb.run(); System.out.println("Problem solution:"); h.dump(2); } } |