<commit_before>#!/usr/bin/ruby
#
# This file contains the MUES::IOEventFilter class, which is an abstract base
# class for filter objects in a MUES::IOEventStream. The filters act as links in
# a Chain of Responsibility
# (http://patterndigest.com/patterns/ChainOfResponsibili.html), acting on the
# contents of MUES::IOEvent objects which are passed up and down the stream,
# modifying them, creating other events based on them, changing their own
# internal state or the state of an associated object based on them, or ignoring
# them, depending on the task which the filter is supposed to accomplish.
# 
# This class also fulfills the <tt>Subject</tt> role of the
# <strong>Observer</strong> design pattern
# (http://patterndigest.com/patterns/Observer.html), with the
# MUES::IOEventStream as the <tt>Observer</tt> part. Filters notify the streams
# they are associated with when they have pending events.
#
# When you define a derivative of IOEventFilter, you will need to define a class
# constant called 'DefaultSortPosition', which is a number between 0 and 1000,
# inclusive. This number is used by the Comparable interface to determine the
# order in which filters should be sorted, and therefore the order in which they
# are given the IOEvents which have entered the stream. Lower values means the
# filter will sort more towards the <strong>output</strong> side of the stream,
# higher values sort towards the <strong>input</strong> side, and middle values
# generally act as modifying, macro, or duplicative filters.
#
# You can also pass a different sort order value for a specific instance to this
# class's #initialize method via <tt>super()</tt>.
#
# == Synopsis
# 
#   require "mues/filters/IOEventFilter"
# 
#   class MyFilter < MUES::IOEventFilter
#     DefaultSortPosition = 550
#     ...
#	  def handleInputEvents( *events )
#	    ...
#	  end
#
#	  def handleOutputEvents( *events )
#	    ...
#	  end
#   end
# 
# == Rcsid
# 
# $Id: ioeventfilter.rb,v 1.16 2002/09/28 10:42:51 deveiant Exp $
# 
# == Authors
# 
# * <NAME> <<EMAIL>>
# 
#:include: COPYRIGHT
#
#---
#
# Please see the file COPYRIGHT for licensing details.
#

require "observer"

require "mues/Object"
require "mues/Mixins"
require "mues/Events"
require "mues/Exceptions"


