1 Functional Visitor Ø Motivation Example Container checking -- Make sure each Container is not overloaded Weight Item + int total( ) w Container + int.

Slides:



Advertisements
Similar presentations
The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Advertisements

Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
The Singleton Pattern II Recursive Linked Structures.
Written by: Dr. JJ Shepherd
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Explaining DJ with AOP. DJ ClassGraph.traverse (this,whereToGo,whatToDo) Intertype declaration Advice.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Exceptions Problems with error reporting so far –Either ignored exceptions or terminated program on first error. –Error handling and regular code mixed.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Java CourseWinter 2009/10. Introduction Object oriented, imperative programming language. Developed: Inspired by C++ programming language.
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
OOP Languages: Java vs C++
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Ics202 Data Structures. U n i v e r s i t y o f H a i l 1. Stacks top push (8)push (2)
Chapter 3 Introduction to Collections – Stacks Modified
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Chap. 1 Classes, Types, and Objects. How Classes Are Declared [ ] class [extends ] [implements,, … ] { // class methods and instance variable definitions.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Programming in Java CSCI-2220 Object Oriented Programming.
Types in programming languages1 What are types, and why do we need them?
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Object-Oriented Programming Simple Stack Implementation.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
Exception-Handling Fundamentals  A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of.
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
DJ: traversal-visitor-style programming in Java Josh Marshall/ Doug Orleans Want to add more on traversal through collections and Aspectual Components.
CS390 - Summer CS 390 Unix Programming Environment Topics to be covered: Introduction to Object-Oriented programming in Java.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Written by: Dr. JJ Shepherd
Overview of C++ Polymorphism
Collections Using Generics in Collections. 2 Chapter Objectives Define the concept and terminology related to collections Explore the basic structure.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
1 Chapter 21 Generics. 2 Objectives F To use generic classes and interfaces (§21.2). F To declare generic classes and interfaces (§21.3). F To understand.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Features of AOP languages AOP languages have the following main elements: –a join point model (JPM) wrt base PL –a specification language for expressing.
Modern Programming Tools And Techniques-I
Sixth Lecture ArrayList Abstract Class and Interface
Java Programming Language
ADAPTIVE PROGRAMMING Sezen ERDEM December 2005.
DemeterF: Functions and Traversals in Combination
CMSC 202 Interfaces.
class PrintOnetoTen { public static void main(String args[]) {
Generics, Lambdas, Reflections
Overview of C++ Polymorphism
Introduction to Object-Oriented Concepts in Java
CMSC 202 Interfaces.
Introduction to Methods and Interfaces
Presentation transcript:

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 Brown University

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 Ø 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 Ø 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 Ø 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 Ø 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 Ø 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 Ø 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 Ø 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 Ø 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.