<commit_before>require "logger"
require_relative "../lib/ellington"

#require "yell"
#Ellington.logger = Yell.new do |logger|
#  logger.adapter STDOUT, :level => [:info], :format => "[%d] [%L] [%h][%p][%t] %m"
#end
Ellington.logger = Logger.new($stdout)

class NumberWithHistory
  attr_reader :original_value, :current_value, :history
  def initialize(value)
    @original_value = value
    @current_value = value
    @history = []
  end

  def add(other)
    value = current_value
    @current_value = current_value + other
    history.push(:before => value, :after => current_value)
    current_value
  end
end

module AdditionStation
  def engage(passenger, options)
    if passenger.add 1
      pass passenger
    else
      fail passenger
    end
  rescue Exception => e
    error passenger
  end
end

# stations -----------------------------------------------------------------
class ExampleStation1 < Ellington::Station
  include AdditionStation
end

class ExampleStation2 < Ellington::Station
  include AdditionStation
end

class ExampleStation3 < Ellington::Station
  include AdditionStation
end

class ExampleStation4 < Ellington::Station
  include AdditionStation
end

class ExampleStation5 < Ellington::Station
  include AdditionStation
end

class ExampleStation6 < Ellington::Station
  include AdditionStation
end

class ExampleStation7 < Ellington::Station
  include AdditionStation
end

class ExampleStation8 < Ellington::Station
  include AdditionStation
end

class ExampleStation9 < Ellington::Station
  include AdditionStation
end

# lines --------------------------------------------------------------------
class ExampleLine1 < Ellington::Line
  one = ExampleStation1.new
  two = ExampleStation2.new
  three = ExampleStation3.new
  stations << one
  stations << two
  stations << three
  goal three.passed
end

class ExampleLine2 < Ellington::Line
  four = ExampleStation4.new
  five = ExampleStation5.new
  six = ExampleStation6.new
  stations << four
  stations << five
  stations << six
  goal six.passed
end

class ExampleLine3 < Ellington::Line
  seven = ExampleStation7.new
  eight = ExampleStation8.new
  nine = ExampleStation9.new
  stations << seven
  stations << eight
  stations << nine
  goal nine.passed
end

# routes -------------------------------------------------------------------
class ExampleRoute1 < Ellington::Route
  line_one = ExampleLine1.new
  line_two = ExampleLine2.new
  line_three = ExampleLine3.new

  lines << line_one
  lines << line_two
  lines << line_three

  goal line_two.passed, line_three.passed

  connect_to line_two, :if => line_one.passed
  connect_to line_three, :if => line_one.failed

  log_passenger_attrs :current_value
end

# conductor ---------------------------------------------------------------
class ExampleConductor < Ellington::Conductor

  def gather_passengers
    (0..999).to_a.sample(10).map do |num|
      num = NumberWithHistory.new(num)
      ticket = Ellington::Ticket.new
      passenger = Ellington::Passenger.new(num, ticket, route.states)
      passenger.current_state = route.initial_state
      passenger.lock
      passenger
    end
  end

end

if ENV["START"]
  route = ExampleRoute1.new
  conductor = ExampleConductor.new(route)
  conductor.start 2
end
<commit_msg>Updated the example to do basic math operations.<commit_after>require "logger"
require_relative "../lib/ellington"

#require "yell"
#Ellington.logger = Yell.new do |logger|
#  logger.adapter STDOUT, :level => [:info], :format => "[%d] [%L] [%h][%p][%t] %m"
#end
Ellington.logger = Logger.new($stdout)

class NumberWithHistory
  attr_reader :original_value, :current_value, :history
  def initialize(value)
    @original_value = value
    @current_value = value
    @history = []
  end

  def calc(operand, other)
    value = current_value
    @current_value = current_value.send(operand, other)
    history.push(:before => value, :after => current_value)
    current_value
  end
end

# stations -----------------------------------------------------------------
class Add10 < Ellington::Station
  def engage(passenger, options)
    raise if rand(100) == 0
    if rand(100) > 5
      passenger.calc :+, 10
      pass passenger
    else
      fail passenger
    end
  rescue
    error passenger
  end
