Search in sources :

Example 1 with VehicleLocationCacheElements

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());
}
Also used : VehicleLocationCacheElements(org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements)

Example 2 with VehicleLocationCacheElements

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;
}
Also used : VehicleLocationCacheElements(org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) ScheduledBlockLocation(org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation) BlockLocation(org.onebusaway.transit_data_federation.services.realtime.BlockLocation)

Example 3 with VehicleLocationCacheElements

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;
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) VehicleLocationCacheEntry(org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheEntry) VehicleLocationCacheElements(org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements) ArrayList(java.util.ArrayList)

Example 4 with VehicleLocationCacheElements

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());
}
Also used : VehicleLocationCacheElements(org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) VehicleLocationRecord(org.onebusaway.realtime.api.VehicleLocationRecord) BlockEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.BlockEntryImpl) BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry) TripEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.TripEntryImpl) Test(org.junit.Test)

Example 5 with VehicleLocationCacheElements

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());
}
Also used : VehicleLocationCacheElements(org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements) BlockInstance(org.onebusaway.transit_data_federation.services.blocks.BlockInstance) BlockEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.BlockEntryImpl) BlockConfigurationEntry(org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry) TripEntryImpl(org.onebusaway.transit_data_federation.impl.transit_graph.TripEntryImpl) Test(org.junit.Test)

Aggregations

VehicleLocationCacheElements (org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElements)11 ArrayList (java.util.ArrayList)6 ScheduledBlockLocation (org.onebusaway.transit_data_federation.services.blocks.ScheduledBlockLocation)4 BlockLocation (org.onebusaway.transit_data_federation.services.realtime.BlockLocation)4 VehicleLocationRecord (org.onebusaway.realtime.api.VehicleLocationRecord)3 BlockInstance (org.onebusaway.transit_data_federation.services.blocks.BlockInstance)3 List (java.util.List)2 Test (org.junit.Test)2 FactoryMap (org.onebusaway.collections.FactoryMap)2 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)2 BlockEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.BlockEntryImpl)2 TripEntryImpl (org.onebusaway.transit_data_federation.impl.transit_graph.TripEntryImpl)2 VehicleLocationCacheElement (org.onebusaway.transit_data_federation.services.realtime.VehicleLocationCacheElement)2 BlockConfigurationEntry (org.onebusaway.transit_data_federation.services.transit_graph.BlockConfigurationEntry)2 Date (java.util.Date)1 Map (java.util.Map)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 Range (org.onebusaway.collections.Range)1 TargetTime (org.onebusaway.transit_data_federation.model.TargetTime)1