Exercise 2 - Writing a Managed Object

The idea behind this exercise is to write a new Managed Object in Java annotated with PonderTalk bindings.  You will then write some PonderTalk which will use your new object.

The basics of actually writing a managed object can be found here. You do not need to use src/MyManagedObject for this exercise, you should start with Timer.java in the top level of the Ponder2Tutorial/src directory because it contains the basic template for this exercise.

Task

Write a new Ponder2 Managed Object in Java called Timer (use Timer.java). This managed object will be capable of sending ticks, in the form of an event, every n seconds.  

The Timer will require several PonderTalk operations, one to remember the event that it will be sending, one to start it with a timeout and one to cancel the operation. The PonderTalk specification for the operations for this should be:

Factory Operation

event: anEventType
Create a new instance with the event to be generated for each tick
e.g.

newTimer := timerFactory event: root/event/tick

Instance Operations

start: secs
Start the timer going, sending the event every secs seconds.
e.g.

newTimer start: 5

cancel
Stop the timer
e.g.

newTimer cancel

Code

This section will help you through writing your Java, your PonderTalk and the testing of your code.

Java

Start with the file Timer.java in the top level source (Ponder2Tutorial/src) directory.  Some parts have already been filled in for you; in other parts comments guide what you should be doing.  You need the constructor for event:, a method for start: and a method for cancel.

PonderTalk

You can use your new Timer by typing commands into the shell but it will be easier to create a file with your PonderTalk  commands in it. Use the file ex2.p2 in the top level tutorial directory.

You will also be using tut1.p2 which imports and creates an alarm called root/alarm. The documentation for alarm contains the PonderTalk that it accepts. tut1.p2 will be read automatically before your ex2.p2 file so you do not need to copy anything.  See running below.

There are several steps to be performed:

  1. Import your Timer
  2. Save it in the domain structure. For this exercise simply put it in the root domain and call it timer
  3. Create an event type with no arguments, put it in root/event and call it tick
  4. Create a policy that uses the tick event and has an action that shows the alarm and sets a short alarm for 2 seconds.
  5. Make the policy active
  6. Start the timer going with a 5 seconds repeat

Running

Use the command ./ant ex2 to run your code.  This will compile your code and run the Ponder2 SMC.  The SMC will read tut1.p2 which imports the Alarm display and then it will read and execute your ex2.p2. You should make small changes at a time to ex2.p2 and try running it.  When you are happy, make the next change.

Testing

If you use ./ant ex2, it will compile Timer, start the Ponder2 SMC and then read tut1.p2 and ex2.p2 for you.

You can also use the shell to test the operations.

If you want to test the Alarm, use ./ant tut1 to read tut1.p2 then:

telnet localhost 13570
$ root/alarm show.
$ root/alarm setShortAlarm: 2.
$ root/alarm hide.
$

If you have created root/timer, you can do:

telnet localhost 13570
$ root/timer cancel.
$ root/timer repeat: 3.
$ root/timer cancel.
$

Finally

You can see how the example should run by running ./ant ex2done

home