|
Advanced_Robots
Advanced robots are for advanced requirements
IntroductionNot all robots are simple. Some should be considered as advanced. Currently, there is only one kind of advanced robot :
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
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 :
A worker class implements the interface RobotWorker (See linkToJavadoc). WWR UsageIn a blip, a command like {my:CAP} would mean : "Hey robots in the top list, if your acronym is my then execute instruction CAP. ConcurrencyNot 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} TipsGeneralWorkers 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;
}InstructionIf you set your worker's instruction to null, with this code public String getInstruction(){ return null;}then it will always process the blips. |