1 19 20 package org.netbeans.modules.tasklist.usertasks.schedule; 21 22 import java.util.Arrays ; 23 import java.util.Collections ; 24 import java.util.Comparator ; 25 import java.util.List ; 26 import org.netbeans.modules.tasklist.core.util.ObjectList; 27 import org.netbeans.modules.tasklist.usertasks.model.Dependency; 28 import org.netbeans.modules.tasklist.usertasks.model.UserTask; 29 import org.netbeans.modules.tasklist.usertasks.util.UTUtils; 30 31 36 public class ScheduleUtils { 37 40 private ScheduleUtils() { 41 } 42 43 48 public static void createPriorityListByPriority(List <UserTask> tasks) { 49 Collections.sort(tasks, new Comparator () { 50 public int compare(Object o1, Object o2) { 51 UserTask ut1 = (UserTask) o1; 52 UserTask ut2 = (UserTask) o2; 53 return ut1.getPriority() - ut2.getPriority(); 54 } 55 }); 56 } 57 58 66 public static void createPriorityListByDuration(List <UserTask> tasks) { 67 Collections.sort(tasks, new Comparator () { 68 public int compare(Object o1, Object o2) { 69 UserTask ut1 = (UserTask) o1; 70 UserTask ut2 = (UserTask) o2; 71 return ut2.getEffort() - ut1.getEffort(); 72 } 73 }); 74 } 75 76 79 private static class UserTaskAndCriticalTime { 80 81 public UserTask ut; 82 83 84 public float ct; 85 } 86 87 95 public static void createPriorityListBackflow(List <UserTask> tasks) { 96 boolean[][] deps = new boolean[tasks.size()][tasks.size()]; 98 for (int i = 0; i < tasks.size(); i++) { 99 ObjectList<Dependency> ds = tasks.get(i).getDependencies(); 100 for (int j = 0; j < ds.size(); j++) { 101 Dependency d = ds.get(j); 102 if (d.getType() == Dependency.END_BEGIN) { 103 UserTask don = d.getDependsOn(); 104 int index = -1; 105 for (int k = 0; k < tasks.size(); k++) { 106 if (tasks.get(k) == don) { 107 index = k; 108 break; 109 } 110 } 111 assert index >= 0; 112 deps[i][index] = true; 113 } 114 } 115 } 116 float[] durations = new float[tasks.size()]; 117 for (int i = 0; i < tasks.size(); i++) { 118 durations[i] = tasks.get(i).getEffort() * 119 tasks.get(i).getProgress() / 100.0f; 120 } 121 float[] ct = backflow(deps, durations); 122 123 UserTaskAndCriticalTime[] v = new UserTaskAndCriticalTime[tasks.size()]; 125 for (int i = 0; i < v.length; i++) { 126 UserTaskAndCriticalTime utct = new UserTaskAndCriticalTime(); 127 utct.ut = tasks.get(i); 128 utct.ct = ct[i]; 129 v[i] = utct; 130 } 131 Arrays.sort(v, new Comparator () { 132 public int compare(Object o1, Object o2) { 133 UserTaskAndCriticalTime ut1 = (UserTaskAndCriticalTime) o1; 134 UserTaskAndCriticalTime ut2 = (UserTaskAndCriticalTime) o2; 135 return Float.compare(ut2.ct, ut1.ct); 136 } 137 }); 138 139 tasks.clear(); 141 for (int i = 0; i < v.length; i++) { 142 tasks.add(v[i].ut); 143 } 144 } 145 146 155 public static float[] backflow(boolean[][] deps, float[] durations) { 156 assert deps.length == durations.length; 157 158 int n = durations.length; 159 if (n == 0) 160 return new float[0]; 161 162 float[] ct = new float[n]; 163 Arrays.fill(ct, -1); 164 165 for (int i = 0; i < n; i++) { 166 backflow2(ct, deps, durations, i); 167 } 168 169 return ct; 170 } 171 172 private static void backflow2(float[] ct, boolean[][] deps, float[] durations, 173 int knot) { 174 if (ct[knot] >= 0) 175 return; 176 177 float max = 0; 178 for (int i = 0; i < durations.length; i++) { 179 if (deps[i][knot]) { 180 backflow2(ct, deps, durations, i); 181 if (ct[i] > max) 182 max = ct[i]; 183 } 184 } 185 ct[knot] = max + durations[knot]; 186 } 187 188 194 public static void sortForDependencies(List <UserTask> tasks) { 195 boolean swapped; 196 do { 197 swapped = false; 198 outer: 199 for (int i = 0; i < tasks.size() - 1; i++) { 200 UserTask t = tasks.get(i); 201 for (int j = i + 1; j < tasks.size(); j++) { 202 UserTask t2 = tasks.get(j); 203 for (Dependency d: t.getDependencies()) { 204 if (d.getType() == Dependency.END_BEGIN && 205 d.getDependsOn() == t2) { 206 swapped = true; 207 tasks.add(i, tasks.remove(j)); 208 break outer; 209 } 210 } 211 } 212 } 213 } while (swapped); 214 } 215 } 216 | Popular Tags |