Class: OMF::Rete::ProcessingTupleStream

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

Overview

A processing tuple stream calls the associated processing block for every incoming tuple and forwards what is being returned by this block to the receiver. The return value of the block is assumed by a tuple as well. If the return value is nil, nothing is forwarded and the incoming tuple is essentially dropped.

Direct Known Subclasses

FilterTupleStream

Instance Attribute Summary collapse

Attributes inherited from AbstractTupleStream

#description, #source

Instance Method Summary collapse

Methods inherited from AbstractTupleStream

#check_for_tuple, #describe, #detach, #index_for_binding

Constructor Details

#initialize(project_pattern, out_description = project_pattern, in_description = nil, receiver = nil, &block) ⇒ ProcessingTupleStream

Returns a new instance of ProcessingTupleStream.



57
58
59
60
61
62
63
64
65
66
# File 'lib/omf_rete/tuple_stream.rb', line 57

def initialize(project_pattern, out_description = project_pattern, in_description = nil, receiver = nil, &block)
  @project_pattern = project_pattern
  super out_description
  @result_size = out_description.size
  if in_description
    self.inDescription = in_description
  end
  @receiver = receiver
  @on_add_block = block
end

Instance Attribute Details

#receiverObject

Returns the value of attribute receiver.



55
56
57
# File 'lib/omf_rete/tuple_stream.rb', line 55

def receiver
  @receiver
end

Instance Method Details

#addTuple(tuple) ⇒ Object



76
77
78
79
80
# File 'lib/omf_rete/tuple_stream.rb', line 76

def addTuple(tuple)
  if (result = process(tuple, @on_add_block))
    @receiver.addTuple(result)
  end
end

#clearObject



88
89
90
# File 'lib/omf_rete/tuple_stream.rb', line 88

def clear()
  @receiver.clear
end

#inDescription=(in_description) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/omf_rete/tuple_stream.rb', line 99

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

#on_add(&block) ⇒ Object



68
69
70
# File 'lib/omf_rete/tuple_stream.rb', line 68

def on_add(&block)
  @on_add_block = block
end

#on_remove(&block) ⇒ Object



72
73
74
# File 'lib/omf_rete/tuple_stream.rb', line 72

def on_remove(&block)
  @on_remove_block = block
end

#removeTuple(tuple) ⇒ Object



82
83
84
85
86
# File 'lib/omf_rete/tuple_stream.rb', line 82

def removeTuple(tuple)
  if (result = process(tuple, @on_remove_block))
    @receiver.removeTuple(result)
  end
end

#source=(source) ⇒ Object



92
93
94
95
96
97
# File 'lib/omf_rete/tuple_stream.rb', line 92

def source=(source)
  super
  if source
    self.inDescription = source.description
  end
end