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.isOk(); 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, ps1; conditions p.getDiscs() > 1; !p.isOk(); ps1 == p.getSub1(); ps1 != null; ps1.isOk(); 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, ps1, ps2; conditions ps1 != null; ps2 != null; ps1 == p.getSub1(); ps2 == p.getSub2(); !p.isOk(); ps1.isOk(); ps2.isOk(); actions p.setOk(true); modified(p); } rule oneDisc { // Base case declarations Hanoi p; conditions p.getDiscs() == 1; !p.isOk(); actions p.addMove(p.getSource(), p.getDestination()); p.setOk(true); modified(p); } }