001    /*
002     * Created on Jun 26, 2005
003     */
004    package toolbus.atom.tool;
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.ToolInstanceManager;
014    import toolbus.atom.Atom;
015    import toolbus.atom.Ref;
016    import toolbus.exceptions.ToolBusException;
017    import toolbus.exceptions.ToolBusExecutionException;
018    import toolbus.parsercup.PositionInformation;
019    import toolbus.process.ProcessExpression;
020    import toolbus.process.ProcessInstance;
021    import toolbus.tool.ToolInstance;
022    import aterm.ATerm;
023    import aterm.ATermAppl;
024    
025    /**
026     * Execute a tool
027     */
028    public class Execute extends Atom{
029            private final Ref tool;
030            private final Ref rvar;
031            
032            public Execute(ATerm tool, ATerm rvar, TBTermFactory tbfactory, PositionInformation posInfo){
033                    super(tbfactory, posInfo);
034                    this.tool = new Ref(tool);
035                    this.rvar = new Ref(rvar);
036                    setAtomArgs(new Ref[]{this.tool, this.rvar});
037            }
038            public ProcessExpression copy(){
039                    Atom a = new Execute(tool.value, rvar.value, tbfactory, getPosInfo());
040                    a.copyAtomAttributes(this);
041                    
042                    return a;
043            }
044            
045            public void compile(ProcessInstance P, Stack<String> calls, State follow) throws ToolBusException{
046                    super.compile(P, calls, follow);
047                    
048                    if(tool.value.getType() != ATerm.APPL) throw new ToolBusExecutionException("Malformed first argument in execute.", getPosInfo());
049                    if(!tbfactory.isResultVar(rvar.value)) throw new ToolBusExecutionException("Second argument of execute should be a result variable.", getPosInfo());
050                    if(!Functions.compatibleTypes(tool.value, rvar.value)) throw new ToolBusExecutionException("Arguments of execute should have the same (tool) type.", getPosInfo());
051            }
052            
053            public boolean execute() throws ToolBusException{
054                    if(!isEnabled()) return false;
055                    
056                    String name = ((ATermAppl) tool.value).getName();
057                    ToolBus toolbus = getToolBus();
058                    ToolInstance toolInstance = new ToolInstance(toolbus.getToolDefinition(name), toolbus);
059                    ToolInstanceManager toolInstanceManager = toolbus.getToolInstanceManager();
060                    toolInstanceManager.addPendingTool(toolInstance);
061                    
062                    getEnv().assignVar((TBTermVar) rvar.value, toolInstance.getToolKey());
063                    toolInstance.executeTool();
064                    //LoggerFactory.log(this.getProcess().getProcessName(), "Execute " + tool.value, IToolBusLoggerConstants.TOOLCOM);
065                    return true;
066            }
067    }