Lecture 28: Generic Extended Visitors and Data Packets

October 29, 2012

Concepts

  • Dr. Wong has redesigned the Java Resources page on his website so that it is easier to navigate. Dr. Wong finds it unlikely that he will be able to discuss all of the minute but important details of some of the topics that weĺl be doing soon, and therefore encourages everybody to visit the Java Resources page for in depth information.
  • What is the “transient” modifier that is applied to the taskView instance variable inside of tasks? During serialization, an objects fields will be encoded into some stream of bytes, which will then be sent across the network. On the other end, Java will instantiate a new class of the same type, then insert all of the values that it finds in the serialized stream.
    • Well, the taskView field is a reference to some other object; at some level just a number that identifies the other object. What would that number mean if we serialized it and sent it somewhere else? It would be meaningless outside the context in which it was created. /* This seems somewhat off-topic to me, because, as discussed below, this won’t happen. Java will attempt to serialize the entire referenced object. */ Transient indicates that the serializer should ignore the so-marked field.
    • Note that normally, during serialization, the serializer will follow references (If Object A references Object B, and A is serialized, B will also be serialized). Be careful, therefore, not to serialize anonymous inner classes that close over other variables, as you can easily end up serializing and transmitting your entire system.
    • Don’t confuse volatile and transient! volatile indicates to the Java Compiler that the value of some variable is not safe to cache (because the variable changes in parallel ways (?)) and that it shouldn’t try to optimize any code that uses the volatile variable.
    • You can override (some interface…).readObject(…) (Something in Pi2.java…) if you need to do some sort of re-initialization whenever your Object is deserialized. You can call stream.defaultReadObject() to perform the normal deserialization, then take the opportunity to initialize your transient fields to some well-defined value.

Now we’re going to talk about generic list frameworks.

  • Let’s take a generic list: IList<E>. What does it mean to run an algorithm on it?
  • public abstract <R, P> R execute(IListAlgo<? super E, R, P> algo, P …)
  • We declare that execute will return a type R: whatever the algorithm returns.
  • We make a generic parameter P for whatever parameters the algorithm takes.
  • ? super E: any algorithm that works on a supertype of E can work on this list of E‘s.
  • Thus, we have a method, with generic parameters R and P (inside of an IList with generic parameter E), which accepts an algorithm that operates on a supertype of E, accepts parameters of type P, and returns a type R, and some number of parameters, each of type P.
  • public abstract R emptyCase(IMTList<? extends E> host, P … inp);
  • Declares a case in a visitor (the algorithm), that operates on any subtype of E.

Quotes

  • “If you want a glass of water, there’s two components, right? The glass, and then the water! You only transmit the water from one glass to another; the glass is like the class, and the water is like the data.”
  • “Never trust another programmer, especially not the one in the mirror.”
  • “This is like the most intensely generic statement I’ve ever seen!” — Caleb
    • “This is the easy one.” — Dr. Wong

Resources


Lab 08: RMI Setup

October 23, 2012

Concepts

  1. Don’t run on the “Rice Visitor” network
  2. Make sure that the following ports are opened on your machines: 2001, 2002, 2099 through 2102
  3. The first 10 minutes were lecture, the rest was “practice setting up RMI with someone else in the lab.”

Resources

  1. Lab 08 webpage: http://www.clear.rice.edu/comp310/f12/labs/lab08/

Lecture 26: More RMI

October 22, 2012

Ambient Temperature: 74.4oF

Concepts

  1. More explanation of the RMI infrastructure.
  2. If you are off-campus you must use VPN before you’ll be able to connect to an on-campus partner.
  3. If you are behind your own router, you will have to make sure that you forward the appropriate ports to your computer.
  4. public <Type> Type executeTask(ITask<Type> _task) throws RemoteException;
    This is a parameterized method. Its parameter is an ITask, which has been parameterized to some arbitrary type. Whatever the type of the ITask is, is what the method will return.

Resources

  1. Lecture 26 webpage: http://www.clear.rice.edu/comp310/f12/lectures/lec26/
  2. Lecture 25 webpage: http://www.clear.rice.edu/comp310/f12/lectures/lec25/
  3. COMP 310 Resources on Generics: http://www.clear.rice.edu/comp310/JavaResources/generics/
  4. Oracle Tutorial on Generic Methods: http://docs.oracle.com/javase/tutorial/extra/generics/methods.html

