Behavior Environments Next: RunsUp: BehaviorsPrevious: Behavior Class

Behavior Environments

A behavior environment of the class BehavEnv  associates behaviors to names in the behavEnv hash table.

The newBehavs hash table contains the behaviors which are defined during current instant, and the definitions of which will only take effect at next instant.

Vector addToBehav contains the instructions which are added to the behavior during the current instant.

The transferBehavs method is called at the beginning of each instant, in order to transfer behavior definitions made during last instant. First, behaviors are transferred from newBehavs, theninstructions from addToBehav (NamedInst  is an auxiliary class to link together an instruction and a name).

newBehav method changes a behavior declaration for next instant. There must be only one definition during current instant, otherwise the effect is to set the behavior body to nothing.

addToBehav method adds an instruction to a behavior; this will take place at next instant.

behavNamed method returns the behavior associated to a name.

public class BehavEnv
  private Hashtable behavEnv = new Hashtable();
  private Hashtable newBehavs = new Hashtable();
  private Vector addToBehav = new Vector();

  public void transferBehavs()
    Enumeration nameEnum = newBehavs.keys();
    while (nameEnum.hasMoreElements())
      String name = (String)nameEnum.nextElement();
      Instruction body = (Instruction)newBehavs.remove(name);
    for (int i = 0; i < addToBehav.size(); i++)
      NamedInst c = (NamedInst)addToBehav.elementAt(i);
      Behavior behav = behavNamed(;
      if (behav != null) behav.add(c.inst());
    addToBehav = new Vector();
  void newBehav(String name,Behavior behav)
    if (newBehavs.containsKey(name)){
      System.out.println("behavior "+name+ 
                         " defined twice in the same instant");
      newBehavs.put(name,new Nothing());
  public void addToBehav(String name,Instruction inst){
    addToBehav.addElement(new NamedInst(name,inst));
  public Behavior behavNamed(String name)
    Behavior i = (Behavior) behavEnv.get(name);
    if (i==null){
      System.out.println("behavior "+name+" does not exist");
    return i;