| Total Complexity | 110 |
| Total Lines | 1263 |
| Duplicated Lines | 30.25 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like it.cnr.istc.pst.platinum.ai.executive.pdb.ExecutivePlanDataBase often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | package it.cnr.istc.pst.platinum.ai.executive.pdb; |
||
| 62 | @TemporalFacadeConfiguration( |
||
| 63 | |||
| 64 | // temporal network |
||
| 65 | network = TemporalNetworkType.STNU, |
||
| 66 | |||
| 67 | // temporal solver |
||
| 68 | solver = TemporalSolverType.APSP |
||
| 69 | ) |
||
| 70 | public class ExecutivePlanDataBase extends FrameworkObject |
||
| 71 | { |
||
| 72 | private PlanProtocolDescriptor plan; // the plan to execute |
||
| 73 | |||
| 74 | @TemporalFacadePlaceholder |
||
| 75 | protected TemporalFacade facade; // temporal data base |
||
| 76 | |||
| 77 | // plan locks |
||
| 78 | private final Object[] locks; |
||
| 79 | |||
| 80 | // plan's nodes |
||
| 81 | protected Map<ExecutionNodeStatus, List<ExecutionNode>> nodes; |
||
| 82 | |||
| 83 | // execution dependency graphs |
||
| 84 | protected Map<ExecutionNode, Map<ExecutionNode, ExecutionNodeStatus[]>> sdg; // start dependency graph - array of disjunctive start conditions |
||
| 85 | protected Map<ExecutionNode, Map<ExecutionNode, ExecutionNodeStatus[]>> edg; // end dependency graph - array of disjunctive end conditions |
||
| 86 | protected Map<ExecutionNode, Map<ExecutionNode, ExecutionNodeStatus[]>> stop; // stop dependency graph - array of disjunctive end conditions |
||
| 87 | |||
| 88 | /** |
||
| 89 | * |
||
| 90 | * @param origin |
||
| 91 | * @param horizon |
||
| 92 | */ |
||
| 93 | protected ExecutivePlanDataBase() { |
||
| 94 | super(); |
||
| 95 | |||
| 96 | // set array of locks |
||
| 97 | this.locks = new Object[ExecutionNodeStatus.values().length]; |
||
| 98 | // add locks |
||
| 99 | for (ExecutionNodeStatus s : ExecutionNodeStatus.values()) { |
||
| 100 | // set lock |
||
| 101 | this.locks[s.getIndex()] = new Object(); |
||
| 102 | } |
||
| 103 | |||
| 104 | // set data structures |
||
| 105 | this.nodes = new HashMap<>(); |
||
|
|
|||
| 106 | for (ExecutionNodeStatus s : ExecutionNodeStatus.values()) { |
||
| 107 | // set the index of execution nodes |
||
| 108 | this.nodes.put(s, new LinkedList<ExecutionNode>()); |
||
| 109 | } |
||
| 110 | |||
| 111 | // set the dependency graph |
||
| 112 | this.sdg = new HashMap<>(); |
||
| 113 | this.edg = new HashMap<>(); |
||
| 114 | this.stop = new HashMap<>(); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * |
||
| 119 | * @return |
||
| 120 | */ |
||
| 121 | public long getOrigin() { |
||
| 122 | return this.facade.getOrigin(); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * |
||
| 127 | * @return |
||
| 128 | */ |
||
| 129 | public long getHorizon() { |
||
| 130 | return this.facade.getHorizon(); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * |
||
| 135 | * @return |
||
| 136 | */ |
||
| 137 | public String export() throws IOException { |
||
| 138 | |||
| 139 | // prepare file |
||
| 140 | //System.out.println("Prepare file plans/exported/plan.txt... "); |
||
| 141 | File output = new File("plans/exported/plan.txt"); |
||
| 142 | // export current plan (if any) to a known file |
||
| 143 | if (this.plan != null) |
||
| 144 | { |
||
| 145 | // get plan encoding |
||
| 146 | String encoding = this.plan.export(); |
||
| 147 | // write to a file |
||
| 148 | System.out.println("Writing file:\n"); |
||
| 149 | try (BufferedWriter writer = new BufferedWriter(new FileWriter(output))) { |
||
| 150 | // get data |
||
| 151 | //System.out.println(encoding +"\n"); |
||
| 152 | writer.write(encoding); |
||
| 153 | //System.out.println("Done!"); |
||
| 154 | writer.close(); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | // get absolute file |
||
| 159 | //System.out.println(output.getAbsolutePath()); |
||
| 160 | return output.getAbsolutePath(); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * |
||
| 165 | * @return |
||
| 166 | */ |
||
| 167 | public String getName() { |
||
| 168 | return this.plan.getName(); |
||
| 169 | } |
||
| 170 | |||
| 171 | |||
| 172 | /** |
||
| 173 | * |
||
| 174 | * @param plan |
||
| 175 | */ |
||
| 176 | public void setup(PlanProtocolDescriptor plan) { |
||
| 177 | |||
| 178 | try { |
||
| 179 | |||
| 180 | // get plan descriptor |
||
| 181 | this.plan = plan; |
||
| 182 | // map token descriptor to nodes |
||
| 183 | Map<TokenProtocolDescriptor, ExecutionNode> dictionary = new HashMap<>(); |
||
| 184 | // check time-lines |
||
| 185 | View Code Duplication | for (TimelineProtocolDescriptor tl : plan.getTimelines()) { |
|
| 186 | |||
| 187 | // setup node arrays |
||
| 188 | ExecutionNode[] list = new ExecutionNode[tl.getTokens().size()]; |
||
| 189 | // list index |
||
| 190 | int counter = 0; |
||
| 191 | // create an execution node for each token |
||
| 192 | for (TokenProtocolDescriptor token : tl.getTokens()) { |
||
| 193 | // check predicate |
||
| 194 | if (!token.getPredicate().toLowerCase().equals("unallocated")) { |
||
| 195 | |||
| 196 | // get token's bound |
||
| 197 | long[] start = token.getStartTimeBounds(); |
||
| 198 | long[] end = token.getEndTimeBounds(); |
||
| 199 | long[] duration = token.getDurationBounds(); |
||
| 200 | |||
| 201 | // set default controllability type |
||
| 202 | ControllabilityType controllability = null; |
||
| 203 | // check specific type |
||
| 204 | if (tl.isExternal()) { |
||
| 205 | // uncontrollable |
||
| 206 | controllability = ControllabilityType.UNCONTROLLABLE; |
||
| 207 | |||
| 208 | } else if (token.getPredicate().startsWith("_")) { |
||
| 209 | // partially controllable token |
||
| 210 | controllability = ControllabilityType.PARTIALLY_CONTROLLABLE; |
||
| 211 | |||
| 212 | } else { |
||
| 213 | // controllable |
||
| 214 | controllability = ControllabilityType.CONTROLLABLE; |
||
| 215 | } |
||
| 216 | |||
| 217 | // set parameter information |
||
| 218 | String signature = token.getPredicate(); |
||
| 219 | String[] paramValues = new String[token.getParameters().size()]; |
||
| 220 | ParameterType[] paramTypes = new ParameterType[token.getParameters().size()]; |
||
| 221 | for (int index = 0; index < token.getParameters().size(); index++) { |
||
| 222 | |||
| 223 | // get parameter |
||
| 224 | ParameterDescriptor param= token.getParameter(index); |
||
| 225 | // check type |
||
| 226 | if (param.getType().equals(ParameterTypeDescriptor.NUMERIC)) |
||
| 227 | { |
||
| 228 | // set type |
||
| 229 | paramTypes[index] = ParameterType.NUMERIC_PARAMETER_TYPE; |
||
| 230 | // set value |
||
| 231 | paramValues[index] = Long.toString(param.getBounds()[0]); |
||
| 232 | } |
||
| 233 | else |
||
| 234 | { |
||
| 235 | // enumeration |
||
| 236 | paramTypes[index] = ParameterType.ENUMERATION_PARAMETER_TYPE; |
||
| 237 | // set value |
||
| 238 | paramValues[index] = param.getValues()[0]; |
||
| 239 | } |
||
| 240 | } |
||
| 241 | |||
| 242 | // create a node |
||
| 243 | ExecutionNode node = this.createNode(tl.getComponent(), tl.getName(), |
||
| 244 | signature, paramTypes, paramValues, |
||
| 245 | start, end, duration, controllability, token.getStartExecutionState()); |
||
| 246 | |||
| 247 | // add node |
||
| 248 | this.addNode(node); |
||
| 249 | // add entry to the dictionary |
||
| 250 | dictionary.put(token, node); |
||
| 251 | |||
| 252 | // add node to list |
||
| 253 | list[counter] = node; |
||
| 254 | counter++; |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | // link subsequent nodes |
||
| 259 | if (list.length > 1) { |
||
| 260 | for (int pos = 0; pos < list.length; pos++) { |
||
| 261 | |||
| 262 | // check first node |
||
| 263 | if (pos == 0) { |
||
| 264 | |||
| 265 | // first node of the timeline |
||
| 266 | ExecutionNode first = list[pos]; |
||
| 267 | // set next node |
||
| 268 | first.setNext(list[pos+1]); |
||
| 269 | |||
| 270 | } else if (pos == list.length - 1) { |
||
| 271 | |||
| 272 | // last node of the timeline |
||
| 273 | ExecutionNode last = list[pos]; |
||
| 274 | // set previous node |
||
| 275 | last.setPrev(list[pos-1]); |
||
| 276 | |||
| 277 | } else { |
||
| 278 | |||
| 279 | // intermediate node |
||
| 280 | ExecutionNode i = list[pos]; |
||
| 281 | // set previous |
||
| 282 | i.setPrev(list[pos-1]); |
||
| 283 | // set next |
||
| 284 | i.setNext(list[pos+1]); |
||
| 285 | } |
||
| 286 | } |
||
| 287 | } |
||
| 288 | } |
||
| 289 | |||
| 290 | // check observations |
||
| 291 | View Code Duplication | for (TimelineProtocolDescriptor tl : plan.getObservations()) { |
|
| 292 | |||
| 293 | // setup node arrays |
||
| 294 | ExecutionNode[] list = new ExecutionNode[tl.getTokens().size()]; |
||
| 295 | // list index |
||
| 296 | int counter = 0; |
||
| 297 | // create an execution node for each token |
||
| 298 | for (TokenProtocolDescriptor token : tl.getTokens()) { |
||
| 299 | // check predicate |
||
| 300 | if (!token.getPredicate().toLowerCase().equals("unallocated")) { |
||
| 301 | |||
| 302 | // get token's bound |
||
| 303 | long[] start = token.getStartTimeBounds(); |
||
| 304 | long[] end = token.getEndTimeBounds(); |
||
| 305 | long[] duration = token.getDurationBounds(); |
||
| 306 | |||
| 307 | // check controllability type |
||
| 308 | // set default controllability type |
||
| 309 | ControllabilityType controllability = null; |
||
| 310 | // check specific type |
||
| 311 | if (tl.isExternal()) { |
||
| 312 | |||
| 313 | // uncontrollable |
||
| 314 | controllability = ControllabilityType.UNCONTROLLABLE; |
||
| 315 | |||
| 316 | } else if (token.getPredicate().startsWith("_")) { |
||
| 317 | |||
| 318 | // partially controllable token |
||
| 319 | controllability = ControllabilityType.PARTIALLY_CONTROLLABLE; |
||
| 320 | |||
| 321 | } else { |
||
| 322 | |||
| 323 | // controllable token |
||
| 324 | controllability = ControllabilityType.CONTROLLABLE; |
||
| 325 | } |
||
| 326 | |||
| 327 | // set parameter information |
||
| 328 | String signature = token.getPredicate(); |
||
| 329 | String[] paramValues = new String[token.getParameters().size()]; |
||
| 330 | ParameterType[] paramTypes = new ParameterType[token.getParameters().size()]; |
||
| 331 | for (int index = 0; index < token.getParameters().size(); index++) { |
||
| 332 | |||
| 333 | // get parameter |
||
| 334 | ParameterDescriptor param= token.getParameter(index); |
||
| 335 | // check type |
||
| 336 | if (param.getType().equals(ParameterTypeDescriptor.NUMERIC)) { |
||
| 337 | // set t ype |
||
| 338 | paramTypes[index] = ParameterType.NUMERIC_PARAMETER_TYPE; |
||
| 339 | // set value |
||
| 340 | paramValues[index] = Long.toString(param.getBounds()[0]); |
||
| 341 | |||
| 342 | } else { |
||
| 343 | |||
| 344 | // enumeration |
||
| 345 | paramTypes[index] = ParameterType.ENUMERATION_PARAMETER_TYPE; |
||
| 346 | // set value |
||
| 347 | paramValues[index] = param.getValues()[0]; |
||
| 348 | } |
||
| 349 | } |
||
| 350 | |||
| 351 | // create a node |
||
| 352 | ExecutionNode node = this.createNode(tl.getComponent(), tl.getName(), |
||
| 353 | signature, paramTypes, paramValues, |
||
| 354 | start, end, duration, controllability, token.getStartExecutionState()); |
||
| 355 | |||
| 356 | // add node |
||
| 357 | this.addNode(node); |
||
| 358 | // add entry to the dictionary |
||
| 359 | dictionary.put(token, node); |
||
| 360 | |||
| 361 | // add node to list |
||
| 362 | list[counter] = node; |
||
| 363 | counter++; |
||
| 364 | } |
||
| 365 | |||
| 366 | } |
||
| 367 | |||
| 368 | // link subsequent nodes |
||
| 369 | for (int pos = 0; pos < list.length; pos++) { |
||
| 370 | |||
| 371 | // check first node |
||
| 372 | if (pos == 0) { |
||
| 373 | |||
| 374 | // first node of the timeline |
||
| 375 | ExecutionNode first = list[pos]; |
||
| 376 | // set next node |
||
| 377 | first.setNext(list[pos+1]); |
||
| 378 | |||
| 379 | } else if (pos == list.length - 1) { |
||
| 380 | // last node of the timeline |
||
| 381 | ExecutionNode last = list[pos]; |
||
| 382 | // set previous ndoe |
||
| 383 | last.setPrev(list[pos-1]); |
||
| 384 | |||
| 385 | } else { |
||
| 386 | |||
| 387 | // intermediate node |
||
| 388 | ExecutionNode i = list[pos]; |
||
| 389 | // set prev |
||
| 390 | i.setPrev(list[pos-1]); |
||
| 391 | // set next |
||
| 392 | i.setNext(list[pos+1]); |
||
| 393 | } |
||
| 394 | } |
||
| 395 | } |
||
| 396 | |||
| 397 | // check relations |
||
| 398 | for (RelationProtocolDescriptor rel : plan.getRelations()) { |
||
| 399 | try { |
||
| 400 | |||
| 401 | // get related nodes |
||
| 402 | ExecutionNode reference = dictionary.get(rel.getFrom()); |
||
| 403 | ExecutionNode target = dictionary.get(rel.getTo()); |
||
| 404 | // add temporal constraints and related execution dependencies |
||
| 405 | this.createConstraintsAndDependencies(reference, target, rel); |
||
| 406 | |||
| 407 | } catch (Exception ex) { |
||
| 408 | |||
| 409 | // exception |
||
| 410 | throw new TemporalConsistencyException("Error while propagating plan's relation " + rel + "\n" + ex.getMessage()); |
||
| 411 | } |
||
| 412 | } |
||
| 413 | |||
| 414 | // check consistency |
||
| 415 | this.facade.verifyTemporalConsistency(); |
||
| 416 | // check the schedule for all temporal intervals |
||
| 417 | for (ExecutionNode node : dictionary.values()) { |
||
| 418 | // check node schedule |
||
| 419 | IntervalScheduleQuery query = this.facade.createTemporalQuery(TemporalQueryType.INTERVAL_SCHEDULE); |
||
| 420 | query.setInterval(node.getInterval()); |
||
| 421 | this.facade.process(query); |
||
| 422 | } |
||
| 423 | |||
| 424 | // prepare log message |
||
| 425 | String msg = ""; |
||
| 426 | // print execution dependency graph (for debug only) |
||
| 427 | for (ExecutionNodeStatus status : this.nodes.keySet()) { |
||
| 428 | // get nodes by status |
||
| 429 | for (ExecutionNode node : this.nodes.get(status)) { |
||
| 430 | |||
| 431 | // print node and the related execution conditions |
||
| 432 | msg += "Execution node " + node + "\n"; |
||
| 433 | msg += "\tNode execution starting conditions:\n"; |
||
| 434 | Map<ExecutionNode, ExecutionNodeStatus[]> dependencies = this.getNodeStartDependencies(node); |
||
| 435 | for (ExecutionNode dep : dependencies.keySet()) { |
||
| 436 | msg += "\t\tCan start if -> " + dep.getId() + ":"+ dep.getGroundSignature() + " is in " + dependencies.get(dep) + "\n"; |
||
| 437 | } |
||
| 438 | |||
| 439 | // get end conditions |
||
| 440 | dependencies = this.getNodeEndDependencies(node); |
||
| 441 | msg += "\tNode execution ending conditions:\n"; |
||
| 442 | for (ExecutionNode dep : dependencies.keySet()) { |
||
| 443 | msg += "\t\tCan end if -> " + dep.getId() + ":" + dep.getGroundSignature() + " is in " + dependencies.get(dep) + "\n"; |
||
| 444 | } |
||
| 445 | } |
||
| 446 | } |
||
| 447 | |||
| 448 | // print log message |
||
| 449 | debug(msg); |
||
| 450 | |||
| 451 | } catch (TemporalIntervalCreationException ex) { |
||
| 452 | throw new RuntimeException(ex.getMessage()); |
||
| 453 | |||
| 454 | } catch (TemporalConsistencyException ex) { |
||
| 455 | throw new RuntimeException(ex.getMessage()); |
||
| 456 | } |
||
| 457 | } |
||
| 458 | |||
| 459 | |||
| 460 | |||
| 461 | /** |
||
| 462 | * |
||
| 463 | * @param reference |
||
| 464 | * @param target |
||
| 465 | * @param rel |
||
| 466 | * @throws Exception |
||
| 467 | */ |
||
| 468 | protected void createConstraintsAndDependencies(ExecutionNode reference, ExecutionNode target, Relation rel) |
||
| 469 | throws Exception { |
||
| 470 | |||
| 471 | // check temporal category |
||
| 472 | if (rel.getCategory().equals(ConstraintCategory.TEMPORAL_CONSTRAINT)) { |
||
| 473 | // check relation |
||
| 474 | switch (rel.getType()) { |
||
| 475 | |||
| 476 | // meets temporal relation |
||
| 477 | case MEETS : { |
||
| 478 | // get meets relation |
||
| 479 | MeetsRelation meets = (MeetsRelation) rel; |
||
| 480 | // prepare meets constraint |
||
| 481 | this.prepareMeetsTemporalConstraint(reference, target, meets.getBounds()); |
||
| 482 | } |
||
| 483 | break; |
||
| 484 | |||
| 485 | // before temporal relation |
||
| 486 | case BEFORE : { |
||
| 487 | // get before relation |
||
| 488 | BeforeRelation before = (BeforeRelation) rel; |
||
| 489 | // prepare before constraint |
||
| 490 | this.prepareBeforeTemporalConstraint(reference, target, before.getBounds()); |
||
| 491 | } |
||
| 492 | break; |
||
| 493 | |||
| 494 | case MET_BY : { |
||
| 495 | // get met-by relation |
||
| 496 | MetByRelation metby = (MetByRelation) rel; |
||
| 497 | this.prepareAfterTemporalConstraint(reference, target, metby.getBounds()); |
||
| 498 | } |
||
| 499 | break; |
||
| 500 | |||
| 501 | case AFTER : { |
||
| 502 | // get after relation |
||
| 503 | AfterRelation after = (AfterRelation) rel; |
||
| 504 | // prepare after constraint |
||
| 505 | this.prepareAfterTemporalConstraint(reference, target, after.getBounds()); |
||
| 506 | } |
||
| 507 | break; |
||
| 508 | |||
| 509 | case CONTAINS : { |
||
| 510 | // get contains relation |
||
| 511 | ContainsRelation contains = (ContainsRelation) rel; |
||
| 512 | // prepare contains constraint |
||
| 513 | this.prepareContainsTemporalConstraint(reference, target, contains.getBounds()); |
||
| 514 | } |
||
| 515 | break; |
||
| 516 | |||
| 517 | case DURING : { |
||
| 518 | // get during relation |
||
| 519 | DuringRelation during = (DuringRelation) rel; |
||
| 520 | // prepare during constraint |
||
| 521 | this.prepareDuringTemporalConstraint(reference, target, during.getBounds()); |
||
| 522 | } |
||
| 523 | break; |
||
| 524 | |||
| 525 | case EQUALS : { |
||
| 526 | // get equals relation |
||
| 527 | EqualsRelation equals = (EqualsRelation) rel; |
||
| 528 | // prepare equals constraint |
||
| 529 | this.prepareEqualsTemporalConstraint(reference, target, equals.getBounds()); |
||
| 530 | } |
||
| 531 | break; |
||
| 532 | |||
| 533 | case STARTS_DURING : { |
||
| 534 | // get starts-during relation |
||
| 535 | StartsDuringRelation sduring = (StartsDuringRelation) rel; |
||
| 536 | // prepare starts-during constraint |
||
| 537 | this.prepareStartsDuringTemporalConstraint(reference, target, sduring.getBounds()); |
||
| 538 | } |
||
| 539 | break; |
||
| 540 | |||
| 541 | case ENDS_DURING : { |
||
| 542 | // get ends-during relation |
||
| 543 | EndsDuringRelation eduring = (EndsDuringRelation) rel; |
||
| 544 | // prepare ends-during constraint |
||
| 545 | this.prepareEndsDuringTemporalConstraint(reference, target, eduring.getBounds()); |
||
| 546 | } |
||
| 547 | break; |
||
| 548 | |||
| 549 | default : { |
||
| 550 | throw new RuntimeException("Unknown relation " + rel.getType()); |
||
| 551 | } |
||
| 552 | |||
| 553 | } |
||
| 554 | } |
||
| 555 | } |
||
| 556 | |||
| 557 | /** |
||
| 558 | * |
||
| 559 | * @param reference |
||
| 560 | * @param target |
||
| 561 | * @param rel |
||
| 562 | */ |
||
| 563 | protected void createConstraintsAndDependencies(ExecutionNode reference, ExecutionNode target, RelationProtocolDescriptor rel) |
||
| 564 | throws Exception { |
||
| 565 | |||
| 566 | // check relation type |
||
| 567 | switch (rel.getType()) { |
||
| 568 | |||
| 569 | // meets temporal relation |
||
| 570 | case "meets" : { |
||
| 571 | // prepare meets constraint |
||
| 572 | this.prepareMeetsTemporalConstraint(reference, target, new long[][] { |
||
| 573 | {0, 0} |
||
| 574 | }); |
||
| 575 | } |
||
| 576 | break; |
||
| 577 | |||
| 578 | case "before" : { |
||
| 579 | // prepare before constraint |
||
| 580 | this.prepareBeforeTemporalConstraint(reference, target, new long[][] { |
||
| 581 | rel.getFirstBound() |
||
| 582 | }); |
||
| 583 | } |
||
| 584 | break; |
||
| 585 | |||
| 586 | case "met-by" : { |
||
| 587 | // prepare after constraint |
||
| 588 | this.prepareMeetsTemporalConstraint(target, reference, new long[][] { |
||
| 589 | {0, 0} |
||
| 590 | }); |
||
| 591 | } |
||
| 592 | break; |
||
| 593 | |||
| 594 | case "after" : { |
||
| 595 | // prepare after constraint |
||
| 596 | this.prepareAfterTemporalConstraint(reference, target, new long[][] { |
||
| 597 | rel.getFirstBound() |
||
| 598 | }); |
||
| 599 | } |
||
| 600 | break; |
||
| 601 | |||
| 602 | case "during" : { |
||
| 603 | // prepare during constraint |
||
| 604 | this.prepareDuringTemporalConstraint(reference, target, new long[][] { |
||
| 605 | rel.getFirstBound(), |
||
| 606 | rel.getSecondBound() |
||
| 607 | }); |
||
| 608 | } |
||
| 609 | break; |
||
| 610 | |||
| 611 | case "contains" : { |
||
| 612 | // prepare contains constraint |
||
| 613 | this.prepareContainsTemporalConstraint(reference, target, new long[][] { |
||
| 614 | rel.getFirstBound(), |
||
| 615 | rel.getSecondBound() |
||
| 616 | }); |
||
| 617 | } |
||
| 618 | break; |
||
| 619 | |||
| 620 | case "equals" : { |
||
| 621 | // prepare equals constraint |
||
| 622 | this.prepareEqualsTemporalConstraint(reference, target, new long[][] { |
||
| 623 | {0, 0}, |
||
| 624 | {0, 0} |
||
| 625 | }); |
||
| 626 | } |
||
| 627 | break; |
||
| 628 | |||
| 629 | case "starts_during" : { |
||
| 630 | // prepare starts-during constraint |
||
| 631 | this.prepareStartsDuringTemporalConstraint(reference, target, new long[][] { |
||
| 632 | rel.getFirstBound(), |
||
| 633 | rel.getSecondBound() |
||
| 634 | }); |
||
| 635 | } |
||
| 636 | break; |
||
| 637 | |||
| 638 | case "ends_during" : { |
||
| 639 | // prepare ends-during constraint |
||
| 640 | this.prepareEndsDuringTemporalConstraint(reference, target, new long[][] { |
||
| 641 | rel.getFirstBound(), |
||
| 642 | rel.getSecondBound() |
||
| 643 | }); |
||
| 644 | } |
||
| 645 | break; |
||
| 646 | |||
| 647 | default : { |
||
| 648 | // unknown temporal relation |
||
| 649 | throw new RuntimeException("Unknown relation " + rel.getType()); |
||
| 650 | } |
||
| 651 | } |
||
| 652 | } |
||
| 653 | |||
| 654 | |||
| 655 | /** |
||
| 656 | * |
||
| 657 | * @return |
||
| 658 | */ |
||
| 659 | public List<ExecutionNode> getNodesByStatus(ExecutionNodeStatus status) { |
||
| 660 | |||
| 661 | // list of nodes |
||
| 662 | List<ExecutionNode> list = new LinkedList<>(); |
||
| 663 | synchronized (this.locks[status.getIndex()]) { |
||
| 664 | // get all nodes with the desired status |
||
| 665 | list.addAll(this.nodes.get(status)); |
||
| 666 | } |
||
| 667 | |||
| 668 | // get the list of node with the desired status |
||
| 669 | return list; |
||
| 670 | } |
||
| 671 | |||
| 672 | /** |
||
| 673 | * |
||
| 674 | * @param node |
||
| 675 | * @param status |
||
| 676 | */ |
||
| 677 | public synchronized void updateNodeStatus(ExecutionNode node, ExecutionNodeStatus status) { |
||
| 678 | |||
| 679 | // remove node from the current status |
||
| 680 | synchronized (this.locks[node.getStatus().getIndex()]) { |
||
| 681 | // remove node from list |
||
| 682 | this.nodes.get(node.getStatus()).remove(node); |
||
| 683 | } |
||
| 684 | |||
| 685 | // add node to the new status |
||
| 686 | synchronized (this.locks[status.getIndex()]) { |
||
| 687 | // add node |
||
| 688 | this.nodes.get(status).add(node); |
||
| 689 | // update status |
||
| 690 | node.setStatus(status); |
||
| 691 | } |
||
| 692 | } |
||
| 693 | |||
| 694 | /** |
||
| 695 | * |
||
| 696 | * @param node |
||
| 697 | * @return |
||
| 698 | */ |
||
| 699 | public Map<ExecutionNode, ExecutionNodeStatus[]> getNodeStartDependencies(ExecutionNode node) { |
||
| 700 | return new HashMap<>(this.sdg.get(node)); |
||
| 701 | } |
||
| 702 | |||
| 703 | /** |
||
| 704 | * |
||
| 705 | * @param node |
||
| 706 | * @return |
||
| 707 | */ |
||
| 708 | public Map<ExecutionNode, ExecutionNodeStatus[]> getNodeEndDependencies(ExecutionNode node) { |
||
| 709 | return new HashMap<>(this.edg.get(node)); |
||
| 710 | } |
||
| 711 | |||
| 712 | /** |
||
| 713 | * |
||
| 714 | * @param node |
||
| 715 | * @return |
||
| 716 | */ |
||
| 717 | public Map<ExecutionNode, ExecutionNodeStatus[]> getNodeStopDependencies(ExecutionNode node) { |
||
| 718 | return new HashMap<>(this.stop.get(node)); |
||
| 719 | } |
||
| 720 | |||
| 721 | |||
| 722 | /** |
||
| 723 | * |
||
| 724 | * @param node |
||
| 725 | */ |
||
| 726 | public void checkSchedule(ExecutionNode node) { |
||
| 727 | |||
| 728 | // check resulting schedule of the interval |
||
| 729 | IntervalScheduleQuery query = this.facade. |
||
| 730 | createTemporalQuery(TemporalQueryType.INTERVAL_SCHEDULE); |
||
| 731 | query.setInterval(node.getInterval()); |
||
| 732 | this.facade.process(query); |
||
| 733 | } |
||
| 734 | |||
| 735 | /** |
||
| 736 | * |
||
| 737 | * @param node |
||
| 738 | */ |
||
| 739 | public void addNode(ExecutionNode node) { |
||
| 740 | |||
| 741 | // check expected initial execution state |
||
| 742 | ExecutionNodeStatus initial = node.getStartExecutionState(); |
||
| 743 | node.setStatus(initial); |
||
| 744 | this.nodes.get(initial).add(node); |
||
| 745 | // setup dependency graph data structures |
||
| 746 | this.sdg.put(node, new HashMap<ExecutionNode, ExecutionNodeStatus[]>()); |
||
| 747 | this.edg.put(node, new HashMap<ExecutionNode, ExecutionNodeStatus[]>()); |
||
| 748 | this.stop.put(node, new HashMap<ExecutionNode, ExecutionNodeStatus[]>()); |
||
| 749 | } |
||
| 750 | |||
| 751 | /** |
||
| 752 | * |
||
| 753 | */ |
||
| 754 | public void clearExecutedNodes() { |
||
| 755 | |||
| 756 | // check executed nodes |
||
| 757 | Set<ExecutionNode> nodes = new HashSet<>(this.nodes.get(ExecutionNodeStatus.EXECUTED)); |
||
| 758 | // clear nodes |
||
| 759 | this.nodes.get(ExecutionNodeStatus.EXECUTED).clear(); |
||
| 760 | // clear execution graph |
||
| 761 | for (ExecutionNode node : nodes) { |
||
| 762 | // clear map entries |
||
| 763 | this.sdg.remove(node); |
||
| 764 | this.edg.remove(node); |
||
| 765 | this.stop.remove(node); |
||
| 766 | |||
| 767 | // clear edges |
||
| 768 | for (ExecutionNode n : this.sdg.keySet()) { |
||
| 769 | // remove edge |
||
| 770 | this.sdg.get(n).remove(node); |
||
| 771 | |||
| 772 | } |
||
| 773 | |||
| 774 | // clear edges |
||
| 775 | for (ExecutionNode n : this.edg.keySet()) { |
||
| 776 | // remove edge |
||
| 777 | this.edg.get(n).remove(node); |
||
| 778 | } |
||
| 779 | |||
| 780 | // clear edges |
||
| 781 | for (ExecutionNode n : this.stop.keySet()) { |
||
| 782 | // remove edge |
||
| 783 | this.stop.get(n).remove(node); |
||
| 784 | } |
||
| 785 | } |
||
| 786 | } |
||
| 787 | |||
| 788 | /** |
||
| 789 | * |
||
| 790 | * @param component |
||
| 791 | * @param timeline |
||
| 792 | * @param signature |
||
| 793 | * @param pTypes |
||
| 794 | * @param pValues |
||
| 795 | * @param start |
||
| 796 | * @param end |
||
| 797 | * @param duration |
||
| 798 | * @param controllability |
||
| 799 | * @param state |
||
| 800 | * @return |
||
| 801 | * @throws TemporalIntervalCreationException |
||
| 802 | */ |
||
| 803 | protected ExecutionNode createNode(String component, String timeline, |
||
| 804 | String signature, ParameterType[] pTypes, String[] pValues, |
||
| 805 | long[] start, long[] end, long[] duration, |
||
| 806 | ControllabilityType controllability, |
||
| 807 | ExecutionNodeStatus state) |
||
| 808 | throws TemporalIntervalCreationException { |
||
| 809 | |||
| 810 | // create temporal interval - consider all intervals as controllable during execution |
||
| 811 | TemporalInterval interval = this.facade.createTemporalInterval(start, end, duration, true); |
||
| 812 | // create predicate |
||
| 813 | NodePredicate predicate = new NodePredicate(component, timeline, signature, pTypes, pValues); |
||
| 814 | // create execution node |
||
| 815 | return new ExecutionNode(predicate, interval, controllability, state); |
||
| 816 | } |
||
| 817 | |||
| 818 | /** |
||
| 819 | * |
||
| 820 | * @param node |
||
| 821 | * @param dependency |
||
| 822 | * @param condition |
||
| 823 | */ |
||
| 824 | protected void addStartExecutionDependency(ExecutionNode node, ExecutionNode dependency, ExecutionNodeStatus[] conditions) { |
||
| 825 | // add node's start dependency and related condition |
||
| 826 | this.sdg.get(node).put(dependency, conditions); |
||
| 827 | } |
||
| 828 | |||
| 829 | /** |
||
| 830 | * |
||
| 831 | * @param node |
||
| 832 | * @param dependency |
||
| 833 | * @param condition |
||
| 834 | */ |
||
| 835 | protected void addEndExecutionDependency(ExecutionNode node, ExecutionNode dependency, ExecutionNodeStatus[] conditions) { |
||
| 836 | // add node's end dependency and related condition |
||
| 837 | this.edg.get(node).put(dependency, conditions); |
||
| 838 | } |
||
| 839 | |||
| 840 | /** |
||
| 841 | * |
||
| 842 | * @param node |
||
| 843 | * @param dependency |
||
| 844 | * @param conditions |
||
| 845 | */ |
||
| 846 | protected void addStopExecutionDependency(ExecutionNode node, ExecutionNode dependency, ExecutionNodeStatus[] conditions) { |
||
| 847 | // add node's end dependency and related condition |
||
| 848 | this.stop.get(node).put(dependency, conditions); |
||
| 849 | } |
||
| 850 | |||
| 851 | /** |
||
| 852 | * |
||
| 853 | * @param node |
||
| 854 | * @return |
||
| 855 | */ |
||
| 856 | public boolean checkEndExecutionDependencies(ExecutionNode node) { |
||
| 857 | |||
| 858 | Map<ExecutionNode, ExecutionNodeStatus[]> dependencies = this.getNodeEndDependencies(node); |
||
| 859 | // set end execution flag |
||
| 860 | boolean canEnd = dependencies.isEmpty(); |
||
| 861 | // check execution flag |
||
| 862 | View Code Duplication | if (!canEnd) { |
|
| 863 | |||
| 864 | // check if conditions are satisfied |
||
| 865 | Iterator<ExecutionNode> it = dependencies.keySet().iterator(); |
||
| 866 | // check all conditions |
||
| 867 | while (it.hasNext() && !canEnd) { |
||
| 868 | |||
| 869 | // get next dependency |
||
| 870 | ExecutionNode d = it.next(); |
||
| 871 | // get end conditions |
||
| 872 | ExecutionNodeStatus[] conditions = dependencies.get(d); |
||
| 873 | // check if one of the disjunctive conditions is satisfied |
||
| 874 | for (ExecutionNodeStatus condition : conditions) { |
||
| 875 | // check condition |
||
| 876 | if (d.getStatus().equals(condition)) { |
||
| 877 | canEnd = true; |
||
| 878 | break; |
||
| 879 | } |
||
| 880 | |||
| 881 | } |
||
| 882 | } |
||
| 883 | } |
||
| 884 | |||
| 885 | // true if the node can end execution |
||
| 886 | return canEnd; |
||
| 887 | } |
||
| 888 | |||
| 889 | /** |
||
| 890 | * |
||
| 891 | * @param node |
||
| 892 | * @return |
||
| 893 | */ |
||
| 894 | public boolean checkStopExecutionDependencies(ExecutionNode node) { |
||
| 895 | |||
| 896 | Map<ExecutionNode, ExecutionNodeStatus[]> dependencies = this.getNodeStopDependencies(node); |
||
| 897 | // set end execution flag |
||
| 898 | boolean canStop = dependencies.isEmpty(); |
||
| 899 | // check execution flag |
||
| 900 | View Code Duplication | if (!canStop) { |
|
| 901 | |||
| 902 | // check if conditions are satisfied |
||
| 903 | Iterator<ExecutionNode> it = dependencies.keySet().iterator(); |
||
| 904 | // check all conditions |
||
| 905 | while (it.hasNext() && !canStop) { |
||
| 906 | |||
| 907 | // get next dependency |
||
| 908 | ExecutionNode d = it.next(); |
||
| 909 | // get end conditions |
||
| 910 | ExecutionNodeStatus[] conditions = dependencies.get(d); |
||
| 911 | // check if one of the disjunctive conditions is satisfied |
||
| 912 | for (ExecutionNodeStatus condition : conditions) { |
||
| 913 | // check condition |
||
| 914 | if (d.getStatus().equals(condition)) { |
||
| 915 | canStop = true; |
||
| 916 | break; |
||
| 917 | } |
||
| 918 | |||
| 919 | } |
||
| 920 | } |
||
| 921 | } |
||
| 922 | |||
| 923 | // true if the node can stop execution |
||
| 924 | return canStop; |
||
| 925 | } |
||
| 926 | |||
| 927 | /** |
||
| 928 | * |
||
| 929 | * @param node |
||
| 930 | * @return |
||
| 931 | */ |
||
| 932 | public boolean checkStartExecutionDependencies(ExecutionNode node) { |
||
| 933 | |||
| 934 | // get node's start dependencies |
||
| 935 | Map<ExecutionNode, ExecutionNodeStatus[]> dependencies = this.getNodeStartDependencies(node); |
||
| 936 | // set execution flag |
||
| 937 | boolean canStart = dependencies.isEmpty(); |
||
| 938 | // check execution flag |
||
| 939 | View Code Duplication | if (!canStart) { |
|
| 940 | |||
| 941 | // check if conditions are satisfied |
||
| 942 | Iterator<ExecutionNode> it = dependencies.keySet().iterator(); |
||
| 943 | // check all conditions |
||
| 944 | while (it.hasNext() && !canStart) { |
||
| 945 | |||
| 946 | // get a dependency parent |
||
| 947 | ExecutionNode d = it.next(); |
||
| 948 | // get start conditions |
||
| 949 | ExecutionNodeStatus[] conditions = dependencies.get(d); |
||
| 950 | // check if one of the disjunctive conditions is satisfied |
||
| 951 | for (ExecutionNodeStatus condition : conditions) { |
||
| 952 | // check condition |
||
| 953 | if (d.getStatus().equals(condition)) { |
||
| 954 | // node can start |
||
| 955 | canStart = true; |
||
| 956 | break; |
||
| 957 | } |
||
| 958 | } |
||
| 959 | } |
||
| 960 | } |
||
| 961 | |||
| 962 | // true if ready |
||
| 963 | return canStart; |
||
| 964 | } |
||
| 965 | |||
| 966 | /** |
||
| 967 | * |
||
| 968 | * @param node |
||
| 969 | * @param duration |
||
| 970 | * @throws TemporalConstraintPropagationException |
||
| 971 | */ |
||
| 972 | public final void scheduleDuration(ExecutionNode node, long duration) |
||
| 973 | throws TemporalConstraintPropagationException { |
||
| 974 | |||
| 975 | // fix start time first |
||
| 976 | FixIntervalDurationConstraint fix = this.facade. |
||
| 977 | createTemporalConstraint(TemporalConstraintType.FIX_INTERVAL_DURATION); |
||
| 978 | fix.setReference(node.getInterval()); |
||
| 979 | fix.setDuration(duration); |
||
| 980 | // propagate constraint |
||
| 981 | this.facade.propagate(fix); |
||
| 982 | try { |
||
| 983 | |||
| 984 | // check the consistency of the resulting network |
||
| 985 | this.facade.verifyTemporalConsistency(); |
||
| 986 | |||
| 987 | } catch (TemporalConsistencyException ex) { |
||
| 988 | // retract propagated constraint and throw exception |
||
| 989 | this.facade.retract(fix); |
||
| 990 | throw new TemporalConstraintPropagationException("Error while propagating duration constraint for node\n- duration= " + duration +"\n- node= " + node + "\n"); |
||
| 991 | } |
||
| 992 | } |
||
| 993 | |||
| 994 | /** |
||
| 995 | * |
||
| 996 | * @param node |
||
| 997 | * @param time |
||
| 998 | * @throws TemporalConstraintPropagationException |
||
| 999 | */ |
||
| 1000 | View Code Duplication | public final void scheduleStartTime(ExecutionNode node, long time) |
|
| 1001 | throws TemporalConstraintPropagationException { |
||
| 1002 | |||
| 1003 | // create constraint |
||
| 1004 | FixTimePointConstraint fix = this.facade. |
||
| 1005 | createTemporalConstraint(TemporalConstraintType.FIX_TIME_POINT); |
||
| 1006 | // set time point |
||
| 1007 | fix.setReference(node.getInterval().getStartTime()); |
||
| 1008 | fix.setTime(time); |
||
| 1009 | // propagate constraint |
||
| 1010 | this.facade.propagate(fix); |
||
| 1011 | try { |
||
| 1012 | |||
| 1013 | // check consistency of the resulting network |
||
| 1014 | this.facade.verifyTemporalConsistency(); |
||
| 1015 | |||
| 1016 | } catch (TemporalConsistencyException ex) { |
||
| 1017 | // retract propagated constraint and throw exception |
||
| 1018 | this.facade.retract(fix); |
||
| 1019 | throw new TemporalConstraintPropagationException("Error while propagating start constraint for node\n- " |
||
| 1020 | + "time= " + time+ "\n- node= " + node + "\n"); |
||
| 1021 | } |
||
| 1022 | } |
||
| 1023 | |||
| 1024 | /** |
||
| 1025 | * |
||
| 1026 | * @param node |
||
| 1027 | * @param end |
||
| 1028 | * @throws TemporalConstraintPropagationException |
||
| 1029 | */ |
||
| 1030 | View Code Duplication | public void scheduleEndTime(ExecutionNode node, long time) |
|
| 1031 | throws TemporalConstraintPropagationException { |
||
| 1032 | |||
| 1033 | // create constraint |
||
| 1034 | FixTimePointConstraint fix = this.facade. |
||
| 1035 | createTemporalConstraint(TemporalConstraintType.FIX_TIME_POINT); |
||
| 1036 | // set time point |
||
| 1037 | fix.setReference(node.getInterval().getEndTime()); |
||
| 1038 | // set time |
||
| 1039 | fix.setTime(time); |
||
| 1040 | // propagate constraint |
||
| 1041 | this.facade.propagate(fix); |
||
| 1042 | try { |
||
| 1043 | |||
| 1044 | // check consistency of the resulting network |
||
| 1045 | this.facade.verifyTemporalConsistency(); |
||
| 1046 | |||
| 1047 | } catch (TemporalConsistencyException ex) { |
||
| 1048 | // retract propagated constraint and throw exception |
||
| 1049 | this.facade.retract(fix); |
||
| 1050 | throw new TemporalConstraintPropagationException("Error while propagating end constraint for node\n" |
||
| 1051 | + "- time= " + time+ "\n- node= " + node + "\n"); |
||
| 1052 | } |
||
| 1053 | } |
||
| 1054 | |||
| 1055 | /** |
||
| 1056 | * |
||
| 1057 | * @param reference |
||
| 1058 | * @param target |
||
| 1059 | * @param bounds |
||
| 1060 | * @throws Exception |
||
| 1061 | */ |
||
| 1062 | protected void prepareBeforeTemporalConstraint(ExecutionNode reference, ExecutionNode target, long[][] bounds) |
||
| 1063 | throws Exception { |
||
| 1064 | |||
| 1065 | // create and propagate temporal constraint |
||
| 1066 | BeforeIntervalConstraint constraint = this.facade. |
||
| 1067 | createTemporalConstraint(TemporalConstraintType.BEFORE); |
||
| 1068 | |||
| 1069 | // set data |
||
| 1070 | constraint.setReference(reference.getInterval()); |
||
| 1071 | constraint.setTarget(target.getInterval()); |
||
| 1072 | constraint.setLowerBound(bounds[0][0]); |
||
| 1073 | constraint.setUpperBound(bounds[0][1]); |
||
| 1074 | // propagate constraint |
||
| 1075 | this.facade.propagate(constraint); |
||
| 1076 | |||
| 1077 | // set execution constraints |
||
| 1078 | this.addStartExecutionDependency(target, reference, new ExecutionNodeStatus[] { |
||
| 1079 | ExecutionNodeStatus.EXECUTED |
||
| 1080 | }); |
||
| 1081 | } |
||
| 1082 | |||
| 1083 | /** |
||
| 1084 | * |
||
| 1085 | * @param reference |
||
| 1086 | * @param target |
||
| 1087 | * @param bounds |
||
| 1088 | * @throws Exception |
||
| 1089 | */ |
||
| 1090 | protected void prepareMeetsTemporalConstraint(ExecutionNode reference, ExecutionNode target, long[][] bounds) |
||
| 1091 | throws Exception { |
||
| 1092 | |||
| 1093 | // create and propagate temporal constraint |
||
| 1094 | MeetsIntervalConstraint constraint = this.facade. |
||
| 1095 | createTemporalConstraint(TemporalConstraintType.MEETS); |
||
| 1096 | // set data |
||
| 1097 | constraint.setReference(reference.getInterval()); |
||
| 1098 | constraint.setTarget(target.getInterval()); |
||
| 1099 | // propagate constraint |
||
| 1100 | this.facade.propagate(constraint); |
||
| 1101 | |||
| 1102 | // set execution constraints |
||
| 1103 | this.addStartExecutionDependency(target, reference, new ExecutionNodeStatus[] { |
||
| 1104 | ExecutionNodeStatus.EXECUTED |
||
| 1105 | }); |
||
| 1106 | } |
||
| 1107 | |||
| 1108 | /** |
||
| 1109 | * |
||
| 1110 | * @param reference |
||
| 1111 | * @param target |
||
| 1112 | * @param bounds |
||
| 1113 | * @throws Exception |
||
| 1114 | */ |
||
| 1115 | protected void prepareAfterTemporalConstraint(ExecutionNode reference, ExecutionNode target, long[][] bounds) |
||
| 1116 | throws Exception { |
||
| 1117 | |||
| 1118 | // create constraint |
||
| 1119 | AfterIntervalConstraint constraint = this.facade. |
||
| 1120 | createTemporalConstraint(TemporalConstraintType.AFTER); |
||
| 1121 | // set references |
||
| 1122 | constraint.setReference(reference.getInterval()); |
||
| 1123 | constraint.setTarget(target.getInterval()); |
||
| 1124 | |||
| 1125 | // set bounds |
||
| 1126 | constraint.setLowerBound(bounds[0][0]); |
||
| 1127 | constraint.setUpperBound(bounds[0][1]); |
||
| 1128 | // propagate temporal constraint |
||
| 1129 | this.facade.propagate(constraint); |
||
| 1130 | |||
| 1131 | // add execution dependencies |
||
| 1132 | this.addStartExecutionDependency(reference, target, new ExecutionNodeStatus[] { |
||
| 1133 | ExecutionNodeStatus.EXECUTED |
||
| 1134 | }); |
||
| 1135 | } |
||
| 1136 | |||
| 1137 | /** |
||
| 1138 | * |
||
| 1139 | * @param reference |
||
| 1140 | * @param target |
||
| 1141 | * @param bounds |
||
| 1142 | * @throws Exception |
||
| 1143 | */ |
||
| 1144 | View Code Duplication | protected void prepareDuringTemporalConstraint(ExecutionNode reference, ExecutionNode target, long[][] bounds) |
|
| 1145 | throws Exception { |
||
| 1146 | |||
| 1147 | // create constraint |
||
| 1148 | DuringIntervalConstraint constraint = this.facade. |
||
| 1149 | createTemporalConstraint(TemporalConstraintType.DURING); |
||
| 1150 | // set references |
||
| 1151 | constraint.setReference(reference.getInterval()); |
||
| 1152 | constraint.setTarget(target.getInterval()); |
||
| 1153 | |||
| 1154 | // set bounds |
||
| 1155 | constraint.setStartTimeBound(bounds[0]); |
||
| 1156 | constraint.setEndTimeBound(bounds[1]); |
||
| 1157 | // propagate temporal constraint |
||
| 1158 | this.facade.propagate(constraint); |
||
| 1159 | |||
| 1160 | |||
| 1161 | // add execution dependencies |
||
| 1162 | this.addStartExecutionDependency(reference, target, new ExecutionNodeStatus[] { |
||
| 1163 | ExecutionNodeStatus.IN_EXECUTION |
||
| 1164 | }); |
||
| 1165 | |||
| 1166 | this.addEndExecutionDependency(reference, target, new ExecutionNodeStatus[] { |
||
| 1167 | ExecutionNodeStatus.IN_EXECUTION, |
||
| 1168 | }); |
||
| 1169 | |||
| 1170 | this.addEndExecutionDependency(target, reference, new ExecutionNodeStatus[] { |
||
| 1171 | ExecutionNodeStatus.EXECUTED, |
||
| 1172 | }); |
||
| 1173 | |||
| 1174 | |||
| 1175 | |||
| 1176 | |||
| 1177 | // add stop execution condition |
||
| 1178 | this.addStopExecutionDependency(reference, target, new ExecutionNodeStatus[] { |
||
| 1179 | ExecutionNodeStatus.IN_EXECUTION, |
||
| 1180 | ExecutionNodeStatus.FAILURE, |
||
| 1181 | }); |
||
| 1182 | |||
| 1183 | // add stop execution condition |
||
| 1184 | this.addStopExecutionDependency(target, reference, new ExecutionNodeStatus[] { |
||
| 1185 | ExecutionNodeStatus.EXECUTED, |
||
| 1186 | ExecutionNodeStatus.FAILURE, |
||
| 1187 | ExecutionNodeStatus.WAITING |
||
| 1188 | }); |
||
| 1189 | } |
||
| 1190 | |||
| 1191 | /** |
||
| 1192 | * |
||
| 1193 | * @param reference |
||
| 1194 | * @param target |
||
| 1195 | * @param bounds |
||
| 1196 | * @throws Exception |
||
| 1197 | */ |
||
| 1198 | View Code Duplication | protected void prepareContainsTemporalConstraint(ExecutionNode reference, ExecutionNode target, long[][] bounds) |
|
| 1199 | throws Exception { |
||
| 1200 | |||
| 1201 | // create constraint |
||
| 1202 | ContainsIntervalConstraint constraint = this.facade. |
||
| 1203 | createTemporalConstraint(TemporalConstraintType.CONTAINS); |
||
| 1204 | // set references |
||
| 1205 | constraint.setReference(reference.getInterval()); |
||
| 1206 | constraint.setTarget(target.getInterval()); |
||
| 1207 | |||
| 1208 | // set bounds |
||
| 1209 | constraint.setStartTimeBound(bounds[0]); |
||
| 1210 | constraint.setEndTimeBound(bounds[1]); |
||
| 1211 | // propagate temporal constraint |
||
| 1212 | this.facade.propagate(constraint); |
||
| 1213 | |||
| 1214 | // add execution dependencies |
||
| 1215 | this.addEndExecutionDependency(reference, target, new ExecutionNodeStatus[] { |
||
| 1216 | ExecutionNodeStatus.EXECUTED, |
||
| 1217 | }); |
||
| 1218 | |||
| 1219 | this.addStartExecutionDependency(target, reference, new ExecutionNodeStatus[] { |
||
| 1220 | ExecutionNodeStatus.IN_EXECUTION |
||
| 1221 | }); |
||
| 1222 | |||
| 1223 | this.addEndExecutionDependency(target, reference, new ExecutionNodeStatus[] { |
||
| 1224 | ExecutionNodeStatus.IN_EXECUTION, |
||
| 1225 | }); |
||
| 1226 | |||
| 1227 | |||
| 1228 | // add stop execution condition |
||
| 1229 | this.addStopExecutionDependency(reference, target, new ExecutionNodeStatus[] { |
||
| 1230 | ExecutionNodeStatus.EXECUTED, |
||
| 1231 | ExecutionNodeStatus.FAILURE, |
||
| 1232 | ExecutionNodeStatus.WAITING |
||
| 1233 | }); |
||
| 1234 | |||
| 1235 | // add stop execution condition |
||
| 1236 | this.addStopExecutionDependency(target, reference, new ExecutionNodeStatus[] { |
||
| 1237 | ExecutionNodeStatus.IN_EXECUTION, |
||
| 1238 | ExecutionNodeStatus.FAILURE, |
||
| 1239 | |||
| 1240 | }); |
||
| 1241 | } |
||
| 1242 | |||
| 1243 | /** |
||
| 1244 | * |
||
| 1245 | * @param reference |
||
| 1246 | * @param target |
||
| 1247 | * @param bounds |
||
| 1248 | * @throws Exception |
||
| 1249 | */ |
||
| 1250 | protected void prepareEqualsTemporalConstraint(ExecutionNode reference, ExecutionNode target, long[][] bounds) |
||
| 1251 | throws Exception { |
||
| 1252 | |||
| 1253 | // create constraint |
||
| 1254 | EqualsIntervalConstraint constraint = this.facade. |
||
| 1255 | createTemporalConstraint(TemporalConstraintType.EQUALS); |
||
| 1256 | // set references |
||
| 1257 | constraint.setReference(reference.getInterval()); |
||
| 1258 | constraint.setTarget(target.getInterval()); |
||
| 1259 | // propagate temporal constraint |
||
| 1260 | this.facade.propagate(constraint); |
||
| 1261 | } |
||
| 1262 | |||
| 1263 | /** |
||
| 1264 | * |
||
| 1265 | * @param reference |
||
| 1266 | * @param target |
||
| 1267 | * @param bounds |
||
| 1268 | * @throws Exception |
||
| 1269 | */ |
||
| 1270 | protected void prepareStartsDuringTemporalConstraint(ExecutionNode reference, ExecutionNode target, long[][] bounds) |
||
| 1271 | throws Exception { |
||
| 1272 | |||
| 1273 | // create constraint |
||
| 1274 | StartsDuringIntervalConstraint constraint = this.facade. |
||
| 1275 | createTemporalConstraint(TemporalConstraintType.STARTS_DURING); |
||
| 1276 | // set references |
||
| 1277 | constraint.setReference(reference.getInterval()); |
||
| 1278 | constraint.setTarget(target.getInterval()); |
||
| 1279 | |||
| 1280 | // set bounds |
||
| 1281 | constraint.setFirstBound(bounds[0]); |
||
| 1282 | constraint.setSecondBound(bounds[1]); |
||
| 1283 | // propagate temporal constraint |
||
| 1284 | this.facade.propagate(constraint); |
||
| 1285 | |||
| 1286 | // add start dependency |
||
| 1287 | this.addStartExecutionDependency(reference, target, new ExecutionNodeStatus[] { |
||
| 1288 | ExecutionNodeStatus.IN_EXECUTION |
||
| 1289 | }); |
||
| 1290 | } |
||
| 1291 | |||
| 1292 | /** |
||
| 1293 | * |
||
| 1294 | * @param reference |
||
| 1295 | * @param target |
||
| 1296 | * @param bounds |
||
| 1297 | * @throws Exception |
||
| 1298 | */ |
||
| 1299 | protected void prepareEndsDuringTemporalConstraint(ExecutionNode reference, ExecutionNode target, long[][] bounds) |
||
| 1300 | throws Exception { |
||
| 1301 | |||
| 1302 | // create constraint |
||
| 1303 | EndsDuringIntervalConstraint constraint = this.facade. |
||
| 1304 | createTemporalConstraint(TemporalConstraintType.ENDS_DURING); |
||
| 1305 | |||
| 1306 | // set references |
||
| 1307 | constraint.setReference(reference.getInterval()); |
||
| 1308 | constraint.setTarget(target.getInterval()); |
||
| 1309 | // set bounds |
||
| 1310 | constraint.setFirstBound(bounds[0]); |
||
| 1311 | constraint.setSecondBound(bounds[1]); |
||
| 1312 | // propagate temporal constraint |
||
| 1313 | this.facade.propagate(constraint); |
||
| 1314 | |||
| 1315 | // add end dependency |
||
| 1316 | this.addEndExecutionDependency(reference, target, new ExecutionNodeStatus[] { |
||
| 1317 | ExecutionNodeStatus.IN_EXECUTION, |
||
| 1318 | }); |
||
| 1319 | |||
| 1320 | |||
| 1321 | // add stop execution condition |
||
| 1322 | this.addStopExecutionDependency(reference, target, new ExecutionNodeStatus[] { |
||
| 1323 | ExecutionNodeStatus.IN_EXECUTION, |
||
| 1324 | ExecutionNodeStatus.FAILURE |
||
| 1325 | }); |
||
| 1328 | } |
The Java documentation explain EnumMap.