Download presentation
Presentation is loading. Please wait.
Published byJonah Moore Modified over 9 years ago
1
1 Functional Visitor Ø Motivation Example Container checking -- Make sure each Container is not overloaded Weight Item + int total( ) w Container + int total( ) + void check() contains Simple Capacity capacity * Ø Initially proposed by Shriram Krishnamurthi @ Brown University
2
2 Ø Motivation Example ØTypical Object-oriented Implementation abstract class Item { int total() {return getWeight().getValue();} } class Simple extends Item { } class Container extends Item { Vector contains; int total() { int t=0; //for each element of the Vector, call total(), then sum // toghether to t; return t; } void check() { if(getCapacity()< this.total()) System.out.println(“oveerloaded”); //for each element of the Vector, if instanceof(Container) // call check() on it. }
3
3 Ø Motivation Example ØHow about Adaptive implementation (by DJ) DifferenceVisitorDJ { {{ // stack handling public void start() { stack = new Stack(); } public void before(Container o) { stack.push(sV.getReturnValue()); } public void after(Container o) { difference = ((Integer) sV.getReturnValue()).intValue() - ((Integer) stack.pop()).intValue(); } public Object getReturnValue() {return new Integer(difference);} }} } ØUsing side effect, lose the power of recursive computation and combinability provided by recursive function
4
4 Ø Motivation Example ØBut we do want the structure-shy feature of Adaptive programming, i.e. DJ. ØWhy not let the Visitor have access to the return value of its sub traversals, so that we can simulate the functional programming style in Visitor? ØIt implies we have to all Visitor methods having return value.
5
5 Ø Interface of Functional Visitor ØThree classes: ØFuntionVisitor, FuntionalOGS, Subtraversal Ø In a subclass of FunctionVisitor, you can define method like: Object around(SomeClass h,Subtraversal st); Object combine(Object[] values); Ø The semantics (informal) of the around method is as follows: when the traversal reaches an object whose type matches SomeClass, the return value of the traversal on this object is determined by the return value of this around method. The return value is available to its parent object for further combination. The Subtraversal class is provided by the run time library and the object will be passed to the around method automatically at run time.
6
6 Ø Interface of Functional Visitor ØIn the body of the Object around(SomeClass h,Subtraversal st), you can control how the sub traversals will going and access the return values of the sub traversals, by one of the following four ways: Object apply(String label) Object[] applySuper() Object[] applyElements(String label) Object[] apply() ØIn your around method, you have the complete control of the traversal Ø If you don't define around method on a class, then the default combine semantics will be applied automatically to the array of Object which get from each of the edges from the class. The way you want to combine is specified as Object combine(Object[] values).
7
7 Ø Interface of Functional Visitor Ø It is backward-compatible with the default DJ package, i.e., if you don't define any around or combine method in a visitor class, then it will behave just the same as an ordinary DJ visitor
8
8 Ø Container Example implemented by Function Visitor class Main { static public void main(String args[]) throws Exception { ClassGraph cg=new ClassGraph(true,false); Container a = Container.parse(System.in); TraversalGraph allWeights = new TraversalGraph( "from Container bypassing -> *,tail,* to Weight",cg); FunctionalOGS fogs=new FunctionalOGS(a,allWeights); fogs.traverse(new DifferenceVisitorDJ()); }
9
9 Ø Container Example implemented by Function Visitor class DifferenceVisitorDJ extends FunctionVisitor { Object combine(Object[] values) { int total=0; for(int i=0; i<values.length; i++) { if(values[i]!=null) total+= ((Integer)values[i]).intValue(); } return new Integer(total); } Object around(Weight w,Subtraversal st) { return w.get_i(); } Object around(Container c,Subtraversal st) { Integer total = (Integer)st.apply("contents"); if(total.intValue() > c.get_capacity().get_i().intValue()) System.out.println(" A Overloaded Container"); return total; }
10
10 Ø Conclusion Ø Functional Visitor combines the power of Adaptive Programming and functional programming Ø Users have more flexibilities to control the traversal at run time Ø Ironically, the functional style is implemented by side effect. Ø You will use it in a more complex situation, i.e., to find unbound variables in a language struct.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.