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.AFun;
013    import aterm.ATerm;
014    import aterm.ATermAppl;
015    import aterm.ATermList;
016    
017    /**
018     * @author paulk, Jul 31, 2002
019     */
020    public class AckEvent extends Atom{
021            private final Ref toolId;
022            private final Ref event;
023            private final Ref callbackData;
024            private ToolInstance toolInstance;
025            
026            public AckEvent(ATerm toolId, ATerm event, ATerm callbackData, TBTermFactory tbfactory, PositionInformation posInfo){
027                    super(tbfactory, posInfo);
028                    
029                    this.toolId = new Ref(toolId);
030                    this.event = new Ref(event);
031                    this.callbackData = new Ref(callbackData);
032                    setAtomArgs(new Ref[]{this.toolId, this.event});
033                    externalNameAsReceivedByTool = "rec-ack-event";
034            }
035            
036            public ProcessExpression copy(){
037                    Atom a = new AckEvent(toolId.value, event.value, callbackData.value, tbfactory, getPosInfo());
038                    a.copyAtomAttributes(this);
039                    
040                    return a;
041            }
042            
043            public void activate(){
044                    toolInstance = null;
045                    super.activate();
046            }
047            
048            public boolean execute() throws ToolBusException{
049                    if(!isEnabled()) return false;
050                    
051                    if(toolInstance == null){
052                            ATerm tid = getEnv().getValue((TBTermVar) toolId.value);
053                            toolInstance = getToolBus().getToolInstanceManager().get(tid);
054                            if(toolInstance == null) return false;
055                    }
056                    
057                    // Construct an ack appl with the right amount of arguments.
058                    AFun ackFun = ((ATermAppl) event.value).getAFun();
059                    int arity = ackFun.getArity();
060                    ATerm[] arguments = new ATerm[arity];
061                    for(int i = 0; i < arity; i++){
062                            arguments[i] = tbfactory.EmptyList;
063                    }
064                    ATermAppl ackAppl = tbfactory.makeAppl(ackFun, arguments);
065                    
066                    // Get the callback data if present.
067                    ATerm callbackTerm = callbackData.value;
068                    if(callbackTerm == null){
069                            callbackTerm = tbfactory.EmptyList;
070                    }else{
071                            callbackTerm = tbfactory.fullSubstitute(callbackTerm, getEnv());
072                            if(callbackTerm == null) throw new ToolBusExecutionException("Illegal callback term pattern: "+callbackTerm+".", getPosInfo());
073                    }
074                    
075                    // Construct the ack event.
076                    ATermList ackEvent = tbfactory.makeList();
077                    ackEvent = ackEvent.insert(callbackTerm);
078                    ackEvent = ackEvent.insert(ackAppl);
079                    
080                    toolInstance.sendAckEvent(ackEvent);
081                    
082                    //LoggerFactory.log(this.getProcess().getProcessName(), "AckEvent " + event.value, IToolBusLoggerConstants.TOOLCOM);
083                    return true;
084            }
085    }