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