module MUES

	# An abstract base filter class for MUES::IOEventStream objects. This class
	# implements the Comparable, Observable, and MUES::Debuggable interfaces.
	class IOEventFilter < Object ; implements Observable, Comparable, MUES::Debuggable, MUES::AbstractClass

		include MUES::TypeCheckFunctions

		### Class constants
		Version = /([\d\.]+)/.match( %q$Revision: 1.16 $ )[1]
		Rcsid = %q$Id: ioeventfilter.rb,v 1.16 2002/09/28 10:42:51 deveiant Exp $
		DefaultSortPosition = 500


		### Create a new filter, optionally setting the sort position to
		### the value specified.
		def initialize( order=nil )
			@sortPosition = if order.nil?
								if self.class.const_defined? "DefaultSortPosition"
									self.class.const_get :DefaultSortPosition
								else
									DefaultSortPosition
								end
							else
								order
							end

			raise TypeError, "Sort position: expected a Fixnum, not a #{@sortPosition.class.name}" unless
				@sortPosition.is_a?( ::Fixnum )
			raise ArgumentError, "Sort position must be between 0 and 1000" unless
				0 <= @sortPosition && @sortPosition <= 1000
			
			@queuedInputEvents = []
			@queuedInputEventsMutex = Mutex.new
			@queuedOutputEvents = []
			@queuedOutputEventsMutex = Mutex.new

			@isFinished = false

			super()
		end


		######
		public
		######

		# The sort position of the filter, which must be a Fixnum between 0 and 1000.
		attr_accessor	:sortPosition

		# The Array of input events which are pending injection into the stream.
		attr_reader		:queuedInputEvents

		# The Array of output events which are pending injection into the stream.
		attr_reader		:queuedOutputEvents

		# A flag for indicating that the filter is finished its role in the
		# stream, and should be removed.
		attr_reader		:isFinished
		alias :isFinished? :isFinished


		### Start filter notifications for the specified stream. Returns an
		### array of events to propagated for startup. Filter subclasses can
		### override this method if they need to do setup tasks before being
		### added to the stream, but they should be sure to call this class's
		### implementation via <tt>super()</tt>, or the filter will not notify
		### the stream when events are pending.
		def start( streamObject )
			add_observer( streamObject )
			[]
		end


		### Stop the filter notifications for the specified stream, returning
		### any final events which should be dispatched on behalf of the
		### filter. Filter subclasses can override this method if they need to
		### do cleanup tasks before being removed from the stream, but they
		### should be sure to call this class's implementation via
		### <tt>super()</tt>, or the filter will continue to notify the stream
		### of pending events.
		def stop( streamObject )
			delete_observer( streamObject )
			@isFinished = true if count_observers.zero?
			[]
		end


		### Add input <tt>events</tt> for this filter to the queue of pending
		### events and notify the containing MUES::IOEventStream/s that there are
		### input events pending.
		def queueInputEvents( *events )
			events.flatten!
			checkEachType( events, MUES::InputEvent )

			debugMsg( 1, "Queueing #{events.size} input events." )
			@queuedInputEventsMutex.synchronize {
				@queuedInputEvents += events
				changed( true ) unless @queuedInputEvents.empty?
			}
			debugMsg( 2, "#{@queuedInputEvents.size} input events now queued." )

			notify_observers( self, 'input' )
			return @queuedInputEvents.size
		end


		### Add output <tt>events</tt> for this filter to the queue of pending
		### events and notify the containing MUES::IOEventStream/s that there are
		### output events pending.
		def queueOutputEvents( *events )
			events.flatten!
			checkEachType( events, MUES::OutputEvent )

			debugMsg( 1, "Queueing #{events.size} output events." )
			@queuedOutputEventsMutex.synchronize {
				@queuedOutputEvents += events
				changed( true ) unless @queuedOutputEvents.empty?
			}
			debugMsg( 2, "#{@queuedOutputEvents.size} output events now queued." )

			notify_observers( self, 'output' )
			return @queuedOutputEvents.size
		end


		### Comparison -- Returns -1, 0, or 1 if the receiver sorts higher,
		### equal to, or lower than the other filter object, respectively,
		### according to its sort position.
		def <=>( otherFilter )
			checkType( otherFilter, MUES::IOEventFilter )
			return ( @sortPosition <=> otherFilter.sortPosition ).nonzero? ||
				@muesid <=> otherFilter.muesid
		end


		### Process the specified input events and return any which are
		### unhandled or new. Events which are returned will be injected into
		### the stream. Filter subclasses should override this method if they
		### wish to process input events.
		def handleInputEvents( *events )
			@queuedInputEventsMutex.synchronize {
				events += @queuedInputEvents
				@queuedInputEvents.clear
			}
			return events.flatten
		end


		### Process the specified output events and return any which are
		### unhandled or new. Events which are returned will be injected into
		### the stream. Filter subclasses should override this method if they
		### wish to process output events.
		def handleOutputEvents( *events )
			@queuedOutputEventsMutex.synchronize {
				events += @queuedOutputEvents
				@queuedOutputEvents.clear
			}
			return events.flatten
		end


		### Return a stringified description of the filter.
		def to_s
			"%s filter [%d]" % [ self.class.name, @sortPosition ]
		end



		#########
		protected
		#########

		### Set the finished flag to <tt>true</tt>.
		def finish
			@isFinished = true
		end


	end # class IOEventFilter
end # module MUES

