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.skeletons;
00032
00033 import java.util.Vector;
00034
00035 import org.objectweb.proactive.calcium.Task;
00036 import org.objectweb.proactive.calcium.exceptions.MuscleException;
00037 import org.objectweb.proactive.calcium.exceptions.EnvironmentException;
00038 import org.objectweb.proactive.calcium.interfaces.Condition;
00039 import org.objectweb.proactive.calcium.interfaces.Conquer;
00040 import org.objectweb.proactive.calcium.interfaces.Divide;
00041 import org.objectweb.proactive.calcium.interfaces.Instruction;
00042 import org.objectweb.proactive.calcium.interfaces.Skeleton;
00043 import org.objectweb.proactive.calcium.statistics.Timer;
00044
00059 public class DaC<T> implements Skeleton<T>, Instruction<T> {
00060
00061 Divide<T> div;
00062 Conquer<T> conq;
00063 Condition<T> cond;
00064 Skeleton<T> child;
00065
00073 public DaC(Divide<T> div, Condition<T> cond, Skeleton<T> child, Conquer<T> conq){
00074
00075 this.div=div;
00076 this.cond=cond;
00077 this.child=child;
00078 this.conq=conq;
00079 }
00080
00081 public Vector<Instruction<T>> getInstructionStack() {
00082
00083 Vector<Instruction<T>> v= new Vector<Instruction<T>>();
00084 v.add(this);
00085
00086 return v;
00087 }
00088
00089 public Task<T> compute(Task<T> t) throws EnvironmentException{
00090
00091
00092 if(t.hasFinishedChild()){
00093 return conquer(t);
00094 }
00095
00096
00097
00098
00099 Timer timer = new Timer();
00100 boolean evalCondition=cond.evalCondition(t.getObject());
00101 timer.stop();
00102 t.getStats().getWorkout().track(cond, timer);
00103
00104 if(evalCondition){
00105 return divide(t);
00106 }
00107
00108 else{
00109 return execute(t);
00110 }
00111 }
00112
00113 protected Task<T> execute(Task<T> t){
00114
00115 Vector<Instruction<T>> currentStack = t.getStack();
00116 currentStack.addAll(child.getInstructionStack());
00117 t.setStack(currentStack);
00118 return t;
00119 }
00120
00121 protected Task<T> divide(Task<T> parent) throws EnvironmentException{
00122
00123
00124
00125
00126
00127
00128 Timer timer = new Timer();
00129 Vector<T> childObjects=div.divide(parent.getObject());
00130 timer.stop();
00131
00132 if(childObjects.size()<=0){
00133 String msg="Parameter was divided into less than 1 part.";
00134 logger.debug(msg);
00135 throw new MuscleException(msg);
00136 }
00137
00138 for(T o:childObjects){
00139 Task<T> child = new Task<T>(o);
00140 child.pushInstruction(this);
00141 parent.addReadyChild(child);
00142 }
00143
00144
00145 parent.pushInstruction(this);
00146 parent.getStats().getWorkout().track(div, timer);
00147 return parent;
00148 }
00149
00150 protected Task<T> conquer(Task<T> parent) throws EnvironmentException {
00151
00158 Vector<T> childObjects = new Vector<T>();
00159
00160 while(parent.hasFinishedChild()){
00161
00162 Task<T> child = parent.getFinishedChild();
00163 childObjects.add(child.getObject());
00164 }
00165
00166 if(childObjects.size() <=0 ){
00167 String msg="Can't conquer less than one child parameter!";
00168 logger.error(msg);
00169 throw new MuscleException(msg);
00170 }
00171
00172 Timer timer = new Timer();
00173 T resultObject=conq.conquer(parent.getObject(), childObjects);
00174 timer.stop();
00175 Task<T> resultTask=parent.reBirth(resultObject);
00176
00177 resultTask.getStats().getWorkout().track(conq, timer);
00178 return resultTask;
00179 }
00180
00181 public String toString(){
00182 return "D&C";
00183 }
00184 }