org/objectweb/proactive/calcium/Interpreter.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
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                         //Take the top instruction
00080                         Instruction<T> inst = task.popInstruction();
00081 
00082                         //Compute the instruction
00083                         int oldId=task.getId();
00084                         
00085                         try {
00086                                 task=inst.compute(task);
00087                         } catch (Exception e) {                 //If an exception happend, we report the exception in the task
00088                                 task.pushInstruction(inst); //we put back the instruction that generated the exception
00089                                 task.setException(e);           //then we add the exception to the task
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                         //If child tasks are present, that's all folks (for now)
00102                         if(task.hasReadyChildTask()){
00103                                 timer.stop();
00104                                 task.getStats().addComputationTime(timer.getTime());
00105                                 return task;
00106                         }
00107                 }//while
00108 
00109                 //The task is finished
00110                 timer.stop();
00111                 task.getStats().addComputationTime(timer.getTime());
00112                 return task;
00113         }
00114 }

Generated on Mon Jan 22 15:16:05 2007 for ProActive by  doxygen 1.5.1