use of org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements in project onebusaway-application-modules by camsys.
the class BlockLocationServiceImpl method getLocationForBlockInstance.
/**
**
* {@link BlockLocationService} Interface
***
*/
@Override
public BlockLocation getLocationForBlockInstance(BlockInstance blockInstance, TargetTime time) {
List<VehicleLocationCacheElements> records = getBlockLocationRecordCollectionForBlock(blockInstance, time);
VehicleLocationCacheElements record = null;
if (!records.isEmpty())
record = records.get(0);
// TODO : find a better way to pick?
return getBlockLocation(blockInstance, record, null, time.getTargetTime());
}
use of org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements in project onebusaway-application-modules by camsys.
the class BlockLocationServiceImpl method getLocationForVehicleAndTime.
@Override
public BlockLocation getLocationForVehicleAndTime(AgencyAndId vehicleId, TargetTime targetTime) {
List<VehicleLocationCacheElements> cacheRecords = getBlockLocationRecordCollectionForVehicle(vehicleId, targetTime);
// multiple collections are returned
if (cacheRecords.size() > 1) {
_log.error("multiple cache entries for vehicle " + vehicleId);
}
for (VehicleLocationCacheElements cacheRecord : cacheRecords) {
BlockInstance blockInstance = cacheRecord.getBlockInstance();
BlockLocation location = getBlockLocation(blockInstance, cacheRecord, null, targetTime.getTargetTime());
if (location != null)
return location;
}
return null;
}
use of org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements in project onebusaway-application-modules by camsys.
the class VehicleLocationRecordCacheImpl method getRecordsForBlockInstance.
@Override
public List<VehicleLocationCacheElements> getRecordsForBlockInstance(BlockInstance blockInstance) {
Set<AgencyAndId> vehicleIds = _vehicleIdsByBlockInstance.get(blockInstance);
List<VehicleLocationCacheElements> records = new ArrayList<VehicleLocationCacheElements>();
if (vehicleIds != null) {
for (AgencyAndId vehicleId : vehicleIds) {
VehicleLocationCacheEntry record = _entriesByVehicleId.get(vehicleId);
if (record != null && record.getBlockInstance().equals(blockInstance))
records.add(record.getElements());
}
}
return records;
}
use of org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements in project onebusaway-application-modules by camsys.
the class VehicleLocationRecordCacheImplTest method testSimpleOperations.
@Test
public void testSimpleOperations() {
long serviceDate = System.currentTimeMillis();
BlockEntryImpl block = block("blockA");
TripEntryImpl trip = trip("tripA", "serviceId");
stopTime(0, null, trip, time(9, 00), 0);
BlockConfigurationEntry blockConfig = linkBlockTrips(block, trip);
BlockInstance blockInstance = new BlockInstance(blockConfig, serviceDate);
VehicleLocationRecordCacheImpl cache = new VehicleLocationRecordCacheImpl();
cache.setBlockLocationRecordCacheWindowSize(20);
List<VehicleLocationCacheElements> records = cache.getRecordsForBlockInstance(blockInstance);
assertEquals(0, records.size());
VehicleLocationCacheElements cacheRecord = cache.getRecordForVehicleId(aid("vehicleA"));
assertNull(cacheRecord);
cache.addRecord(blockInstance, record(20, "blockA", serviceDate, "vehicleA", 10.0), null, null);
records = cache.getRecordsForBlockInstance(blockInstance);
assertEquals(1, records.size());
cacheRecord = records.get(0);
VehicleLocationRecord record = cacheRecord.getLastElement().getRecord();
assertEquals(20, record.getTimeOfRecord());
assertEquals(blockInstance, cacheRecord.getBlockInstance());
assertEquals(aid("vehicleA"), record.getVehicleId());
VehicleLocationCacheElements cacheRecord2 = cache.getRecordForVehicleId(aid("vehicleA"));
assertSame(cacheRecord, cacheRecord2);
cache.addRecord(blockInstance, record(30, "blockA", serviceDate, "vehicleA", 20.0), null, null);
records = cache.getRecordsForBlockInstance(blockInstance);
assertEquals(1, records.size());
cacheRecord = records.get(0);
record = cacheRecord.getLastElement().getRecord();
assertEquals(30, record.getTimeOfRecord());
assertEquals(blockInstance, cacheRecord.getBlockInstance());
assertEquals(aid("vehicleA"), record.getVehicleId());
cacheRecord2 = cache.getRecordForVehicleId(aid("vehicleA"));
assertSame(cacheRecord, cacheRecord2);
cache.addRecord(blockInstance, record(40, "blockA", serviceDate, "vehicleB", 5.0), null, null);
records = cache.getRecordsForBlockInstance(blockInstance);
assertEquals(2, records.size());
cacheRecord = cache.getRecordForVehicleId(aid("vehicleA"));
record = cacheRecord.getLastElement().getRecord();
assertEquals(30, record.getTimeOfRecord());
assertEquals(blockInstance, cacheRecord.getBlockInstance());
assertEquals(aid("vehicleA"), record.getVehicleId());
cacheRecord = cache.getRecordForVehicleId(aid("vehicleB"));
record = cacheRecord.getLastElement().getRecord();
assertEquals(40, record.getTimeOfRecord());
assertEquals(blockInstance, cacheRecord.getBlockInstance());
assertEquals(aid("vehicleB"), record.getVehicleId());
cache.clearRecordsForVehicleId(aid("vehicleA"));
records = cache.getRecordsForBlockInstance(blockInstance);
assertEquals(1, records.size());
cacheRecord = cache.getRecordForVehicleId(aid("vehicleA"));
assertNull(cacheRecord);
cacheRecord = cache.getRecordForVehicleId(aid("vehicleB"));
assertEquals(aid("vehicleB"), cacheRecord.getLastElement().getRecord().getVehicleId());
}
use of org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements in project onebusaway-application-modules by camsys.
the class VehicleLocationRecordCacheImplTest method testClearCache.
@Test
public void testClearCache() throws InterruptedException {
long serviceDate = System.currentTimeMillis();
BlockEntryImpl blockA = block("blockA");
TripEntryImpl tripA = trip("tripA", "serviceId");
stopTime(0, null, tripA, time(9, 00), 0);
BlockConfigurationEntry blockConfigA = linkBlockTrips(blockA, tripA);
BlockInstance instanceA = new BlockInstance(blockConfigA, serviceDate);
BlockEntryImpl blockB = block("blockB");
TripEntryImpl tripB = trip("tripB", "serviceId");
stopTime(0, null, tripB, time(9, 00), 0);
BlockConfigurationEntry blockConfigB = linkBlockTrips(blockB, tripB);
BlockInstance instanceB = new BlockInstance(blockConfigB, serviceDate);
VehicleLocationRecordCacheImpl cache = new VehicleLocationRecordCacheImpl();
cache.setBlockLocationRecordCacheWindowSize(20);
cache.addRecord(instanceA, record(20, "blockA", serviceDate, "vehicleA", 10.0), null, null);
Thread.sleep(100);
cache.addRecord(instanceB, record(30, "blockB", serviceDate, "vehicleB", 20.0), null, null);
Thread.sleep(100);
cache.addRecord(instanceA, record(40, "blockA", serviceDate, "vehicleC", 20.0), null, null);
Thread.sleep(100);
cache.addRecord(instanceB, record(50, "blockB", serviceDate, "vehicleD", 20.0), null, null);
cache.clearStaleRecords(System.currentTimeMillis() - 150);
VehicleLocationCacheElements cacheRecord = cache.getRecordForVehicleId(aid("vehicleA"));
assertNull(cacheRecord);
cacheRecord = cache.getRecordForVehicleId(aid("vehicleB"));
assertNull(cacheRecord);
cacheRecord = cache.getRecordForVehicleId(aid("vehicleC"));
assertEquals(aid("vehicleC"), cacheRecord.getLastElement().getRecord().getVehicleId());
cacheRecord = cache.getRecordForVehicleId(aid("vehicleD"));
assertEquals(aid("vehicleD"), cacheRecord.getLastElement().getRecord().getVehicleId());
List<VehicleLocationCacheElements> records = cache.getRecordsForBlockInstance(instanceA);
assertEquals(1, records.size());
records = cache.getRecordsForBlockInstance(instanceB);
assertEquals(1, records.size());
}
Aggregations