Writing our MyAppenderRobot Profile Servlet (not required but nice)
Episode 12:Writing an Advanced Google Wave Robot using WadRobotFramework
The best part of it all is that you could combine all of this into a single Robot that can respond to one or more commands. For example, take a look at Today’s Special Robot (see http://ppandhi.wordpress.com/2009/11/08/todays-special-robot/) that can respond to more than one command. It can give you the quotes, day in history, word of the day, cricket score, your daily horoscope by simply responding to the command that you type in.
So for example, you could write a robot and give it commands like this: 1. {myrobot:doCommand1}
2. {myrobot:doCommand2}
3. {myrobot:doCommandN} and so on.
In this article, we are going to see exactly how to achieve the above command Robot that will delegate its work to different workers who are responsible for executing the
command i.e. doing the work.
Let us get a few definitions in place first:
1. The Command Robot: This is the main class of your Robot and you need to extend the
org.wadael.waverobotfrmwrk.advanced.WithWorkersRobot. You need to have an identifier for your robot, which is a unique ID for your Robot. Let us call it GAEJRobot.
2. Each Command Robot is capable of following instructions or commands. These instructions are executed by the Workers.
3. A Worker is a Command implementation that performs a certain logic. For e.g. fetching a stock quote, getting a word of a day, sending a Tweet, sending an email, etc. Each worker will indicate what instruction or command it obeys.
As an example, say you want to write an Advanced Robot class (WithWorkersRobot) whose identifier is named GAEJRobot that can responds to the following two
commands: a. SendTweet
b. GiveWordOfTheDay
So, you will implement two Workers and register (add) them to the GAEJRobot class. The two worker classes will be :
• SendTweetWorker which says that listens to an instruction named tweet and it will implement its core logic in the doWork() method.
• GiveWordOfTheDayWorker which says that it listens to an instruction named
Now, in your Wave Conversation, you can give the following text in the Blip (of course after adding the Robot as a participant).
1. {GAEJRobot:tweet} 2. {GAEJRobot:wotd}
Voila! The WadRobotFramework will then do the heavy lifting for you. It roughly works as follows:
• When you submit a Blip, it will scan the Blip Text for the identifier and see if it matches itself.
• If yes, it will scan out the instructions and invoke the doWork() method of the Worker Robot that implements the instruction.
This is not all. The WadRobotFramework has thought about parameters that you may need to pass to your Robot. For e.g. consider the following fictitious instruction that you need to give to a Stock Quote Robot.
{StockQuoteRobot:getquote GOOG} or {StockQuoteRobot:getquote GOOG,MSFT,ADBE,ORCL,IBM}
In short the format is as follows:
{RobotIdentifier:Instruction<space>[parameters]}
So in the above two examples the parameter GOOG and the parameter string
“GOOG,MSFT,ADBE,ORCL,IBM” will be passed to the doWork() method of your
RobotWorker that has registered with the Advanced Robot and who implements the getquote instruction. Please read this last statement clearly with the highlighted words as the key pieces required to build out an Advanced Robot.
Simple yet powerful and it opens up a wide range of Robots that you can start writing today. So let me get started and demonstrate the basic flow to get an Advanced Robot up and running which can accept 1 or more instructions. The Robot does not do anything specific except for simply demonstrating the flow. Readers are expected to extend it with their ideas brimming inside their minds.
To understand what we will build, it helps to take a look at the running robot as shown below:
Episode 12:Writing an Advanced Google Wave Robot using WadRobotFramework 142
You will notice that I have added my robot called MyAdvancedRobot. The identifier for the Robot is GAEJRobot and the Robot has two workers (Worker1 and Worker2) registered with it, which implement the instructions command1 and command2
respectively.
Now when I submit the text {GAEJRobot:command1} , the doWork() method of the Worker1 is invoked. It simply accepts the command and prints out that it received the command with no parameters passed to it.
Similarly, look at the wave conversation below:
Here I give the command2 to the GAEJRobot and I am also passing a string of parameters. When I click the Done button, the doWork() method of the Worker2 is invoked. It simply accepts the command and prints out that it received the command with the parameter string. Now, it could have processed the parameters and performed its logic accordingly. This demonstrates how the wiring is done by WadRobotFramework to make your life easier in writing Advanced Google Wave Robots.
Let us start writing code now. I will assume that you are fairly conversant now with the typical project structure and requirements of writing a Google Wave Robot. If not, I suggest that you first go through these tutorials in the following order:
• Episode 11: Develop Simple Google Wave Robots using the WadRobotFramework