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

io.rouz.flo.Task Maven / Gradle / Ivy

package io.rouz.flo;

import com.google.auto.value.AutoValue;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;

/**
 * TODO:
 * d make inputs lazily instantiated to allow short circuiting graph generation
 * d output task graph
 * . external outputs - outputs that might already be available (ie a file on disk)
 *   - can be implemented with a regular task, but better support can be added
 * . identity task
 *
 * @param   A type carrying the execution metadata of this task
 */
@AutoValue
public abstract class Task implements Serializable {

  public abstract TaskId id();

  public abstract Class type();

  abstract EvalClosure code();

  abstract Fn>> lazyInputs();

  public List> inputs() {
    return lazyInputs().get();
  }

  public Stream> inputsInOrder() {
    return inputsInOrder(new HashSet<>());
  }

  private Stream> inputsInOrder(Set visits) {
    return inputs().stream()
        .filter(input -> !visits.contains(input.id()))
        .flatMap(input -> {
          visits.add(input.id());
          return Stream.concat(
              input.inputsInOrder(visits),
              Stream.of(input)
          );
        });
  }

  public static NamedTaskBuilder named(String taskName, Object... args) {
    return new NTB(TaskId.create(taskName, args));
  }

  public static  Task create(Fn code, Class type, String taskName, Object... args) {
    return create(Collections::emptyList, type, tc -> tc.value(code), TaskId.create(taskName, args));
  }

  static  Task create(
      Fn>> inputs, Class type, EvalClosure code, TaskId taskId) {
    return new AutoValue_Task<>(taskId, type, code, inputs);
  }

  /**
   * This is only needed because a generic lambda can not be implemented
   * in {@link #named(String, Object...)}.
   */
  private static final class NTB implements NamedTaskBuilder {

    private final TaskId taskId;

    private NTB(TaskId taskId) {
      this.taskId = taskId;
    }

    @Override
    public  TaskBuilder ofType(Class type) {
      return TaskBuilderImpl.rootBuilder(taskId, type);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy