<commit_before>require 'threadlock'

Thread.abort_on_exception = true

module Hegemon
  
  #***
  # Accessor functions
  #***
  
  def state;      @_hegemon_state;                   end
  def states;     @_hegemon_states.keys;             end
  def state_obj;  @_hegemon_states[@_hegemon_state]; end
  def state_objs; @_hegemon_states.clone;            end
  
  threadlock :state, :states, :state_obj, :state_objs, :lock=>:@_hegemon_lock
  
  #***
  # Declarative functions
  #***
  # Run these inside of your object's #initialize method
  #  to set the parameters of the state machine
  # All user-provided action blocks will have the scope of the instance object
  #  in which the state machine parameters are initialized, and all parameters
  #  should be initialized in the same place (in #initialize, typically)
  #***
  
  ##
  # Bypass all transition requirements and actions to directly impose state +s+
  # This should not be used in the public API except to set the initial state
  def impose_state(s); @_hegemon_state = s; end
  threadlock :impose_state, :lock=>:@_hegemon_lock
  
  ##
  # Declare a state in the state machine
  # [+state+] The state name to use, as a symbol
  # [+&block+] The state's declarative block 
  #  (refer to Declarative Functions of HegemonState)
  def declare_state(state, &block)
    @_hegemon_states ||= Hash.new
    @_hegemon_states[state] = HegemonState.new(self, state, &block)
  end
  threadlock :declare_state, :lock=>:@_hegemon_lock
  
  # Attempt a transition from the current state to state +s+
  def request_state(s, *flags)
    return false unless @_hegemon_states[@_hegemon_state].transitions[s]
    @_hegemon_states[@_hegemon_state].transitions[s].try(*flags)
  end
  threadlock :request_state, :lock=>:@_hegemon_lock
  
  # Check for relevant state updates and do.
  #  Using :only_auto flag will ignore all transitions with auto_update false
  def update_state(*flags)
    return false unless @_hegemon_states[@_hegemon_state]
    trans = @_hegemon_states[@_hegemon_state].transitions
    trans = trans.select{|k,t| t.auto_update} if (flags.include? :only_auto)
    trans.each {|k,t| return true if t.try}<commit_msg>Started rdoc documentation work<commit_after>require 'threadlock'

Thread.abort_on_exception = true

module Hegemon
  
  #***
  # Accessor functions
  #***
  
  def state;      @_hegemon_state;                   end
  def states;     @_hegemon_states.keys;             end
  def state_obj;  @_hegemon_states[@_hegemon_state]; end
  def state_objs; @_hegemon_states.clone;            end
  
  threadlock :state, :states, :state_obj, :state_objs, :lock=>:@_hegemon_lock
  
  #***
  # Declarative functions
  #***
  # Run these inside of your object's #initialize method
  #  to set the parameters of the state machine
  # All user-provided action blocks will have the scope of the instance object
  #  in which the state machine parameters are initialized, and all parameters
  #  should be initialized in the same place (in #initialize, typically)
  #***
  
  ##
  # :category: Declarative Methods
  #
  # Declare a state in the state machine.
  #
  # [+state+]
  #   The state name to use, as a symbol
  # [+block+]
  #   The state's declarative block.  
  #   All code within gets evaluated not in its original binding,
  #   but in the context of a new HegemonState instance object.  
  #   For stable use, only call methods in this block that are
  #   listed in HegemonState@Declarative+Methods.
  # 
  # The state and associated state machine data and methods will be
  # associated with the object referred to by +self+ object in the context
  # in which the declarative method is called.
  # 
  # This means that in typical usage, the declarative methods listed 
  # in Hegemon@Declarative+Methods (including +state_declare+) should be 
  # called only from within an instance method, such as +initialize+.
  # 
  # The following example creates a skeleton state machine in each
  # +MyStateMachine+ instance object with two states: +:working+ and +:idle+.
  # 
  #   class MyStateMachine
  #     include Hegemon
  #     
  #     def initialize
  #       state_declare :working do
  #         # various HegemonState Declarative Methods
  #       end
  #       state_declare :idle do
  #         # various HegemonState Declarative Methods
  #       end
  #     end
  #   end
  # 
  def declare_state(state, &block)
    @_hegemon_states ||= Hash.new
    @_hegemon_states[state] = HegemonState.new(self, state, &block)
  end
  threadlock :declare_state, :lock=>:@_hegemon_lock
  
  ##
  # :category: Declarative Methods
  # Bypass all transition requirements and actions to directly impose state +s+
  # This should not be used in the public API except to set the initial state
  def impose_state(s); @_hegemon_state = s; end
  threadlock :impose_state, :lock=>:@_hegemon_lock
  
  # Attempt a transition from the current state to state +s+
  def request_state(s, *flags)
    return false unless @_hegemon_states[@_hegemon_state].transitions[s]
    @_hegemon_states[@_hegemon_state].transitions[s].try(*flags)
  end
  threadlock :request_state, :lock=>:@_hegemon_lock
  
  # Check for relevant state updates and do.
  #  Using :only_auto flag will ignore all transitions with auto_update false
  def update_state(*flags)
    return false unless @_hegemon_states[@_hegemon_state]
    trans = @_hegemon_states[@_hegemon_state].transitions
    trans = trans.select{|k,t| t.auto_update} if (flags.include? :only_auto)
    trans.each {|k,t| return true if t.try}
{'fraction_non_alphanumeric': 0.08690153568202348, 'fraction_numerical': 0.0, 'mean_word_length': 3.522875816993464, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '1493402', 'n_tokens_mistral': 1795, 'n_tokens_neox': 1758, 'n_words': 563}