Search in sources :

Example 1 with GtfsRelationalDaoImpl

use of org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl in project onebusaway-application-modules by camsys.

the class BundleBuildingServiceImpl method build.

/**
 * call FederatedTransitDataBundleCreator
 */
@Override
public int build(BundleBuildRequest request, BundleBuildResponse response) {
    /*
     * this follows the example from FederatedTransitDataBundleCreatorMain
     */
    PrintStream stdOut = System.out;
    PrintStream logFile = null;
    // pass a mini spring context to the bundle builder so we can cleanup
    ConfigurableApplicationContext context = null;
    try {
        File outputPath = new File(response.getBundleDataDirectory());
        File loggingPath = new File(response.getBundleOutputDirectory());
        // beans assume bundlePath is set -- this will be where files are written!
        System.setProperty("bundlePath", outputPath.getAbsolutePath());
        String logFilename = outputPath + File.separator + "bundleBuilder.out.txt";
        logFile = new PrintStream(new FileOutputStream(new File(logFilename)));
        // swap standard out for logging
        System.setOut(logFile);
        configureLogging(System.out);
        FederatedTransitDataBundleCreator creator = new FederatedTransitDataBundleCreator();
        Map<String, BeanDefinition> beans = new HashMap<String, BeanDefinition>();
        creator.setContextBeans(beans);
        List<GtfsBundle> gtfsBundles = createGtfsBundles(response);
        List<String> contextPaths = new ArrayList<String>();
        contextPaths.add(BUNDLE_RESOURCE);
        BeanDefinitionBuilder bean = BeanDefinitionBuilder.genericBeanDefinition(GtfsBundles.class);
        bean.addPropertyValue("bundles", gtfsBundles);
        beans.put("gtfs-bundles", bean.getBeanDefinition());
        bean = BeanDefinitionBuilder.genericBeanDefinition(GtfsRelationalDaoImpl.class);
        beans.put("gtfsRelationalDaoImpl", bean.getBeanDefinition());
        BeanDefinitionBuilder multiCSVLogger = BeanDefinitionBuilder.genericBeanDefinition(MultiCSVLogger.class);
        multiCSVLogger.addPropertyValue("basePath", loggingPath);
        beans.put("multiCSVLogger", multiCSVLogger.getBeanDefinition());
        BeanDefinitionBuilder entityReplacementLogger = BeanDefinitionBuilder.genericBeanDefinition(EntityReplacementLoggerImpl.class);
        beans.put("entityReplacementLogger", entityReplacementLogger.getBeanDefinition());
        BeanDefinitionBuilder requestDef = BeanDefinitionBuilder.genericBeanDefinition(BundleRequestResponse.class);
        requestDef.addPropertyValue("request", request);
        requestDef.addPropertyValue("response", response);
        beans.put("bundleRequestResponse", requestDef.getBeanDefinition());
        // configure for NYC specifics
        BeanDefinitionBuilder bundle = BeanDefinitionBuilder.genericBeanDefinition(FederatedTransitDataBundle.class);
        bundle.addPropertyValue("path", outputPath);
        beans.put("bundle", bundle.getBeanDefinition());
        BeanDefinitionBuilder outputDirectoryReference = BeanDefinitionBuilder.genericBeanDefinition(String.class);
        outputDirectoryReference.addPropertyValue("", response.getBundleOutputDirectory());
        // TODO move this to application-context-bunlde-admin.xml and have it look for config to turn on/off
        BeanDefinitionBuilder task = null;
        if (isStifTaskApplicable()) {
            addStifTask(beans, request, response);
        }
        if (isStopConsolidationApplicable()) {
            addStopConsolidationMappings(beans, request, response);
        }
        _log.debug("setting outputPath=" + outputPath);
        creator.setOutputPath(outputPath);
        creator.setContextPaths(contextPaths);
        // manage our own overrides, as we use our own context
        Properties cmdOverrides = new Properties();
        cmdOverrides.setProperty(ARG_THROW_EXCEPTION_INVALID_STOPS, "false");
        cmdOverrides.setProperty(ARG_LENIENT_ARRIVAL_DEPARTURE, "true");
        if (this.getStopVerificationURL() != null) {
            cmdOverrides.setProperty("stopVerificationTask.path", this.getStopVerificationURL());
            cmdOverrides.setProperty("stopVerificationDistanceTask.path", this.getStopVerificationURL());
        }
        String stopMappingUrl = getStopMappingUrl();
        if (stopMappingUrl != null) {
            cmdOverrides.setProperty("stopConsolidationFileTask.stopConsolidationUrl", stopMappingUrl);
        }
        creator.setAdditionalBeanPropertyOverrides(cmdOverrides);
        BeanDefinitionBuilder propertyOverrides = BeanDefinitionBuilder.genericBeanDefinition(PropertyOverrideConfigurer.class);
        propertyOverrides.addPropertyValue("properties", cmdOverrides);
        beans.put("myCustomPropertyOverrides", propertyOverrides.getBeanDefinition());
        // manage our own context to recover from exceptions
        Map<String, BeanDefinition> contextBeans = new HashMap<String, BeanDefinition>();
        contextBeans.putAll(beans);
        context = ContainerLibrary.createContext(contextPaths, contextBeans);
        creator.setContext(context);
        response.addStatusMessage("building bundle");
        monitorStatus(response, creator.getStatusMessages());
        creator.run();
        demonitorStatus();
        // If this is a rebuild of a bundle, re-use the previous bundle id.
        updateBundleId(request, response);
        response.addStatusMessage("bundle build complete");
        return 0;
    } catch (Exception e) {
        _log.error(e.toString(), e);
        response.setException(e);
        return 1;
    } catch (Throwable t) {
        _log.error(t.toString(), t);
        response.setException(new RuntimeException(t.toString()));
        return -1;
    } finally {
        if (context != null) {
            try {
                /*
           * here we cleanup the spring context so we can process follow on requests.
           */
                context.stop();
                context.close();
            } catch (Throwable t) {
                _log.error("buried context close:", t);
            }
        }
        // restore standard out
        deconfigureLogging(System.out);
        System.setOut(stdOut);
        if (logFile != null) {
            logFile.close();
        }
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) PrintStream(java.io.PrintStream) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Properties(java.util.Properties) RemoteConnectFailureException(org.springframework.remoting.RemoteConnectFailureException) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) FederatedTransitDataBundleCreator(org.onebusaway.transit_data_federation.bundle.FederatedTransitDataBundleCreator) GtfsBundle(org.onebusaway.transit_data_federation.bundle.model.GtfsBundle) FileOutputStream(java.io.FileOutputStream) GtfsRelationalDaoImpl(org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 2 with GtfsRelationalDaoImpl

use of org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl in project OpenTripPlanner by opentripplanner.

the class GtfsModule method buildGraph.

@Override
public void buildGraph(Graph graph, HashMap<Class<?>, Object> extra) {
    // we're about to add another agency to the graph, so clear the cached timezone
    // in case it should change
    // OTP doesn't currently support multiple time zones in a single graph;
    // at least this way we catch the error and log it instead of silently ignoring
    // because the time zone from the first agency is cached
    graph.clearTimeZone();
    MultiCalendarServiceImpl service = new MultiCalendarServiceImpl();
    GtfsStopContext stopContext = new GtfsStopContext();
    try {
        for (GtfsBundle gtfsBundle : gtfsBundles) {
            // apply global defaults to individual GTFSBundles (if globals have been set)
            if (cacheDirectory != null && gtfsBundle.cacheDirectory == null)
                gtfsBundle.cacheDirectory = cacheDirectory;
            if (useCached != null && gtfsBundle.useCached == null)
                gtfsBundle.useCached = useCached;
            GtfsMutableRelationalDao dao = new GtfsRelationalDaoImpl();
            GtfsContext context = GtfsLibrary.createContext(gtfsBundle.getFeedId(), dao, service);
            GTFSPatternHopFactory hf = new GTFSPatternHopFactory(context);
            hf.setStopContext(stopContext);
            hf.setFareServiceFactory(_fareServiceFactory);
            hf.setMaxStopToShapeSnapDistance(gtfsBundle.getMaxStopToShapeSnapDistance());
            loadBundle(gtfsBundle, graph, dao);
            CalendarServiceDataFactoryImpl csfactory = new CalendarServiceDataFactoryImpl();
            csfactory.setGtfsDao(dao);
            CalendarServiceData data = csfactory.createData();
            service.addData(data, dao);
            hf.subwayAccessTime = gtfsBundle.subwayAccessTime;
            hf.maxInterlineDistance = gtfsBundle.maxInterlineDistance;
            hf.run(graph);
            if (gtfsBundle.doesTransfersTxtDefineStationPaths()) {
                hf.createTransfersTxtTransfers();
            }
            if (gtfsBundle.linkStopsToParentStations) {
                hf.linkStopsToParentStations(graph);
            }
            if (gtfsBundle.parentStationTransfers) {
                hf.createParentStationTransfers();
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    // We need to save the calendar service data so we can use it later
    CalendarServiceData data = service.getData();
    graph.putService(CalendarServiceData.class, data);
    graph.updateTransitFeedValidity(data);
    graph.hasTransit = true;
    graph.calculateTransitCenter();
}
Also used : GtfsMutableRelationalDao(org.onebusaway.gtfs.services.GtfsMutableRelationalDao) CalendarServiceData(org.onebusaway.gtfs.model.calendar.CalendarServiceData) GtfsBundle(org.opentripplanner.graph_builder.model.GtfsBundle) GtfsContext(org.opentripplanner.gtfs.GtfsContext) GtfsStopContext(org.opentripplanner.routing.edgetype.factory.GtfsStopContext) GtfsRelationalDaoImpl(org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl) GTFSPatternHopFactory(org.opentripplanner.routing.edgetype.factory.GTFSPatternHopFactory) IOException(java.io.IOException) CalendarServiceDataFactoryImpl(org.opentripplanner.calendar.impl.CalendarServiceDataFactoryImpl) MultiCalendarServiceImpl(org.opentripplanner.calendar.impl.MultiCalendarServiceImpl)

Example 3 with GtfsRelationalDaoImpl

use of org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl in project onebusaway-gtfs-modules by OneBusAway.

the class GtfsMerger method run.

public void run(List<File> inputPaths, File outputPath) throws IOException {
    GtfsRelationalDaoImpl mergedDao = new GtfsRelationalDaoImpl();
    mergedDao.setPackShapePoints(true);
    mergedDao.setPackStopTimes(true);
    List<EntityMergeStrategy> strategies = new ArrayList<EntityMergeStrategy>();
    buildStrategies(strategies);
    /**
     * For each entity merge strategy, we keep track of a mapping from raw GTFS
     * ids to entities, if the particular entity type has an identifier. This
     * will be used to detect id conflicts between subsequent runs of each merge
     * strategy on different feeds. We can't use the AgencyAndId ids in the DAO
     * because it might be possible for two entities with the same id but
     * different agency prefixes to sneak in. Since we ultimately serialize the
     * data to a GTFS feed with no agency prefixes, we need to track the raw id.
     */
    Map<EntityMergeStrategy, Map<String, Object>> rawEntityIdMapsByMergeStrategy = new HashMap<EntityMergeStrategy, Map<String, Object>>();
    for (EntityMergeStrategy strategy : strategies) {
        rawEntityIdMapsByMergeStrategy.put(strategy, new HashMap<String, Object>());
    }
    /**
     * We iterate over the input feeds in reverse order, such that entities from
     * the newest feeds are added first and older entities are potentially
     * dropped.
     */
    long newestFile = Long.MIN_VALUE;
    for (int index = inputPaths.size() - 1; index >= 0; --index) {
        File inputPath = inputPaths.get(index);
        String prefix = getIndexAsPrefix(index, inputPaths.size());
        FileTime fileTime = null;
        if (inputPath.isFile()) {
            fileTime = ((FileTime) Files.readAttributes(inputPath.toPath(), "lastModifiedTime").get("lastModifiedTime"));
            if (fileTime != null && fileTime.toMillis() > newestFile) {
                newestFile = fileTime.toMillis();
            }
        }
        _log.info("reading input: " + inputPath + " with lastModifiedTime " + fileTime);
        GtfsReader reader = new GtfsReader();
        reader.setInputLocation(inputPath);
        GtfsRelationalDaoImpl dao = new GtfsRelationalDaoImpl();
        dao.setPackShapePoints(true);
        dao.setPackStopTimes(true);
        reader.setEntityStore(dao);
        reader.run();
        for (EntityMergeStrategy strategy : strategies) {
            _log.info("strategy=" + strategy.getClass());
            GtfsMergeContext context = new GtfsMergeContext(dao, mergedDao, prefix, rawEntityIdMapsByMergeStrategy.get(strategy));
            strategy.merge(context);
        }
    }
    _log.info("writing merged output: " + outputPath);
    GtfsWriter writer = new GtfsWriter();
    writer.setOutputLocation(outputPath);
    writer.run(mergedDao);
    if (outputPath.isFile()) {
        _log.info("setting merged file lastModified to " + new Date(newestFile));
        Files.setAttribute(outputPath.toPath(), "lastModifiedTime", FileTime.fromMillis(newestFile));
    } else {
        _log.info("outputPath not a file, skipping");
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FileTime(java.nio.file.attribute.FileTime) Date(java.util.Date) GtfsReader(org.onebusaway.gtfs.serialization.GtfsReader) GtfsRelationalDaoImpl(org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl) EntityMergeStrategy(org.onebusaway.gtfs_merge.strategies.EntityMergeStrategy) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File) GtfsWriter(org.onebusaway.gtfs.serialization.GtfsWriter)

Example 4 with GtfsRelationalDaoImpl

use of org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl in project onebusaway-gtfs-modules by OneBusAway.

the class FareAtrributeAgencyTest method testAgenciesOnBartGtfs.

@Test
public void testAgenciesOnBartGtfs() throws Exception {
    GtfsRelationalDaoImpl dao = new GtfsRelationalDaoImpl();
    GtfsReader gtfsReader = new GtfsReader();
    gtfsReader.setEntityStore(dao);
    gtfsReader.setInputLocation(GtfsTestData.getBartGtfs());
    gtfsReader.run();
    final Collection<FareAttribute> fareAttributes = dao.getAllFareAttributes();
    final Set<String> agencyIdsInFareAttr = fareAttributes.stream().map(FareAttribute::getAgencyId).collect(Collectors.toSet());
    boolean isContainsBothExpectedAgencies = agencyIdsInFareAttr.contains("AirBART") && agencyIdsInFareAttr.contains("BART");
    if (!isContainsBothExpectedAgencies) {
        throw new Exception("Does not contain one of the expected agencies in the fare attributes");
    }
}
Also used : FareAttribute(org.onebusaway.gtfs.model.FareAttribute) GtfsReader(org.onebusaway.gtfs.serialization.GtfsReader) GtfsRelationalDaoImpl(org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl) Test(org.junit.Test)

Example 5 with GtfsRelationalDaoImpl

use of org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl in project onebusaway-gtfs-modules by OneBusAway.

the class UpdateLibrary method clearDaoCache.

public static void clearDaoCache(GtfsRelationalDao dao) {
    if (dao instanceof GtfsRelationalDaoImpl) {
        GtfsRelationalDaoImpl daoImpl = (GtfsRelationalDaoImpl) dao;
        daoImpl.clearAllCaches();
    }
}
Also used : GtfsRelationalDaoImpl(org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl)

Aggregations

GtfsRelationalDaoImpl (org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl)34 Test (org.junit.Test)19 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)13 Agency (org.onebusaway.gtfs.model.Agency)10 Trip (org.onebusaway.gtfs.model.Trip)9 GtfsReader (org.onebusaway.gtfs.serialization.GtfsReader)8 GtfsMutableRelationalDao (org.onebusaway.gtfs.services.GtfsMutableRelationalDao)7 CalendarServiceData (org.onebusaway.gtfs.model.calendar.CalendarServiceData)6 File (java.io.File)5 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)5 GtfsTransformStrategy (org.onebusaway.gtfs_transformer.services.GtfsTransformStrategy)5 TransformContext (org.onebusaway.gtfs_transformer.services.TransformContext)5 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 Before (org.junit.Before)4 Route (org.onebusaway.gtfs.model.Route)3 ServiceCalendar (org.onebusaway.gtfs.model.ServiceCalendar)3 ServiceCalendarDate (org.onebusaway.gtfs.model.ServiceCalendarDate)3 Stop (org.onebusaway.gtfs.model.Stop)3 IOException (java.io.IOException)2