private void writeMinimalTransferTimes() { List<Tuple<String, String>> attributes = new ArrayList<>(5); MinimalTransferTimes.MinimalTransferTimesIterator iter = this.schedule.getMinimalTransferTimes().iterator(); if (iter.hasNext()) { this.writeStartTag(Constants.MINIMAL_TRANSFER_TIMES, attributes); while (iter.hasNext()) { iter.next(); attributes.clear(); attributes.add(createTuple(Constants.FROM_STOP, iter.getFromStopId().toString())); attributes.add(createTuple(Constants.TO_STOP, iter.getToStopId().toString())); attributes.add(createTuple(Constants.TRANSFER_TIME, iter.getSeconds())); this.writeStartTag(Constants.RELATION, attributes, true); } this.writeEndTag(Constants.MINIMAL_TRANSFER_TIMES); } }
@Test public void testNotTransitive() { MinimalTransferTimes mtt = new MinimalTransferTimesImpl(); mtt.set(this.stopId1, this.stopId2, 180.0); mtt.set(this.stopId2, this.stopId3, 240.0); Assert.assertEquals(180.0, mtt.get(this.stopId1, this.stopId2), 0.0); Assert.assertEquals(240.0, mtt.get(this.stopId2, this.stopId3), 0.0); Assert.assertEquals(Double.NaN, mtt.get(this.stopId1, this.stopId3), 0.0); Assert.assertEquals(Double.NaN, mtt.get(this.stopId3, this.stopId1), 0.0); mtt.set(this.stopId1, this.stopId3, 480.0); Assert.assertEquals(480.0, mtt.get(this.stopId1, this.stopId3), 0.0); }
@Test public void testRemove() { MinimalTransferTimes mtt = new MinimalTransferTimesImpl(); Assert.assertEquals(Double.NaN, mtt.get(this.stopId1, this.stopId2), 0.0); mtt.set(this.stopId1, this.stopId2, 180.0); mtt.set(this.stopId1, this.stopId3, 240.0); Assert.assertEquals(180.0, mtt.get(this.stopId1, this.stopId2), 0.0); Assert.assertEquals(240.0, mtt.get(this.stopId1, this.stopId3), 0.0); Assert.assertEquals(180.0, mtt.remove(this.stopId1, this.stopId2), 0.0); Assert.assertEquals(Double.NaN, mtt.get(this.stopId1, this.stopId2), 0.0); Assert.assertEquals(240.0, mtt.get(this.stopId1, this.stopId3), 0.0); Assert.assertEquals(Double.NaN, mtt.remove(this.stopId1, this.stopId4), 0.0); // we never set it, let's not throw an exception Assert.assertEquals(Double.NaN, mtt.get(this.stopId1, this.stopId4), 0.0); }
@Test public void testIterator_withRemove() { MinimalTransferTimes mtt = new MinimalTransferTimesImpl(); mtt.set(this.stopId1, this.stopId2, 180); mtt.set(this.stopId2, this.stopId3, 240); mtt.set(this.stopId3, this.stopId1, 300); MinimalTransferTimes.MinimalTransferTimesIterator iter = mtt.iterator(); int count = 0; while (iter.hasNext()) { mtt.remove(this.stopId2, this.stopId3); count = 0; iter = mtt.iterator(); while (iter.hasNext()) { count++; mtt.remove(this.stopId1, this.stopId2); count = 0; iter = mtt.iterator(); while (iter.hasNext()) { count++; mtt.remove(this.stopId3, this.stopId1); iter = mtt.iterator(); Assert.assertFalse(iter.hasNext());
MinimalTransferTimes mtt = new MinimalTransferTimesImpl(); MinimalTransferTimes.MinimalTransferTimesIterator iter = mtt.iterator(); Assert.assertFalse(iter.hasNext()); mtt.set(this.stopId1, this.stopId2, 120.0); iter = mtt.iterator(); Assert.assertTrue(iter.hasNext()); try { } catch (NoSuchElementException expected) {} mtt.set(this.stopId1, this.stopId3, 180); mtt.set(this.stopId2, this.stopId3, 240); mtt.set(this.stopId1, this.stopId2, 300); // overwrite an existing entry iter = mtt.iterator(); int count = 0; boolean found1to2 = false;
@Test public void testValidator_Transfers_missingStop() { Scenario scenario = ScenarioUtils.createScenario(ConfigUtils.createConfig()); TransitSchedule schedule = scenario.getTransitSchedule(); TransitScheduleFactory factory = schedule.getFactory(); Id<TransitStopFacility> id1 = Id.create("stop1", TransitStopFacility.class); Id<TransitStopFacility> id2 = Id.create("stop2", TransitStopFacility.class); Id<TransitStopFacility> id3 = Id.create("stop3", TransitStopFacility.class); Id<TransitStopFacility> id4 = Id.create("stop4", TransitStopFacility.class); schedule.addStopFacility(factory.createTransitStopFacility(id1, new Coord(10000, 10000), false)); schedule.addStopFacility(factory.createTransitStopFacility(id2, new Coord(20000, 10000), false)); schedule.getMinimalTransferTimes().set(id1, id3, 120); TransitScheduleValidator.ValidationResult result = TransitScheduleValidator.validateTransfers(schedule); Assert.assertEquals("Should warn against missing stop3.", 1, result.getIssues().size()); Assert.assertTrue("Message should contain hint about stop3 being missing. " + result.getIssues().get(0).getMessage(), result.getIssues().get(0).getMessage().contains("stop3")); schedule.getMinimalTransferTimes().remove(id1, id3); schedule.getMinimalTransferTimes().set(id4, id2, 120); result = TransitScheduleValidator.validateTransfers(schedule); Assert.assertEquals("Should warn against missing stop4.", 1, result.getIssues().size()); Assert.assertTrue("Message should contain hint about stop4 being missing. " + result.getIssues().get(0).getMessage(), result.getIssues().get(0).getMessage().contains("stop4")); }
Id<TransitStopFacility> toStop = Id.create(atts.getValue(Constants.TO_STOP), TransitStopFacility.class); double transferTime = Time.parseTime(atts.getValue(Constants.TRANSFER_TIME)); this.schedule.getMinimalTransferTimes().set(fromStop, toStop, transferTime); } else if (Constants.ATTRIBUTE.equals(name)) { this.attributesDelegate.startTag(name, atts, context, this.currentAttributes);
@Test public void testValidator_Transfers_implausibleTime() { Scenario scenario = ScenarioUtils.createScenario(ConfigUtils.createConfig()); TransitSchedule schedule = scenario.getTransitSchedule(); TransitScheduleFactory factory = schedule.getFactory(); Id<TransitStopFacility> id1 = Id.create(1, TransitStopFacility.class); Id<TransitStopFacility> id2 = Id.create(2, TransitStopFacility.class); Id<TransitStopFacility> id3 = Id.create(3, TransitStopFacility.class); Id<TransitStopFacility> id4 = Id.create(4, TransitStopFacility.class); schedule.addStopFacility(factory.createTransitStopFacility(id1, new Coord(10000, 10000), false)); schedule.addStopFacility(factory.createTransitStopFacility(id2, new Coord(20000, 10000), false)); schedule.getMinimalTransferTimes().set(id1, id2, 120); TransitScheduleValidator.ValidationResult result = TransitScheduleValidator.validateTransfers(schedule); Assert.assertTrue(result.getIssues().isEmpty()); schedule.getMinimalTransferTimes().set(id1, id2, 0); result = TransitScheduleValidator.validateTransfers(schedule); Assert.assertEquals("Should warn against implausible transfer time.", 1, result.getIssues().size()); }
@Test public void testSetGet() { MinimalTransferTimes mtt = new MinimalTransferTimesImpl(); Assert.assertEquals(Double.NaN, mtt.get(this.stopId1, this.stopId2), 0.0); mtt.set(this.stopId1, this.stopId2, 180.0); mtt.set(this.stopId1, this.stopId3, 240.0); Assert.assertEquals(180.0, mtt.get(this.stopId1, this.stopId2), 0.0); Assert.assertEquals(240.0, mtt.get(this.stopId1, this.stopId3), 0.0); // overwrite a value mtt.set(this.stopId1, this.stopId2, 300.0); Assert.assertEquals(300.0, mtt.get(this.stopId1, this.stopId2), 0.0); Assert.assertEquals(Double.NaN, mtt.get(this.stopId1, this.stopId4), 0.0); }
public static ValidationResult validateTransfers(final TransitSchedule schedule) { ValidationResult result = new ValidationResult(); MinimalTransferTimes transferTimes = schedule.getMinimalTransferTimes(); MinimalTransferTimes.MinimalTransferTimesIterator iter = transferTimes.iterator(); Set<Id> missingFromStops = new HashSet<>(); Set<Id> missingToStops = new HashSet<>(); while (iter.hasNext()) { iter.next(); Id<TransitStopFacility> fromStopId = iter.getFromStopId(); Id<TransitStopFacility> toStopId = iter.getToStopId(); double transferTime = iter.getSeconds(); if (fromStopId == null && toStopId == null) { result.addError("Minimal Transfer Times: both fromStop and toStop are null."); } else if (fromStopId == null) { result.addError("Minimal Transfer Times: fromStop = null, toStop " + toStopId + "."); } else if (toStopId == null) { result.addError("Minimal Transfer Times: fromStop " + fromStopId + ", toStop = null."); } if (transferTime <= 0) { result.addWarning("Minimal Transfer Times: fromStop " + fromStopId + " toStop " + toStopId + " with transferTime = " + transferTime); } if (schedule.getFacilities().get(fromStopId) == null && missingFromStops.add(fromStopId)) { result.addError("Minimal Transfer Times: fromStop " + fromStopId + " does not exist in schedule."); } if (schedule.getFacilities().get(toStopId) == null && missingToStops.add(toStopId)) { result.addError("Minimal Transfer Times: toStop " + toStopId + " does not exist in schedule."); } } return result; }
@Test public void testNotBidirection() { MinimalTransferTimes mtt = new MinimalTransferTimesImpl(); mtt.set(this.stopId1, this.stopId2, 180.0); mtt.set(this.stopId1, this.stopId3, 240.0); Assert.assertEquals(180.0, mtt.get(this.stopId1, this.stopId2), 0.0); Assert.assertEquals(Double.NaN, mtt.get(this.stopId2, this.stopId1), 0.0); Assert.assertEquals(240.0, mtt.get(this.stopId1, this.stopId3), 0.0); Assert.assertEquals(Double.NaN, mtt.get(this.stopId3, this.stopId1), 0.0); mtt.set(this.stopId3, this.stopId1, 120.0); Assert.assertEquals(120.0, mtt.get(this.stopId3, this.stopId1), 0.0); Assert.assertEquals(240.0, mtt.get(this.stopId1, this.stopId3), 0.0); }
@Test public void testIterator_empty() { MinimalTransferTimes mtt = new MinimalTransferTimesImpl(); MinimalTransferTimes.MinimalTransferTimesIterator iter = mtt.iterator(); Assert.assertFalse(iter.hasNext()); try { iter.getFromStopId(); Assert.fail("expected Exception, got none."); } catch (NoSuchElementException expected) {} try { iter.getToStopId(); Assert.fail("expected Exception, got none."); } catch (NoSuchElementException expected) {} try { iter.getSeconds(); Assert.fail("expected Exception, got none."); } catch (NoSuchElementException expected) {} try { iter.next(); Assert.fail("expected Exception"); } catch (NoSuchElementException expected) {} try { iter.getFromStopId(); // there should still be an exception after calling next() Assert.fail("expected Exception, got none."); } catch (NoSuchElementException expected) {} }
@Test public void testGetWithDefault() { MinimalTransferTimes mtt = new MinimalTransferTimesImpl(); double defaultSeconds = 60.0; Assert.assertEquals(Double.NaN, mtt.get(this.stopId1, this.stopId2), 0.0); Assert.assertEquals(60.0, mtt.get(this.stopId1, this.stopId2, defaultSeconds), 0.0); mtt.set(this.stopId1, this.stopId2, 180.0); mtt.set(this.stopId1, this.stopId3, 240.0); Assert.assertEquals(180.0, mtt.get(this.stopId1, this.stopId2, defaultSeconds), 0.0); Assert.assertEquals(240.0, mtt.get(this.stopId1, this.stopId3, defaultSeconds), 0.0); Assert.assertEquals(defaultSeconds, mtt.get(this.stopId1, this.stopId4, defaultSeconds), 0.0); }
schedule.addStopFacility(stop2); schedule.getMinimalTransferTimes().set(stop1.getId(), stop2.getId(), 300.0); schedule.getMinimalTransferTimes().set(stop2.getId(), stop1.getId(), 360.0); Assert.assertEquals(300, schedule2.getMinimalTransferTimes().get(stop1.getId(), stop2.getId()), 0.0); Assert.assertEquals(360, schedule2.getMinimalTransferTimes().get(stop2.getId(), stop1.getId()), 0.0); Assert.assertEquals(Double.NaN, schedule2.getMinimalTransferTimes().get(stop1.getId(), stop1.getId()), 0.0);