Search in sources :

Example 1 with TaskDefinition

use of org.onebusaway.transit_data_federation.bundle.model.TaskDefinition in project onebusaway-application-modules by camsys.

the class FederatedTransitDataBundleCreator method getTaskDefinitionsByName.

private Map<String, TaskDefinition> getTaskDefinitionsByName(Map<String, TaskDefinition> taskDefinitions) {
    Map<String, TaskDefinition> taskDefinitionsByTaskName = new HashMap<String, TaskDefinition>();
    for (TaskDefinition taskDefinition : taskDefinitions.values()) {
        String taskName = taskDefinition.getTaskName();
        if (taskName == null || taskName.trim().length() == 0) {
            throw new IllegalStateException("no taskName property defined for task definition: " + taskDefinition);
        }
        taskDefinitionsByTaskName.put(taskName, taskDefinition);
    }
    return taskDefinitionsByTaskName;
}
Also used : TaskDefinition(org.onebusaway.transit_data_federation.bundle.model.TaskDefinition) HashMap(java.util.HashMap)

Example 2 with TaskDefinition

use of org.onebusaway.transit_data_federation.bundle.model.TaskDefinition in project onebusaway-application-modules by camsys.

the class FederatedTransitDataBundleCreator method getTaskList.

private List<TaskDefinition> getTaskList(ApplicationContext context) throws UnknownTaskException {
    Map<String, TaskDefinition> taskDefinitions = context.getBeansOfType(TaskDefinition.class);
    Map<String, TaskDefinition> taskDefinitionsByTaskName = getTaskDefinitionsByName(taskDefinitions);
    DirectedGraph<String> graph = new DirectedGraph<String>();
    for (TaskDefinition taskDefinition : taskDefinitions.values()) {
        String taskName = taskDefinition.getTaskName();
        graph.addNode(taskName);
        String before = taskDefinition.getBeforeTaskName();
        if (before != null) {
            if (!taskDefinitionsByTaskName.containsKey(before))
                throw new UnknownTaskException(before);
            graph.addEdge(taskName, before);
        }
        List<String> afters = taskDefinition.getAfterTaskNames();
        if (!CollectionsLibrary.isEmpty(afters)) {
            for (String after : afters) {
                if (!taskDefinitionsByTaskName.containsKey(after))
                    throw new UnknownTaskException(after);
                graph.addEdge(after, taskName);
            }
        }
    }
    List<String> taskNames = graph.getTopologicalSort(null);
    List<TaskDefinition> taskDefinitionsInOrder = new ArrayList<TaskDefinition>();
    for (String taskName : taskNames) {
        TaskDefinition task = taskDefinitionsByTaskName.get(taskName);
        taskDefinitionsInOrder.add(task);
    }
    return taskDefinitionsInOrder;
}
Also used : TaskDefinition(org.onebusaway.transit_data_federation.bundle.model.TaskDefinition) DirectedGraph(org.onebusaway.transit_data_federation.impl.DirectedGraph) ArrayList(java.util.ArrayList)

Example 3 with TaskDefinition

use of org.onebusaway.transit_data_federation.bundle.model.TaskDefinition in project onebusaway-application-modules by camsys.

the class FederatedTransitDataBundleCreator method run.

/**
 * Build the bundle!
 *
 * @throws IOException
 * @throws ClassNotFoundException
 * @throws UnknownTaskException
 */
public void run() throws IOException, ClassNotFoundException, UnknownTaskException {
    _outputPath.mkdirs();
    setSystemProperties();
    ConfigurableApplicationContext context = _context;
    boolean closeContextOnCompletion = false;
    if (context == null) {
        List<String> contextPaths = getPrimaryApplicatonContextPaths();
        Map<String, BeanDefinition> contextBeans = getPrimaryBeanDefintions();
        context = ContainerLibrary.createContext(contextPaths, contextBeans);
        closeContextOnCompletion = true;
    }
    List<TaskDefinition> taskDefinitions = getTaskList(context);
    Set<String> taskNames = getReducedTaskList(taskDefinitions);
    // Clear cache files
    FederatedTransitDataBundle bundle = context.getBean(FederatedTransitDataBundle.class);
    clearExistingCacheFiles(bundle);
    int taskSize = taskNames.size();
    int i = 0;
    for (TaskDefinition def : taskDefinitions) {
        String taskName = def.getTaskName();
        if (taskNames.contains(taskName)) {
            i++;
            System.out.println("== " + taskName + " =====>");
            _status.addMessage("running task " + taskName + " (" + i + "/" + taskSize + ")");
            Runnable task = getTask(context, def.getTask(), def.getTaskBeanName());
            if (task == null)
                throw new IllegalStateException("unknown task bean with name: " + taskName);
            task.run();
        } else {
            Runnable task = getTask(context, def.getTaskWhenSkipped(), def.getTaskWhenSkippedBeanName());
            if (task != null) {
                System.out.println("== skipping " + taskName + " =====>");
                _status.addMessage("skipping task " + taskName);
                task.run();
            }
        }
    }
    // We don't need this context anymore
    if (closeContextOnCompletion) {
        context.stop();
        context.close();
        context = null;
    }
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) TaskDefinition(org.onebusaway.transit_data_federation.bundle.model.TaskDefinition) FederatedTransitDataBundle(org.onebusaway.transit_data_federation.services.FederatedTransitDataBundle)

Example 4 with TaskDefinition

use of org.onebusaway.transit_data_federation.bundle.model.TaskDefinition in project onebusaway-application-modules by camsys.

the class FederatedTransitDataBundleCreator method getReducedTaskList.

private Set<String> getReducedTaskList(List<TaskDefinition> taskDefinitions) throws UnknownTaskException {
    // Check task names first
    List<String> tasks = new ArrayList<String>();
    for (TaskDefinition taskDef : taskDefinitions) {
        if (!isTaskEnabled(taskDef))
            continue;
        String taskName = taskDef.getTaskName();
        tasks.add(taskName);
    }
    for (String task : _onlyTasks) {
        if (!tasks.contains(task))
            throw new UnknownTaskException(task);
    }
    for (String task : _skipTasks) {
        if (!tasks.contains(task))
            throw new UnknownTaskException(task);
    }
    if (_skipToTask != null) {
        int index = tasks.indexOf(_skipToTask);
        if (index == -1)
            throw new UnknownTaskException(_skipToTask);
        for (int i = 0; i < index; i++) tasks.remove(0);
    }
    if (!_onlyTasks.isEmpty())
        tasks.retainAll(_onlyTasks);
    tasks.removeAll(_skipTasks);
    return new HashSet<String>(tasks);
}
Also used : TaskDefinition(org.onebusaway.transit_data_federation.bundle.model.TaskDefinition) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Aggregations

TaskDefinition (org.onebusaway.transit_data_federation.bundle.model.TaskDefinition)4 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 DirectedGraph (org.onebusaway.transit_data_federation.impl.DirectedGraph)1 FederatedTransitDataBundle (org.onebusaway.transit_data_federation.services.FederatedTransitDataBundle)1 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1