Lecture 25: Remote Method Invocation

October 19, 2012

Ambient temperature: 73.4oF

Concepts

  1. Remote Method Invocation (RMI): What if your adapters in the MVC architecture took their input, schlepped it across a network to another computer, talked to an object on that computer, got its response, and schlepped it back across the network to you, instead of the simple wrappers around the model or view that they’ve been in assignments thus far? Well, then you’d have RMI, a way to call methods and interact with objects on different computers, over a network, in a way that looks like those objects are actually sitting there on your own computer. Properly-set-up RMI frameworks can make it very easy to write programs that operate over a network.
  2. In an MVC setup, the Model may be another MVC trio. The view may be another MVC trio. The controller will usually not be an MVC trio. In an RMI setup, there may be two distinct MVC trios on either end of the network, or the Model and Controller may be on the server, with lots of Views as clients. MVC describes three components and how they interact; it doesn’t require that all three always appear together, nor that they appear only in sets of three.
  3. “The RMI Registry” is someplace that RMI clients can consult to find out “who is online, and how do I talk to them?” RMI Servers add themselves into the registry for a given lookup key. Then, clients can ask “Is there a ‘hello’ server?” instead of having to search all possible places that a server could be, and checking the type of each server (which is not a task that is actually possible).

Quotes

  1. Once I talk to the stub, the stub, like E.T., knows how to call home.” – Dr. Wong

Resources

  1. Lecture 25 Webpage: http://www.clear.rice.edu/comp310/f12/lectures/lec25/
  2. RMI Theory on COMP 310 webpage: http://www.clear.rice.edu/comp310/JavaResources/RMI/
  3. Oracle’s Tutorial on setting up RMI, complete with working sample code: http://docs.oracle.com/javase/6/docs/technotes/guides/rmi/hello/hello-world.html

Even More 2-3-4 Trees

October 17, 2012

Ambient temperature: 74.1oF

Concepts

  1. Q&A session about HW06
    1. Don’t confuse “ticks per quarter note” with “ticks per default note.”
    2. Don’t fret over tuplets.
  2. 2-3-4 Trees: Deletion Heuristics (slides 21 -> end). Basically, “Push the element to delete down to child nodes, until it is at a leaf node. Then, just remove it.” That allows you to keep the tree balanced while doing the deletion.

Resources

  1. Homework 06 webpage: http://www.clear.rice.edu/comp310/f12/assignments/hw06/
  2. 2-3-4 Tree Powerpoint: http://www.clear.rice.edu/comp310/f12/lectures/lec21/DP4SBT.ppt

Lab 07: Playing Music in Java

October 16, 2012

Concepts

  1. Set up Eclipse to pull the provided code from the course’s SVN repository.
  2. The format and meaning of ABC notation explained on the lab webpage.
  3. Traditional music notation may have modifiers (tempo, key, etc) that change the sound that a given note should produce. MIDI has no notion of these modifiers, so you must be sure to properly apply any modifiers in the ABC file to the notes before setting them up to play via MIDI.

Quotes

  1. None

Resources

  1. Lab 07 webpage: http://www.clear.rice.edu/comp310/f12/labs/lab07/
  2. Setting up Eclipse to load the provided code: http://www.clear.rice.edu/comp310/f12/assignments/hw06/setup.html

2-3-4 Trees Continued

October 15, 2012

Ambient Temperature: 72.9oF

Concepts

  1. 2-3-4 Tree Powerpoint, slides 18 -> 20
  2. When implementing a self-balancing 2-3-4 tree (or any complex data structure, for that matter), think before you code. Take the time to devise solutions that decouple interfaces from implementation, and that separate the variant and invariant behaviors. When applying code to those solutions, again take the time to plan a solution that takes full advantage of the high-level interfaces and abstractions that you have set up.
  3. The exhaustive exposition of 2-3-4 tree implementation attempts to make the above point by presenting a concrete example that exhibits all of the desired qualities: Yes, you could have an insert(…), delete(…), etc method on the tree’s class, but that limits the tree’s behaviors to the few that you personally define. By instead providing an accept(TreeVisitor …) method, you are creating a tree that can perform arbitrary behaviors. If you wish to include some “out of the box” behaviors (like insert, delete, etc), you could either provide wrapper methods or a visitor factory. However, once you admit the visitor pattern into your data structure, it is inconsistent and likely inconvenient to maintain non-visitor algorithms (like a traditional delete(…) method) alongside visitor algorithms.
  4. This level of abstraction is absolutely necessary in large projects with multiple groups of developers, so you need to get comfortable with if you plan to ever get paid for producing code.
  5. Short preview of HW06 provided code: The visitor pattern was used to create the parser for the ABC music language. None of that is code that we’ll have to read or modify.

