001 package toolbus.atom.tool; 002 003 import toolbus.TBTermFactory; 004 import toolbus.TBTermVar; 005 import toolbus.atom.Atom; 006 import toolbus.atom.Ref; 007 import toolbus.exceptions.ToolBusException; 008 import toolbus.exceptions.ToolBusExecutionException; 009 import toolbus.parsercup.PositionInformation; 010 import toolbus.process.ProcessExpression; 011 import toolbus.tool.ToolInstance; 012 import aterm.ATerm; 013 014 /** 015 * @author paulk, Aug 7, 2002 016 */ 017 public class Do extends Atom{ 018 private final Ref toolId; 019 private final Ref request; 020 private ToolInstance toolInstance; 021 022 public Do(ATerm toolId, ATerm request, TBTermFactory tbfactory, PositionInformation posInfo){ 023 super(tbfactory, posInfo); 024 025 this.toolId = new Ref(toolId); 026 this.request = new Ref(request); 027 setAtomArgs(new Ref[]{this.toolId, this.request}); 028 externalNameAsReceivedByTool = "rec-do"; 029 } 030 031 public ProcessExpression copy(){ 032 Atom a = new Do(toolId.value, request.value, tbfactory, getPosInfo()); 033 a.copyAtomAttributes(this); 034 035 return a; 036 } 037 038 public void activate(){ 039 toolInstance = null; 040 super.activate(); 041 } 042 043 public boolean execute() throws ToolBusException{ 044 if(!isEnabled()) return false; 045 046 if(toolInstance == null){ 047 ATerm tid = getEnv().getValue((TBTermVar) toolId.value); 048 toolInstance = getToolBus().getToolInstanceManager().get(tid); 049 if(toolInstance == null) return false; 050 } 051 052 if(toolInstance.tryDoEval()){ 053 ATerm req = tbfactory.fullSubstitute(request.value, getEnv()); 054 if(req == null) throw new ToolBusExecutionException("Illegal do request pattern: "+request.value+".", getPosInfo()); 055 056 toolInstance.sendDo(req); 057 //LoggerFactory.log(this.getProcess().getProcessName(), "Do " + request.value, IToolBusLoggerConstants.TOOLCOM); 058 return true; 059 } 060 return false; 061 } 062 }