001 /**
002 * @author paulk, Jul 24, 2002
003 */
004 package toolbus.atom;
005
006 import java.util.Stack;
007
008 import toolbus.Functions;
009 import toolbus.State;
010 import toolbus.TBTermFactory;
011 import toolbus.TBTermVar;
012 import toolbus.ToolBus;
013 import toolbus.exceptions.ToolBusException;
014 import toolbus.exceptions.ToolBusExecutionException;
015 import toolbus.parsercup.PositionInformation;
016 import toolbus.process.ProcessCall;
017 import toolbus.process.ProcessExpression;
018 import toolbus.process.ProcessInstance;
019 import aterm.ATerm;
020 import aterm.ATermAppl;
021 import aterm.ATermList;
022
023 public class Create extends Atom{
024 private final Ref pcall;
025 private final Ref rvar;
026
027 public Create(ATerm c, ATerm v, TBTermFactory tbfactory, PositionInformation posInfo){
028 super(tbfactory, posInfo);
029 pcall = new Ref(c);
030 rvar = new Ref(v);
031 setAtomArgs(new Ref[]{pcall, rvar});
032 }
033
034 public ProcessExpression copy(){
035 Atom a = new Create(pcall.value, rvar.value, tbfactory, getPosInfo());
036 a.copyAtomAttributes(this);
037 return a;
038 }
039
040 public void compile(ProcessInstance P, Stack<String> calls, State follow) throws ToolBusException{
041 super.compile(P, calls, follow);
042
043 if(pcall.value.getType() != ATerm.APPL) throw new ToolBusExecutionException("Malformed first argument in create.", getPosInfo());
044 if(!tbfactory.isResultVar(rvar.value)) throw new ToolBusExecutionException("Second argument of create should be a result variable.", getPosInfo());
045 }
046
047 public boolean execute() throws ToolBusException{
048 if(!isEnabled()) return false;
049
050 String name = ((ATermAppl) pcall.value).getName();
051 ATermList cargs = ((ATermAppl) pcall.value).getArguments();
052 ATermList evargs = (ATermList) Functions.eval(cargs, getProcess(), getEnv());
053
054 ToolBus TB = getToolBus();
055
056 ProcessInstance P = TB.addProcess(new ProcessCall(name, evargs, false, TB.getTBTermFactory(), null));
057
058 getEnv().assignVar((TBTermVar) rvar.value, tbfactory.makeInt(P.getProcessId()));
059 return true;
060 }
061 }