001 /*
002 * Created on Oct 6, 2005
003 */
004 package toolbus.atom.tool;
005
006 import toolbus.TBTermFactory;
007 import toolbus.TBTermVar;
008 import toolbus.ToolInstanceManager;
009 import toolbus.atom.Atom;
010 import toolbus.atom.Ref;
011 import toolbus.exceptions.ToolBusException;
012 import toolbus.parsercup.PositionInformation;
013 import toolbus.process.ProcessExpression;
014 import toolbus.tool.ToolInstance;
015 import aterm.ATerm;
016 import aterm.ATermAppl;
017
018 /**
019 * @author paulk
020 */
021 public class Connect extends Atom{
022 private final Ref toolId;
023 private final boolean followsExecute;
024
025 public Connect(ATerm toolId, TBTermFactory tbfactory, PositionInformation posInfo, boolean followsExecute){
026 super(tbfactory, posInfo);
027
028 this.toolId = new Ref(toolId);
029 setAtomArgs(new Ref[]{this.toolId});
030
031 this.followsExecute = followsExecute;
032
033 externalNameAsReceivedByTool = "snd-connect";
034 }
035
036 public ProcessExpression copy(){
037 Atom a = new Connect(toolId.value, tbfactory, getPosInfo(), followsExecute);
038 a.copyAtomAttributes(this);
039
040 return a;
041 }
042
043 /**
044 * @see toolbus.StateElement#execute()
045 */
046 public boolean execute() throws ToolBusException{
047 if(!isEnabled()) return false;
048
049 // System.err.println("Connect.execute called; toolname = " + toolId.value);
050 ATerm tid = getEnv().getValue((TBTermVar) toolId.value);
051
052 String toolname = ((ATermAppl) ((TBTermVar) toolId.value).getVarType()).getName();
053 ToolInstanceManager toolInstanceManager = getToolBus().getToolInstanceManager();
054
055 ToolInstance ti;
056 if(followsExecute){
057 ti = toolInstanceManager.activatePendingTool(tid);
058 }else{
059 ti = toolInstanceManager.activateDynamiclyConnectedTool(toolname);
060 }
061 if(ti == null) return false;
062
063 // System.err.println(getProcess().getProcessName() + ": id = " + id);
064
065 ATerm toolid = ti.getToolKey();
066 getEnv().assignVar((TBTermVar) toolId.value, toolid);
067 // System.err.println("Connect.execute (" + id + ") assigns: " + toolid);
068 //LoggerFactory.log(this.getProcess().getProcessName(), "Connect " + toolid, IToolBusLoggerConstants.TOOLCOM);
069 return true;
070 }
071
072 }