00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030  
00031 package org.objectweb.proactive.calcium;
00032 
00033 import java.io.Serializable;
00034 
00035 import org.apache.log4j.Logger;
00036 import org.objectweb.proactive.calcium.exceptions.PanicException;
00037 import org.objectweb.proactive.calcium.interfaces.Instruction;
00038 import org.objectweb.proactive.calcium.statistics.Timer;
00039 import org.objectweb.proactive.core.util.log.Loggers;
00040 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00041 
00061 public class Interpreter implements Serializable {
00062         static Logger logger = ProActiveLogger.getLogger(Loggers.SKELETONS_STRUCTURE);
00063         
00064         public Interpreter(){
00065         }
00066         
00067         public <T> Task<T> interpret(Task<T> task){
00068                 
00069                 Timer timer=new Timer(true);
00070                 while(task.hasInstruction()){
00071                         if(logger.isDebugEnabled()){
00072                                 logger.debug("--Stack Top-- Task="+task.getId()+" "+task.getObject());
00073                                 String tmp="";
00074                                 for(Instruction c:task.getStack()) tmp=c+"\n"+tmp;
00075                                 tmp=tmp.trim();
00076                                 logger.debug(tmp);
00077                         }
00078 
00079                         
00080                         Instruction<T> inst = task.popInstruction();
00081 
00082                         
00083                         int oldId=task.getId();
00084                         
00085                         try {
00086                                 task=inst.compute(task);
00087                         } catch (Exception e) {                 
00088                                 task.pushInstruction(inst); 
00089                                 task.setException(e);           
00090                                 return task;
00091                         }
00092 
00093                         if(oldId!=task.getId()){
00094                                 String msg = "Panic error, task id changed while interpreting! "+oldId+"->"+task.getId();
00095                                 task.pushInstruction(inst);
00096                                 task.setException(new PanicException(msg));
00097                                 logger.error(msg);
00098                                 return task;
00099                         }
00100                         
00101                         
00102                         if(task.hasReadyChildTask()){
00103                                 timer.stop();
00104                                 task.getStats().addComputationTime(timer.getTime());
00105                                 return task;
00106                         }
00107                 }
00108 
00109                 
00110                 timer.stop();
00111                 task.getStats().addComputationTime(timer.getTime());
00112                 return task;
00113         }
00114 }