190 likes | 210 Views
Learn how to generate GAIGS XML scripts by executing programs, directly supported by Java in a structured environment. Explore support classes and functionalities for generating GAIGS XML scripts efficiently.
E N D
Generating GAIGS XML Scripts I Integrating Algorithm Visualization into Computer Science Education Grand Valley State University June 13-16, 2006
Generating Scripts as theOutput of Executing Programs • GAIGS XML scripts can be generated by programs written in any programming language. • However, the JHAVÉenvironment is designed to directly support programs written in Java. • Input to programs must be specified on the command line. • The first command line parameter is the file name the script is to be written to. • Support classes are available which can be used to directly generate the required GAIGS XML.
Support Classes forGAIGS XML Generation • ShowFile: Handles the actual writing to the script file. • Structure Classes: Basically one for each of the GAIGS built-in structures. • Linear Structures: GAIGSstack, GAIGSqueue, GAIGSlist • Arrays: GAIGSarray (includes bar graphs) • Trees and Graphs: GAIGStree, GAIGSgraph • Labels: GAIGSlabel • Question Classes: Support various aspects of generating questions in scripts. • XMLfibQuestion, XMLmcQuestion, XMLmsQuestion, XMLtfQuestion
The ShowFile Class • The ShowFile class is responsible for all writing to the script file. • Constructors: • ShowFile(String fileName) • fileName is the name of the file to be written to. • file is opened, and preliminary XML written to it. • Key Methods: • writeSnap(String title, Double titleSize, GAIGSdatastr… ds) • writes to the file the XML for a snap with the title and each of the listed structures. titleSize parameter is optional. • close() • writes any questions and closes the file.
The GAIGSstack Class I • GAIGSstack functions in the usual way as a stack (with push and pop operations. • But also stores color and other information in a way that can remain hidden (if desired) from the client class. • Constructors: • GAIGSstack() • create a stack using defaults for location and color. • GAIGSstack(String n, String c, double x1, y1, x2, y2, size) • create a stack with name n, color c, location <(x1,y1),(y2,y2)>, and fontSize size.
The GAIGSstack Class II • Key Methods: • pop() • removes the most recently added item from the stack. • push(Object o) • adds an item to the stack (with the default color). • push(Object o, String c) • adds an item to the stack with color c. • Key Inherited Methods (from GAIGSlist) • isEmpty() • returns true if the stack has no elements. • peek() • returns (but does not remove) the most recently added item from the stack.
import exe.*; import java.io.*; publicclass Example1 { staticfinalString title = "Stack Example"; staticfinaldouble titleSize = 0.08; publicstaticvoid main (String [] args) throws IOException { GAIGSstack stack = new GAIGSstack(); ShowFile show = new ShowFile(args[0]); int itemsToAdd = Integer.parseInt(args[1]); for (int i = 0; i < itemsToAdd ; i++) { stack.push(i); show.writeSnap(title, titleSize, stack); } show.close(); } } Example 1 Code: Using the ShowFile and GAIGSstack Classes
ShowFile constructor ShowFile writeSnap ShowFile close <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE show PUBLIC "-//JHAVE//DTD GAIGS SHO//EN" "gaigs_sho.dtd"> <show> <snap> <title>Stack Example</title> <stack> <bounds x1="0.0" y1="0.0" x2="1.0" y2="1.0" fontsize="0.05"/> <list_item color="#FFFFFF"> <label>0</label> </list_item> </stack> </snap> <questions> </questions> </show> Example 1 Output: itemsToAdd == 1
GAIGSqueue and GAIGSlist Classes • Much like GAIGSstack, these provide, respectively, basic queue and list functionality. • GAIGSqueue: enqueue and dequeue operations • GAIGSlist: many but not all the operations from the List interface • Like the Java collection classes, GAIGSqueue inherits from GAISlist (as does GAIGSstack). • Please consult the GAIGS API for details on these classes
import exe.*; import java.io.*; publicclass Example2 { staticfinalString title = "Stack and Queue"; publicstaticvoid main (String [] args) throws IOException { GAIGSqueue queue = new GAIGSqueue("Queue", "#666666", 0, 0.2, 0.5, 0.7, 0.1); GAIGSstack stack = new GAIGSstack("Stack", "#DDDDDD", 0.5, 0.2, 1, 0.7, 0.1); ShowFile show = new ShowFile(args[0]); int itemsToAdd = Integer.parseInt(args[1]); for (int i = 0; i < itemsToAdd ; i++) { queue.enqueue(i); stack.push(i); show.writeSnap(title, queue, stack); } show.close(); } } Example 2 Code: Multiple Structures in a Snapshot
… <snap> <title>Stack and Queue Example</title> <queue> <name>Queue</name> <bounds x1="0.0" y1="0.2" x2="0.5" y2="0.7" fontsize="0.1"/> <list_item color="#666666"> <label>0</label> </list_item> </queue> <stack> <name>Stack</name> <bounds x1="0.5" y1="0.2" x2="1.0" y2="0.7" fontsize="0.1"/> <list_item color="#DDDDDD"> <label>0</label> </list_item> </stack> </snap> … Example 2 Partial Output: This is the First Snap
GAIGSarray I • GAIGS provides support for one and twodimensional arrays. • Row labels can be specified, and if the array is 2-d column labels as well. • If the array is a 1-d array of int, it can be shown either in the usual format or as a bar graph. • Here we just briefly consider the 1-d case, please consult the GAIGS API for complete details on this class
GAIGSarray II • Constructor: • GAIGSarray (String s, boolean bar, color c, double x1, y1, x2, y2, size) • create a label with name s, color c, location <(x1,y1),(y2,y2)>, and fontSize size.Display as a bargraph if bar == true. • Key Methods: • set(Object o, int loc) and set(Object o, int loc, String c) • set location loc to have value o, optionally with color c • get(int loc) • return the value at location loc • setColor(int loc, String c) • set the color of the item at location loc to c. • setName(String s) • set the name of this structure to s
GAIGSlabel • GAIGSlabel is a work-around for the fact that labels are not (yet) supported in GAIGS. • It is really just an structure that has no elements, hence only the structure's name is displayed. • Constructor: • GAIGSlabel (String s, double x1, y1, x2, y2, size) • create a label with name s, location <(x1,y1),(y2,y2)>, and fontSize size. • the label is placed within these bounds, but is not aligned with any of them. • Key Method: • setLabel(String s) • set the label to s.
GAIGSarray GAIGSlabel Exercise:Create a Bubblesort Visualization • Our exercise will be to create a complete visualization for the (infamous) bubblesort algorithm • Supplied code will create the snapshot shown below:
Supplied Code import java.io.*; import java.util.Random; import exe.*; publicclass Sort { staticfinal String TITLE = null; // no title staticint arraySize; // # of items to sort static GAIGSarray items; // the array of items static GAIGSlabel message; // for status messages Exercise Code: Preamble
publicstaticvoid main(String args[]) throws IOException { // process program parameters and create the show file object ShowFile show = new ShowFile(args[0]); arraySize = Integer.parseInt(args[1]); // define the two structures in the show snapshots items = new GAIGSarray(arraySize, true, "BubbleSort", "#999999", 0.1, 0.1, 0.9, 0.9, 0.07); message = new GAIGSlabel("Initial Array Values", 0.1, -0.45, 0.9, 0.35, 0.07); // initialize the array to be sorted & show it loadArray(); show.writeSnap(TITLE, items, message); // +++ add bubblesort code here +++ // visualization is done show.close(); } Exercise Code: Main method
// Load the array with values from 1 to the array size, then // shuffle these values so that they appear in random order. privatestaticvoid loadArray () { Random rand = new Random(); for (int i = 0; i < arraySize; i++) items.set(i+1,i); for (int i = 0; i < arraySize-1; i++) swap(i, i + (Math.abs(rand.nextInt()) % (arraySize - i)) ); } // Swap two items in the array. privatestaticvoid swap (int loc1, int loc2) { Object temp = items.get(loc1); items.set(items.get(loc2), loc1); items.set(temp, loc2); } Exercise Code: Support Routines
Time to Work! • Below is working code for the bubblesort algorithm. • Your job: • Incorporate it into the supplied code for this exercise. • Decide when snapshots should be taken (when do the interesting events occur?). • Use coloring to show the ongoing actions of the algorithm. • Use the label and the name of the array to produce useful messages about the status of the algorithm. for (int pass = 1; pass < arraySize; pass++) for (int i = 0; i < arraySize-pass; i++) if ((Integer)(items.get(i)) > (Integer)(items.get(i+1))) swap(i, i+1); Bubblesort Code