Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007
Tutorial: Exporting data in a Squeak project (.pr) file
Brought to you by the Squeak Assassins.
To complete the milestones this semester, our group needed to keep some sort of persistent data along with our milestone project files. For instance, the data harvested from the Georgia Tech Alumni Association online campus map consisted of coordinates, names and images. Although this data could be newly harvested from the web every time the project file was loaded, this approach would introduce a significant delay at load time. More importantly, if the Alumni Association site was down, our program would be unable to retrieve the necessary data to function.
In addition, in later milestones, there was also a need for data that could not be retrieved from an online source. For example, in Milestone 5, our program used a large version of the campus map image found on the gtalumni.org site that had been edited in Photoshop so all non-addressable buildings were clearly marked. This kind of data would need to be stored in the .pr file or distributed as a separate file along with the project file. Our group chose to store the data in the project file. What follows is a tutorial explaining how to store persistent data other than class definitions in a Squeak project file.
First, you need to create a class method that can be called when the project is loaded that will grab the data out of the project (more on that shortly) and perform other necessary initialization functions. Pictured below is a class method called start that will be called upon loading of the project. In this example, a new transcript will be opened and 'Foobar' will be shown on the transcript.
Next you need to tell the World to execute this method when the project file is loaded.
This is done by executing World when: #aboutToEnterWorld send: #<messageName> to: <objectName>. This only needs to be executed once per project because the World instance is unique to every project and is exported automatically. Just replace <messageName> with the name of the message (the # sign is needed because the message name is a symbol), and replace <objectName> with the name of the object which receives this message. In this example, we are sending the Test1 class the "start" message.
Now we can test this by leaving this project (going to the previous project) and reentering. The #aboutToEnterWorld event is triggered when the project is loaded from a .pr file or when the project is entered by clicking on it (as in the picture below).
As soon as the project is entered, the start method is executed. You can see that the new transcript has appeared and 'Foobar' has been written to the transcript.
Now, the trick to storing persistent data lies in the project's changeset's structures. Pictured below is an explorer window exploring Project current changeSet. In the current project's changeSet, there is a Dictionary named structures. This Dictionary holds information about the instance and class variables of every class in the current project's changeSet (Note: until the creation of a class or instance variable in a class that will be included in the project, structures will be nil, not an empty Dictionary).
You can add any data that you like to Project current changeSet structures, and it will be exported with the .pr file. Just add to it like a standard Dictionary (but be careful to add entries with names that will not confict with a class, because the Dictionary is used for record keeping by Squeak).
Now modify the start method that we created earlier to retrieve the stored data from Project current changeSet structures. In this example, a class variable in Demo1 is set with the retrieved data (remember, structures is just a Dictionary, so it is accessed with at: and at: put:).
Finally, you can see below that the data is retrieved successfully from the project after an export and the start method in Demo1 is called as soon as the project loads.
That about wraps it up: you should now be able to make projects that are self contained and do not rely on external files or the Internet to store persistent data. Note, however, that if you change the name of the project, you will need to repeat this process, because Project current will be different.
As an added bonus, you can also use the World when: #aboutToEnterWorld ... event by itself to create projects that automatically start your program upon loading.
Links to this Page
- Cases last edited on 30 July 2011 at 2:33 am by r59h132.res.gatech.edu
- Squeak Assassins Cases Page last edited on 28 April 2002 at 4:33 pm by adsl-80-94-187.asm.bellsouth.net