/*
 *  Agent's ALLL direct/indirect plan execution 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 TExecute extends TAction {

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

    @Override
    public TObject cloneObject(){
	TExecute newExecute=new TExecute((TPlan)PList.cloneObject());
	return(newExecute);
    }

    // ===== GET STRING CONTENT =====

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

    @Override
    public String getContent(){
	if(PList.getType()==TConstants._Plan_Direct_Script)
            return("@"+((TPlan)PList).getContent());
	else
            return("@^("+PList.get(0).getContent()+")");
    }

    // ===== ACTION EXECUTION =====

    @Override
    public TObject execute(TAgent agent){
	TObject register=agent.getRegister();
	if(PList.getType()==TConstants._Plan_Direct_Script){
            agent.expandPlan((TPlan)PList);
            agent.increasePlanLevel();
            return(register);
	}
	else{
            if(PList.getType()==TConstants._Plan_Indirect_Call){
                if(agent.expandByName(PList.get(0).getContent())){
                    agent.increasePlanLevel();
                    return(register);
                }
                else
                    return(new TFail());
            }
            else
            {
                TMass._Terminal.reportError("Illegal plan execution");
            }
	}
	return(new TFail());
    }

    // ===== CONSTRUCTOR =====
	
    public TExecute(TPlan plan){
        super(TConstants._Execution);
	PList=(TList)plan.cloneObject();
    }
	
}
