001 /* 002 * Created on Jun 25, 2005 @author paulk 003 */ 004 package toolbus.atom.note; 005 006 import java.util.ArrayList; 007 import java.util.Iterator; 008 import java.util.List; 009 import java.util.Set; 010 011 import toolbus.TBTermFactory; 012 import toolbus.atom.Atom; 013 import toolbus.atom.Ref; 014 import toolbus.exceptions.ToolBusException; 015 import toolbus.exceptions.ToolBusExecutionException; 016 import toolbus.matching.MatchStore; 017 import toolbus.parsercup.PositionInformation; 018 import toolbus.process.ProcessExpression; 019 import toolbus.process.ProcessInstance; 020 import aterm.ATerm; 021 022 public class SndNote extends Atom{ 023 public final ATerm notePattern; 024 025 public SndNote(ATerm note, TBTermFactory tbfactory, PositionInformation posInfo){ 026 super(tbfactory, posInfo); 027 this.notePattern = note; 028 setAtomArgs(new Ref[]{new Ref(note)}); 029 } 030 031 public ProcessExpression copy(){ 032 Atom a = new SndNote(notePattern, tbfactory, getPosInfo()); 033 a.copyAtomAttributes(this); 034 return a; 035 } 036 037 public boolean execute() throws ToolBusException{ 038 if(isEnabled()){ 039 MatchStore matchStore = getToolBus().getMatchStore(); 040 Set<ProcessInstance> notePartners = matchStore.getPossibleNotePartners(notePattern); 041 042 if(notePartners.size() == 0) return true; 043 044 ATerm theNote = tbfactory.fullSubstitute(notePattern, getEnv()); 045 if(theNote == null) throw new ToolBusExecutionException("Illegal note pattern: "+theNote+".", getPosInfo()); 046 047 Iterator<ProcessInstance> notePartnersIterator = notePartners.iterator(); 048 while(notePartnersIterator.hasNext()){ 049 ProcessInstance pi = notePartnersIterator.next(); 050 051 pi.putNoteInQueue(theNote); 052 } 053 return true; 054 } 055 return false; 056 } 057 058 public ProcessInstance[] debugExecute() throws ToolBusException{ 059 if(isEnabled()){ 060 MatchStore matchStore = getToolBus().getMatchStore(); 061 Set<ProcessInstance> notePartners = matchStore.getPossibleNotePartners(notePattern); 062 063 if(notePartners.size() == 0) return new ProcessInstance[0]; 064 065 ATerm theNote = tbfactory.fullSubstitute(notePattern, getEnv()); 066 if(theNote == null) throw new ToolBusExecutionException("Illegal note pattern: "+theNote+".", getPosInfo()); 067 068 List<ProcessInstance> subscribedPartners = new ArrayList<ProcessInstance>(); 069 Iterator<ProcessInstance> notePartnersIterator = notePartners.iterator(); 070 while(notePartnersIterator.hasNext()){ 071 ProcessInstance pi = notePartnersIterator.next(); 072 073 pi.putNoteInQueue(theNote); 074 subscribedPartners.add(pi); 075 } 076 077 ProcessInstance[] partners = new ProcessInstance[subscribedPartners.size()]; 078 return subscribedPartners.toArray(partners); 079 } 080 return null; 081 } 082 }