// A simple Demeter/Java program for checking capacity constraints Container { traversal allWeights( CheckingVisitor cV, SummingVisitor sV ) { to Weight;} (@ // traverse while counting and summing void checkCapacity() throws Exception { SummingVisitor sV = new SummingVisitor(); CheckingVisitor cV = new CheckingVisitor(); cV.set_sV(sV); this.allWeights(cV, sV); } @) } SummingVisitor { init (@ total = new Integer(0); @) before Weight (@ System.out.println("sum " + total.intValue()); total = new Integer(total.intValue() + host.get_i().intValue()); @) } CheckingVisitor { init (@ initial = new Integer(0); @) before Container (@ this.set_initial(sV.get_total()); System.out.println(" start new container "); @) after Container (@ int cap = host.get_capacity().get_i().intValue(); int diff = sV.get_total().intValue() - this.get_initial().intValue(); if (diff > host.get_capacity().get_i().intValue()) { System.out.println(" total weight " + diff + " but limit is = " + cap + " OVER CAPACITY "); }; System.out.println(" end container "); @) } Main { (@ static public void main(String args[]) throws Exception { // Runtime rt = Runtime.getRuntime(); // rt.traceMethodCalls(true); Container a = Container.parse(System.in); a.checkCapacity(); System.out.println("done "); } @) }