Comp 212 Homework 3
Visitor Pattern Exercises

Due Monday Oct. 4, 1999 10:00a.m. (in class)

Preparation

Study the class lecture notes on the Visitor pattern and lab #4.

Problems

  1. You are to apply the visitor design pattern to the boolean expression classes defined in problem #2 of homework #2 as follows.

    Define an interface called  IBEVisitor   (as in Interface for Boolean Expression Visitor) with Object as the return type for the various visitor methods (forConstant, etc.) in the interface.  The visitor methods should have the appropriate parameter list as discussed in the lectures and lab #4. 

    In lieu of the method Constant eval (Environment env), define the hook method Object accept (IBEVisitor v, Object input) for ABooleanExpression and all of its sublasses.

    Define an expression evaluator as a concrete class EvalVisitor implementing IBEVisitor.   The input Object for the visitor methods of EvalVisitor should be an Environment object, and the output Object should be a Constant object.  Again, review lab 4 and the lecture notes if you do not understand this pattern language.

    Write appropriate test code to test your design.

  2. Add an if-else expression to the syntax of Boolean expressions where an if-else has the form
    if a then b else C
    

    where a, b, and c are ABooleanExpression.  It is logically equivalent to (and (> a b) (> !a c)) and is also called a conditional expression.  Use a concrete class called IfElse to represent an if-else expression. In the interest of brevity, the toString() method should produce the string

    (? a b c)
    

    for the expression

    if a then b else c
    

    Modify the toString method for Constant so that

    Constant (true) prints as T

    Constant (false) prints as F

    The toString methods for the other classes should remain the same.

    Modify IBEVisitor and EvalVisitor to match with the new taxonomy for ABooleanExpression.

    Write appropriate test code to test your revised design.

  3. Using the same definition of ABooleanExpression and IBEVisitor as in the preceding problem, define a visitor class ConvertVisitor implementing IBEVisitor that converts ABooleanExpression objects to pure if expresssions.  A pure if expression is defined as follows: constants are pure if expresssions, variables are pure if expressions, and if-else are pure if expressions. The following are examples of conversion from non-pure if to pure if.
    (& A B) --> (? A B F)
    (| A B) --> (? A T B)
    (> A B) --> (? A B T)
    !A --> (? A F T)
    

    Write appropriate test code to test your ConvertVisitor class.

Submission

Submit separate hard copies of the UML diagram showing the class designs in questions #1 and #3, respectively.   Submit a separate hard copy for each of the above questions, and staple them separately.  Attach to each question a README text explaining what the problem is about and what you have or have not done, and any other specific details that might help the grading of your question.  Your paper will not be graded if you do not follow this submission instruction.  Auditors for the course should not submit any paper.


dxnguyen@cs.rice.edu