<commit_msg>- Added a finished? alias for @isFinished.<commit_after>#!/usr/bin/ruby
#
# This file contains the MUES::IOEventFilter class, which is an abstract base
# class for filter objects in a MUES::IOEventStream. The filters act as links in
# a Chain of Responsibility
# (http://patterndigest.com/patterns/ChainOfResponsibili.html), acting on the
# contents of MUES::IOEvent objects which are passed up and down the stream,
# modifying them, creating other events based on them, changing their own
# internal state or the state of an associated object based on them, or ignoring
# them, depending on the task which the filter is supposed to accomplish.
# 
# This class also fulfills the <tt>Subject</tt> role of the
# <strong>Observer</strong> design pattern
# (http://patterndigest.com/patterns/Observer.html), with the
# MUES::IOEventStream as the <tt>Observer</tt> part. Filters notify the streams
# they are associated with when they have pending events.
#
# When you define a derivative of IOEventFilter, you will need to define a class
# constant called 'DefaultSortPosition', which is a number between 0 and 1000,
# inclusive. This number is used by the Comparable interface to determine the
# order in which filters should be sorted, and therefore the order in which they
# are given the IOEvents which have entered the stream. Lower values means the
# filter will sort more towards the <strong>output</strong> side of the stream,
# higher values sort towards the <strong>input</strong> side, and middle values
# generally act as modifying, macro, or duplicative filters.
#
# You can also pass a different sort order value for a specific instance to this
# class's #initialize method via <tt>super()</tt>.
#
# == Synopsis
# 
#   require "mues/filters/IOEventFilter"
# 
#   class MyFilter < MUES::IOEventFilter
#     DefaultSortPosition = 550
#     ...
#	  def handleInputEvents( *events )
#	    ...
#	  end
#
#	  def handleOutputEvents( *events )
#	    ...
#	  end
#   end
# 
# == Rcsid
# 
# $Id: ioeventfilter.rb,v 1.17 2002/10/04 05:08:44 deveiant Exp $
# 
# == Authors
# 
# * <NAME> <<EMAIL>>
# 
#:include: COPYRIGHT
#
#---
#
# Please see the file COPYRIGHT for licensing details.
#

require "observer"

require "mues/Object"
require "mues/Mixins"
require "mues/Events"
require "mues/Exceptions"


