Software Design and Development Fall 2000 COM 1205This assignment is stored in file $SD/hw/5
Assignment 5
Due date: Thursday, Nov. 2
in file assign.html
Use the following header for your homework submission.
Course number: Name: Account name: Assignment number: Date:Whenever you have questions about course material, please send e-mail to:
mail lieber com1205-grader@ccs.neu.edu
The CHANGES and BUG files (or the same files in the src directory of the DemeterJ distribution) tell you about the history of the project and the bugs we know and are working on. The CHANGES file is currently the most reliable source of documentation for DemeterJ. Some parts of the User Manual are dated. The BUGS file.
For this homework part you are encouraged to write a Java program from scratch using DJ. You should not use any tools except a Java compiler and interpreter (no DemeterJ but using DJ is allowed) and of course, an editor and a machine which can run and compile Java.
Write a program for the class dictionary in: $SD/hw/5/visitor-by-hand/program.cd
The relevant classes are:
A = "a" <b1> B <c1> C [D] "enda". B : E | F. C = . D = "d" . E = "e". F = <g1> G <h1> H <a1> A. G =. H = "h".Write three methods in one program for this class dictionary but the three methods are allowed to use only one traversal function traverse(Visitor) attached to all of the above classes.
The three tasks to be done by the three methods are:
$SD/hw/5/visitor-by-hand/program.beh
This also gives you a hint on how to write a PrintVisitor manually.
Call this function of class A: void print().
For more information on PrintVisitor, see the PrintVisitor.beh file in the generated directory (usually called gen) of your homeworks and the source code for DemeterJ which actually generates those files. DemeterJ contains a behavior file which generates the PrintVisitor. See genprinvis.beh in the DemeterJ source code directory for generation (or in the DemeterJ distribution).
For more information on DisplayVisitor, see gendisplayvis.beh in the DemeterJ source code directory for generation which generates the DisplayVisitor.beh files for DemeterJ.
Call this function of class A: int countG().
in class Main: static public void main(String args[]) throws Exception { A a = ... // write constructor calls to build an A-object. See: // /proj/adaptive/www/sources/DemeterJava/examples/j-c-bypassing/program.beh // for an example. System.out.println("print:"); a.g_print(); System.out.println(); System.out.println("tree structure:"); a.display(); System.out.println("count:"); int result = a.countG(); System.out.println(result + " done "); } print() should be implemented by a call traverse(p) where p is a PrintVisitor-object. display() should be implemented by a call traverse(t) where t is a DisplayVisitor-object. countG() should be implemented by a call traverse(s) where s is a CountVisitor-object.To achieve what you want, you need to introduce an abstract Visitor class. For an example, see the abstract UniversalVisitor class which DemeterJ uses. See file program.xcd in the generated directory (usually called gen). See also the the context object paper (journal version) for an extended discussion.
You need an abstract class:
Visitor : PrintVisitor | DisplayVisitor | CountVisitor.If you use DJ, it will provide an abstract visitor class. For the abstract Visitor class you define empty before and after methods which have a host argument. In the subclasses you override the methods where the behavior needs to be non-empty.
Your traversal functions will call the before and after methods of the visitor. First the before method is called, then the traversal is done and then the after method is called.
Turn in your complete Java-program and the output produced for your three test cases.
Repeat PART 1 for the class dictionary in: $SD/hw/5/visitor-by-hand2/program.cd
The relevant classes of the cd are:
A = "a" X C [D]. X = B. B : E | F. C = . D = "d" . E = "e". F = G H. G =. H = "h".Notice that the class dictionary is only slightly different from the one in PART 1.
Now the restriction of using DemeterJ is lifted. You can now take advantage of the brain power which CCS students and faculty have put into DemeterJ. We hope you will be pleased by how the task is now simplified so that you can do it easily yourself in 30 minutes.
Do PART 1 and PART 2 using DemeterJ. Copy the directories:
$SD/hw/5/visitor-by-hand/* and $SD/hw/5/visitor-by-hand2/*
and modify the program.cd and program.beh and program.input files if needed.
To regenerate:
demeterj test
Note that sometimes it is necessary to use "demeterj clean" before "demeterj test".
Make sure you add the visitor classes to the class dictionary.
Please hand in only a short description of the modifications, if any, you did to program.cd and program.beh. What is the number of lines you wrote in your Java program? What is the number of lines in the program.beh and program.cd files? Turn in the two numbers and the ratio of "pure Java" divided by "DemeterJ". Turn in only the files that you changed.
To be good at object-oriented software development, you need to have several skills, including:
We first start with simple class dictionaries. Therefore, first do the work described in 17.5.1 and 17.5.2 and 17.5.3 (except 17.5.3.3) on pages 551 and 552 of the AP-book. Instead of using Demeter/C++, use DemeterJ to test your sentences. This will give you an opportunity to learn about the Java Compiler Compiler. Type "javacc" to find out about all the options JavaCC offers. Also visit the JavaCC website.
Specifically, this means that you will not use sem-check to test your class dictionaries, but
demeterj test
to test your class dictionary and its inputs. It is possible that for some bugs in your class dictionary, the Java compiler will complain and not DemeterJ nor the Java Compiler Compiler. Turn in your class dictionaries and your sentences and for each class dictionary a statement (comment) which says: My class dictionary was accepted by DemeterJ and the Java Compiler Compiler without warning or error.
See http://www.ccs.neu.edu/research/demeter/DemeterJava/ on how to use DemeterJ.
When writing your class dictionaries, follow the Terminal-Buffer rule described on page 393 of the AP book.
Also follow the following Repetition-Buffer rule: A repetition class should be used as the only part class of a construction class. Example:
Body = "{" [Initialize] PathDirective Wrappers "}". Wrappers = [ < wrappers> Wrapper_SList ] . Wrapper_SList ~ Wrapper { Wrapper } .Here Wrappers is the buffer class for the wrapper list. The reason for buffering repetition classes is that their names might change later. If we need to pass around wrappers, it is better to pass around Wrappers-objects than Wrapper_SList-object since Wrapper_SList might be renamed to Wrapper_CVector later.
Now after you have gained experience in designing small class dictionaries, we go for a larger one. This class dictionary is about what you learn in the AP-book: propagation patterns.
Adaptive object-oriented programming has three key ingredients: class dictionaries, traversals and visitors. But sometimes, those ingredients require us to write long programs and therefore, we look for abbreviations, also called syntactic sugar. Propagation patterns are such a mechanism to make it easier to program with traversals and visitors.
Consider the task of computing the total of all salaries paid by a company. We need to define a method called sum_salaries for class Company. To implement the method we need to define a visitor class called SummingVisitor. We also need to define a traversal method "allSalaries". The body of method sum_salaries will instantiate the Visitor and call the method allSalaries with the SummingVisitor-object as argument. For a related example, see http://www.ccs.neu.edu/research/demeter/sources/DemeterJava/examples/j-c-holding/ in file holding.beh.
This all can be said with a propagation pattern:
Company { traversal-pp int sum_salaries() { initialize (@ 0 @) // initialize return_val to 0 to Salary // traversal before Salary (@ return_val = return_val + host.get(v).intValue(): @) } }Propagation patterns are implemented in DemeterJ (as adaptive methods) and the purpose of this part is to design a part of a class dictionary for a slightly different language than DemeterJ.
Extend the class dictionary below so that it can handle the following input:
Company { traversal-pp int sum_salaries() { initialize (@ 0 @) // initialize return_val to 0 to Salary // traversal before Salary (@ return_val = return_val + host.get(v).intValue(): @) } } Shape { traversal-pp Integer gp1x(){ bypassing {-> *,p2,*, -> *,y,*} to Integer before Integer (@ return_val = this; @) } } ClassGraph { traversal-pp void constrClassNames() { through :cdef ClassDef to Construct // the class definition is made available in variable cdef before Construct (@ System.out.println(cdef.getClassName()); @) } } X { traversal-pp int f(float f, int i, A a) { initialize (@ 0 @) bypassing {X,Y, -> *,r,R} through {:x X, :y Y, -> *,r,:rv R} // the wrappers below may refer to x,y and rv to Z before {R,S} (@ ... @) after {U,V} (@ ... @) } }The class dictionary:
// -- class dictionary for DemeterJ with propagation patterns ////////////////////// // Behavior (methods). ////////////////////// // man g_print (Demeter/C++) explains the pretty printing commands // *l *s + - ProgramBehavior = [ <behavior> DList(ClassBehavior) ] . ClassBehavior = ClassName ClassMethods. ClassMethods = "{" *l + [ <methods> SList(Method) ] - "}" . Method : Traversal | TraversalPP| Behavior. Behavior : Wrapper | Verbatim. // Class graph traversal specifications. Traversal = "traversal" TraversalName TraversalArgs "{" *l + PathDirective ";" - *l "}". TraversalPP = "traversal-pp" <returnType> UNKNOWN1 UNKNOWN2 UNKNOWN3 UNKNOWN4. Args = "(" [ <l> Commalist(UNKNOWN5) ] ")". Arg = <typ> JavaTypeName <arg> Variable. Body = "{" [UNKNOWN6] UNKNOWN7 UNKNOWN8 "}". Initialize = "initialize" UNKNOWN9. Wrappers = *l + [ <wrappers> SList(UNKNOWN10) ] - . TraversalArgs = "(" [ <visitors> Commalist(Visitor) ] ")" . Visitor = ClassName VisitorName. PathDirective = [ BypassingDirective ] [ ThroughDirective ] TargetDirective. BypassingDirective = "bypassing" <glob> GlobSpec. ThroughDirective = "through" <glob> GlobSpec. TargetDirective : To | ToStop *common* <targets> ClassGlobSpec. To = "to". ToStop = "to-stop". GlobSpec : OneGlob | GlobSet. OneGlob = Glob. GlobSet = "{" [ <globs> Commalist(Glob) ] "}". Glob : ClassGlob | EdgeGlob. EdgeGlob : PartGlob | SubclassGlob | SuperclassGlob. ClassGlob = <dest> ClassNameGlob. PartGlob = "->" <source> ClassNameGlob "," <edge> PartNameGlob "," <dest> ClassNameGlob. SubclassGlob = "=>" <source> ClassNameGlob "," <dest> ClassNameGlob. SuperclassGlob = ":>" <source> ClassNameGlob "," <dest> ClassNameGlob. ClassNameGlob : ClassNameExact | AnyClass. ClassNameExact = [UNKNOWN11 Variable] ClassName. AnyClass = "*". PartNameGlob : PartNameExact | AnyPart. PartNameExact = PartName. AnyPart = "*". ClassGlobSpec : OneClassGlob | ClassGlobSet. OneClassGlob = ClassGlob. ClassGlobSet = "{" <classglobs> Commalist(ClassGlob) "}". // Before and after wrappers. Wrapper : Before | After *common* <hosts> HostSpec JavaCode. Before = "before". After = "after". HostSpec : ClassGlobSpec. // Verbatim java code. Verbatim = JavaCode. // Terminal buffer classes. DirName = <name> Ident. ClassName = <name> Ident. PartName = <name> Ident. TraversalName = <name> Ident. VisitorName = <name> Ident. MethodName = <name> Ident. JavaCode = <code> Text. JavaTypeName = <name> Ident. Variable = <name> Ident. // Parameterized class definitions. List(S) ~ {S}. SList(S) ~ S { *l S } *l . DList(S) ~ S { *l *l S } *l . Commalist(S) ~ S {"," S}. Barlist(S) ~ S { *l "|" S}. Main = .For this part turn in the UNKNOWNs along with the class dictionaries as mentioned earlier.
V = <return_val> WhatEverType.If the propagation pattern has arguments, we need an additional visitor class called ArgV which has as many parts as the propagation pattern has arguments. The initalization code is used to initialize return_val.
For example, the propagation pattern:
Company { traversal-pp int sum_salaries() { initialize (@ 0 @) // initialize return_val to 0 to Salary // traversal before Salary (@ return_val = return_val + host.get(v).intValue(); @) } }is translated into
Company { (@ int sum_salaries() { SummingVisitor sv = new SummingVisitor( 0 ); this.allSalaries(sv); return sv.get_return_val(); @) traversal allSalaries(SummingVisitor sv) { to Salary; } } SummingVisitor { before Salary (@ return_val = return_val + host.get(v).intValue(); @) }As this example shows, propagation patterns are an abbreviated form for certain kinds of adaptive programs. But propagation patterns don't take advantage of the full power of visitors: They use only very simple visitors.
For doing the assignments you will need to use the DemeterJ tool. You can do that by
Options 1 and 3 require no installation on your part but access will be a bit slow depending on your location.
Option 2 will be faster but you need to install DemeterJ following the instructions off the DemeterJ resource page.
From dougo@ccs.neu.edu Tue Oct 24 15:01:37 2000 Karl Lieberherr writes: > Hi Doug: > > Jon and I have difficulty to see what it means when we get > a NullPointerException inside a DJ class such as Visitor. > > Is there a way to get the system always report user errors > in user classes? Oops, you're right, it discards the stack trace from the exception caused by the visitor. I will fix this, but in the meantime, you can add this method to your visitor classes: public void invokeMethod(String name, Object obj, Class cl) { try { Method meth = getMethod(name, cl); if (meth != null) { meth.setAccessible(true); meth.invoke(this, new Object[] { obj }); } } catch (SecurityException e) { throw new RuntimeException("\n" + e); } catch (IllegalAccessException e) { throw new RuntimeException("\n" + e); } catch (InvocationTargetException e) { e.printStackTrace(); // prints to stderr throw new RuntimeException("\n" + e.getTargetException()); } } It is important to add: import java.lang.reflect.*; to the top of your .cd file, or .java file if you're not using DemeterJ. To add the above code is only necessary in the particular case when you get an exception from your visitor and you can't figure out where it is occurring. The best way to implement the details of this bug fix is to make your own MyVisitor class that has the added method invokeMethod, and then have all your other visitors descend from MyVisitor rather than directly from Visitor. More specifically, in file *.cd you put: import edu.neu.ccs.demeter.dj.*; import java.lang.reflect.*; ... noparse MyVisitor : RemoveVisitor | CommandVisitor common extends Visitor. // add any visitor classes you might have // to MyVisitor. CommandVisitor = . RemoveVisitor = . and you have a separate file repair.beh: MyVisitor { {{ public void invokeMethod(String name, Object obj, Class cl) { try { Method meth = getMethod(name, cl); if (meth != null) { meth.setAccessible(true); meth.invoke(this, new Object[] { obj }); } } catch (SecurityException e) { throw new RuntimeException("\n" + e); } catch (IllegalAccessException e) { throw new RuntimeException("\n" + e); } catch (InvocationTargetException e) { e.printStackTrace(); // prints to stderr throw new RuntimeException("\n" + e.getTargetException()); } } }} } Don't forget to mention repair.beh in your *.prj file.