org/objectweb/proactive/branchnbound/core/Task.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.IOException;
00034 import java.io.Serializable;
00035 import java.util.Vector;
00036 
00037 import org.apache.log4j.Logger;
00038 import org.objectweb.proactive.ProActive;
00039 import org.objectweb.proactive.core.util.log.Loggers;
00040 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00041 
00042 
00050 public abstract class Task implements Serializable, Comparable {
00051     protected static Logger logger = ProActiveLogger.getLogger(Loggers.P2P_SKELETONS_MANAGER);
00052     protected Result initLowerBound;
00053     protected Result initUpperBound;
00054     protected Worker worker = null;
00055     protected Object bestKnownSolution = null;
00056 
00060     public Task() {
00061         // nothing to do
00062     }
00063 
00068     public abstract Result execute();
00069 
00075     public abstract Vector split();
00076 
00082     public Result gather(Result[] results) {
00083         Result best = null;
00084         for (int i = 0; i < results.length; i++) {
00085             Result current = results[i];
00086             if (best == null) {
00087                 if (current.isAnException()) {
00088                     continue;
00089                 }
00090                 best = current;
00091             } else {
00092                 best = best.returnTheBest(current);
00093             }
00094         }
00095         return best;
00096     }
00097 
00101     public abstract void initLowerBound();
00102 
00106     public abstract void initUpperBound();
00107 
00112     public void setWorker(Worker worker) {
00113         this.worker = worker;
00114     }
00115 
00119     public int compareTo(Object arg) {
00120         Task t = (Task) arg;
00121         if (this.equals(t)) {
00122             return 0;
00123         } else if (this.hashCode() > t.hashCode()) {
00124             return -1;
00125         } else {
00126             return 1;
00127         }
00128     }
00129 
00135     public void setBestKnownSolution(Object newBestKnownResult) {
00136         if (this.bestKnownSolution != null) {
00137             synchronized (this.bestKnownSolution) {
00138                 if (((Comparable) this.bestKnownSolution).compareTo(
00139                             newBestKnownResult) > 0) {
00140                     this.bestKnownSolution = newBestKnownResult;
00141                 }
00142             }
00143         }
00144     }
00145 
00149     public void immediateTerminate() {
00150         try {
00151             ProActive.getBodyOnThis().terminate();
00152         } catch (IOException e) {
00153             logger.fatal("Couldn't terminate the task", e);
00154         }
00155     }
00156 }

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