Part 1

Objectives

  • Gain a familarity with the Fossil source code management system
  • Download and run the Pharo Smalltalk enviroment

Steps

  • Fossil
  • Downloading and running Pharo

Introduction to Fossil

Fossil is a Distributed Version Control System (DVCS) designed to help you manage and maintain your source code, as well as collaborate on it with others. Fossil can help you track what's change in your source code, move it around to different machines you're working on, let you and a friend work on the same code and then merge your changes, and much more. Microsoft, Google, Apple, Mozilla and every other organization use a VCS, and you should too! There are other Distributed Version Control Systems such as Git and Mercurial that have their fans. (Git is the most widely used, but actually isn't the best to use for human-sized projects. Fossil has many attributes that make it work particularly well for small teams.)

You will have a personal Fossil repository for CPS506 (as well as shared ones, if you want for shared work with classmates). The instructions for accessing it will be here shortly.

Introduction to Pharo

This lab uses material from Chapter 2 of Updated Pharo by Example (http://files.pharo.org/books/updated-pharo-by-example). Portions from that text are used under the CC BY-SA license.

Pharo is a GUI-based programming enviroment for the Smalltalk programming language. Smalltalk is one of the first languages designed for object-oriented programming in the late-70s and early 80s.

Smalltalk is based on a virtual machine, similar to Java, which interprets bytecode and makes it platform independant. One of the unique features of Smalltalk is that all developement and changes are done in the Smalltalk enviroment itself. All classes (including their code) and objects (including all of their state) are stored inside an Image that saves the complete state of the system. When you save the image, close the VM, and then re-open it again, perhaps on another machine, everything will be exactly as you left it.

Downloading and running Pharo

In order to use Pharo we need to download the VM and system image for the platform of our choice, Ubuntu in the labs. The following commands will download Pharo to a temporary directory and then unpack it to a folder called bin in your home directory:

cd /tmp
curl -O http://files.pharo.org/platform/Pharo5.0-linux.zip
mkdir ~/bin
cd ~/bin
unzip /tmp/Pharo5.0-linux.zip

We can then run Pharo using the shell script in the main directory:

cd ~/bin/pharo5.0
./pharo

The shared folder is of special note, as it contains the three main components of our pharo enviroment.

Sources file

The sources file contains source code for parts of Pharo that don't change frequently. Sources file is important because the image file format stores only objects including compiled methods and their bytecode and not their source code. Typically a new sources file is generated once per major release of Pharo.

Changes file

The changes file logs of all source code modifications (especially all the changes you did while programming) since the .sources file was generated. Each release provides a near empty file named for the release, for example Pharo5.0.changes. This facilitates a per method history for diffs or reverting. It means that even if you did not manage to save the image file on a crash or you just forgot, you can recover your changes from this file. A changes file is always coupled with a image file. They work in pair.

Image file

The image file provides a frozen in time snapshot of a running Pharo system. This is the file where all objects are stored and as such it's a cross platform format. An image file contains the live state of all objects of the system (including classes and compiled methods, since they are objects too) at a given point. An image is a virtual object container. The file is named for the release (like Pharo5.0.image) and it is synched with the Pharo5.0.changes file.

Welcome to the World

Once Pharo is running, you should see a single large window, possibly containing some open playground windows. You might notice a menu bar, but Pharo mainly makes use of context-dependent pop-up menus.

Clicking anywhere on the background of the Pharo window will display the World Menu, which contains many of the Pharo tools, utilities and settings. At the top of the World Menu, you will see a list of several core tools in Pharo, including the System Browser, the Playground, the Monticello package manager, and others.

Pharo offers three ways to interact with the system using a mouse or other pointing device.

  • click (or left-click): this is the most often used mouse button, and is normally equivalent to left-clicking (or clicking a single-mouse button without any modifier key). For example, click on the background of the Pharo window to bring up the World menu .
  • action-click (or right-click): this is the next most used button. It is used to bring up a contextual menu that offers different sets of actions depending on where the mouse is pointing. If you do not have a multibutton mouse, then normally you will configure the control modifier key to action-click with the mouse button.
  • meta-click: Finally, you may meta-click on any object displayed in the image to activate the ”morphic halo”, an array of handles that are used to perform operations on the on-screen objects themselves, such as inspecting or resizing them. In Pharo, how you meta-click depends on your operating system: either you must hold Shift-Ctrl or Shift-Alt (on Windows or Linux) or Shift-Option (on OS X) while clicking

Open the menu, save your enviroment and exit.

Part 2

Objectives

  • Gain a grasp of the basics of Smalltalk programming
  • Implement a simple Smalltalk class

Steps

  • The playground as a repl
  • The tutorial
  • A simple Caesar Cipher
  • Filetree and submitting your code

Introduction

Open Pharo again and everything should be as you left it (remember the image from Part 1?).

Time to Play

The playground is an enviroment in the Smalltalk world that lets us run (evaluate) expressions and potentionally see the results.

Click to open the world menu and select playground. Try entering the following code:

ProfStef go.

This sends the go message to the class (object) ProfStef. Everything in Smalltalk is composed of objects and messages. There are three types of messages: Unary, Binary and Keyword.

Try hightlighting the code, right click and choose do-it. This opens the built in Smalltalk tutorial. Go ahead and work your way through the rest of the tutorial which will introduce you to Smalltalk.

Caesar Cipher

The Caesar Cipher is a simple substition cipher known to have been used from at least the time of Julius Caesar. To encrypt a message (plaintext) we take each letter in the message and switch it with the 3rd letter that follows it in the alphabet, wrapping back to the begining if we go off the end. To decrypt an encrypted message (ciphertext) we simply reverse the process.

Write a simple Caesar class with encrypt and decrypt methods which take a string and returns a string which is the encrypted/decrypted message. Start by opening the class browser, right clicking and choosing add class. When you add the class, specify the package as: 'CPS506-lab1'.

  • In the category initialize-release
    initialize
        n:= 3.
      
  • In the category encrypt
    encrypt: aString
        "encrypt a string using the caeser cipher"
        |array|
        array:=aString asByteArray.
        ^(array collect: [ :each | each + n ]) asString.
      
  • In the category encrypt
    decrypt: aString
        "decrypt a string using the caeser cipher"
        |array|
        array:=aString asByteArray.
        ^(array collect: [ :each | each - n ]) asString.
      

After adding the appropriate methods, you should be able to use your class from the playground with code similar to:

|aString|
aString:=Caesar new encrypt: 'CAESAR'.
Transcript show: aString.

Which ouputs 'FDHVDU' on the Transcript. Chapter 11 of Pharo by Example maybe useful for it's coverage of Collections and Iterators in Smalltalk.

Packages, Montecello and Filetrees

As you have read, code in Pharo is organized into packages. When you open the System Browser, all the packages in the system are displayed on the right. Make sure the code for your Caesar class resides in a package with the name CPS506-Lab1.

Pharo has it's own built in version control system called Montecello, which works well for code that lives inside the Smalltalk image (a.k.a everything in Pharo). However, complications arise when we want to interface Pharo's image based model, with a more traditional file based model, such as is used by Unix and Fossil. To bridge the gap, we use a plug-in called Filetree.

Now open the Montecello browser from the World menu. As you can see, Montecello maintains a list of packages along with their versions. We are going to create a new version of our CPS506-Lab1 package and save it to a Filetree repository (i.e. a directory). First, select your package from the left and then click on repository. Select Filetree:// from the pop-up and then select the Monticello folder into which you will save the package. Now you should see the filetree repository listed on the right pane of the Montecello browser. Finally, click save to create a new version and enter a comment (perhaps, Inital version).

If you browse to the directory, you can see that Filetree has created a tree of .st files to represent the methods of your Smalltalk classes, along with some accompanying metadata. This can now be commited to Fossil.

You should verify that you successfully uploaded your files to the course repository by visiting your page again and examining what is stored there.