| Total Complexity | 78 |
| Total Lines | 598 |
| Duplicated Lines | 7.69 % |
| Changes | 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.framework.time.tn.SimpleTemporalNetworkWithUncertainty 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.framework.time.tn; |
||
| 20 | public final class SimpleTemporalNetworkWithUncertainty extends TemporalNetwork |
||
| 21 | { |
||
| 22 | private Map<Integer, TimePoint> points; |
||
| 23 | // set of all constraints added to the network |
||
| 24 | private Map<TimePoint, Map<TimePoint, Set<TimePointDistanceConstraint>>> requirements; |
||
| 25 | // contingent links |
||
| 26 | private Map<TimePoint, Map<TimePoint, TimePointDistanceConstraint>> contingents; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * |
||
| 30 | * @param origin |
||
| 31 | * @param horizon |
||
| 32 | */ |
||
| 33 | public SimpleTemporalNetworkWithUncertainty(long origin, long horizon) { |
||
| 34 | super(origin, horizon); |
||
| 35 | |||
| 36 | // initialize data structures |
||
| 37 | this.points = new HashMap<>(); |
||
| 38 | this.requirements = new HashMap<>(); |
||
| 39 | this.contingents = new HashMap<>(); |
||
| 40 | |||
| 41 | // add the origin to the network |
||
| 42 | this.points.put(this.tpOrigin.getId(), this.tpOrigin); |
||
| 43 | this.contingents.put(this.tpOrigin, new HashMap<TimePoint, TimePointDistanceConstraint>()); |
||
| 44 | this.requirements.put(this.tpOrigin, new HashMap<TimePoint, Set<TimePointDistanceConstraint>>()); |
||
| 45 | |||
| 46 | // add the horizon to the network |
||
| 47 | this.points.put(this.tpHorizion.getId(), this.tpHorizion); |
||
| 48 | this.contingents.put(this.tpHorizion, new HashMap<TimePoint, TimePointDistanceConstraint>()); |
||
| 49 | this.requirements.put(this.tpHorizion, new HashMap<TimePoint, Set<TimePointDistanceConstraint>>()); |
||
| 50 | |||
| 51 | try { |
||
| 52 | |||
| 53 | // create constraint |
||
| 54 | TimePointDistanceConstraint oh = this.createDistanceConstraint( |
||
| 55 | this.tpOrigin, |
||
| 56 | this.tpHorizion, |
||
| 57 | new long[] {horizon, horizon}, |
||
| 58 | true); |
||
| 59 | |||
| 60 | // add constraint |
||
| 61 | this.doAddConstraint(oh); |
||
| 62 | } |
||
| 63 | catch (InconsistentDistanceConstraintException ex) { |
||
| 64 | throw new RuntimeException(ex.getMessage()); |
||
|
|
|||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * |
||
| 70 | */ |
||
| 71 | @Override |
||
| 72 | public int size() { |
||
| 73 | return this.points.size(); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * |
||
| 78 | */ |
||
| 79 | @Override |
||
| 80 | public TimePoint getTimePoint(int id) |
||
| 81 | throws TimePointNotFoundException { |
||
| 82 | |||
| 83 | // check network |
||
| 84 | if (!this.points.containsKey(id)) { |
||
| 85 | throw new TimePointNotFoundException("The network does not contain any time point with id= " + id); |
||
| 86 | } |
||
| 87 | |||
| 88 | // get time point |
||
| 89 | return this.points.get(id); |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * |
||
| 94 | */ |
||
| 95 | @Override |
||
| 96 | public List<TimePoint> getTimePoints() { |
||
| 97 | // get all time points |
||
| 98 | return new ArrayList<>(this.points.values()); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Get the list of all constraints concerning the time point. |
||
| 103 | * |
||
| 104 | * The method returns the list of all requirement and contingent constraints starting from the time point. |
||
| 105 | */ |
||
| 106 | @Override |
||
| 107 | public List<TimePointDistanceConstraint> getConstraints(TimePoint point) { |
||
| 108 | |||
| 109 | // set of constraints |
||
| 110 | Set<TimePointDistanceConstraint> set = new HashSet<>(); |
||
| 111 | // check requirement constraints |
||
| 112 | if (this.requirements.containsKey(point)) { |
||
| 113 | // get all requirement constraints |
||
| 114 | for (Set<TimePointDistanceConstraint> outs : this.requirements.get(point).values()) { |
||
| 115 | // add constraints |
||
| 116 | set.addAll(outs); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | // check contingent constraints |
||
| 121 | if (this.contingents.containsKey(point)) { |
||
| 122 | // get all contingent constraints |
||
| 123 | for (TimePointDistanceConstraint contingent : this.contingents.get(point).values()) { |
||
| 124 | // add constraints |
||
| 125 | set.add(contingent); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | // get the list of outgoing constraints |
||
| 130 | return new ArrayList<>(set); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Get the list of all constraints concerning the two time points. |
||
| 135 | * |
||
| 136 | * The method returns the list of all requirement or contingent |
||
| 137 | * constraints concerning between the time points. Indeed only one |
||
| 138 | * type of constraint is allowed between two time points. Namely two |
||
| 139 | * time points are connected by a requirement constraint or a contingent |
||
| 140 | * constraint |
||
| 141 | */ |
||
| 142 | @Override |
||
| 143 | public List<TimePointDistanceConstraint> getConstraints(TimePoint tp1, TimePoint tp2) { |
||
| 144 | |||
| 145 | // list of constraints between the two time points |
||
| 146 | Set<TimePointDistanceConstraint> set = new HashSet<>(); |
||
| 147 | |||
| 148 | // check contingent constraints |
||
| 149 | if (this.contingents.containsKey(tp1) && this.contingents.get(tp1).containsKey(tp2)) { |
||
| 150 | |||
| 151 | // add requirement constraint |
||
| 152 | set.add(this.contingents.get(tp1).get(tp2)); |
||
| 153 | |||
| 154 | } |
||
| 155 | else if (this.requirements.containsKey(tp1) && this.requirements.get(tp1).containsKey(tp2)) { |
||
| 156 | |||
| 157 | // add requirement constraint |
||
| 158 | set.addAll(this.requirements.get(tp1).get(tp2)); |
||
| 159 | |||
| 160 | } else { |
||
| 161 | |||
| 162 | // no constraint between time points |
||
| 163 | } |
||
| 164 | |||
| 165 | |||
| 166 | // get the list |
||
| 167 | return new ArrayList<>(set); |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Return the constraint between the origin and the time point. |
||
| 172 | * |
||
| 173 | * Note that only requirement constraints can be specified between the origin |
||
| 174 | * and a time point. |
||
| 175 | */ |
||
| 176 | View Code Duplication | @Override |
|
| 177 | public List<TimePointDistanceConstraint> getConstraintFromOrigin(TimePoint point) { |
||
| 178 | |||
| 179 | // prepare list of constraints |
||
| 180 | Set<TimePointDistanceConstraint> set = new HashSet<>(); |
||
| 181 | // check contingencies |
||
| 182 | if (this.contingents.containsKey(this.tpOrigin) && this.contingents.get(this.tpOrigin).containsKey(point)) { |
||
| 183 | |||
| 184 | // add contingent constraint |
||
| 185 | set.add(this.contingents.get(this.tpOrigin).get(point)); |
||
| 186 | |||
| 187 | } else if (this.requirements.containsKey(this.tpOrigin) && this.requirements.get(this.tpOrigin).containsKey(point)) { |
||
| 188 | |||
| 189 | // add all requirement constraints |
||
| 190 | set.addAll(this.requirements.get(this.tpOrigin).get(point)); |
||
| 191 | |||
| 192 | } else { |
||
| 193 | |||
| 194 | // no constraints |
||
| 195 | } |
||
| 196 | |||
| 197 | // get list of constraints |
||
| 198 | return new ArrayList<>(set); |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * |
||
| 203 | */ |
||
| 204 | View Code Duplication | @Override |
|
| 205 | public List<TimePointDistanceConstraint> getConstraintToHorizon(TimePoint point) { |
||
| 206 | |||
| 207 | // prepare list of constraints |
||
| 208 | Set<TimePointDistanceConstraint> set = new HashSet<>(); |
||
| 209 | // check contingencies |
||
| 210 | if (this.contingents.containsKey(point) && this.contingents.get(point).containsKey(this.tpHorizion)) { |
||
| 211 | |||
| 212 | // add contingent constraint |
||
| 213 | set.add(this.contingents.get(point).get(this.tpHorizion)); |
||
| 214 | |||
| 215 | } else if (this.requirements.containsKey(point) && this.requirements.get(point).containsKey(this.tpHorizion)) { |
||
| 216 | |||
| 217 | // add all requirement constraints |
||
| 218 | set.addAll(this.requirements.get(point).get(this.tpHorizion)); |
||
| 219 | |||
| 220 | } else { |
||
| 221 | |||
| 222 | // no constraints |
||
| 223 | } |
||
| 224 | |||
| 225 | // get list of constraints |
||
| 226 | return new ArrayList<>(set); |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * |
||
| 231 | * @return |
||
| 232 | */ |
||
| 233 | @Override |
||
| 234 | public List<TimePointDistanceConstraint> getContingentConstraints() { |
||
| 235 | // set of constraints |
||
| 236 | Set<TimePointDistanceConstraint> set = new HashSet<>(); |
||
| 237 | // check time points |
||
| 238 | for (TimePoint point : this.points.values()) { |
||
| 239 | // add contingent constraints if any |
||
| 240 | if (this.contingents.containsKey(point)) { |
||
| 241 | // add all (outgoing) contingent constraints |
||
| 242 | set.addAll(this.contingents.get(point).values()); |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | // get the list |
||
| 247 | return new ArrayList<>(set); |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * |
||
| 252 | */ |
||
| 253 | @Override |
||
| 254 | public List<TimePointDistanceConstraint> getConstraints() { |
||
| 255 | // set of constraints |
||
| 256 | Set<TimePointDistanceConstraint> set = new HashSet<>(); |
||
| 257 | // check time points |
||
| 258 | for (TimePoint point : this.points.values()) { |
||
| 259 | |||
| 260 | // add contingent constraints |
||
| 261 | if (this.contingents.containsKey(point)) { |
||
| 262 | set.addAll(this.contingents.get(point).values()); |
||
| 263 | } |
||
| 264 | |||
| 265 | // add requirements |
||
| 266 | if (this.requirements.containsKey(point)) { |
||
| 267 | // check targets |
||
| 268 | for (TimePoint target : this.requirements.get(point).keySet()) { |
||
| 269 | // add requirements |
||
| 270 | set.addAll(this.requirements.get(point).get(target)); |
||
| 271 | } |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | // get the list |
||
| 276 | return new ArrayList<>(set); |
||
| 277 | } |
||
| 278 | |||
| 279 | |||
| 280 | /** |
||
| 281 | * |
||
| 282 | */ |
||
| 283 | @Override |
||
| 284 | public String toString() { |
||
| 285 | String str = "{\n" |
||
| 286 | + "\ttype: \"stnu\",\n" |
||
| 287 | + "\tnetwork: [\n"; |
||
| 288 | |||
| 289 | // print data about points and links |
||
| 290 | for (TimePoint tp : this.points.values()) { |
||
| 291 | // print data about current time point |
||
| 292 | str += "\t\t{\n" |
||
| 293 | + "\t\t\tpoint: " + tp.getId() + ",\n"; |
||
| 294 | if (this.requirements.containsKey(tp)) { |
||
| 295 | str += "\t\t\trequirements: [\n"; |
||
| 296 | for (Set<TimePointDistanceConstraint> rels : this.requirements.get(tp).values()) { |
||
| 297 | for (TimePointDistanceConstraint rel : rels) { |
||
| 298 | str += "\t\t\t\t{ point: " + rel.getTarget().getId() + ", lb: " + rel.getDistanceLowerBound() + ", ub: " + rel.getDistanceUpperBound() + "}\n"; |
||
| 299 | } |
||
| 300 | } |
||
| 301 | } |
||
| 302 | |||
| 303 | str += "\t\t\t],\n"; |
||
| 304 | str += "\t\t\tcontingencies: [\n"; |
||
| 305 | if (this.contingents.containsKey(tp)) { |
||
| 306 | for (TimePointDistanceConstraint rel : this.contingents.get(tp).values()) { |
||
| 307 | str += "\t\t\t\t{ point: " + rel.getTarget().getId()+ ", lb: " + rel.getDistanceLowerBound() + ", ub: " + rel.getDistanceUpperBound() +"}, \n"; |
||
| 308 | } |
||
| 309 | } |
||
| 310 | |||
| 311 | str += "\t\t\t]\n" |
||
| 312 | + "\t\t},\n"; |
||
| 313 | } |
||
| 314 | |||
| 315 | // close network description |
||
| 316 | str += "\t]\n" |
||
| 317 | + "}\n"; |
||
| 318 | // print network |
||
| 319 | return str; |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | */ |
||
| 324 | @Override |
||
| 325 | protected void doAddTimePoint(TimePoint tp) |
||
| 326 | throws InconsistentDistanceConstraintException { |
||
| 327 | |||
| 328 | // add the point to the network |
||
| 329 | this.points.put(tp.getId(), tp); |
||
| 330 | // create constraint from the origin |
||
| 331 | TimePointDistanceConstraint c0p = this.createDistanceConstraint( |
||
| 332 | this.tpOrigin, |
||
| 333 | tp, |
||
| 334 | new long[] { |
||
| 335 | tp.getDomLb(), |
||
| 336 | tp.getDomUb() |
||
| 337 | }, |
||
| 338 | true); |
||
| 339 | |||
| 340 | // add distance constraint |
||
| 341 | this.doAddConstraint(c0p); |
||
| 342 | |||
| 343 | // create constraint to horizon |
||
| 344 | TimePointDistanceConstraint cpH = this.createDistanceConstraint( |
||
| 345 | tp, |
||
| 346 | this.tpHorizion, |
||
| 347 | new long[] { |
||
| 348 | this.horizon - tp.getDomUb(), |
||
| 349 | this.horizon - tp.getDomLb() |
||
| 350 | }, |
||
| 351 | true); |
||
| 352 | |||
| 353 | // add distance constraint |
||
| 354 | this.doAddConstraint(cpH); |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * |
||
| 359 | */ |
||
| 360 | @Override |
||
| 361 | protected List<TimePointDistanceConstraint> doRemoveTimePoint(TimePoint point) |
||
| 362 | { |
||
| 363 | // list of removed constraints |
||
| 364 | Set<TimePointDistanceConstraint> removed = new HashSet<>(); |
||
| 365 | |||
| 366 | // check time point constraints |
||
| 367 | if (this.requirements.containsKey(point)) { |
||
| 368 | |||
| 369 | // get related network |
||
| 370 | Map<TimePoint, Set<TimePointDistanceConstraint>> net = this.requirements.get(point); |
||
| 371 | // add constraints to remove |
||
| 372 | for (Set<TimePointDistanceConstraint> set : net.values()) { |
||
| 373 | // add constraints |
||
| 374 | removed.addAll(set); |
||
| 375 | } |
||
| 376 | |||
| 377 | // remove entry from the network |
||
| 378 | this.requirements.remove(point); |
||
| 379 | |||
| 380 | // check other associated constraints |
||
| 381 | for (TimePoint other : this.requirements.keySet()) { |
||
| 382 | // check constraints to the point |
||
| 383 | if (this.requirements.get(other).containsKey(point)) { |
||
| 384 | |||
| 385 | // get constraints |
||
| 386 | removed.addAll(this.requirements.get(other).get(point)); |
||
| 387 | // remove data |
||
| 388 | this.requirements.get(other).remove(point); |
||
| 389 | } |
||
| 390 | } |
||
| 391 | } |
||
| 392 | |||
| 393 | // check contingent constraints |
||
| 394 | if (this.contingents.containsKey(point)) { |
||
| 395 | |||
| 396 | // get constraints |
||
| 397 | removed.addAll(this.contingents.get(point).values()); |
||
| 398 | // clear data |
||
| 399 | this.contingents.remove(point); |
||
| 400 | |||
| 401 | // check other constraints |
||
| 402 | for (TimePoint other : this.contingents.keySet()) { |
||
| 403 | // check constraints to the point |
||
| 404 | if (this.contingents.get(other).containsKey(point)) { |
||
| 405 | |||
| 406 | // get constraint |
||
| 407 | removed.add(this.contingents.get(other).get(point)); |
||
| 408 | // remove data |
||
| 409 | this.contingents.get(other).remove(point); |
||
| 410 | |||
| 411 | } |
||
| 412 | } |
||
| 413 | |||
| 414 | } |
||
| 415 | |||
| 416 | // finally remove point |
||
| 417 | this.points.remove(point.getId()); |
||
| 418 | // get the list |
||
| 419 | return new ArrayList<>(removed); |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * The STNU can handle only one contingent constraint for each pair of time points. |
||
| 424 | * |
||
| 425 | * The method throws an exception when trying to override an existing contingent |
||
| 426 | * constraint either by adding a contingent or a requirement constraint. |
||
| 427 | */ |
||
| 428 | @Override |
||
| 429 | protected boolean doAddConstraint(TimePointDistanceConstraint constraint) |
||
| 430 | throws InconsistentDistanceConstraintException |
||
| 431 | { |
||
| 432 | // change flag |
||
| 433 | boolean change = false; |
||
| 434 | // get reference and target time points |
||
| 435 | TimePoint reference = constraint.getReference(); |
||
| 436 | TimePoint target = constraint.getTarget(); |
||
| 437 | |||
| 438 | // check if related time points exits |
||
| 439 | if (!this.points.containsKey(reference.getId()) || !this.points.containsKey(target.getId())) { |
||
| 440 | // unknown time points |
||
| 441 | throw new InconsistentDistanceConstraintException("Unknown time points:\n" |
||
| 442 | + "- reference= " + reference + "\n" |
||
| 443 | + "- target= " + target + "\n"); |
||
| 444 | } |
||
| 445 | |||
| 446 | // check controllability of the constraint to add |
||
| 447 | if (constraint.isControllable()) |
||
| 448 | { |
||
| 449 | // check if the definition of a contingent constraint between the two time points |
||
| 450 | if (this.contingents.containsKey(reference) && this.contingents.get(reference).containsKey(target)) { |
||
| 451 | // a contingent link exists |
||
| 452 | throw new NotCompatibleConstraintsFoundException("A contingent constraint already exist between time points:\n" |
||
| 453 | + "- reference time point= " + reference + "\n" |
||
| 454 | + "- target time point= " + target + "\n" |
||
| 455 | + "- constraint= " + constraint + "\n" |
||
| 456 | + "- contingent constraint= " + this.contingents.get(reference).get(target) + "\n"); |
||
| 457 | } |
||
| 458 | |||
| 459 | // get current constraint bounds |
||
| 460 | long[] bounds = this.getConstraintBounds(reference, target); |
||
| 461 | // check the feasibility of the current constraint |
||
| 462 | if (bounds != null && (constraint.getDistanceLowerBound() > bounds[1] || constraint.getDistanceUpperBound() < bounds[0])) { |
||
| 463 | // not feasible constraint |
||
| 464 | throw new IntervalDisjunctionException("Disjunctive interval bounds are not allowed\n" |
||
| 465 | + "- current bound intersection= [" + bounds[0] + ", " + bounds[1] + "]\n" |
||
| 466 | + "- constraint bounds= [" + constraint.getDistanceLowerBound() + ", " + constraint.getDistanceUpperBound() + "]\n" |
||
| 467 | + "- constraint= " + constraint + "\n"); |
||
| 468 | } |
||
| 469 | |||
| 470 | |||
| 471 | // check structures |
||
| 472 | if (!this.requirements.containsKey(reference)) { |
||
| 473 | this.requirements.put(reference, new HashMap<>()); |
||
| 474 | } |
||
| 475 | |||
| 476 | if (!this.requirements.get(reference).containsKey(target)) { |
||
| 477 | this.requirements.get(reference).put(target, new HashSet<>()); |
||
| 478 | } |
||
| 479 | |||
| 480 | // the requirement constraint can be safely added to the network |
||
| 481 | this.requirements.get(reference).get(target).add(constraint); |
||
| 482 | // check if the new constraint entail a change in the distance bound between the time points |
||
| 483 | change = bounds == null || constraint.getDistanceLowerBound() > bounds[0] || |
||
| 484 | constraint.getDistanceUpperBound() < bounds[1]; |
||
| 485 | |||
| 486 | } else { |
||
| 487 | |||
| 488 | // check if a contingent constraint already exists |
||
| 489 | if (this.contingents.containsKey(reference) && this.contingents.get(reference).containsKey(target)) { |
||
| 490 | // contingent constraint cannot be overwritten |
||
| 491 | throw new NotCompatibleConstraintsFoundException("Contingent constraints cannot be overwritten:\n" |
||
| 492 | + "- reference time point= " + reference + "\n" |
||
| 493 | + "- target time point= " + target + "\n" |
||
| 494 | + "- existing contingent constraint= " + this.contingents.get(reference).get(target) + "\n" |
||
| 495 | + "- invalid constraint= " + constraint + "\n"); |
||
| 496 | } |
||
| 497 | |||
| 498 | // check structures |
||
| 499 | if (!this.contingents.containsKey(reference)) { |
||
| 500 | this.contingents.put(reference, new HashMap<>()); |
||
| 501 | } |
||
| 502 | |||
| 503 | // add contingent constraint |
||
| 504 | this.contingents.get(reference).put(target, constraint); |
||
| 505 | // set change flag |
||
| 506 | change = true; |
||
| 507 | } |
||
| 508 | |||
| 509 | // get flag |
||
| 510 | return change; |
||
| 511 | } |
||
| 512 | |||
| 513 | /** |
||
| 514 | * |
||
| 515 | */ |
||
| 516 | @Override |
||
| 517 | protected boolean doRemoveDistanceConstraint(TimePointDistanceConstraint c) { |
||
| 518 | |||
| 519 | // change flag |
||
| 520 | boolean change = false; |
||
| 521 | // get time points involved |
||
| 522 | TimePoint reference = c.getReference(); |
||
| 523 | TimePoint target = c.getTarget(); |
||
| 524 | |||
| 525 | // check if requirement constraint |
||
| 526 | if (c.isControllable()) { |
||
| 527 | |||
| 528 | // remove requirement constraint |
||
| 529 | if (this.requirements.containsKey(reference) && this.requirements.get(reference).containsKey(target)) { |
||
| 530 | |||
| 531 | // check intersection constraint |
||
| 532 | long[] bound1 = this.getConstraintBounds(reference, target); |
||
| 533 | // remove requirement constraint |
||
| 534 | this.requirements.get(reference).get(target).remove(c); |
||
| 535 | // clear data structure if necessary |
||
| 536 | if (this.requirements.get(reference).get(target).isEmpty()) { |
||
| 537 | // remove entry |
||
| 538 | this.requirements.get(reference).remove(target); |
||
| 539 | } |
||
| 540 | |||
| 541 | if (this.requirements.get(reference).isEmpty()) { |
||
| 542 | // remove entry |
||
| 543 | this.requirements.remove(reference); |
||
| 544 | } |
||
| 545 | |||
| 546 | // check again intersection constraint |
||
| 547 | long[] bound2 = this.getConstraintBounds(reference, target); |
||
| 548 | // set change flag |
||
| 549 | change = bound2 == null || bound1[0] != bound2[0] || bound1[1] != bound2[1]; |
||
| 550 | } |
||
| 551 | |||
| 552 | } else { |
||
| 553 | |||
| 554 | // remove contingent constraint |
||
| 555 | if (this.contingents.containsKey(reference) && this.contingents.get(reference).containsKey(target)) { |
||
| 556 | |||
| 557 | // remove contingent constraint |
||
| 558 | this.contingents.get(reference).remove(target); |
||
| 559 | this.contingents.remove(reference); |
||
| 560 | // set change flag |
||
| 561 | change = true; |
||
| 562 | } |
||
| 563 | } |
||
| 564 | |||
| 565 | // get change flag |
||
| 566 | return change; |
||
| 567 | } |
||
| 568 | |||
| 569 | /** |
||
| 570 | * |
||
| 571 | */ |
||
| 572 | @Override |
||
| 573 | public long[] getConstraintBounds(TimePoint reference, TimePoint target) { |
||
| 574 | |||
| 575 | // set default bounds |
||
| 576 | long[] bounds = new long[] { |
||
| 577 | 0, |
||
| 578 | this.horizon |
||
| 579 | }; |
||
| 580 | |||
| 581 | // check constraints between time points |
||
| 582 | if (this.contingents.containsKey(reference) && this.contingents.get(reference).containsKey(target)) { |
||
| 583 | |||
| 584 | // get constraint |
||
| 585 | TimePointDistanceConstraint constraint = this.contingents.get(reference).get(target); |
||
| 586 | // get bounds |
||
| 587 | bounds[0] = constraint.getDistanceLowerBound(); |
||
| 588 | bounds[1] = constraint.getDistanceUpperBound(); |
||
| 589 | |||
| 590 | } else if (this.requirements.containsKey(reference) && this.requirements.get(reference).containsKey(target)) { |
||
| 591 | |||
| 592 | // compute bound intersections |
||
| 593 | for (TimePointDistanceConstraint constraint : this.requirements.get(reference).get(target)) { |
||
| 594 | |||
| 595 | // update intersection |
||
| 596 | bounds[0] = Math.max(bounds[0], constraint.getDistanceLowerBound()); |
||
| 597 | bounds[1] = Math.min(bounds[1], constraint.getDistanceUpperBound()); |
||
| 598 | } |
||
| 599 | |||
| 600 | } else { |
||
| 601 | |||
| 602 | // no constraint, no bound can be computed |
||
| 603 | bounds = null; |
||
| 604 | } |
||
| 605 | |||
| 606 | // get bounds |
||
| 607 | return bounds; |
||
| 608 | } |
||
| 609 | |||
| 610 | /** |
||
| 611 | * |
||
| 612 | */ |
||
| 613 | @Override |
||
| 614 | public void printDiagnosticData() { |
||
| 615 | // print temporal network |
||
| 616 | System.out.println("Temporal Network:\n" |
||
| 617 | + this + "\n"); |
||
| 618 | } |
||
| 621 |