org/objectweb/proactive/branchnbound/core/Worker.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.branchnbound.core;
00032 
00033 import java.io.Serializable;
00034 import java.util.Vector;
00035 
00036 import org.apache.log4j.Logger;
00037 import org.objectweb.proactive.ActiveObjectCreationException;
00038 import org.objectweb.proactive.ProActive;
00039 import org.objectweb.proactive.branchnbound.core.exception.NoResultsException;
00040 import org.objectweb.proactive.branchnbound.core.queue.TaskQueue;
00041 import org.objectweb.proactive.core.group.Group;
00042 import org.objectweb.proactive.core.group.ProActiveGroup;
00043 import org.objectweb.proactive.core.node.NodeException;
00044 import org.objectweb.proactive.core.util.log.Loggers;
00045 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00046 import org.objectweb.proactive.core.util.wrapper.BooleanWrapper;
00047 
00048 
00056 public class Worker implements Serializable {
00057     protected static Logger logger = ProActiveLogger.getLogger(Loggers.P2P_SKELETONS_WORKER);
00058     private Worker selfWorkerGroup = null;
00059     private Result bestCurrentResult = null;
00060     private TaskQueue taskProvider = null;
00061     private String workerNodeUrl = null;
00062     private Task currentTask = null;
00063 
00067     public Worker() {
00068         // The emplty constructor
00069     }
00070 
00075     public Worker(TaskQueue taskProvider) {
00076         this.taskProvider = taskProvider;
00077     }
00078 
00084     public Result execute(Task task) {
00085         if (this.bestCurrentResult == null) {
00086             this.bestCurrentResult = this.taskProvider.getBestCurrentResult();
00087         }
00088         Exception exception = null;
00089         Task activedTask = null;
00090         try {
00091             // Activing the task
00092             if (this.workerNodeUrl == null) {
00093                 this.workerNodeUrl = ProActive.getBodyOnThis().getNodeURL();
00094             }
00095             activedTask = (Task) ProActive.turnActive(ProActive.getFutureValue(
00096                         task), workerNodeUrl);
00097             activedTask.setWorker((Worker) ProActive.getStubOnThis());
00098             this.currentTask = activedTask;
00099             ProActive.setImmediateService(this.currentTask, "setBestKnownResult");
00100             ProActive.setImmediateService(this.currentTask, "immediateTerminate");
00101         } catch (ActiveObjectCreationException e) {
00102             logger.fatal("Couldn't actived the task", e);
00103             exception = e;
00104         } catch (NodeException e) {
00105             logger.fatal("A problem with the task's node", e);
00106             exception = e;
00107         } catch (Exception e) {
00108             logger.fatal("Failed immediate service", e);
00109             exception = e;
00110         }
00111 
00112         if (this.bestCurrentResult.getException() != null) {
00113             this.bestCurrentResult = null;
00114         }
00115         if (activedTask != null) {
00116             activedTask.setBestKnownSolution(this.bestCurrentResult.getSolution());
00117             activedTask.initLowerBound();
00118             activedTask.initUpperBound();
00119 
00120             return activedTask.execute();
00121         }
00122         logger.fatal("The task was not actived");
00123         if (exception == null) {
00124             return new Result(new NoResultsException("The task was not actived"));
00125         }
00126         return new Result(exception);
00127     }
00128 
00133     public void setWorkerGroup(Worker workerGroup) {
00134         Group group = ProActiveGroup.getGroup(workerGroup);
00135         group.remove(ProActive.getStubOnThis());
00136         this.selfWorkerGroup = workerGroup;
00137     }
00138 
00144     public void setBestCurrentResult(Result newBest) {
00145         if ((this.bestCurrentResult == null) ||
00146                 newBest.isBetterThan(this.bestCurrentResult)) {
00147             this.bestCurrentResult = newBest;
00148             if (this.selfWorkerGroup != null) {
00149                 this.selfWorkerGroup.informNewBestResult(this.bestCurrentResult);
00150                 this.taskProvider.informNewBestResult(this.bestCurrentResult);
00151             }
00152             if (this.currentTask != null) {
00153                 this.currentTask.setBestKnownSolution(this.bestCurrentResult.getSolution());
00154             }
00155             logger.debug("A new best result was localy found: " +
00156                 this.bestCurrentResult);
00157         }
00158         logger.debug("The new best result is NOT BETTER");
00159     }
00160 
00164     public Result getBestCurrentResult() {
00165         if (this.bestCurrentResult == null) {
00166             return new Result(new NoResultsException());
00167         }
00168         return this.bestCurrentResult;
00169     }
00170 
00176     public void informNewBestResult(Result newBest) {
00177         if ((this.bestCurrentResult == null) ||
00178                 newBest.isBetterThan(this.bestCurrentResult)) {
00179             this.bestCurrentResult = newBest;
00180             if (this.currentTask != null) {
00181                 this.currentTask.setBestKnownSolution(this.bestCurrentResult.getSolution());
00182             }
00183             if (logger.isInfoEnabled()) {
00184                 logger.info("I was informed from a new remote best result: " +
00185                     this.bestCurrentResult);
00186             }
00187         }
00188     }
00189 
00194     public void sendSubTasksToTheManager(Vector subTaskList) {
00195         if (logger.isDebugEnabled()) {
00196             logger.debug("The task sends " + subTaskList.size() + " sub tasks");
00197         }
00198         this.taskProvider.addAll(subTaskList);
00199     }
00200 
00204     public BooleanWrapper isHungry() {
00205         return this.taskProvider.isHungry();
00206     }
00207 
00211     public void immediateStopComputation() {
00212         this.currentTask.immediateTerminate();
00213         this.currentTask = null;
00214     }
00215 
00219     public Task getCurrentTask() {
00220         return this.currentTask;
00221     }
00222 
00226     public void alive() {
00227         // nothing to do here
00228     }
00229 
00233     public void reset() {
00234         this.bestCurrentResult = null;
00235         this.currentTask = null;
00236     }
00237 }

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