Quotes

  1. “We don’t have any servers at all. We just have Macbooks and beef jerky.” – Siegfried Bilstein, EGraphs
  2. “I have an anonymous inner class inside an anonymous inner class, and I’m gonna have another anonymous inner class inside of here…” – Dr. Wong

Resources

  1. 2-3-4 Tree Powerpoint: http://www.clear.rice.edu/comp310/f12/lectures/lec21/DP4SBT.ppt

2-3-4 Trees

October 10, 2012

Ambient temperature: 73.8oF

Concepts

  1. 2-3-4 tree theory and implementation, slides 1 -> 17.
  2. Identify the invariant properties of the data structure, and build variant behaviors with them.

Quotes

  1. “Imagine a really big Christmas tree sitting on a little spike on the bottom, maybe you’re cheap and buy a really little spike on the bottom, you look at the big ones and they’re really expensive so you buy a cheap one, and you put your tree on it (extends arms and tilts back and forth).” – Dr. Wong

Resources

  1. 2-3-4 Tree Powerpoint: http://www.clear.rice.edu/comp310/f12/lectures/lec21/DP4SBT.ppt
  2. 2-3-4 Tree Demo: http://www.clear.rice.edu/comp310/f12/lectures/lec21/dp4sbt_demo.html

Lecture 20: Extended Visitor Design Pattern

October 8, 2012

Ambient Temperature: 74.1oF

Concepts

  1. Extended Visitor Design Pattern: Instead of a host calling visitor.this_type_of_host_case(…), the host can call visitor.case_for_host( host_type, parameters… ). This allows your visitor to have a set_case_for_host( host_type, ICase _case ) method that will add behavior to the visitor for the specified host type. The end result is that you can support an arbitrarily large number of hosts that can be set at design-time, compile-time, and/or run-time.
  2. Make sure to have a”default” command for host types that do not have a specific command mapped to them.
  3. Lecture 21 (using the extended visitor pattern to implement self-balancing tree data structures) was briefly introduced.

Quotes

Resources

  1. Lecture 20 Webpage: http://www.clear.rice.edu/comp310/f12/lectures/lec20/
  2. Demo of Exended Visitors: http://www.clear.rice.edu/comp310/f12/demos/ExtendedVisitorDemo/
  3. Lecture 21 Webpage: http://www.clear.rice.edu/comp310/f12/lectures/lec21/

Lecture 19 Part 3: More Visitors

October 5, 2012

Ambient temperature: 74.5oF

Concepts

  1. There are three approaches to the “sum” algorithm on the NEList/MTList list structure:
    1. nonEmptyCase(…) { return MY_VALUE + rest_of_list.execute( this ); }
      This is TAIL recursion, and accumulates the return value on the STACK. Another name for it is “Forward Accumulation.”
    2. nonEmptyCase(…) { return rest_of_list.execute( this ) + MY_VALUE; }
      This is HEAD recursion (and therefore bad), and accumulates the return value on the STACK. Don’t do this. Another name for this is “Reverse Accumulation.”
    3. nonEmptyCase(…) { return rest_of_list.execute( new SumVisitor( MY_VALUE + VALUE ) ); }
      This is TAIL recursion, and accumulates the return value in an INSTANCE VARIABLE inside the SumVisitor. This version allows your visitors to have, preserve, pass, and mutate STATE, which allows for more complex algorithms.
  2. Most of the list algorithms traverse the list; therefore list traversal is an INVARIANT behavior and should be abstracted out. Instead, “what to do with each subsequent value” is encoded in an Accumulator object, which is passed down the list by the visitor.

Resources

  1. Lecture 19 webpage: http://www.clear.rice.edu/comp310/f12/lectures/lec19/