sk.robotics.rlcs.agentspace
Class Agent

java.lang.Object
  extended by sk.robotics.rlcs.agentspace.Agent
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
CollisionAgent, ConnectionAgent, CopierAgent, ExecuterAgent, FileLoaderAgent, FileToucherAgent, FollowAgent, IdentifyAgent, ImageFilterAgent, ImageLoaderAgent, ImageLoaderImgGenAgent, ImageLoaderSimAgent, ImageServerAgent, ImageStreamAgent, ImitateAgent, Main, MoveAgentBase, MovementAgent, OdometryAgent, PathAgent, ReadDetectionAgent, RoamAgent, Scheduler, SimDetectionAgent, SimDetectionAgent2

public abstract class Agent
extends java.lang.Object
implements java.lang.Runnable

Base class for agents. Steps to implement a new agent, launchable from XML configuration:

  1. Derive a class from Agent (this class).
  2. Derive a config class from Agent.Cfg, and make it member class of the agent class.
  3. In the agent class, define a constructor taking an object of the config class, and passing it to the constructor of the base class.
  4. Override senseSelectAct() or senseSelectAct(int) to do something when the agent is triggered. The version with int parameters can distinguish between different trigger events.
  5. Agent activation is configured in the config object using timerPeriodMs and triggers fields. You can define default values for these field in construtor of the config class.
To launch the agent from XML configuration, simply include it's config object in the configuration.


Nested Class Summary
 class Agent.AgentThread
          Agent threads are of this class, to ease permission checking.
static class Agent.Cfg
          Config base class.
static interface Agent.Hidden
          Annotation used for hiding agents in web interface.
static class Agent.ThreadCreator
           
 class Agent.WorkerThreadGroup
          Thread group class for agent-launched worker threads.
 
Field Summary
protected static java.text.DateFormat logTimeFormat
           
protected static Space space
          The space, a convenience shortcut usable in derived classes.
protected  Agent.AgentThread thread
           
protected  Agent.WorkerThreadGroup workerThreads
           
 
Constructor Summary
protected Agent()
          Default constructor, for use by custom agents, which don't use standard configs.
protected Agent(Agent.Cfg cfg)
          Agent constructor.
 
Method Summary
protected  void cleanup()
          Cleanup method, called just before the agent thread finishes.
 void exit()
          Quit the agent.
 Agent.Cfg getConfig()
          Get agent config.
 long getIdleTime()
          Get time passed since last sense-select-act.
 Agent.WorkerThreadGroup getWorkerThreadGroup()
          Access to the worker threads group.
 boolean isAlive()
          Thread alive check.
 void kill()
          Kill the agent thread forcibly.
protected  void log(int level, java.lang.Object msg)
          Logging.
protected  void log(int level, java.lang.String fmt, java.lang.Object... params)
           
protected  void log(int level, java.lang.Throwable e)
           
protected  void log(int level, java.lang.Throwable e, java.lang.Object msg)
           
protected  void log(int level, java.lang.Throwable e, java.lang.String fmt, java.lang.Object... params)
           
 void run()
          Main loop.
protected  void senseSelectAct()
           
protected  void senseSelectAct(int triggerIDs)
          Sense-select-act, called upon trigger().
 void setLogLevel(int level)
          Set logging level.
protected  void setPeriodicTrigger(long periodMs)
           
protected  void setPeriodicTrigger(long periodMs, int triggerId)
          Register for periodic triggers.
static void sleep(long millis)
          A sleep convenience helper.
 void start()
          Start the agent thread.
protected  void startup()
          Initialization method called from the agent's thread.
 java.lang.String toString()
          Debug string.
 void trigger()
           
 void trigger(int triggerId)
          Trigger (wake up) the agent.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

thread

protected Agent.AgentThread thread

workerThreads

protected Agent.WorkerThreadGroup workerThreads

logTimeFormat

protected static final java.text.DateFormat logTimeFormat

space

protected static Space space
The space, a convenience shortcut usable in derived classes.

Constructor Detail

Agent

protected Agent(Agent.Cfg cfg)
Agent constructor. To be called by derived classes' contructors.


Agent

protected Agent()
Default constructor, for use by custom agents, which don't use standard configs.

Method Detail

setLogLevel

public final void setLogLevel(int level)
Set logging level.


start

public final void start()
Start the agent thread.


kill

public final void kill()
Kill the agent thread forcibly. Use this only as fallback, if exit() does not work.


isAlive

public final boolean isAlive()
Thread alive check.


startup

protected void startup()
Initialization method called from the agent's thread. Can be overriden to perform special thread-specific initialization, for which constructor is not appropriate, or do anything which should be done only once the agent's thread is started.


cleanup

protected void cleanup()
Cleanup method, called just before the agent thread finishes. Can be overriden to cleanup any allocated resources.


setPeriodicTrigger

protected void setPeriodicTrigger(long periodMs,
                                  int triggerId)
Register for periodic triggers.

Parameters:
periodMs - Automatic trigger period.
triggerId - The value to pass to trigger() function.

setPeriodicTrigger

protected void setPeriodicTrigger(long periodMs)

senseSelectAct

protected void senseSelectAct(int triggerIDs)
Sense-select-act, called upon trigger(). One of the two overloads should be overriden. The overload without arguments is for convenience, can be used if derived agent doesn't need to distinguish between different types of triggers.

Parameters:
triggerIDs - Or-ed combination of triggered ID's (params for trigger()).

senseSelectAct

protected void senseSelectAct()

trigger

public final void trigger(int triggerId)
Trigger (wake up) the agent.

Parameters:
triggerId - Trigger ID - will be or-ed with other ID's to pass to senseSelectAct().

trigger

public final void trigger()

exit

public final void exit()
Quit the agent.


getIdleTime

public final long getIdleTime()
Get time passed since last sense-select-act.


getConfig

public final Agent.Cfg getConfig()
Get agent config.


run

public final void run()
Main loop. Call start() to launch it.

Specified by:
run in interface java.lang.Runnable

sleep

public static void sleep(long millis)
A sleep convenience helper. Removes the necessity to catch the exception thrown from Thread.sleep(). But of course, the sleep is then not guaranteed to last for given time.


getWorkerThreadGroup

public final Agent.WorkerThreadGroup getWorkerThreadGroup()
Access to the worker threads group. This method is public, so that SecurityManager has access to it, but only the parent agent thread is allowed to call it.


toString

public java.lang.String toString()
Debug string.

Overrides:
toString in class java.lang.Object

log

protected void log(int level,
                   java.lang.Object msg)
Logging.


log

protected void log(int level,
                   java.lang.String fmt,
                   java.lang.Object... params)

log

protected void log(int level,
                   java.lang.Throwable e)

log

protected void log(int level,
                   java.lang.Throwable e,
                   java.lang.Object msg)

log

protected void log(int level,
                   java.lang.Throwable e,
                   java.lang.String fmt,
                   java.lang.Object... params)