Class: OMF::Rete::ResultTupleStream

Inherits:
AbstractTupleStream show all
Defined in:
lib/omf_rete/tuple_stream.rb

Overview

A result tuple stream calls the associated processing block for every incoming tuple.

TODO: This should really be a subclass of ProcessingTupleStream, but we have supress_duplicates in this class which may be useful for ProcessingTupleStream as well.

Instance Attribute Summary

Attributes inherited from AbstractTupleStream

#description, #source

Instance Method Summary collapse

Methods inherited from AbstractTupleStream

#describe, #detach, #index_for_binding

Constructor Details

#initialize(description, supress_duplicates = true, &block) ⇒ ResultTupleStream

Returns a new instance of ResultTupleStream.



149
150
151
152
153
154
155
# File 'lib/omf_rete/tuple_stream.rb', line 149

def initialize(description, supress_duplicates = true, &block)
  super description
  @block = block
  if supress_duplicates
    @results = Set.new
  end
end

Instance Method Details

#addTuple(tuple) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/omf_rete/tuple_stream.rb', line 170

def addTuple(tuple)
  if @result_map
    ta = @result_map.collect do |i| tuple[i] end
  else
    ta = tuple
  end
  rtuple = Tuple.new(ta, @description)
  if @results
    if @results.add?(ta)
      @block.arity == 1 ? @block.call(rtuple) : @block.call(rtuple, :add)
    end
  else
    @block.arity == 1 ? @block.call(rtuple) : @block.call(rtuple, :add)
  end
end

#check_for_tuple(tuple) ⇒ Object

Return true if tuple can be produced by this stream. A ResultStream only narrows a stream, so we need to potentially expand it (with nil) and pass it up to the source of this stream.



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/omf_rete/tuple_stream.rb', line 209

def check_for_tuple(tuple)
  if @sourcce
    # should check if +tuple+ has the same size as description
    if @result_map
      # need to expand
      unless @expand_map
        @expand_map = @source.description.collect do |name|
          index = @description.find_index do |n2| name == n2 end
        end
      end
      up_tuple = @expand_map.collect do |i| i nil? ? nil : tuple[i] end
    else
      up_tuple = tuple
    end
    @source.check_for_tuple(up_tuple)
  end
end

#clearObject



199
200
201
202
# File 'lib/omf_rete/tuple_stream.rb', line 199

def clear()
  @results.clear if @results
  @block.call(nil, :cleared) if @block.arity == 2
end

#removeTuple(tuple) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/omf_rete/tuple_stream.rb', line 186

def removeTuple(tuple)
  if @result_map
    ta = @result_map.collect do |i| tuple[i] end
  else
    ta = tuple
  end
  rtuple = Tuple.new(ta, @description)
  if @results
    @results.delete(ta)
  end
  @block.arity == 1 ? @block.call(rtuple) : @block.call(rtuple, :remove)
end

#source=(source) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/omf_rete/tuple_stream.rb', line 157

def source=(source)
  @source = source
  if @source.description != @description
    @result_map = @description.collect do |name|
      index = @source.description.find_index do |n2| name == n2 end
      if index.nil?
        raise "Unknown selector '#{name}'"
      end
      index
    end
  end
end