| 1 | package de.uka.ipd.sdq.pcmsolver.transformations.pcm2lqn; |
| 2 | |
| 3 | import java.math.BigInteger; |
| 4 | import java.util.Date; |
| 5 | import java.util.List; |
| 6 | import java.util.ListIterator; |
| 7 | import java.util.Stack; |
| 8 | |
| 9 | import org.apache.log4j.Logger; |
| 10 | import org.eclipse.emf.common.util.EList; |
| 11 | import org.eclipse.emf.ecore.EObject; |
| 12 | |
| 13 | import LqnCore.ActivityDefType; |
| 14 | import LqnCore.ActivityListType; |
| 15 | import LqnCore.ActivityMakingCallType; |
| 16 | import LqnCore.ActivityOrType; |
| 17 | import LqnCore.ActivityPhasesType; |
| 18 | import LqnCore.ActivityType; |
| 19 | import LqnCore.AndJoinListType; |
| 20 | import LqnCore.EntryType; |
| 21 | import LqnCore.LqnCoreFactory; |
| 22 | import LqnCore.LqnModelType; |
| 23 | import LqnCore.OrListType; |
| 24 | import LqnCore.OutputEntryDistributionType; |
| 25 | import LqnCore.PhaseActivities; |
| 26 | import LqnCore.PrecedenceType; |
| 27 | import LqnCore.ProcessorType; |
| 28 | import LqnCore.ReplyActivityType; |
| 29 | import LqnCore.ReplyEntryType; |
| 30 | import LqnCore.SchedulingType; |
| 31 | import LqnCore.SemaphoreType; |
| 32 | import LqnCore.ServiceType; |
| 33 | import LqnCore.SingleActivityListType; |
| 34 | import LqnCore.SolverParamsType; |
| 35 | import LqnCore.TaskActivityGraph; |
| 36 | import LqnCore.TaskOptionType; |
| 37 | import LqnCore.TaskSchedulingType; |
| 38 | import LqnCore.TaskType; |
| 39 | import LqnCore.TypeType; |
| 40 | import de.uka.ipd.sdq.pcm.repository.PassiveResource; |
| 41 | import de.uka.ipd.sdq.pcmsolver.runconfig.PCMSolverWorkflowRunConfiguration; |
| 42 | |
| 43 | public class LqnBuilder { |
| 44 | |
| 45 | private static Logger logger = Logger.getLogger(LqnBuilder.class.getName()); |
| 46 | |
| 47 | private LqnCoreFactory lqnFactory = LqnCoreFactory.eINSTANCE; |
| 48 | private LqnModelType lqnModel = null; |
| 49 | |
| 50 | private Stack<TaskActivityGraph> taskGraphStack = new Stack<TaskActivityGraph>(); |
| 51 | |
| 52 | private boolean isLQSimAnalysis; |
| 53 | private boolean isInfiniteTaskMultiplicity; |
| 54 | |
| 55 | public LqnBuilder(boolean isInfTask) { |
| 56 | lqnModel = lqnFactory.createLqnModelType(); |
| 57 | lqnModel.setName("PCM2LQN_Model"); |
| 58 | |
| 59 | setIsLQSimAnalysis(false); |
| 60 | this.isInfiniteTaskMultiplicity = isInfTask; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Creates a new processor if no processor with the given id exists yet. |
| 65 | * Otherwise, returns the matching existing processor. |
| 66 | * See {@link #getProcessorTypeFromModel(String)} for how to match the id. |
| 67 | * |
| 68 | * @param id |
| 69 | * @return |
| 70 | */ |
| 71 | public ProcessorType addProcessor(String id) { |
| 72 | ProcessorType pt = getProcessorTypeFromModel(id); |
| 73 | if (pt == null) { |
| 74 | pt = lqnFactory.createProcessorType(); |
| 75 | pt.setName(id + "_Processor"); |
| 76 | pt.setMultiplicity(new BigInteger("1")); |
| 77 | pt.setScheduling(SchedulingType.FCFS); |
| 78 | lqnModel.getProcessor().add(pt); |
| 79 | } |
| 80 | return pt; |
| 81 | } |
| 82 | |
| 83 | private ProcessorType getProcessorTypeFromModel(String id) { |
| 84 | EList<ProcessorType> list = lqnModel.getProcessor(); |
| 85 | for (ProcessorType procType : list) { |
| 86 | if (procType.getName().equals(id + "_Processor")) { |
| 87 | return procType; |
| 88 | } |
| 89 | } |
| 90 | return null; |
| 91 | } |
| 92 | |
| 93 | private EntryType getEntryTypeFromTask(String id, TaskType tt) { |
| 94 | EList<EntryType> entryList = tt.getEntry(); |
| 95 | for (EntryType entryType : entryList) { |
| 96 | if (entryType.getName().equals(id + "_Entry")) { |
| 97 | return entryType; |
| 98 | } |
| 99 | } |
| 100 | return null; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Checks whether the passed {@link ProcessorType} contains a task. |
| 105 | * If yes, the first task is returned. If no, a new task is created and |
| 106 | * added to the processor. |
| 107 | * |
| 108 | * Two entries are created for the task if it is newly created: A wait entry and a signal |
| 109 | * entry. The entry ids are created using {@link #getSignalEntryId(String)} and |
| 110 | * {@link #getWaitEntryId(String)} (use these for accessing the entries, using the id of the |
| 111 | * passive resource). |
| 112 | * |
| 113 | * The multiplicity of the task are set to the passed capacity. |
| 114 | * |
| 115 | * @param id The id of the {@link PassiveResource} |
| 116 | * @param pt |
| 117 | * @param capacity The {@link PassiveResource}'s capacity |
| 118 | * @return |
| 119 | */ |
| 120 | public TaskType addSemaphoreTask(String id, ProcessorType pt, int capacity){ |
| 121 | |
| 122 | TaskType tt = addTask(id, pt); |
| 123 | |
| 124 | //if this task has been newly created, it has no semaphore scheduling yet |
| 125 | //and thus needs to be initialised. |
| 126 | if (!tt.getScheduling().equals(TaskSchedulingType.SEMAPHORE)){ |
| 127 | |
| 128 | //set this task to be a semaphore (see LQN userman Sec. 3.2.3) |
| 129 | tt.setScheduling(TaskSchedulingType.SEMAPHORE); |
| 130 | //set the capacity (see LQN userman Sec. 1.1.2) |
| 131 | tt.setMultiplicity(BigInteger.valueOf(capacity)); |
| 132 | |
| 133 | LqnCoreFactory lqnFactory = LqnCoreFactory.eINSTANCE; |
| 134 | |
| 135 | EntryType wait = lqnFactory.createEntryType(); |
| 136 | wait.setName(Pcm2LqnHelper.getWaitEntryId(id)); |
| 137 | wait.setType(TypeType.PH1PH2); |
| 138 | wait.setSemaphore(SemaphoreType.WAIT); |
| 139 | tt.getEntry().add(wait); |
| 140 | |
| 141 | ActivityPhasesType aptWait = this.addActivityPhases(id); |
| 142 | PhaseActivities paWait = this.addPhaseActivities(aptWait); |
| 143 | wait.setEntryPhaseActivities(paWait); |
| 144 | |
| 145 | EntryType signal = lqnFactory.createEntryType(); |
| 146 | signal.setName(Pcm2LqnHelper.getSignalEntryId(id)); |
| 147 | signal.setType(TypeType.PH1PH2); |
| 148 | signal.setSemaphore(SemaphoreType.SIGNAL); |
| 149 | tt.getEntry().add(signal); |
| 150 | |
| 151 | ActivityPhasesType aptSignal = this.addActivityPhases(id); |
| 152 | PhaseActivities paSignal = this.addPhaseActivities(aptSignal); |
| 153 | signal.setEntryPhaseActivities(paSignal); |
| 154 | |
| 155 | } |
| 156 | return tt; |
| 157 | |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Checks whether the passed {@link ProcessorType} contains a task. |
| 162 | * If yes, the first task is returned. If no, a new task is created and |
| 163 | * added to the processor. |
| 164 | * |
| 165 | * @param id |
| 166 | * @param pt |
| 167 | * @return |
| 168 | */ |
| 169 | public TaskType addTask(String id, ProcessorType pt) { |
| 170 | |
| 171 | TaskType tt = null; |
| 172 | if (!pt.getTask().isEmpty()) { |
| 173 | tt = pt.getTask().get(0); |
| 174 | } |
| 175 | if (tt == null) { |
| 176 | tt = lqnFactory.createTaskType(); |
| 177 | tt.setName(id + "_Task"); |
| 178 | tt.setMultiplicity(new BigInteger("1")); |
| 179 | |
| 180 | tt.setThinkTime("0.0"); |
| 181 | tt.setActivityGraph(TaskOptionType.YES); |
| 182 | |
| 183 | if (this.isInfiniteTaskMultiplicity){ |
| 184 | tt.setScheduling(TaskSchedulingType.INF); |
| 185 | } else{ |
| 186 | tt.setScheduling(TaskSchedulingType.FCFS); |
| 187 | tt.setMultiplicity(new BigInteger("10000")); |
| 188 | } |
| 189 | |
| 190 | ServiceType st = lqnFactory.createServiceType(); |
| 191 | st.setName("MyService"); |
| 192 | tt.getService().add(st); |
| 193 | |
| 194 | pt.getTask().add(tt); |
| 195 | } |
| 196 | return tt; |
| 197 | } |
| 198 | |
| 199 | public TaskType addTaskForResourceDemand(String id, ProcessorType pt) { |
| 200 | |
| 201 | TaskType tt = null; |
| 202 | if (!pt.getTask().isEmpty()) { |
| 203 | EList<TaskType> taskList = pt.getTask(); |
| 204 | for (TaskType t : taskList) { |
| 205 | if (t.getName().equals(id + "_Task")) { |
| 206 | return t; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | if (tt == null) { |
| 211 | tt = lqnFactory.createTaskType(); |
| 212 | tt.setName(id + "_Task"); |
| 213 | tt.setMultiplicity(new BigInteger("1")); |
| 214 | |
| 215 | tt.setThinkTime("0.0"); |
| 216 | tt.setScheduling(TaskSchedulingType.FCFS); |
| 217 | tt.setActivityGraph(TaskOptionType.YES); |
| 218 | |
| 219 | ServiceType st = lqnFactory.createServiceType(); |
| 220 | st.setName("MyService"); |
| 221 | tt.getService().add(st); |
| 222 | |
| 223 | pt.getTask().add(tt); |
| 224 | } |
| 225 | return tt; |
| 226 | } |
| 227 | |
| 228 | public EntryType addEntry(String id, TaskType tt) { |
| 229 | EntryType et = getEntryTypeFromTask(id, tt); |
| 230 | if (et == null) { |
| 231 | et = lqnFactory.createEntryType(); |
| 232 | et.setName(id + "_Entry"); |
| 233 | et.setType(TypeType.NONE); // actually TypeType.GRAPH, but not |
| 234 | // supported by lqns |
| 235 | tt.getEntry().add(et); |
| 236 | } |
| 237 | return et; |
| 238 | } |
| 239 | |
| 240 | public TaskActivityGraph addTaskActivityGraph(TaskType tt) { |
| 241 | TaskActivityGraph taskActivityGraph = tt.getTaskActivities(); |
| 242 | if (taskActivityGraph == null) { |
| 243 | taskActivityGraph = lqnFactory.createTaskActivityGraph(); |
| 244 | tt.setTaskActivities(taskActivityGraph); |
| 245 | } |
| 246 | taskGraphStack.push(taskActivityGraph); |
| 247 | return taskActivityGraph; |
| 248 | } |
| 249 | |
| 250 | public OutputEntryDistributionType addOutputEntryDistributionType( |
| 251 | EntryType et) { |
| 252 | OutputEntryDistributionType oedt = lqnFactory |
| 253 | .createOutputEntryDistributionType(); |
| 254 | oedt.setPhase(new BigInteger("1")); |
| 255 | oedt.setMin("0.0"); |
| 256 | oedt.setMax("10.0"); |
| 257 | et.getServiceTimeDistribution().add(oedt); |
| 258 | return oedt; |
| 259 | } |
| 260 | |
| 261 | public ReplyActivityType addReplyActivity(String id, String startId, |
| 262 | String stopId) { |
| 263 | EList<ReplyEntryType> list = taskGraphStack.peek().getReplyEntry(); |
| 264 | if (list.size() > 0) { |
| 265 | return list.get(0).getReplyActivity().get(0); |
| 266 | } |
| 267 | |
| 268 | ReplyActivityType rat = lqnFactory.createReplyActivityType(); |
| 269 | // rat.setName((String)doSwitch(getStartAction(sb))); |
| 270 | rat.setName(stopId); |
| 271 | |
| 272 | ReplyEntryType ret = lqnFactory.createReplyEntryType(); |
| 273 | ret.setName(id + "_Entry"); |
| 274 | ret.getReplyActivity().add(rat); |
| 275 | taskGraphStack.peek().getReplyEntry().add(ret); |
| 276 | return rat; |
| 277 | } |
| 278 | |
| 279 | public PrecedenceType addSequencePrecedence(String firstId, String secondId) { |
| 280 | EList<PrecedenceType> list = taskGraphStack.peek().getPrecedence(); |
| 281 | for (PrecedenceType precType : list) { |
| 282 | SingleActivityListType salt = precType.getPre(); |
| 283 | if (salt != null) { |
| 284 | if (salt.getActivity().getName().equals(firstId)) { |
| 285 | return precType; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | PrecedenceType pt = lqnFactory.createPrecedenceType(); |
| 291 | |
| 292 | SingleActivityListType saltPre = lqnFactory |
| 293 | .createSingleActivityListType(); |
| 294 | ActivityType atPre = lqnFactory.createActivityType(); |
| 295 | atPre.setName(firstId); |
| 296 | saltPre.setActivity(atPre); |
| 297 | |
| 298 | SingleActivityListType saltPost = lqnFactory |
| 299 | .createSingleActivityListType(); |
| 300 | ActivityType atPost = lqnFactory.createActivityType(); |
| 301 | atPost.setName(secondId); |
| 302 | saltPost.setActivity(atPost); |
| 303 | |
| 304 | pt.setPre(saltPre); |
| 305 | pt.setPost(saltPost); |
| 306 | |
| 307 | taskGraphStack.peek().getPrecedence().add(pt); |
| 308 | return pt; |
| 309 | } |
| 310 | |
| 311 | public ActivityDefType addActivityDef(String id) { |
| 312 | EList<ActivityDefType> list = taskGraphStack.peek().getActivity(); |
| 313 | for (ActivityDefType actDefType : list) { |
| 314 | if (actDefType.getName().equals(id)) |
| 315 | return actDefType; |
| 316 | } |
| 317 | |
| 318 | ActivityDefType adt = lqnFactory.createActivityDefType(); |
| 319 | adt.setHostDemandMean("0.0"); |
| 320 | adt.setName(id); |
| 321 | taskGraphStack.peek().getActivity().add(adt); |
| 322 | return adt; |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Add or reuse an activity making call with call means = 1.0 |
| 327 | * @param callId |
| 328 | * @param targetId |
| 329 | * @param callType |
| 330 | * @return |
| 331 | */ |
| 332 | public ActivityMakingCallType addActivityMakingCall(String callId, |
| 333 | String targetId, CallType callType) { |
| 334 | return addActivityMakingCall(callId, targetId, callType, 1.0); |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Add or reuse an activity making call |
| 339 | * @param callId |
| 340 | * @param targetId |
| 341 | * @param callType |
| 342 | * @param callMeans |
| 343 | * @return |
| 344 | */ |
| 345 | public ActivityMakingCallType addActivityMakingCall(String callId, |
| 346 | String targetId, CallType callType, double callMeans) { |
| 347 | EList<ActivityDefType> actList = taskGraphStack.peek().getActivity(); |
| 348 | // check the existing ActivityMakingCallTypes and possibly reuse one of them |
| 349 | for (ActivityDefType actDefType : actList) { |
| 350 | if (actDefType.getName().equals(callId)) { |
| 351 | EList<ActivityMakingCallType> list2 = null; |
| 352 | if (callType == CallType.SYNCH){ |
| 353 | list2 = actDefType.getSynchCall(); |
| 354 | } else { |
| 355 | list2 = actDefType.getAsynchCall(); |
| 356 | } |
| 357 | for (ActivityMakingCallType amct : list2) { |
| 358 | if (amct.getDest().equals(targetId)){ |
| 359 | // should not be incremented when this part of the system is just revisited due to parameters in different contexts / or replication |
| 360 | //amct.setCallsMean(""+(getDoubleFromValue(amct.getCallsMean())+callMeans)); |
| 361 | amct.setCallsMean("1.0"); |
| 362 | return amct; |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | // no existing matching ActivityMakingCallType found, use a new one. |
| 369 | ActivityMakingCallType amct = lqnFactory.createActivityMakingCallType(); |
| 370 | amct.setDest(targetId); |
| 371 | amct.setCallsMean(""+callMeans); |
| 372 | |
| 373 | EList<ActivityDefType> list = taskGraphStack.peek().getActivity(); |
| 374 | for (ActivityDefType adt : list) { |
| 375 | if (adt.getName().equals(callId)) { |
| 376 | if (callType == CallType.SYNCH){ |
| 377 | adt.getSynchCall().add(amct); |
| 378 | } else { |
| 379 | adt.getAsynchCall().add(amct); |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | return amct; |
| 385 | } |
| 386 | |
| 387 | private double getDoubleFromValue(Object callsMean) { |
| 388 | if (callsMean instanceof String){ |
| 389 | return Double.valueOf((String)callsMean); |
| 390 | } if (callsMean instanceof Double){ |
| 391 | return ((Double)callsMean).doubleValue(); |
| 392 | } else { |
| 393 | throw new RuntimeException("Cannot parse LQN value "+callsMean.getClass().getName()+" "+callsMean.toString()); |
| 394 | } |
| 395 | |
| 396 | } |
| 397 | |
| 398 | public PrecedenceType addBeginBranchPrecedence(String id) { |
| 399 | EList<PrecedenceType> list = taskGraphStack.peek().getPrecedence(); |
| 400 | for (PrecedenceType precType : list) { |
| 401 | if (precType.getPre()!= null){ |
| 402 | if (precType.getPre().getActivity().getName().equals(id)) { |
| 403 | return precType; |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | PrecedenceType ptBegin = getInitialPrecedence(id); |
| 409 | |
| 410 | OrListType oltPreBegin = lqnFactory.createOrListType(); // branch |
| 411 | ptBegin.setPostOR(oltPreBegin); |
| 412 | taskGraphStack.peek().getPrecedence().add(ptBegin); |
| 413 | |
| 414 | return ptBegin; |
| 415 | } |
| 416 | |
| 417 | private PrecedenceType getInitialPrecedence(String id) { |
| 418 | PrecedenceType ptBegin = lqnFactory.createPrecedenceType(); |
| 419 | SingleActivityListType saltPreBegin = lqnFactory |
| 420 | .createSingleActivityListType(); |
| 421 | ActivityType atPre = lqnFactory.createActivityType(); |
| 422 | atPre.setName(id); |
| 423 | saltPreBegin.setActivity(atPre); |
| 424 | ptBegin.setPre(saltPreBegin); |
| 425 | return ptBegin; |
| 426 | } |
| 427 | |
| 428 | |
| 429 | public PrecedenceType addBeginForkPrecedence(String id){ |
| 430 | EList<PrecedenceType> list = taskGraphStack.peek().getPrecedence(); |
| 431 | for (PrecedenceType precType : list) { |
| 432 | if (precType.getPre()!= null){ |
| 433 | if (precType.getPre().getActivity().getName().equals(id)) { |
| 434 | return precType; |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | PrecedenceType ptBegin = getInitialPrecedence(id); |
| 440 | |
| 441 | ActivityListType alt = lqnFactory.createActivityListType(); |
| 442 | ptBegin.setPostAND(alt); |
| 443 | taskGraphStack.peek().getPrecedence().add(ptBegin); |
| 444 | return ptBegin; |
| 445 | } |
| 446 | |
| 447 | public PrecedenceType addEndForkPrecedence(){ |
| 448 | PrecedenceType ptEnd = lqnFactory.createPrecedenceType(); |
| 449 | AndJoinListType ajlt = lqnFactory.createAndJoinListType(); |
| 450 | ptEnd.setPreAND(ajlt); |
| 451 | |
| 452 | SingleActivityListType saltPostEnd = lqnFactory.createSingleActivityListType(); |
| 453 | ActivityType atPostEnd = lqnFactory.createActivityType(); |
| 454 | saltPostEnd.setActivity(atPostEnd); |
| 455 | |
| 456 | ptEnd.setPost(saltPostEnd); |
| 457 | taskGraphStack.peek().getPrecedence().add(ptEnd); |
| 458 | return ptEnd; |
| 459 | } |
| 460 | |
| 461 | |
| 462 | public PrecedenceType addEndBranchPrecedence() { |
| 463 | PrecedenceType ptEnd = lqnFactory.createPrecedenceType(); |
| 464 | ActivityListType altPreEnd = lqnFactory.createActivityListType(); // merge |
| 465 | ptEnd.setPreOR(altPreEnd); |
| 466 | |
| 467 | SingleActivityListType saltPostEnd = lqnFactory |
| 468 | .createSingleActivityListType(); |
| 469 | ActivityType atPostEnd = lqnFactory.createActivityType(); |
| 470 | // atPostEnd.setName(successorId); |
| 471 | saltPostEnd.setActivity(atPostEnd); |
| 472 | |
| 473 | ptEnd.setPost(saltPostEnd); |
| 474 | taskGraphStack.peek().getPrecedence().add(ptEnd); |
| 475 | return ptEnd; |
| 476 | } |
| 477 | |
| 478 | public ActivityOrType addActivityOrType(String startId, String branchProb, |
| 479 | PrecedenceType pt) { |
| 480 | EList<ActivityOrType> list = pt.getPostOR().getActivity(); |
| 481 | for (ActivityOrType aot : list) { |
| 482 | if (aot.getName().equals(startId)) |
| 483 | return aot; |
| 484 | } |
| 485 | |
| 486 | ActivityOrType aot = lqnFactory.createActivityOrType(); |
| 487 | aot.setProb(branchProb); |
| 488 | aot.setName(startId); |
| 489 | pt.getPostOR().getActivity().add(aot); |
| 490 | return aot; |
| 491 | } |
| 492 | |
| 493 | public ActivityType addActivityType(String stopId, PrecedenceType pt) { |
| 494 | EList<ActivityType> list = pt.getPreOR().getActivity(); |
| 495 | for (ActivityType at : list) { |
| 496 | if (at.getName().equals(stopId)) |
| 497 | return at; |
| 498 | } |
| 499 | |
| 500 | ActivityType at = lqnFactory.createActivityType(); |
| 501 | at.setName(stopId); |
| 502 | pt.getPreOR().getActivity().add(at); |
| 503 | return at; |
| 504 | } |
| 505 | |
| 506 | public PhaseActivities addPhaseActivities(ActivityPhasesType apt) { |
| 507 | PhaseActivities pa = lqnFactory.createPhaseActivities(); |
| 508 | pa.getActivity().add(apt); |
| 509 | return pa; |
| 510 | } |
| 511 | |
| 512 | public ActivityPhasesType addActivityPhases(String id) { |
| 513 | ActivityPhasesType apt = lqnFactory.createActivityPhasesType(); |
| 514 | apt.setHostDemandMean("0.0"); |
| 515 | apt.setPhase(new BigInteger("1")); |
| 516 | apt.setName(id + "_Activity"); |
| 517 | return apt; |
| 518 | } |
| 519 | |
| 520 | public TaskType getTaskForProcessor(String processorId) { |
| 521 | EList<ProcessorType> list = lqnModel.getProcessor(); |
| 522 | for (ProcessorType pt : list) { |
| 523 | if (pt.getName().equals(processorId + "_Processor")) { |
| 524 | return pt.getTask().get(0); |
| 525 | } |
| 526 | } |
| 527 | logger.error("Could not find processor in Lqn Model."); |
| 528 | return null; |
| 529 | } |
| 530 | |
| 531 | public void finalizeLqnModel(PCMSolverWorkflowRunConfiguration config) { |
| 532 | |
| 533 | // Two branches may generated two precedences with exactly |
| 534 | // the same activities, which causes errors in the lqn solver. |
| 535 | // this one needs to be deleted. |
| 536 | // This can happen if this SEFF is called from multiple contexts. |
| 537 | // TODO: should we not be able to distinguish multiple contexts? |
| 538 | List<ProcessorType> processors = this.lqnModel.getProcessor(); |
| 539 | // We want to compare the precedences for all processors and all their |
| 540 | // activities |
| 541 | for (ProcessorType processorType : processors) { |
| 542 | if (processorType == null) |
| 543 | continue; |
| 544 | List<TaskType> tasks = processorType.getTask(); |
| 545 | for (TaskType taskType : tasks) { |
| 546 | if (taskType == null) |
| 547 | continue; |
| 548 | TaskActivityGraph taskActivityGraph = taskType |
| 549 | .getTaskActivities(); |
| 550 | if (taskActivityGraph == null) |
| 551 | continue; |
| 552 | List<PrecedenceType> precedences = taskActivityGraph |
| 553 | .getPrecedence(); |
| 554 | // Compare each precedence entry with each other. We only |
| 555 | // compare the pre entries. |
| 556 | for (int i = 0; i < precedences.size() - 1; i++) { |
| 557 | // using an temporary variable here for iteration to avoid |
| 558 | // concurrent modification exceptions. |
| 559 | // Size is checked again each time, so this should be safe. |
| 560 | PrecedenceType precedenceType = precedences.get(i); |
| 561 | // Only compare with the ones further down in the list, so |
| 562 | // we compare each pair just once. |
| 563 | for (ListIterator<PrecedenceType> iteratorToCompareTo = precedences |
| 564 | .listIterator(i + 1); iteratorToCompareTo.hasNext();) { |
| 565 | // using an iterator here to be able to directly remove |
| 566 | // a found duplicate element. |
| 567 | PrecedenceType precedenceTypeToCompareTo = (PrecedenceType) iteratorToCompareTo |
| 568 | .next(); |
| 569 | // logger.debug("comparing item "+i+" with item "+nextIndex); |
| 570 | if (duplicatePrecedence(precedenceType, |
| 571 | precedenceTypeToCompareTo)) { |
| 572 | iteratorToCompareTo.remove(); |
| 573 | // logger.warn("removed precedence with index "+nextIndex); |
| 574 | } |
| 575 | |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | SolverParamsType spt = LqnCoreFactory.eINSTANCE |
| 582 | .createSolverParamsType(); |
| 583 | |
| 584 | spt.setConvVal(config.getConvValue()); |
| 585 | |
| 586 | int itLimit = Integer.parseInt(config.getItLimit()); |
| 587 | spt.setItLimit(itLimit); |
| 588 | |
| 589 | int printInt = Integer.parseInt(config.getPrintInt()); |
| 590 | spt.setPrintInt(printInt); |
| 591 | |
| 592 | spt.setUnderrelaxCoeff(config.getUnderCoeff()); |
| 593 | |
| 594 | spt.setComment("Generated by PCM2LQN on " + new Date()); |
| 595 | |
| 596 | lqnModel.setSolverParams(spt); |
| 597 | |
| 598 | } |
| 599 | |
| 600 | /** |
| 601 | * Checks whether all pre entries of the precedence are different. |
| 602 | * |
| 603 | * @param precedenceType |
| 604 | * @param precedenceTypeToCompareTo |
| 605 | * @return |
| 606 | */ |
| 607 | private boolean duplicatePrecedence(PrecedenceType precedenceType, |
| 608 | PrecedenceType precedenceTypeToCompareTo) { |
| 609 | |
| 610 | boolean same = false; |
| 611 | |
| 612 | // first compare normal pre |
| 613 | if (precedenceType.getPre() != null |
| 614 | && precedenceTypeToCompareTo.getPre() != null |
| 615 | && precedenceType.getPre().getActivity() != null |
| 616 | && precedenceTypeToCompareTo.getPre().getActivity() != null) { |
| 617 | if (precedenceType.getPre().getActivity().getName().equals( |
| 618 | precedenceTypeToCompareTo.getPre().getActivity().getName())) { |
| 619 | same = true; |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | // compare pre-AND |
| 624 | if (precedenceType.getPreAND() != null |
| 625 | && precedenceTypeToCompareTo.getPreAND() != null) { |
| 626 | List<ActivityType> activityList = precedenceType.getPreAND() |
| 627 | .getActivity(); |
| 628 | List<ActivityType> activityListToCompareTo = precedenceTypeToCompareTo |
| 629 | .getPreAND().getActivity(); |
| 630 | if (listContainsSameActivities(activityList, |
| 631 | activityListToCompareTo)) |
| 632 | same = true; |
| 633 | } |
| 634 | |
| 635 | // compare pre-OR |
| 636 | if (precedenceType.getPreOR() != null |
| 637 | && precedenceTypeToCompareTo.getPreOR() != null) { |
| 638 | List<ActivityType> activityList = precedenceType.getPreOR() |
| 639 | .getActivity(); |
| 640 | List<ActivityType> activityListToCompareTo = precedenceTypeToCompareTo |
| 641 | .getPreOR().getActivity(); |
| 642 | if (listContainsSameActivities(activityList, |
| 643 | activityListToCompareTo)) |
| 644 | same = true; |
| 645 | } |
| 646 | return same; |
| 647 | } |
| 648 | |
| 649 | private boolean listContainsSameActivities(List<ActivityType> activityList, |
| 650 | List<ActivityType> activityListToCompareTo) { |
| 651 | if (activityList == null || activityListToCompareTo == null |
| 652 | || activityList.size() == 0 |
| 653 | || activityListToCompareTo.size() != activityList.size()) { |
| 654 | return false; |
| 655 | } |
| 656 | for (int i = 0; i < activityList.size(); i++) { |
| 657 | ActivityType activity1 = activityList.get(i); |
| 658 | ActivityType activity2 = activityListToCompareTo.get(i); |
| 659 | if (!activity1.getName().equals(activity2.getName())) { |
| 660 | return false; |
| 661 | } |
| 662 | } |
| 663 | return true; |
| 664 | } |
| 665 | |
| 666 | public void restoreFormerTaskActivityGraph() { |
| 667 | taskGraphStack.pop(); |
| 668 | } |
| 669 | |
| 670 | public LqnModelType getLqnModel() { |
| 671 | return lqnModel; |
| 672 | } |
| 673 | |
| 674 | public ProcessorType getProcessor(String processorId) { |
| 675 | EList<ProcessorType> list = lqnModel.getProcessor(); |
| 676 | for (ProcessorType pt : list) { |
| 677 | if (pt.getName().equals(processorId + "_Processor")) { |
| 678 | return pt; |
| 679 | } |
| 680 | } |
| 681 | return null; |
| 682 | } |
| 683 | |
| 684 | public boolean isLQSimAnalysis() { |
| 685 | return this.isLQSimAnalysis; |
| 686 | } |
| 687 | |
| 688 | public void setIsLQSimAnalysis(boolean isQLSim){ |
| 689 | this.isLQSimAnalysis = isQLSim; |
| 690 | Pcm2LqnHelper.shortenIds = this.isLQSimAnalysis; |
| 691 | } |
| 692 | |
| 693 | public void addActivityToPostAnd(String startId, PrecedenceType ptBegin) { |
| 694 | ActivityType at = lqnFactory.createActivityType(); |
| 695 | at.setName(startId); |
| 696 | ptBegin.getPostAND().getActivity().add(at); |
| 697 | } |
| 698 | |
| 699 | public void addActivityToPreAnd(String stopId, PrecedenceType ptEnd) { |
| 700 | ActivityType at = lqnFactory.createActivityType(); |
| 701 | at.setName(stopId); |
| 702 | ptEnd.getPreAND().getActivity().add(at); |
| 703 | } |
| 704 | |
| 705 | public void setPoolCapacity(Integer poolCapacity) { |
| 706 | TaskActivityGraph tag = taskGraphStack.peek(); |
| 707 | TaskType task = (TaskType)tag.eContainer(); |
| 708 | task.setMultiplicity(new BigInteger(poolCapacity.toString())); |
| 709 | task.setScheduling(TaskSchedulingType.FCFS); |
| 710 | } |
| 711 | |
| 712 | } |
| 713 | |
| 714 | enum CallType {SYNCH, ASYNCH} |