All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.annot8.common.implementations.pipelines.SimplePipelineBuilder Maven / Gradle / Ivy

There is a newer version: 0.2.0
Show newest version
/* Annot8 (annot8.io) - Licensed under Apache-2.0. */
package io.annot8.common.implementations.pipelines;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.annot8.common.implementations.context.SimpleContext;
import io.annot8.common.implementations.factories.ItemCreator;
import io.annot8.core.components.Annot8Component;
import io.annot8.core.components.Processor;
import io.annot8.core.components.Resource;
import io.annot8.core.components.Source;
import io.annot8.core.exceptions.IncompleteException;
import io.annot8.core.settings.Settings;

public class SimplePipelineBuilder implements PipelineBuilder {

  private static final Logger LOGGER = LoggerFactory.getLogger(SimplePipeline.class);

  protected ItemQueue itemQueue;
  protected ItemCreator itemCreator;

  // Use a linked hash map so the addition order = configuration order
  private final Map> sourcesToConfiguration = new LinkedHashMap<>();
  private final Map> processorToConfiguration =
      new LinkedHashMap<>();
  private final Map> resourcesToConfiguration =
      new LinkedHashMap<>();
  private final Map resourcesToId = new HashMap<>();

  public PipelineBuilder withItemQueue(ItemQueue itemQueue) {
    this.itemQueue = itemQueue;
    return this;
  }

  public PipelineBuilder withItemCreator(ItemCreator itemCreator) {
    this.itemCreator = itemCreator;
    return this;
  }

  public PipelineBuilder addResource(
      final String id, final Resource resource, final Collection configuration) {
    resourcesToConfiguration.put(resource, nonNullCollection(configuration));
    resourcesToId.put(resource, id);
    return this;
  }

  public PipelineBuilder addSource(final Source source, final Collection configuration) {
    sourcesToConfiguration.put(source, nonNullCollection(configuration));
    return this;
  }

  public PipelineBuilder addProcessor(
      final Processor processor, final Collection configuration) {
    processorToConfiguration.put(processor, nonNullCollection(configuration));
    return this;
  }

  public Pipeline build() throws IncompleteException {

    if (itemCreator == null) {
      throw new IncompleteException("No item creator specified");
    }

    if (itemQueue == null) {
      LOGGER.warn("No item queue specified, (using SimpleItemQueue as an basic implementation)");
      this.itemQueue = new SimpleItemQueue();
    }

    Map configuredResources = new HashMap<>();

    resourcesToConfiguration.forEach(
        (resource, settings) -> {
          if (configureComponent(configuredResources, resource, settings)) {
            String id = resourcesToId.get(resource);
            configuredResources.put(id, resource);
          }
        });

    List configuredSources =
        configureAllComponents(configuredResources, sourcesToConfiguration);
    List configurePipelines =
        configureAllComponents(configuredResources, processorToConfiguration);

    return new SimplePipeline(
        itemCreator, itemQueue, configuredResources, configuredSources, configurePipelines);
  }

  protected  List configureAllComponents(
      Map configuredResources,
      Map> componentToConfiguration) {

    return componentToConfiguration
        .entrySet()
        .stream()
        .filter(e -> configureComponent(configuredResources, e.getKey(), e.getValue()))
        .map(Map.Entry::getKey)
        .collect(Collectors.toList());
  }

  protected boolean configureComponent(
      Map configuredResources,
      final Annot8Component component,
      final Collection configuration) {

    // TODO: COmpletely ignore capabilties here.. we could check for resources etc

    try {
      final SimpleContext context = new SimpleContext(configuration, configuredResources);
      component.configure(context);
      return true;
    } catch (final Exception e) {
      LOGGER.error("Failed to configure component {}", component.getClass().getName(), e);
    }
    return false;
  }

  private Collection nonNullCollection(Collection configuration) {
    return configuration == null ? Collections.emptySet() : configuration;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy