<commit_before>require 'tree'
require 'securerandom'
require 'gush/printable'
require 'gush/metadata'
require 'gush/edge'
require 'gush/node'

module Gush
  class Workflow < Node
    include Gush::Printable
    include Gush::Metadata

    attr_accessor :nodes

    def initialize(name, options = {})
      @name = name
      @nodes = []
      configure unless options[:configure] == false
    end

    def configure
    end

    def find_job(name)
      @nodes.find { |node| node.name == name.to_s || node.class.to_s == name.to_s }
    end

    def finished?
      nodes.all?(&:finished)
    end

    def running?
      nodes.any?(&:enqueued)
    end

    def failed?
      nodes.any?(&:failed)
    end

    def run(klass, deps = {})
      node = klass.new(klass.to_s)

      if deps[:after]
        parent = find_job(deps[:after])
        if parent.nil?
          raise "Job #{deps[:after]} does not exist in the graph. Register it first."
        end
        edge = Edge.new(parent, node)
        parent.connect_to(node)
        node.connect_from(parent)
      end

      if deps[:before]
        child = find_job(deps[:before])
        if child.nil?
          raise "Job #{deps[:before]} does not exist in the graph. Register it first."
        end

        edge = Edge.new(node, child)
        node.connect_to(child)
        child.connect_from(node)
      end

      @nodes << node
    end


    def to_json
      hash = {
        name: @name,
        klass: self.class.to_s,
        nodes: @nodes.map(&:as_json)
      }

      JSON.dump(hash)
    end

    def next_jobs<commit_msg>Allow passing arrays of jobs to before/after<commit_after>require 'tree'
require 'securerandom'
require 'gush/printable'
require 'gush/metadata'
require 'gush/edge'
require 'gush/node'

module Gush
  class Workflow < Node
    include Gush::Printable
    include Gush::Metadata

    attr_accessor :nodes

    def initialize(name, options = {})
      @name = name
      @nodes = []
      configure unless options[:configure] == false
    end

    def configure
    end

    def find_job(name)
      @nodes.find { |node| node.name == name.to_s || node.class.to_s == name.to_s }
    end

    def finished?
      nodes.all?(&:finished)
    end

    def running?
      nodes.any?(&:enqueued)
    end

    def failed?
      nodes.any?(&:failed)
    end

    def run(klass, deps = {})
      node = klass.new(klass.to_s)

      deps_after = [*deps[:after]]
      deps_after.each do |dep|
        parent = find_job(dep)
        if parent.nil?
          raise "Job #{dep} does not exist in the graph. Register it first."
        end
        edge = Edge.new(parent, node)
        parent.connect_to(node)
        node.connect_from(parent)
      end

      deps_before = [*deps[:before]]
      deps_before.each do |dep|
        child = find_job(dep)
        if child.nil?
          raise "Job #{dep} does not exist in the graph. Register it first."
        end

        edge = Edge.new(node, child)
        node.connect_to(child)
        child.connect_from(node)
      end

      @nodes << node
    end


    def to_json
      hash = {
        name: @name,
        klass: self.class.to_s,
        nodes: @nodes.map(&:as_json)
      }

      JSON.dump(hash)
    end

    def next_jobs
{'fraction_non_alphanumeric': 0.11152073732718894, 'fraction_numerical': 0.0, 'mean_word_length': 2.0601503759398496, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2089386', 'n_tokens_mistral': 1079, 'n_tokens_neox': 1052, 'n_words': 269}