module MUES

	# An abstract base filter class for MUES::IOEventStream objects. This class
	# implements the Comparable, Observable, and MUES::Debuggable interfaces.
	class IOEventFilter < Object ; implements Observable, Comparable, MUES::Debuggable, MUES::AbstractClass

		include MUES::TypeCheckFunctions

		### Class constants
		Version = /([\d\.]+)/.match( %q$Revision: 1.17 $ )[1]
		Rcsid = %q$Id: ioeventfilter.rb,v 1.17 2002/10/04 05:08:44 deveiant Exp $
		DefaultSortPosition = 500


		### Create a new filter, optionally setting the sort position to
		### the value specified.
		def initialize( order=nil )
			@sortPosition = if order.nil?
								if self.class.const_defined? "DefaultSortPosition"
									self.class.const_get :DefaultSortPosition
								else
									DefaultSortPosition
								end
							else
								order
							end

			raise TypeError, "Sort position: expected a Fixnum, not a #{@sortPosition.class.name}" unless
				@sortPosition.is_a?( ::Fixnum )
			raise ArgumentError, "Sort position must be between 0 and 1000" unless
				0 <= @sortPosition && @sortPosition <= 1000
			
			@queuedInputEvents = []
			@queuedInputEventsMutex = Mutex.new
			@queuedOutputEvents = []
			@queuedOutputEventsMutex = Mutex.new

			@isFinished = false

			super()
		end


		######
		public
		######

		# The sort position of the filter, which must be a Fixnum between 0 and 1000.
		attr_accessor	:sortPosition

		# The Array of input events which are pending injection into the stream.
		attr_reader		:queuedInputEvents

		# The Array of output events which are pending injection into the stream.
		attr_reader		:queuedOutputEvents

		# A flag for indicating that the filter is finished its role in the
		# stream, and should be removed. Aliases: #isFinished?, #finished?
		attr_reader		:isFinished
		alias :isFinished? :isFinished
		alias :finished? :isFinished


		### Start filter notifications for the specified stream. Returns an
		### array of events to propagated for startup. Filter subclasses can
		### override this method if they need to do setup tasks before being
		### added to the stream, but they should be sure to call this class's
		### implementation via <tt>super()</tt>, or the filter will not notify
		### the stream when events are pending.
		def start( streamObject )
			add_observer( streamObject )
			[]
		end


		### Stop the filter notifications for the specified stream, returning
		### any final events which should be dispatched on behalf of the
		### filter. Filter subclasses can override this method if they need to
		### do cleanup tasks before being removed from the stream, but they
		### should be sure to call this class's implementation via
		### <tt>super()</tt>, or the filter will continue to notify the stream
		### of pending events.
		def stop( streamObject )
			delete_observer( streamObject )
			@isFinished = true if count_observers.zero?
			[]
		end


		### Add input <tt>events</tt> for this filter to the queue of pending
		### events and notify the containing MUES::IOEventStream/s that there are
		### input events pending.
		def queueInputEvents( *events )
			events.flatten!
			checkEachType( events, MUES::InputEvent )

			debugMsg( 1, "Queueing #{events.size} input events." )
			@queuedInputEventsMutex.synchronize {
				@queuedInputEvents += events
				changed( true ) unless @queuedInputEvents.empty?
			}
			debugMsg( 2, "#{@queuedInputEvents.size} input events now queued." )

			notify_observers( self, 'input' )
			return @queuedInputEvents.size
		end


		### Add output <tt>events</tt> for this filter to the queue of pending
		### events and notify the containing MUES::IOEventStream/s that there are
		### output events pending.
		def queueOutputEvents( *events )
			events.flatten!
			checkEachType( events, MUES::OutputEvent )

			debugMsg( 1, "Queueing #{events.size} output events." )
			@queuedOutputEventsMutex.synchronize {
				@queuedOutputEvents += events
				changed( true ) unless @queuedOutputEvents.empty?
			}
			debugMsg( 2, "#{@queuedOutputEvents.size} output events now queued." )

			notify_observers( self, 'output' )
			return @queuedOutputEvents.size
		end


		### Comparison -- Returns -1, 0, or 1 if the receiver sorts higher,
		### equal to, or lower than the other filter object, respectively,
		### according to its sort position.
		def <=>( otherFilter )
			checkType( otherFilter, MUES::IOEventFilter )
			return ( @sortPosition <=> otherFilter.sortPosition ).nonzero? ||
				@muesid <=> otherFilter.muesid
		end


		### Process the specified input events and return any which are
		### unhandled or new. Events which are returned will be injected into
		### the stream. Filter subclasses should override this method if they
		### wish to process input events.
		def handleInputEvents( *events )
			@queuedInputEventsMutex.synchronize {
				events += @queuedInputEvents
				@queuedInputEvents.clear
			}
			return events.flatten
		end


		### Process the specified output events and return any which are
		### unhandled or new. Events which are returned will be injected into
		### the stream. Filter subclasses should override this method if they
		### wish to process output events.
		def handleOutputEvents( *events )
			@queuedOutputEventsMutex.synchronize {
				events += @queuedOutputEvents
				@queuedOutputEvents.clear
			}
			return events.flatten
		end


		### Return a stringified description of the filter.
		def to_s
			"%s filter [%d]" % [ self.class.name, @sortPosition ]
		end



		#########
		protected
		#########

		### Set the finished flag to <tt>true</tt>.
		def finish
			@isFinished = true
		end


	end # class IOEventFilter
end # module MUES


{'fraction_non_alphanumeric': 0.09435270389512164, 'fraction_numerical': 0.008949955880499181, 'mean_word_length': 3.752021563342318, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 59, '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': '502015', 'n_tokens_mistral': 5222, 'n_tokens_neox': 4491, 'n_words': 1758}