/*
 *  Agent's ALLL receive message action class
 *
 *  2004 - 2011 Frantisek Zboril jr.
 *  Faculty of Information Technology
 *  zborilf@fit.vutbr.cz
 *  http://www.fit.vutbr.cz/~zborilf
 *
 * (C) Brno University of Technology
 * Freeware licence VUT v Brně
 * https://wis.fit.vutbr.cz/FIT/db/dir.php/dr/VZ2007/externiInformace/softwareNaWebu/Priloha2_rozh23.pdf

 */

package jTMass.core.jAMass.jActions;

import jTMass.*;
import jTMass.core.jAMass.*;
import jTMass.core.jALLL.jTObjects.*;

public class TReceive extends TAction {
	
    TObject PSenderName;

    // ===== CLONING =====

    @Override
    public TObject cloneObject(){
        TObject newSender=PSenderName.cloneObject();
	TReceive newReception=new TReceive(newSender);
	return(newReception);
    }

    // ===== STRING CONTENT READERS =====

    @Override
    public String getActionName(){
        return("RECEIVE");
    }

    @Override
    public String getContent(){
    	return("?("+PSenderName.getContent()+")");
    }

    // ===== WRITERS =====

    void setSender(TObject senderName){
	PSenderName=senderName;
    }

    // ===== REGISTER MAINTENANCE=====

    @Override
    public TObject substituteRegisters(TAgent agent){
	super.substituteRegisters(agent);
	PSenderName.substituteRegisters(agent);
	return(this);
    }
	
    @Override
    public void matureRegisters(){
	super.matureRegisters();
	PSenderName.matureRegisters();
    }

    // ===== ACTION EXECUTION =====
	
    @Override
    public TObject execute(TAgent agent){
	super.execute(agent);
	PSenderName=PSenderName.substituteRegisters(agent);
	if(PSenderName instanceof TAtom) 
		return(agent.getInput((TAtom)PSenderName));	
        return(null);
    }
		
    public TReceive(TObject id){
	super(TConstants._Reception);
	if((id instanceof TAtom)||(id instanceof TRegister))
            PSenderName=id;
	else{
            TMass._Terminal.reportError("Wrong sender name");
            PSenderName=null;
	}
    }
	
}