end

class Add100 < Ellington::Station
  def engage(passenger, options)
    raise if rand(100) == 0
    if rand(100) > 5
      passenger.calc :+, 100
      pass passenger
    else
      fail passenger
    end
  rescue
    error passenger
  end
end

class Add1000 < Ellington::Station
  def engage(passenger, options)
    raise if rand(100) == 0
    if rand(100) > 5
      passenger.calc :+, 1000
      pass passenger
    else
      fail passenger
    end
  rescue
    error passenger
  end
end

class MultiplyBy10 < Ellington::Station
  def engage(passenger, options)
    raise if rand(100) == 0
    if rand(100) > 5
      passenger.calc :*, 10
      pass passenger
    else
      fail passenger
    end
  rescue
    error passenger
  end
end

class MultiplyBy100 < Ellington::Station
  def engage(passenger, options)
    raise if rand(100) == 0
    if rand(100) > 5
      passenger.calc :*, 100
      pass passenger
    else
      fail passenger
    end
  rescue
    error passenger
  end
end

class MultiplyBy1000 < Ellington::Station
  def engage(passenger, options)
    raise if rand(100) == 0
    if rand(100) > 5
      passenger.calc :*, 1000
      pass passenger
    else
      fail passenger
    end
  rescue
    error passenger
  end
end

class DivideBy10 < Ellington::Station
  def engage(passenger, options)
    raise if rand(100) == 0
    if rand(100) > 5
      passenger.calc :/, 10
      pass passenger
    else
      fail passenger
    end
  rescue
    error passenger
  end
end

class DivideBy100 < Ellington::Station
  def engage(passenger, options)
    raise if rand(100) == 0
    if rand(100) > 5
      passenger.calc :*, 100
      pass passenger
    else
      fail passenger
    end
  rescue
    error passenger
  end
end

class DivideBy1000 < Ellington::Station
  def engage(passenger, options)
    raise if rand(100) == 0
    if rand(100) > 5
      passenger.calc :*, 1000
      pass passenger
    else
      fail passenger
    end
  rescue
    error passenger
  end
end

# lines --------------------------------------------------------------------
class Addition < Ellington::Line
  stations << Add10.new
  stations << Add100.new
  stations << Add1000.new
  goal stations.last.passed
end

class Multiplication < Ellington::Line
  stations << MultiplyBy10.new
  stations << MultiplyBy100.new
  stations << MultiplyBy1000.new
  goal stations.last.passed
end

class Division < Ellington::Line
  stations << DivideBy10.new
  stations << DivideBy100.new
  stations << DivideBy1000.new
  goal stations.last.passed
end

# route -------------------------------------------------------------------
class BasicMath < Ellington::Route
  addition = Addition.new
  multiplication = Multiplication.new
  division = Division.new

  lines << addition
  lines << multiplication
  lines << division

  goal multiplication.passed, division.passed

  connect_to division, :if => addition.passed
  connect_to multiplication, :if => addition.failed

  log_passenger_attrs :current_value
end

# conductor ---------------------------------------------------------------
class NumberConductor < Ellington::Conductor

  def gather_passengers
    (0..999).to_a.sample(10).map do |num|
      num = NumberWithHistory.new(num)
      ticket = Ellington::Ticket.new
      passenger = Ellington::Passenger.new(num, ticket, route.states)
      passenger.current_state = route.initial_state
      passenger.lock
      passenger
    end
  end

end

if ENV["START"]
  route = BasicMath.new
  conductor = NumberConductor.new(route)
  conductor.start 1
end

{'fraction_non_alphanumeric': 0.14653846153846153, 'fraction_numerical': 0.02487179487179487, 'mean_word_length': 2.528267752148349, 'pattern_counts': {'":': 0, '<': 79, '<?xml version=': 0, '>': 25, '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': '2078547', 'n_tokens_mistral': 2388, 'n_tokens_neox': 2240, 'n_words': 637}