My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
Advanced_Robots  
Advanced robots are for advanced requirements
Updated Feb 26, 2010 by wad...@gmail.com

Introduction

Not all robots are simple. Some should be considered as advanced. Currently, there is only one kind of advanced robot :

  • WithWorkersRobot

but, it's enough to code the most advanced robots :)

If you have taken a look at the Today's special robot, you have seen it can answer to several commands like

  • Quotes
  • Cricket Score
  • horo:SIGN

and with the horo command, you can pass parameters. I liked it and it motivated the creation of this framework.

WithWorkersRobot (WWR)

As for it's name, it is a robot that has workers. See it as a (real-life) factory chain, a WWR is a chain of workers.

Workers ?

What is a worker?

A worker is an element of work, a reusable element of work.

A worker defines :

  • the instruction (see Advanced_Vocabulary) it responds to.
  • how it must respond to a request
  • if it allows the blip to be processed further, or stop process (on error for example, up to you)
  • the description of its work

A worker class implements the interface RobotWorker (See linkToJavadoc).

WWR Usage

In a blip, a command like {my:CAP} would mean : "Hey robots in the top list, if your acronym is my then execute instruction CAP.

Concurrency

Not thinking about multiple robots invocation but to the presence of several robots based on this framework in the list of authors of a wave. This is made possible by the usage of the acronym. For example my in {my:CAP}

Tips

General

Workers are executed in the order they were chained to the robot.

For example, an advanced robot that defines len and CAP instructions. First to indicate length and the second to CAPitalize the blip's text.

public MyWithWorkersRobot(){
	 super();
	 addRobotWorker( new LengthWorker() ); 
         addRobotWorker( new CapitalizeWorker() );
	}

with

class CapitalizeWorker implements RobotWorker{
	@Override
	public String getDescription() {
		return "Capitalizes the blip's content";
	}
	
	@Override
	public String getInstruction() {
		return "CAP";
	}
	
	/**
	 * No rocket science here, this worker just capitalizes the blip's content.
	 */
	@Override
	public boolean doWork(RobotMessageBundle bun, Blip bli, Event ev,
			String params) 
	{
		String txt = bli.getDocument().getText() ;
		txt = txt.toUpperCase();
		bli.getDocument().replace(txt);  
		return true; /* important so that potential following workers get executed. */
	}
}

and

class LengthWorker implements RobotWorker {
@Override
	public String getInstruction() {
		return "len";
	}

	@Override
	public String getDescription() {
		return "Displays the length of the blip in a child blip";
	}

	@Override
	public boolean doWork(RobotMessageBundle bun, Blip bli, Event eve,String params) 
	{	
		Blip lengthChild = bli.createChild();
		lengthChild.getDocument().append("length : " +  bli.getDocument().getText().length() + " characters");
		return true;
	}

Instruction

If you set your worker's instruction to null, with this code

public String getInstruction(){ return null;}

then it will always process the blips.


Sign in to add a comment
Powered by Google Project Hosting