package adaptive.agents.tutor;
import adaptive.core.*;

/*****************************************************************************
 *
 * Copyright 2000, Institute for Complex Engineered Systems,
 *                 Carnegie Mellon University
 * PROJECT: Self-adaptive Software
 * DESCRIPTION: agent to display a waveform in a small gui frame
 *
 * @author Jonathan R Jackson <A HREF="mailto:jackson4@andrew.cmu.edu">jackson4@andrew.cmu.edu>/A>
 *
 * REVISION HISTORY:
 *
 * $Log: PBAWaveDisplay.java,v $
 *
 * 
 ****************************************************************************/
public class PBAWaveDisplay extends PBAgent
{
    final static protected boolean DEBUG=true;
    final static float FREQ=10;//in Hz;
    
    //put internal structure definitions here
    protected WaveFrame display;
    /**********************************************************************
     Description: Required default constructor required by Agent interface.
     sets the number, type, and names for input and output ports
     @param none
     @return nada
     @exception none
    **********************************************************************/
  public void initialize(){
      setNumInputsOutputs(1,0);
      setInputPortType(Object.class,0);
      setInputPortName("Object",0);
      this.setSleepTime((int)(1000/FREQ));
  } /* initialize() */

/**********************************************************************
<! Description: > initializes output ports' values.
@param none
@return boolean (true=success),(false=failure)
@exception none
**********************************************************************/
  protected boolean initOutputVals(){
      return( true );

  } /* initOutputVals() */
    

/**********************************************************************
Description: allocates data internals.
@param none
@return nada
@exception none
**********************************************************************/
  protected void allocateInternals(){
      display=new WaveFrame("Square Wave",0,2000,-0.1f,1.1f,20);
      display.show();
      display.start();
  } /* allocateInternals() */
	    


/**********************************************************************
Description: Required main loop of the agent.
     Called with each cycle of operation for the agent
@param none
@return nada
@exception none
**********************************************************************/
  public void runLoop(){
      Boolean input=(Boolean)getInput(0);
      if (input.booleanValue()){
	  display.update(1.0f);
      }
      else{
	  display.update(0.0f);
      }

  } /* runLoop() */
  public int getModuleModeCapability(){
      //return LOOP_ONLY;
      //return EVENT_ONLY;
      return EVENT_OR_LOOP;
  }
  public void runEvent(TriggerEvent te){
      int[] changedPorts=te.getPortNumbers();
	  if (changedPorts.length>0){//doublecheck that a change has occurred
	  Boolean input=(Boolean)getInput(0);
	  if (input.booleanValue()){
	      display.update(1.0f);
	  }
	  else{
	      display.update(0.0f);
	  }
      }
  }
}
