// BEHAVIOR //======================================================================== // A simple Demeter/Java program for checking capacity // constraints for recursive containers. // Three behavioral concerns: Checking, Summing, Difference // abbreviated C S I // Two generic concerns: traversal, behavior modification (visitor) // count transition points, except in formal or actual parameters Container { traversal allWeights( //1 // ordering of visitors is important // difference must be initalized before it is used // after methods are called in reverse order of appearance // it is important that DifferenceVisitor is after CheckingVisitor // could also use: CheckingVisitor DifferenceVisitor SummingVisitor ordering CheckingVisitor cV, SummingVisitor sV, DifferenceVisitor iV ) { to Weight;} (@ // traverse while counting and summing int checkCapacity() { // CheckingVisitor cV = new CheckingVisitor(iV, null); // SummingVisitor sV = new SummingVisitor(); // DifferenceVisitor iV = new DifferenceVisitor(null, null, sV); CheckingVisitor cV = new CheckingVisitor(); //1 SummingVisitor sV = new SummingVisitor(); //1 DifferenceVisitor iV = new DifferenceVisitor(); //1 cV.set_iV(iV); //2 iV.set_sV(sV); //2 this.allWeights(cV, sV, iV ); //1 return (cV.get_return_val()); //1 } @) } //1 SummingVisitor { init (@ total = new Integer(0); @) before Weight (@ System.out.println("sum " + total.intValue()); total = new Integer(total.intValue() + host.get_i().intValue()); @) return int (@ total.intValue() @) } //1 CheckingVisitor { init (@ violations = new Integer(0); @) before Container (@ System.out.println(" start new container "); @) after Container (@ int cap = host.get_capacity().get_i().intValue(); int diff = iV.get_return_val(); if (diff > cap) { this.set_violations(new Integer(get_violations().intValue() + 1)); System.out.println(" total weight " + diff + " but limit is = " + cap + " OVER CAPACITY "); }; System.out.println(" end container "); @) // Demeter/Java requires to give the return type after return return int (@ violations.intValue() @) } //1 // it is better to return difference DifferenceVisitor { // stack handling init (@ stack = new Stack(); @) before Container (@ stack.push(sV.get_total()); @) after Container (@ difference = sV.get_return_val() - ((Integer) stack.pop()).intValue(); @) return int (@ difference @) } Main { (@ static public void main(String args[]) throws Exception { Container a = Container.parse(System.in); int result = a.checkCapacity(); if (result == 2) { System.out.println("SUCCESS"); } else { System.out.println(" FAILURE " + result); } System.out.println("done "); System.out.println("DJ start "); // DJ part ClassGraph cg = Main.buildClassGraph(); System.out.println(); TraversalGraph allWeights = new TraversalGraph( "from Container to Weight",cg); System.out.println(" check container "); int result2 = a.checkCapacityDJ(allWeights); System.out.println(" DONE: check container "); if (result2 == 2) { System.out.println("SUCCESS"); } else { System.out.println(" FAILURE " + result2); } } @) }