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.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
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 }