Software Design and Development Homework 1

Winter 2003 COM 1205

(you can download the text version (http://www.ccs.neu.edu/home/lieber/com1205/w03/hw/1/assign.html) here by right clicking)




Whenever you have questions about course material, please send e-mail to:

lieberherr@ccs.neu.edu
com1205-grader@ccs.neu.edu
 

The teaching assistant is Pengcheng Wu (wupc@ccs). He is a PhD student in our college and has actively contributed to the aspect-oriented technology we are using.



Due date: This assignment is stored in file $SD/hw/1 in file assign.html
SD=/home/lieber/.www/com1205/w03

THEME: Reverse Engineering of Java programs
Learn basics of XML by studying Demeter example:
Data type definitions (DTDs), documents and
Java code generation from XML DTDs.




On CCS Northeastern machines,
SD=/home/lieber/.www/com1205/w03
DemJ=/proj/adaptive/www/DemeterJava
DJ=/proj/adaptive/www/DJ
D=/proj/adaptive/www/
 

On the WWW,
SD = http://www.ccs.neu.edu/home/lieber/com1205/w03
DemJ = http://www.ccs.neu.edu/research/demeter/DemeterJava
DJ = http://www.ccs.neu.edu/research/demeter/DJ
D = http://www.ccs.neu.edu/research/demeter/
 

To comply with college structures, we also maintain:
/course/com1205/w03 = SD and
/course/com1205/.www = http://www.ccs.neu.edu/course/com1205 = http://www.ccs.neu.edu/home/lieber/com1205.html

The directory containing administrative information and homeworks is: $SD



Use the following header for your homework submissions and put it at the top:

Course number: COM 1205
Name:
Account name:
Assignment number:
Date:

Do NOT include your student id number in any work you submit because some of your work may be put on the web and you don't want your social security number visible.


READING:
Read chapters 1, 3 and 4 of the AP book.
Read: chapter 1: A Quick Tour of Java in the Java book by Arnold/Gosling or
the approximately equivalent information in your favorite Java book.

Skim read the following:
$DemJ/forReadersAPBook/APbookWithDJ.html
$DemJ/forReadersAPBook/APbookWithDemJava.html
$DemJ/forReadersAPBook/simple-example.txt
 

About UML class diagrams at www.rational.com/uml
About XML: http://www.xml.com/axml/axml.html
(XML = eXtensible Markup Language is a W3C Recommendation,i.e., it is a standard.)
 

Read about the Law of Demeter:

Read section 26: "Decoupling and the Law of Demeter" in the text book The Pragmatic Programmer (TPP). Bring an answer to the Challenges question on page 142 to the next class.

The following is from the wiki page shown below:
You can play with yourself.
You can play with your own toys (but you can't take them apart),
You can play with toys that were given to you.
And you can play with toys you've made yourself.
For more information:
http://c2.com/cgi/wiki?LawOfDemeter

Extra credit assignment (honor students): Read the above web page carefully and write a response to this wiki webpage. I think that some of the authors make incorrect statements and I believe that the DJ tool is not known to the authors. This is a suitable extra credit assignment after the 5th week of classes.

Second extra credit assignment for honor students: Write an AspectJ program that checks the object form of the Law of Demeter as described in TPP.


PART A:
==================================
 

This homework is about reverse engineering of Java programs into an object-oriented design.
Reverse engineering means to figure out the design from the program text.
 

Consider the Java program in directory
$SD/hw/1/binary-tree

The files in directory gen with suffix *.java make up the program.

(Note: The files in gen have been generated by DemeterJ from the files tree.cd and tree.beh. While you study the Java programs, you also learn how DemeterJ generates code. The file tree.cd is very similar to an XML schema or an XML document type definition (DTD). The code generation process is similar to the code generation process described in JSR 31 by SUN. This Demeter-like code generation capability is planned to be put into the Java 2 Platform
http://www.ccs.neu.edu/research/demeter/technology-transfer/XML/index2.html.
The file tree.input that will be used later to create a Java object is similar to an XML document. So we have the following correspondences:

XML Document type definition or schema :: tree.cd (class dictionary)
XML document :: tree.input (sentence)
Java program processing XML document :: tree.beh (behavior file)

The following file gives you information on the code generation process:
http://www.ccs.neu.edu/research/demeter/DemeterJava/quick-help/TABLE-OF-CONTENTS.txt
 

Start your reverse engineering effort with file Tree.java.
Most of the organization of this file is described in file tree.cd.

Answer the following questions:

1. Draw a UML class diagram for the java source files in $SD/hw/1/binary-tree/gen. (For information on UML, see http://www.rational.com/uml/)

Turn in a picture of the UML class diagram. Use your favorite drawing tool to create it and turn in the file that your drawing tool (maybe a pen) produces.

Your UML class diagram will contain directed associations.A directed association looks like:

--------                   --------
|  A   |                1  |  B   |
|      | ----------------->|      |
--------                x  --------

A is the source class, B the target class and x is the name of the role.
In the Java implementation, A and B are classes and A has an instance variable x of class B.

Association is a synonym for relation. The directed association above presents a binary relation. You think of it as a set of pairs, namely (A-object,B-object) pairs. A natural way to implement such a relation is to store the B-object which is
associated with a given A-object in the instance variable x of the A-object.

The name "association" comes from the OMT method. It has found its way into the UML vocabulary.

Do not put methods and their signatures into your UML class diagram.
==============

2. Describe the behavior of the program in English.
Try to summarize the program as much as possible. Do not just translate each line in the program into English, i.e. SummingVisitor s = SummingVisitor.parse("0"); => Declare SummingVisitor s and set it to whatever returns from calling SummingVisitor.parse method with "0" as argument. Try to describe the overall behavior of the methods. Do only these methods:
sum_labels()
print_leftTrees()
print_allTrees()
and the methods they call.

Here is a start: When
cd gen
cd classes
java Main < ../../tree.input
cd ../..

runs, the Java virtual machine executes the byte code in file Main.class. This code will execute function Main.main which creates a tree object from file tree.input in variable tree using a table-driven parser whose functioning will not be explained here; the parser is automatically generated by a parser generator belonging to Webgain (it is free):
----------
The JavaCC home page is at http://www.webgain.com/products/java_cc/
----------
In the following we only explain the behavior of sum_labels(). The behavior is implemented through a traversal which visits all Label-objects and a SummingVisitor ...
 

The output produced by the program is in file out.


PART B:

Has two subparts: do points 1-2 as in part A, but
now reverse-engineer the program in

$SD/hw/1/graph

Explain methods countInhRels and countInhRels2 only.


NOTE ABOUT PARTS A AND B:

You can answer the questions by studying the files of the form
*.cd and *.beh. In addition, you should study the generated Java code (in the gen directory) although it might be confusing. Also keep in mind that there are several ways to generate the code and you see here only one way. For example in the JAXB approach by SUN mentioned earlier there is a separate file that allows you to control the details of code generation. DemeterJ uses a fixed method.

You are asked to wade through a tremendous amount of Java code:
  for binary-tree: a few thousand lines of Java code generated from 67 lines in tree.beh and tree.cd
  for graph: a few thousand lines of Java code generated out of 54 lines in class-graph.beh and class-graph.cd

When you study the Java code keep in mind that it must be very regular since it is produced from a small amount of information. Also keep in mind that not all generated code is actually used by the simple application programs. You have to focus only on the code which is used.
 


PART C:

Take a look at:
 

http://www.ccs.neu.edu/research/demeter/DJ/
 

and get a basic understanding of the package.

Find the unknowns in the following Java program: The program prints the city where an employee lives. You should find the UNKNOWNs by using knowledge from your Java book and by what is in the DJ package documentation. Only after you have found all the UNKNOWNs you may want to run the Java program.

// Main.java
import edu.neu.ccs.demeter.dj.*;
class Main {
  static public void main(String args[]) throws Exception {

    // Build object
    Employee e = new Employee(
      new UNKNOWN1(
        new UNKNOWN2(
          new UNKNOWN3(
            new String("Boston")))),
      new UNKNOWN4(),
      new Date());

    ClassGraph cg = buildClassGraph();
    System.out.println("City where Employee lives");
    System.out.println(e.get_city(cg));
    System.out.println("City where Employee lives: UNKNOWN5 ");
    System.out.println(e.UNKNOWN6.UNKNOWN7.city.UNKNOWN8);

    System.out.println("DONE");
  }

  public static ClassGraph UNKNOWN9() {
   ClassGraph cg=new  ClassGraph(true,false);
   // true: include all fields 
   // false: do NOT include all non-void no-argument methods
  
  System.out.println("The UNKNOWN10 version is: " + cg.getVersion());
  System.out.println("UNKNOWN11" + "=============================");
  System.out.println(cg);
  System.out.println("end class graph " + "=============================");
  return cg;
 }

}

// 
import edu.neu.ccs.demeter.dj.*;
class UNKNOWN12 {
  UNKNOWN13(PersonalInfo personalInfo_, Date d, Date a) {
    personalInfo = UNKNOWN14;
    departure = UNKNOWN15;
    arrival = UNKNOWN16;
  }
  PersonalInfo personalInfo;
  Date arrival;
  Date departure;
  String get_city(ClassGraph cg) {
    return (String) cg.UNKNOWN17(this,
    "from Employee through City to java.lang.String");
  }
}

// PersonalInfo.java
import edu.neu.ccs.demeter.dj.*;
class PersonalInfo {
  Address address; 
  PersonalInfo(Address address_) {
    address = address_;
  }
}


// Address.java
import edu.neu.ccs.demeter.dj.*;
class Address {
  Address(City city_) {
    city = city_;
  }
  City city;
}

// City.java
import edu.neu.ccs.demeter.dj.*;
class City {
  City(String name_) {
    name = name_;
  }
  String name;
}


// Date.java
class Date {
}


The DJ version is: DJ version 0.8.2
The class graph is=============================
Address = <city> City extends java.lang.Object.
City = <name> java.lang.String extends java.lang.Object.
java.lang.Object : City | Address | java.lang.String | Date | PersonalInfo | Employee | Main common .
java.lang.String = extends java.lang.Object implements java.io.Serializable, java.lang.Comparable.
java.io.Serializable : java.lang.String common .
java.lang.Comparable : java.lang.String common .
Date = extends java.lang.Object.
Employee = <personalInfo> PersonalInfo <arrival> Date <departure> Date extends java.lang.Object.
PersonalInfo = <address> Address extends java.lang.Object.
Main = extends java.lang.Object.
java.util.Collection = <elements> java.lang.Object.
edu.neu.ccs.demeter.dj.Collection = <elements> java.lang.Object.
end class graph =============================
City where Employee lives
UNKNOWN18
City where Employee lives: bad way
UNKNOWN19
DONE
 

To provide the answers,
use the file $SD/hw/1/UNKNOWNs
 

An UNKNOWN may be multiple words.


PART D:

Try to compile and run the Java programs in binary-tree and graph on your PC or workstation at work.

Please put the rt.jar file into your class path. See: http://www.ccs.neu.edu/research/demeter/software/docs/install.html
 

If you use the CCS .software file, use: CLASSPATH=.:/proj/demsys/demjava/rt.jar

Run "resoft" after the change.

Turn in the output produced produced by running the program you compiled.


PART E:
Read the Jan/Feb 2001 MIT Technology Review article http://www.techreview.com/magazine/jan01/tr10_toc.asp about the 10 most promissing technologies that will change the world. Read the entry: Untangling Code (see below for a free copy): http://www.techreview.com/magazine/jan01/tr10_kiczales.asp

Free copy: For educational personal use only by Northeastern students.

Answer the following question: How is Aspect-Oriented Programming called at Northeastern University according to the article?



Submission
=================
Please follow the following rules:
 

WHERE
Bring your hw solutions to class or put them into the mail box of the teaching assistant.

ONE SUBMISSION ONLY PER HOMEWORK
Please submit only once when you are done or on the due date, submit a partial solution.

You may turn in your hw for full credit up to two working days late. This means, if it is due on Thursday, you can turn it in the following Monday. If it is due on day x, it means 4.30 pm on that day.

For late homework we will deduct 20% per day late, unless you present a very good reason to the teaching assistant.



The Java programs used in this homework have been generated by DemeterJ.


 
 

1. What kind of preparation do I need for this course?
Experience with object-oriented design and programming is an absolute prerequisite.
Get an account on Northeastern machines if you don't have one already and you want one. You can get access to the necessary information on the WWW. For all work you can use any machine of your choice which runs the latest version of Java. But you are responsible for installing the necessary software on your own machine.

2. What am I going to do if I join the class late?
All messages which I send to the class are archived in http://www.ccs.neu.edu/home/lieber/com1205/w03/archive/ If you join the class late, you can read that archive to get up-to-date.

3. How to download DJ and DemeterJ

The instructions for downloading DJ and DemeterJ are at: http://www.ccs.neu.edu/research/demeter/software/docs/install.html

4. Where can I find the instructions for DemeterJ?
DemeterJ is not needed for this homework, but if you are curious, the instructions are at URL:
 

http://www.ccs.neu.edu/research/demeter/DemeterJava/

5. How can I do my homework on my own computer? If you want to do your homework on your own computer, you need a Java development environment on your PC or workstation and Internet access. In each class usually several students use Windows or Linux to do their homework on their own machine.