From 302b755ee64bfe7db8c6f6d8102ea87ded245dc7 Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Tue, 18 Feb 2020 14:35:39 +0100 Subject: [PATCH] Correctly handle crashes of more than two trains --- .../model/ModelRailwaySimulation.java | 60 ++++++++++--------- testcoverage_input.txt | 11 +++- testcoverage_output.txt | 9 +++ 3 files changed, 52 insertions(+), 28 deletions(-) diff --git a/src/edu/kit/informatik/model/ModelRailwaySimulation.java b/src/edu/kit/informatik/model/ModelRailwaySimulation.java index b089d52..dba0fdb 100644 --- a/src/edu/kit/informatik/model/ModelRailwaySimulation.java +++ b/src/edu/kit/informatik/model/ModelRailwaySimulation.java @@ -466,36 +466,42 @@ public class ModelRailwaySimulation { } }); // check for block collisions - trains.values().stream().filter(train -> train.isPlaced() && collisions.stream().noneMatch((x) -> x.contains(train))).forEach(train -> { - Set occupiedByThisTrain = nextOccupiedRails.get(train); - trains.values().stream().filter((x) -> x != train).forEach((otherTrain) -> { - Set occupiedByOtherTrainPreviously = occupiedRails.get(otherTrain); - Set occupiedByOtherTrain = nextOccupiedRails.get(otherTrain); - if (occupiedByOtherTrain == null || occupiedByOtherTrainPreviously == null) { - return; - } - boolean anyIntersection = occupiedByOtherTrain.stream().anyMatch(occupiedByThisTrain::contains) - || occupiedByOtherTrainPreviously.stream().anyMatch(occupiedByThisTrain::contains); - if (anyIntersection) { - train.removeFromRails(); - // try to find/merge existing collisions - Set existingCollision = null; - for (Set collision : collisions) { - if (collision.contains(otherTrain)) { + trains.values().stream().filter(train -> train.isPlaced() + && collisions.stream().noneMatch((x) -> x.contains(train))).forEach(train -> { + Set occupiedByThisTrain = nextOccupiedRails.get(train); + trains.values().stream().filter((x) -> x != train).forEach((otherTrain) -> { + Set occupiedByOtherTrainPreviously = occupiedRails.get(otherTrain); + Set occupiedByOtherTrain = nextOccupiedRails.get(otherTrain); + if (occupiedByOtherTrain == null || occupiedByOtherTrainPreviously == null) { + return; + } + boolean anyIntersection = occupiedByOtherTrain.stream().anyMatch(occupiedByThisTrain::contains) + || occupiedByOtherTrainPreviously.stream().anyMatch(occupiedByThisTrain::contains); + if (anyIntersection) { + train.removeFromRails(); + // try to find/merge existing collisions + Set existingCollision = null; + for (Set collision : collisions) { + if (collision.contains(otherTrain) || collision.contains(train)) { + if (existingCollision == null) { + existingCollision = collision; + collision.add(train); + collision.add(otherTrain); + } else { + existingCollision.addAll(collision); + existingCollision.add(train); + existingCollision.add(otherTrain); + collisions.remove(collision); + break; + } + } + } if (existingCollision == null) { - existingCollision = collision; - collision.add(train); - } else { - existingCollision.addAll(collision); + collisions.add(new HashSet<>(Arrays.asList(train, otherTrain))); } } - } - if (existingCollision == null) { - collisions.add(new HashSet<>(Arrays.asList(train, otherTrain))); - } - } - }); - }); + }); + }); return collisions; } diff --git a/testcoverage_input.txt b/testcoverage_input.txt index 63bc8fd..4cec915 100644 --- a/testcoverage_input.txt +++ b/testcoverage_input.txt @@ -60,4 +60,13 @@ add train 2 T2-Rho add track (1,0) -> (10,0) put train 1 at (7,0) in direction -1,0 put train 5 at (0,0) in direction -1,0 -step 8 \ No newline at end of file +step 8 +delete track 1 +delete track 2 +add track (0,0) -> (10,0) +add track (10,0) -> (20,0) +add track (20,0) -> (30,0) +put train 1 at (10,0) in direction 1,0 +put train 2 at (15,0) in direction 1,0 +put train 5 at (20,0) in direction -1,0 +step 1 diff --git a/testcoverage_output.txt b/testcoverage_output.txt index 8378a14..91d936d 100644 --- a/testcoverage_output.txt +++ b/testcoverage_output.txt @@ -78,3 +78,12 @@ OK OK Crash of train 1 Crash of train 5 +OK +OK +1 +2 +3 +OK +OK +OK +Crash of train 1,2,5